Class: Pwrake::Option

Inherits:
Hash
  • Object
show all
Defined in:
lib/pwrake/option/option.rb,
lib/pwrake/option/option_filesystem.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



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

def initialize
  load_pwrake_conf
  init_options
  init_pass_env
  if self['SHOW_CONF']
    require "yaml"
    YAML.dump(self,$stdout)
    exit
  elsif self['REPORT_DIR']
    require 'pwrake/report'
    Report.new(self,[]).report_html
    exit
  end
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



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

def counter
  @counter
end

#host_mapObject (readonly)

Returns the value of attribute host_map.



322
323
324
# File 'lib/pwrake/option/option.rb', line 322

def host_map
  @host_map
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#queue_classObject (readonly)

Returns the value of attribute queue_class.



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

def queue_class
  @queue_class
end

#worker_optionObject (readonly)

Returns the value of attribute worker_option.



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

def worker_option
  @worker_option
end

#worker_progsObject (readonly)

Returns the value of attribute worker_progs.



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

def worker_progs
  @worker_progs
end

Instance Method Details

#clear_gfarm2fsObject



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

def clear_gfarm2fs
  setup_hosts
  d = File.join(self['GFARM_BASEDIR'],self['GFARM_PREFIX'])
  rcmd = "
for i in #{d}*; do
  if [ -d \"$i\" ]; then
case \"$i\" in
  *_000) ;;
  *) fusermount -u $i; rmdir $i ;;
esac
  fi
