Module: Pwrake::Option

Included in:
Master
Defined in:
lib/pwrake/option.rb

Constant Summary collapse

DEFAULT_CONFFILES =
["pwrake_conf.yaml","PwrakeConf.yaml"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#core_listObject (readonly)

Returns the value of attribute core_list.



127
128
129
# File 'lib/pwrake/option.rb', line 127

def core_list
  @core_list
end

#counterObject (readonly)

Returns the value of attribute counter.



128
129
130
# File 'lib/pwrake/option.rb', line 128

def counter
  @counter
end

#logfileObject (readonly)

Returns the value of attribute logfile.



129
130
131
# File 'lib/pwrake/option.rb', line 129

def logfile
  @logfile
end

#queue_classObject (readonly)

Returns the value of attribute queue_class.



130
131
132
# File 'lib/pwrake/option.rb', line 130

def queue_class
  @queue_class
end

#shell_classObject (readonly)

Returns the value of attribute shell_class.



131
132
133
# File 'lib/pwrake/option.rb', line 131

def shell_class
  @shell_class
end

#task_loggerObject (readonly)

Returns the value of attribute task_logger.



132
133
134
# File 'lib/pwrake/option.rb', line 132

def task_logger
  @task_logger
end

Instance Method Details

#cwd_relative_if_under_homeObject



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/pwrake/option.rb', line 231

def cwd_relative_if_under_home
  home = Pathname.new(ENV['HOME']).realpath
  path = pwd = Pathname.pwd.realpath
  while path != home
    if path.root?
      return pwd.to_s
    end
    path = path.parent
  end
  return pwd.relative_path_from(home).to_s
end

#cwd_relative_to_homeObject



227
228
229
# File 'lib/pwrake/option.rb', line 227

def cwd_relative_to_home
  Pathname.pwd.relative_path_from(Pathname.new(ENV['HOME'])).to_s
end

#feedback_options(a) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/pwrake/option.rb', line 199

def feedback_options(a)
  a.each do |k|
    if v=@opts[k]
      m = (k.downcase+"=").to_sym
      Rake.application.options.send(m,v)
    end
  end
end

#finish_optionObject

—– finish —–



431
432
433
434
# File 'lib/pwrake/option.rb', line 431

def finish_option
  @task_logger.close if @task_logger
  Log.close
end

#format_time_pid(v) ⇒ Object



9
10
11
# File 'lib/pwrake/option.rb', line 9

def format_time_pid(v)
  START_TIME.strftime(v).sub("%$","%05d"%Process.pid)
end

#init_loggerObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/pwrake/option.rb', line 274

def init_logger
  if Rake.application.options.debug
    Log.level = Log::DEBUG
  elsif Rake.application.options.trace
    Log.level = Log::INFO
  else
    Log.level = Log::WARN
  end

  if @logfile
    logdir = File.dirname(@logfile)
    if !File.directory?(logdir)
      mkdir_p logdir
    end
    Log.open(@logfile)
  else
    Log.open($stdout)
  end

  if @tasklog
    @task_logger = File.open(@tasklog,'w')
    h = %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
    ].join(',')+"\n"
    @task_logger.print h
  end
end

#init_optionObject

—– init —–



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pwrake/option.rb', line 107

def init_option
  @host_group = []
  init_options
  init_pass_env
  init_logger
  Log.info "Options:"
  @opts.each do |k,v|
  Log.info " #{k} = #{v.inspect}"
  end
  if @opts['SHOW_CONF']
    require "yaml"
    YAML.dump(@opts,$stdout)
    exit
  end
  if @opts['GC_PROFILE']
    GC::Profiler.enable
  end
  @counter = Counter.new
end

#init_optionsObject



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
197
# File 'lib/pwrake/option.rb', line 138

