Class: Pwrake::TaskWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pwrake/task/task_wrapper.rb

Constant Summary collapse

LOCK =
Monitor.new
@@current_id =
1
@@task_logger =
nil
@@instances =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, task_args = nil) ⇒ TaskWrapper

Returns a new instance of TaskWrapper.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pwrake/task/task_wrapper.rb', line 15

def initialize(task,task_args=nil)
  @task = task
  @task_args = task_args
  @property = task.property
  @task_id = @@current_id
  @@current_id += 1
  @location = []
  @group = []
  @group_id = nil
  @suggest_location = nil
  @file_stat = nil
  @input_file_size = nil
  @input_file_mtime = nil
  @rank = nil
  @priority = nil
  @executed = false
  @assigned = []
  @exec_host = nil
  @exec_host_id = nil
  @tried_hosts = {}
  @n_retry = @property.retry || Rake.application.pwrake_options["RETRY"] || 1
  @@instances << self
end

Instance Attribute Details

#assignedObject (readonly)

Returns the value of attribute assigned.



44
45
46
# File 'lib/pwrake/task/task_wrapper.rb', line 44

def assigned
  @assigned
end

#exec_hostObject

Returns the value of attribute exec_host.



48
49
50
# File 'lib/pwrake/task/task_wrapper.rb', line 48

def exec_host
  @exec_host
end

#exec_host_idObject

Returns the value of attribute exec_host_id.



48
49
50
# File 'lib/pwrake/task/task_wrapper.rb', line 48

def exec_host_id
  @exec_host_id
end

#executedObject

Returns the value of attribute executed.



47
48
49
# File 'lib/pwrake/task/task_wrapper.rb', line 47

def executed
  @executed
end

#file_statObject (readonly)

Returns the value of attribute file_stat.



42
43
44
# File 'lib/pwrake/task/task_wrapper.rb', line 42

def file_stat
  @file_stat
end

#groupObject (readonly)

Returns the value of attribute group.



42
43
44
# File 'lib/pwrake/task/task_wrapper.rb', line 42

def group
  @group
end

#group_idObject (readonly)

Returns the value of attribute group_id.



42
43
44
# File 'lib/pwrake/task/task_wrapper.rb', line 42

def group_id
  @group_id
end

#locationObject

Returns the value of attribute location.



43
44
45
# File 'lib/pwrake/task/task_wrapper.rb', line 43

def location
  @location
end

#n_used_coresObject (readonly)

Returns the value of attribute n_used_cores.



46
47
48
# File 'lib/pwrake/task/task_wrapper.rb', line 46

def n_used_cores
  @n_used_cores
end

#shell_idObject

Returns the value of attribute shell_id.



49
50
51
# File 'lib/pwrake/task/task_wrapper.rb', line 49

def shell_id
  @shell_id
end

#statusObject

Returns the value of attribute status.



49
50
51
# File 'lib/pwrake/task/task_wrapper.rb', line 49

def status
  @status
end

#taskObject (readonly)

Returns the value of attribute task.



42
43
44
# File 'lib/pwrake/task/task_wrapper.rb', line 42

def task
  @task
end

#task_idObject (readonly)

Returns the value of attribute task_id.



42
43
44
# File 'lib/pwrake/task/task_wrapper.rb', line 42

def task_id
  @task_id
end

#tried_hostsObject (readonly)

Returns the value of attribute tried_hosts.



45
46
47
# File 'lib/pwrake/task/task_wrapper.rb', line 45

def tried_hosts
  @tried_hosts
end

Class Method Details

.clear_rankObject



71
72
73
74
75
76
# File 'lib/pwrake/task/task_wrapper.rb', line 71

def self.clear_rank
  LOCK.synchronize do
    Log.debug "#{self}.clear_rank"
    @@instances.each{|w| w.clear_rank}
  end
end

.close_task_loggerObject



67
68
69
# File 'lib/pwrake/task/task_wrapper.rb', line 67

def self.close_task_logger
  @@task_logger.close if @@task_logger
end

.format_time(t) ⇒ Object



51
52
53
# File 'lib/pwrake/task/task_wrapper.rb', line 51

def self.format_time(t)
  t.strftime("%F %T.%L")
end

.init_task_logger(option) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pwrake/task/task_wrapper.rb', line 55

def self.init_task_logger(option)
  if dir = option['LOG_DIR']
    fn = File.join(dir,option['TASK_CSV_FILE'])
    @@task_logger = CSV.open(fn,'w')
    @@task_logger.puts %w[
      task_id task_name start_time end_time elap_time preq
      preq_host preq_loc exec_host shell_id has_action executed ncore
      file_size file_mtime file_host write_loc
    ]
  end
end

Instance Method Details