done
sleep 1
for i in #{d}*_000; do
  if [ -d \"$i\" ]; then
fusermount -u $i; rmdir $i
  fi
done
"
  threads = []
  @host_map.each do |k,hosts|
    hosts.each do |info|
      threads << Thread.new do
        system "ssh #{info.name} '#{rcmd}'"
      end
    end
  end
  threads.each{|t| t.join}
end

#cwd_relative_if_under_homeObject



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/pwrake/option/option.rb', line 270

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



266
267
268
# File 'lib/pwrake/option/option.rb', line 266

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

#feedback_optionsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pwrake/option/option.rb', line 204

def feedback_options
  opts = Rake.application.options
  ['DRYRUN',
   'IGNORE_SYSTEM',
   'IGNORE_DEPRECATE',
   'LOAD_SYSTEM',
   'NOSEARCH',
   'RAKELIB',
   'SHOW_PREREQS',
   'SILENT',
   'TRACE',
   'BACKTRACE',
   'TRACE_OUTPUT',
   'TRACE_RULES'
  ].each do |k|
    if v=self[k]
      m = (k.downcase+"=").to_sym
      opts.send(m,v)
    end
  end
  case opts.trace_output
  when 'stdout'
    opts.trace_output = $stdout
  when 'stderr', nil
    opts.trace_output = $stderr
  end
end

#finish_optionObject

—– finish —–



357
358
359
# File 'lib/pwrake/option/option.rb', line 357

def finish_option
  Log.close
end

#format_time_pid(v) ⇒ Object



200
201
202
# File 'lib/pwrake/option/option.rb', line 200

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

#initObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pwrake/option/option.rb', line 26

def init
  Log.info "Options:"
  self.each do |k,v|
	Log.info " #{k} = #{v.inspect}"
  end
  #@counter = Counter.new
  setup_hosts
  setup_filesystem # require 'option_filesystem.rb'
  #
  if self['LOG_DIR'] && self['GC_LOG_FILE']
    GC::Profiler.enable
  end
end

#init_optionsObject



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

def init_options
  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
    self[key] = val if !val.nil?
    instance_variable_set("@"+key.downcase, val)
  end

  feedback_options

  Rake.verbose(false) if Rake.application.options.silent
end

#init_pass_envObject




284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/pwrake/option/option.rb', line 284

def init_pass_env
  if envs = self['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?
      self.delete('PASS_ENV')
    else
      self['PASS_ENV'] = pass_env
    end
  end
end

#load_pwrake_confObject

—– init —–



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pwrake/option/option.rb', line 47

def load_pwrake_conf
  # 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
  self['PWRAKE_CONF'] = pwrake_conf
  if pwrake_conf.nil?
    @yaml = {}
  else
    #Log.debug "load pwrake_conf=#{pwrake_conf}"
    require "yaml"
    @yaml = open(pwrake_conf){|f| YAML.load(f) }
  end
end

#max_postprocess_poolObject



78
79
80
81
82
83
84
85
# File 'lib/pwrake/option/option_filesystem.rb', line 78

def max_postprocess_pool
  case @filesystem
  when 'gfarm'
    self['MAX_GFWHERE_WORKER']
  else
    1
  end
end

#mount_type(d = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pwrake/option/option_filesystem.rb', line 98

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



113
114
115
116
117
118
119
# File 'lib/pwrake/option/option_filesystem.rb', line 113

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

#option_dataObject



96
97
98
99
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
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
198
# File 'lib/pwrake/option/option.rb', line 96

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

    'FILESYSTEM',
    'SSH_OPTION',
    'PASS_ENV',
    ['SHELL_COMMAND', proc{|v| v||ENV['SHELL']}],
    ['SHELL_RC','SHELLRC'],
    'GFARM2FS_OPTION',
    'GFARM2FS_DEBUG',
    ['GFARM2FS_DEBUG_WAIT', proc{|v| v ? v.to_i : 1}],
    'GNU_TIME',
    'DEBUG',
    'PLOT_PARALLELISM',
    'SHOW_CONF',
    ['REPORT_DIR','REPORT'],
    'FAILED_TARGET', # rename(default), delete, leave
    'FAILURE_TERMINATION', # wait, kill, continue
    'QUEUE_PRIORITY', # RANK(default), FIFO, LIFO, DFS
    'NOACTION_QUEUE_PRIORITY', # FIFO(default), LIFO, RAND
    #'NUM_NOACTION_THREADS', # default=4 when gfarm, else 1
    'GRAPH_PARTITION',
    'PLOT_PARTITION',

    ['HOSTFILE','HOSTS'],
    ['LOG_DIR','LOG',
      proc{|v|
        if v
          if v == "" || !v.kind_of?(String)
            v = "Pwrake%Y%m%d-%H%M%S"
          end
          d = v = format_time_pid(v)
          i = 1
          while File.exist?(d)
            d = "#{v}.#{i}"
            i += 1
          end
          d
        end
      }],
    ['LOG_FILE',
      proc{|v|
        if v.kind_of?(String) && v != ""
          v
        else
          "pwrake.log"
        end
      }],
    ['TASK_CSV_FILE',
      proc{|v|
        if v.kind_of?(String) && v != ""
          v
        else
          "task.csv"
        end
      }],
    ['COMMAND_CSV_FILE',
      proc{|v|
        if v.kind_of?(String) && v != ""
          v
        else
          "command.csv"
        end
      }],
    ['GC_LOG_FILE',
     proc{|v|
       if v
         if v.kind_of?(String) && v != ""
           v
         else
           "gc.log"
         end
       end
     }],
    ['NUM_THREADS', proc{|v| v && v.to_i}],
    ['SHELL_START_INTERVAL', proc{|v| (v || 0.012).to_f}],
    ['HEARTBEAT', proc{|v| (v || 240).to_i}],
    ['RETRY', proc{|v| (v || 1).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 || '/'}],
    ['MAX_GFWHERE_WORKER', proc{|v| (v || 8).to_i}],
    ['MASTER_HOSTNAME', proc{|v| (v || begin;`hostname -f`;rescue;end || '').chomp}],
    ['WORK_DIR', proc{|v|
       v ||= '%CWD_RELATIVE_TO_HOME'
       v.sub('%CWD_RELATIVE_TO_HOME',cwd_relative_to_home)
     }],
  ]
end

#parse_opt(s) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/pwrake/option/option.rb', line 251

def parse_opt(s)
  case s
  when /^(false|nil|off)$/i
    false
  when /^(true|on)$/i
    true
  when $stdout
    "stdout"
  when $stderr
    "stderr"
  else
    s
  end
end

#postprocess(runner) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/pwrake/option/option_filesystem.rb', line 87

def postprocess(runner)
  case @filesystem
  when 'gfarm'
    require "pwrake/gfarm/gfarm_postprocess"
    GfarmPostprocess.new(runner)
  else
    require "pwrake/master/postprocess"
    Postprocess.new(runner)
  end
end

#search_opts(keys) ⇒ Object

Priority of Option:

command_option > ENV > pwrake_conf > DEFAULT_OPTIONS


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/pwrake/option/option.rb', line 234

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

#setup_filesystemObject



12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/pwrake/option/option_filesystem.rb', line 12

def setup_filesystem

  @worker_progs = %w[
    parallel/processor_count
    pwrake/worker/writer
    pwrake/worker/log_executor
    pwrake/worker/executor
    pwrake/worker/invoker
    pwrake/worker/shared_directory
  ]
  @worker_option = {
    :base_dir  => "",
    :work_dir  => self['WORK_DIR'],
    :log_dir   => self['LOG_DIR'],
    :pass_env  => self['PASS_ENV'],
    :ssh_option => self['SSH_OPTION'],
    :shell_command => self['SHELL_COMMAND'],
    :shell_rc  => self['SHELL_RC'],
    :heartbeat => self['HEARTBEAT']
  }

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

  #n_noaction_th = self['NUM_NOACTION_THREADS']

  case @filesystem
  when 'gfarm'
    require "pwrake/queue/locality_aware_queue"
    require "pwrake/gfarm/gfarm_path"
    GfarmPath.subdir = self['GFARM_SUBDIR']
    @filesystem  = 'gfarm'
    base = self['GFARM_BASEDIR']
    prefix = self['GFARM_PREFIX']
    mntpnt = "#{base}/#{prefix}"
    @worker_option.merge!({
      :shared_directory => "GfarmDirectory",
      :base_dir => mntpnt,
      :work_dir => GfarmPath.pwd.to_s,
      :gfarm2fs_option => self['GFARM2FS_OPTION'],
      :gfarm2fs_debug => self['GFARM2FS_DEBUG'],
      :gfarm2fs_debug_wait => self['GFARM2FS_DEBUG_WAIT'],
      :single_mp => self['GFARM_SINGLE_MP']
    })
    @worker_progs.push "pwrake/worker/gfarm_directory"

	if self['DISABLE_AFFINITY']
	  @queue_class = "TaskQueue"
	else
	  @queue_class = "LocalityAwareQueue"
	end
    #@num_noaction_threads = (n_noaction_th || [8,@host_map.num_threads].max).to_i
  else
    @filesystem  = 'nfs'
    @queue_class = "TaskQueue"
    #@num_noaction_threads = (n_noaction_th || 1).to_i
    @worker_option[:shared_directory] = "SharedDirectory"
  end
  @worker_progs.push "pwrake/worker/worker_main"
  Log.debug "@queue_class=#{@queue_class}"
end

#setup_hostsObject



316
317
318
319
320
321
# File 'lib/pwrake/option/option.rb', line 316

def setup_hosts
  if @hostfile && @num_threads
    raise "Cannot set `hostfile' and `num_threads' simultaneously"
  end
  @host_map = HostMap.new(@hostfile || @num_threads)
end