def init_options
  # Read pwrake_conf
  @pwrake_conf = Rake.application.options.pwrake_conf

  if @pwrake_conf
    if !File.exist?(@pwrake_conf)
      raise "Configuration file not found: #{@pwrake_conf}"
    end
  else
    @pwrake_conf = DEFAULT_CONFFILES.find{|fn| File.exist?(fn)}
  end

  if @pwrake_conf.nil?
    @yaml = {}
  else
    Log.debug "--- @pwrake_conf=#{@pwrake_conf}"
    require "yaml"
    @yaml = open(@pwrake_conf){|f| YAML.load(f) }
  end

  @opts = {'PWRAKE_CONF' => @pwrake_conf,
    'THREAD_CREATE_INTERVAL' => 0.006,
  }

  option_data.each do |a|
    prc = nil
    keys = []
    case a
    when String
      keys << a
    when Array
      a.each do |x|
        case x
        when String
          keys << x
        when Proc
          prc = x
        end
      end
    end
    key = keys[0]
    val = search_opts(keys)
    val = prc.call(val) if prc
    @opts[key] = val if !val.nil?
    instance_variable_set("@"+key.downcase, val)
  end

  feedback_options [
   'DRYRUN',
   'IGNORE_SYSTEM',
   'IGNORE_DEPRECATE',
   'LOAD_SYSTEM',
   'NOSEARCH',
   'RAKELIB',
   'SHOW_PREREQS',
   'SILENT',
   'TRACE',
   'TRACE_RULES']
  Rake.verbose(false) if Rake.application.options.silent
end

#init_pass_envObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/pwrake/option.rb', line 243

def init_pass_env
  if envs = @opts['PASS_ENV']
    pass_env = {}

    case envs
    when Array
      envs.each do |k|
        k = k.to_s
        if v = ENV[k]
          pass_env[k] = v
        end
      end
    when Hash
      envs.each do |k,v|
        k = k.to_s
        if v = ENV[k] || v
          pass_env[k] = v
        end
      end
    else
      raise "invalid option for PASS_ENV in pwrake_conf.yaml"
    end

    if pass_env.empty?
      @opts.delete('PASS_ENV')
    else
      @opts['PASS_ENV'] = pass_env
    end
  end
end

#mount_type(d = nil) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/pwrake/option.rb', line 406

def mount_type(d=nil)
  mtab = '/etc/mtab'
  if File.exist?(mtab)
    d ||= mountpoint_of_cwd
    open(mtab,'r') do |f|
      f.each_line do |l|
        if /#{d} (?:type )?(\S+)/o =~ l
          return $1
        end
      end
    end
  end
  nil
end

#mountpoint_of_cwdObject



421
422
423
424
425
426
427
# File 'lib/pwrake/option.rb', line 421

def mountpoint_of_cwd
  d = Pathname.pwd
  while !d.mountpoint?
    d = d.parent
  end
  d
end

#option_dataObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pwrake/option.rb', line 24

def option_data
  [
    'DRYRUN',
    'IGNORE_SYSTEM',
    'IGNORE_DEPRECATE',
    'LOAD_SYSTEM',
    'NOSEARCH',
    'RAKELIB',
    'SHOW_PREREQS',
    'SILENT',
    'TRACE',
    'TRACE_RULES',

    'FILESYSTEM',
    'SSH_OPTION',
    'PASS_ENV',
    'GNU_TIME',
    'DEBUG',
    'PLOT_PARALLELISM',
    'HALT_QUEUE_WHILE_SEARCH',
    'THREAD_CREATE_INTERVAL',
    'SHOW_CONF',
    'FAILED_TARGET', # rename(default), delete, leave
    'QUEUE_PRIORITY', # DFS(default), FIFO,
    'STEAL_WAIT',
    'STEAL_WAIT_MAX',

    ['HOSTFILE','HOSTS'],
    ['LOGFILE','LOG',
      proc{|v|
        if v
          # turn trace option on
          Rake.application.options.trace = true
          if v == "" || !v.kind_of?(String)
            v = "Pwrake%Y%m%d-%H%M%S_%$.log"
          end
          format_time_pid(v)
        end
      }],
    ['TASKLOG',
      proc{|v|
        if v
          if v == "" || !v.kind_of?(String)
            v = "Pwrake%Y%m%d-%H%M%S_%$.task"
          end
          format_time_pid(v)
        end
      }],
    ['PROFILE',
      proc{|v|
        if v
          if v == "" || !v.kind_of?(String)
            v = "Pwrake%Y%m%d-%H%M%S_%$.csv"
          end
          format_time_pid(v)
        end
      }],
    ['GC_PROFILE',
     proc{|v|
        if v
          if v == "" || !v.kind_of?(String)
            v = "Pwrake%Y%m%d-%H%M%S_%$.gcprof"
          end
          format_time_pid(v)
        end
     }],
    ['NUM_THREADS', proc{|v| v && v.to_i}],
    ['DISABLE_AFFINITY', proc{|v| v || ENV['AFFINITY']=='off'}],
    ['DISABLE_STEAL', proc{|v| v || ENV['STEAL']=='off'}],
    ['GFARM_BASEDIR', proc{|v| v || '/tmp'}],
    ['GFARM_PREFIX', proc{|v| v || "pwrake_#{ENV['USER']}"}],
    ['GFARM_SUBDIR', proc{|v| v || '/'}],
    #['MASTER_HOSTNAME', proc{|v| v || `hostname -f`.chomp}],
    ['WORK_DIR',proc{|v|
        v ||= '$HOME/%CWD_RELATIVE_TO_HOME'
        v.sub('%CWD_RELATIVE_TO_HOME',cwd_relative_to_home)
      }]
  ]