#acceptable_for(host_info) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/pwrake/task/task_wrapper.rb', line 371

def acceptable_for(host_info)
  unless host_info
    return true
  end
  unless @property.accept_host(host_info)
    return false
  end
  if @property.reserve
    return true
  end
  host_info.accept_core(@property.use_cores(host_info.ncore))
end

#clear_rankObject



295
296
297
# File 'lib/pwrake/task/task_wrapper.rb', line 295

def clear_rank
  @rank = nil
end

#file_locality(nodes) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/pwrake/task/task_wrapper.rb', line 198

def file_locality(nodes)
  if nodes.empty? || !@exec_host_id
    nil  # not available
  elsif nodes.any?{|node|
      HostMap.ipmatch_for_name(node).include?(@exec_host_id)}
    "L"  # Local
  else
    "R"  # Remote
  end
end

#file_mtimeObject



303
304
305
# File 'lib/pwrake/task/task_wrapper.rb', line 303

def file_mtime
  @file_stat ? @file_stat.mtime : Time.at(0)
end

#file_sizeObject



299
300
301
# File 'lib/pwrake/task/task_wrapper.rb', line 299

def file_size
  @file_stat ? @file_stat.size : 0
end

#has_action?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/pwrake/task/task_wrapper.rb', line 221

def has_action?
  !@task.actions.empty?
end

#has_input_file?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/pwrake/task/task_wrapper.rb', line 217

def has_input_file?
  is_file_task? && !prerequisites.empty?
end

#has_output_file?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/pwrake/task/task_wrapper.rb', line 213

def has_output_file?
  is_file_task? && !actions.empty?
end

#input_file_mtimeObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/pwrake/task/task_wrapper.rb', line 317

def input_file_mtime
  if has_input_file? && @input_file_mtime.nil?
    hash = Hash.new
    max_sz = 0
    prerequisites.each do |preq|
      t = Rake.application[preq].wrapper
      sz = t.file_size
      if sz > 0
        hash[t] = sz
        if sz > max_sz
          max_sz = sz
        end
      end
    end
    half_max_sz = max_sz / 2
    hash.each do |t,sz|
      if sz > half_max_sz
        time = t.file_mtime
        if @input_file_mtime.nil? || @input_file_mtime < time
          @input_file_mtime = time
        end
      end
    end
  end
  @input_file_mtime
end

#input_file_sizeObject



307
308
309
310
311
312
313
314
315
# File 'lib/pwrake/task/task_wrapper.rb', line 307

def input_file_size
  unless @input_file_size
    @input_file_size = 0
    prerequisites.each do |preq|
      @input_file_size += Rake.application[preq].wrapper.file_size
    end
  end
  @input_file_size
end

#is_file_task?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/pwrake/task/task_wrapper.rb', line 209

def is_file_task?
  @task.kind_of?(Rake::FileTask)
end

#log_taskObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pwrake/task/task_wrapper.rb', line 120

def log_task
  @time_end = Time.now
  @clock_end = Pwrake.clock
  #
  sug_host = suggest_location()
  shell = Pwrake::Shell.current
  #
  if sug_host && !sug_host.empty? && shell && !actions.empty?
    Rake.application.count( sug_host, shell.host )
  end
  return if !@@task_logger
  #
  if @clock_start
    elap = @clock_end - @clock_start
  else
    Loag.debug "@clock_start is not defined for #{@task.class}[#{name}]"
    elap = 0
  end
  if has_output_file?
    RANK_STAT.add_sample(rank,elap)
  end
  #
  # locality check
  loc_na = true
  preq_loc = prerequisites.map do |preq|
    locs = Rake.application[preq].wrapper.location
    if loc = file_locality(locs)
      loc_na = false
      loc
    else
      "n"
    end
  end.join("")
  preq_loc = nil if loc_na
  write_loc = file_locality(@location)
  #
  if @file_stat
    fstat = [ @file_stat.size, @file_stat.mtime,
              self.location.join('|'), write_loc ]
  else
    fstat = [nil]*4
  end
  #
  # task_id task_name start_time end_time elap_time preq preq_host
  # preq_loc exec_host shell_id has_action executed ncore
  # file_size file_mtime file_host write_loc
  #
  row = [ @task_id, name, @time_start, @time_end, "%.6f"%elap,
          prerequisites, sug_host, preq_loc, @exec_host, @shell_id,
          (actions.empty?) ? 0 : 1,
          (@executed) ? 1 : 0,
          @n_used_cores,
        ] + fstat
  row.map!{|x|
    if x.kind_of?(Time)
      TaskWrapper.format_time(x)
    elsif x.kind_of?(Array)
      if x.empty?
        nil
      else
        x.join('|')
      end
    else
      x
    end
  }
  @@task_logger << row
  #
  clsname = @task.class.to_s.sub(/^(Rake|Pwrake)::/o,"")
  msg = '%s[%s] status=%s id=%d elap=%.6f exec_host=%s' %
    [clsname,name,@status,@task_id,elap,@exec_host]
  if @status=="end"
    Log.info msg
  else
    Log.error msg
  end
