Class: Pwrake::TaskWrapper

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

Constant Summary collapse

@@current_id =
1
@@task_logger =
nil

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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pwrake/task/task_wrapper.rb', line 11

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
  @suggest_location = nil
  @file_stat
  @input_file_size
  @input_file_mtime
  @rank
  @priority
  @lock_rank = Monitor.new
  @executed = false
  @assigned = []
  @exec_host = nil
  @nretry = @property.retry || Rake.application.pwrake_options["RETRY"] || 0
end

Instance Attribute Details

#assignedObject (readonly)

Returns the value of attribute assigned.



38
39
40
# File 'lib/pwrake/task/task_wrapper.rb', line 38

def assigned
  @assigned
end

#exec_hostObject

Returns the value of attribute exec_host.



40
41
42
# File 'lib/pwrake/task/task_wrapper.rb', line 40

def exec_host
  @exec_host
end

#executedObject

Returns the value of attribute executed.



39
40
41
# File 'lib/pwrake/task/task_wrapper.rb', line 39

def executed
  @executed
end

#file_statObject (readonly)

Returns the value of attribute file_stat.



36
37
38
# File 'lib/pwrake/task/task_wrapper.rb', line 36

def file_stat
  @file_stat
end

#groupObject (readonly)

Returns the value of attribute group.



36
37
38
# File 'lib/pwrake/task/task_wrapper.rb', line 36

def group
  @group
end

#group_idObject (readonly)

Returns the value of attribute group_id.



36
37
38
# File 'lib/pwrake/task/task_wrapper.rb', line 36

def group_id
  @group_id
end

#locationObject

Returns the value of attribute location.



37
38
39
# File 'lib/pwrake/task/task_wrapper.rb', line 37

def location
  @location
end

#shell_idObject

Returns the value of attribute shell_id.



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

def shell_id
  @shell_id
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#taskObject (readonly)

Returns the value of attribute task.



36
37
38
# File 'lib/pwrake/task/task_wrapper.rb', line 36

def task
  @task
end

#task_idObject (readonly)

Returns the value of attribute task_id.



36
37
38
# File 'lib/pwrake/task/task_wrapper.rb', line 36

def task_id
  @task_id
end

Class Method Details

.close_task_loggerObject



58
59
60
# File 'lib/pwrake/task/task_wrapper.rb', line 58

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

.format_time(t) ⇒ Object



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

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

.init_task_logger(option) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/pwrake/task/task_wrapper.rb', line 47

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
      exec_host shell_id has_action executed file_size file_mtime file_host
    ]
  end
end

Instance Method Details

#file_mtimeObject



241
242
243
# File 'lib/pwrake/task/task_wrapper.rb', line 241

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

#file_sizeObject



237
238
239
# File 'lib/pwrake/task/task_wrapper.rb', line 237

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

#has_action?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/pwrake/task/task_wrapper.rb', line 167

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

#has_input_file?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/pwrake/task/task_wrapper.rb', line 163

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

#has_output_file?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/pwrake/task/task_wrapper.rb', line 159

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

#input_file_mtimeObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/pwrake/task/task_wrapper.rb', line 255

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



245
246
247
248
249
250
251
252
253
# File 'lib/pwrake/task/task_wrapper.rb', line 245

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)


155
156
157
# File 'lib/pwrake/task/task_wrapper.rb', line 155

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

#log_taskObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
# File 'lib/pwrake/task/task_wrapper.rb', line 100

def log_task
  @time_end = Time.now
  #
  loc = suggest_location()
  shell = Pwrake::Shell.current
  #
  if loc && !loc.empty? && shell && !actions.empty?
    Rake.application.count( loc, shell.host )
  end
  return if !@@task_logger
  #
  elap = @time_end - @time_start
  if has_output_file?
    RANK_STAT.add_sample(rank,elap)
  end
  #
  if @file_stat
    fstat = [@file_stat.size, @file_stat.mtime, self.location.join('|')]
  else
    fstat = [nil]*3
  end
  #
  # task_id task_name start_time end_time elap_time preq preq_host
  # exec_host shell_id has_action executed file_size file_mtime file_host
  #
  row = [ @task_id, name, @time_start, @time_end, elap,
          prerequisites, loc, @exec_host, @shell_id,
          (actions.empty?) ? 0 : 1,
          (@executed) ? 1 : 0,
          *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" %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

#n_used_cores(host_info = nil) ⇒ Object



305
306
307
# File 'lib/pwrake/task/task_wrapper.rb', line 305

def n_used_cores(host_info=nil)
  @n_used_cores ||= @property.n_used_cores(host_info)
end

#postprocess(location) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pwrake/task/task_wrapper.rb', line 82

def postprocess(location)
  @executed = true if !@task.actions.empty?
  tm_taskend = Time.now
  if is_file_task?
    t = Time.now
    if File.exist?(name)
      @file_stat = File::Stat.new(name)
      @location = location
    end
  end
  #Log.debug "postprocess time=#{Time.now-tm_taskend}"
  log_task
  @shell.current_task = nil if @shell
  if @status=="end"
    @task.pw_enq_subsequents
  end
end

#preprocessObject



62
63
64
65
66
67
# File 'lib/pwrake/task/task_wrapper.rb', line 62

def preprocess
  if @shell = Pwrake::Shell.current
    @shell.current_task = self
  end
  @time_start = Time.now
end

#priorityObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/pwrake/task/task_wrapper.rb', line 282

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=#{name} priority=#{@priority} sum_file_size=#{sum_sz}"
  end
  @priority || 0
end

#rankObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/pwrake/task/task_wrapper.rb', line 211

def rank
  #@lock_rank.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 max_rank < r
            max_rank = r
          end
        end
        if has_output_file?
          step = 1
        else
          step = 0
        end
        @rank = max_rank + step
      end
      Log.debug "Task[#{name}] rank=#{@rank.inspect}"
    end
  #end
  @rank
end

#retryObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pwrake/task/task_wrapper.rb', line 69

def retry
  if @nretry > 0
    s="retry task: #{name}"
    Log.debug(s)
    $stderr.puts(s)
    @nretry -= 1
    Rake.application.task_queue.enq(self)
    true
  else
    false
  end
end

#suggest_locationObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/pwrake/task/task_wrapper.rb', line 183

def suggest_location
  if has_input_file? && @suggest_location.nil?
    @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
      Log.debug "loc_fsz=#{loc_fsz.inspect} half_max_fsz=#{half_max_fsz}"
      loc_fsz.each do |h,sz|
        if sz > half_max_fsz
          @suggest_location << h
        end
      end
    end
  end
  @suggest_location
end

#suggest_location=(a) ⇒ Object



179
180
181
# File 'lib/pwrake/task/task_wrapper.rb', line 179

def suggest_location=(a)
  @suggest_location = a
end