end

#parse_opt(s) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/pwrake/option.rb', line 13

def parse_opt(s)
  case s
  when /false|nil|off/i
    false
  when /true|on/i
    true
  else
    s
  end
end

#pwrake_optionsObject



134
135
136
# File 'lib/pwrake/option.rb', line 134

def pwrake_options
  @opts
end

#search_opts(keys) ⇒ Object

Option order:

command_option > ENV > pwrake_conf > DEFAULT_OPTIONS


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/pwrake/option.rb', line 210

def search_opts(keys)
  val = Rake.application.options.send(keys[0].downcase.to_sym)
  return parse_opt(val) if !val.nil?
  #
  keys.each do |k|
    val = ENV[k.upcase]
    return parse_opt(val) if !val.nil?
  end
  #
  return nil if !@yaml
  keys.each do |k|
    val = @yaml[k.upcase]
    return val if !val.nil?
  end
  nil
end

#set_filesystemObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/pwrake/option.rb', line 361

def set_filesystem
  if fn = @opts["PROFILE"]
    Shell.profiler.open(fn,@opts['GNU_TIME'],@opts['PLOT_PARALLELISM'])
  end

  @shell_opt = {
    :work_dir  => @opts['WORK_DIR'],
    :pass_env  => @opts['PASS_ENV'],
    :ssh_opt   => @opts['SSH_OPTION']
  }

  if @filesystem.nil?
    case mount_type
    when /gfarm2fs/
      @opts['FILESYSTEM'] = @filesystem = 'gfarm'
    end
  end

  case @filesystem
  when 'gfarm'
    require "pwrake/locality_aware_queue"
    require "pwrake/gfarm_feature"
    GfarmPath.subdir = @opts['GFARM_SUBDIR']
    @filesystem  = 'gfarm'
    @shell_class = GfarmShell
    @shell_opt.merge!({
      :work_dir  => Dir.pwd,
      :single_mp => @opts['GFARM_SINGLE_MP'],
      :basedir   => @opts['GFARM_BASEDIR'],
      :prefix    => @opts['GFARM_PREFIX']
    })
  if @opts['DISABLE_AFFINITY']
    @queue_class = TaskQueue
  else
    @queue_class = LocalityAwareQueue
  end
    @postprocess = GfarmPostprocess.new
    Log.debug "--- @queue_class=#{@queue_class}"
  else
    @filesystem  = 'nfs'
    @shell_class = Shell
    @queue_class = TaskQueue
  end
end

#set_hostsObject



310
311
312
313
314
315
316
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/pwrake/option.rb', line 310

def set_hosts
  if @hostfile && @num_threads
    raise "Cannot set `hostfile' and `num_threads' simultaneously"
  end
  if @hostfile
    require "socket"
    tmplist = []
    File.open(@hostfile) do |f|
      re = /\[\[([\w\d]+)-([\w\d]+)\]\]/o
      while l = f.gets
        l = $1 if /^([^#]*)#/ =~ l
        host, ncore, group = l.split
        if host
          if re =~ host
            hosts = ($1..$2).map{|i| host.sub(re,i)}
          else
            hosts = [host]
          end
          hosts.each do |host|
            begin
              host = Socket.gethostbyname(host)[0]
            rescue
              Log.info "-- FQDN not resoved : #{host}"
            end
            ncore = (ncore || 1).to_i
            group = (group || 0).to_i
            tmplist << ([host] * ncore.to_i)
            @host_group[group] ||= []
            @host_group[group] << host
          end
        end
      end
    end
    #
    @core_list = []
    begin # alternative order
      sz = 0
      tmplist.each do |a|
        @core_list << a.shift if !a.empty?
        sz += a.size
      end
    end while sz>0
    @num_threads = @core_list.size
  else
    @num_threads = 1 if !@num_threads
    @core_list = ['localhost'] * @num_threads
  end
  Log.info "num_cores=#{@core_list.size}"
end

#setup_optionObject

—– setup —–



305
306
307
308
# File 'lib/pwrake/option.rb', line 305

def setup_option
  set_hosts
  set_filesystem
end