end

#no_more_retryObject



87
88
89
# File 'lib/pwrake/task/task_wrapper.rb', line 87

def no_more_retry
  @n_retry == 0
end

#postprocess(postproc) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/pwrake/task/task_wrapper.rb', line 91

def postprocess(postproc)
  @executed = true if !@task.actions.empty?
  if is_file_task?
    if File.exist?(name)
      @file_stat = File::Stat.new(name)
      @location = postproc.run(self)
    end
  end
  log_task
end

#preprocessObject



78
79
80
81
# File 'lib/pwrake/task/task_wrapper.rb', line 78

def preprocess
  @time_start = Time.now
  @clock_start = Pwrake.clock
end

#priorityObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/pwrake/task/task_wrapper.rb', line 344

def priority
  if has_input_file? && @priority.nil?
    sum_tm = 0
    sum_sz = 0
    prerequisites.each do |preq|
      pq = Rake.application[preq].wrapper
      sz = pq.file_size
      if sz > 0
        tm = pq.file_mtime - START_TIME
        sum_tm += tm * sz
        sum_sz += sz
      end
    end
    if sum_sz > 0
      @priority = sum_tm / sum_sz
    else
      @priority = 0
    end
    Log.debug "task=#{name} priority=#{@priority} sum_file_size=#{sum_sz}"
  end
  @priority || 0
end

#rankObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/pwrake/task/task_wrapper.rb', line 266

def rank
  LOCK.synchronize do
  if @rank.nil?
    if subsequents.nil? || subsequents.empty?
      @rank = 0
    else
      max_rank = 0
      subsequents.each do |subsq|
        r = subsq.wrapper.rank
        if r
          if max_rank < r
            max_rank = r
          end
        else
          Log.warn "subsq.wrapper.rank=#{rank.inspect}"
        end
      end
      if has_output_file?
        step = 1
      else
        step = 0
      end
      @rank = max_rank + step
    end
  end
  @rank
  end
end

#retry?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/pwrake/task/task_wrapper.rb', line 83

def retry?
  @status != "end" && @n_retry > 0
end

#retry_or_subsequentObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pwrake/task/task_wrapper.rb', line 102

def retry_or_subsequent
  @tried_hosts[@exec_host] = true
  if @status=="end"
    @task.pw_enq_subsequents
  elsif @n_retry > 0
    @suggest_location = []
    s="retry task (retry_count=#{@n_retry}): #{name}"
    Log.warn(s)
    $stderr.puts(s)
    @n_retry -= 1
    Rake.application.task_queue.enq(self)
  else
    s="give up retry (retry_count=#{@n_retry}): #{name}"
    Log.error(s)
    $stderr.puts(s)
  end
end

#set_used_cores(ncore) ⇒ Object



367
368
369
# File 'lib/pwrake/task/task_wrapper.rb', line 367

def set_used_cores(ncore)
  @n_used_cores = ncore
end

#suggest_locationObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/pwrake/task/task_wrapper.rb', line 237

def suggest_location
  if has_input_file? && @suggest_location.nil?
    cl = Pwrake.clock
    @suggest_location = []
    loc_fsz = Hash.new(0)
    prerequisites.each do |preq|
      t = Rake.application[preq].wrapper
      loc = t.location
      fsz = t.file_size
      if loc && fsz > 0
        loc.each do |h|
          loc_fsz[h] += fsz
        end
      end
    end
    #Log.debug "input=#{prerequisites.join('|')}"
    if !loc_fsz.empty?
      half_max_fsz = loc_fsz.values.max / 2
      loc_fsz.each do |h,sz|
        if sz > half_max_fsz
          @suggest_location << h
        end
      end
      Log.debug "locate:%.6f #{name} loc_fsz=#{loc_fsz.inspect} half_max_fsz=#{half_max_fsz} suggest=#{@suggest_location.inspect}"%(Pwrake.clock-cl)
    end
  end
  @suggest_location
end

#suggest_location=(a) ⇒ Object



233
234
235
# File 'lib/pwrake/task/task_wrapper.rb', line 233

def suggest_location=(a)
  @suggest_location = a
end

#tried_host?(host_info) ⇒ Boolean

Returns:

  • (Boolean)


384
385
386
# File 'lib/pwrake/task/task_wrapper.rb', line 384

def tried_host?(host_info)
  host_info && @tried_hosts[host_info.name]
end