Class: Pwrake::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/worker/executor.rb

Constant Summary collapse

ENV =
{
"OMPI_APP_CTX_NUM_PROCS" => nil,
"OMPI_COMM_WORLD_LOCAL_RANK" => nil,
"OMPI_COMM_WORLD_LOCAL_SIZE" => nil,
"OMPI_COMM_WORLD_NODE_RANK" => nil,
"OMPI_COMM_WORLD_RANK" => nil,
"OMPI_COMM_WORLD_SIZE" => nil,
"OMPI_FILE_LOCATION" => nil,
"OMPI_FIRST_RANKS" => nil,
"OMPI_MCA_db" => nil,
"OMPI_MCA_ess" => nil,
"OMPI_MCA_ess_base_jobid" => nil,
"OMPI_MCA_ess_base_vpid" => nil,
"OMPI_MCA_grpcomm" => nil,
"OMPI_MCA_initial_wdir" => nil,
"OMPI_MCA_mpi_yield_when_idle" => nil,
"OMPI_MCA_orte_app_num" => nil,
"OMPI_MCA_orte_bound_at_launch" => nil,
"OMPI_MCA_orte_ess_jobid" => nil,
"OMPI_MCA_orte_ess_node_rank" => nil,
"OMPI_MCA_orte_ess_num_procs" => nil,
"OMPI_MCA_orte_ess_vpid" => nil,
"OMPI_MCA_orte_hnp_uri" => nil,
"OMPI_MCA_orte_local_daemon_uri" => nil,
"OMPI_MCA_orte_num_nodes" => nil,
"OMPI_MCA_orte_num_restarts" => nil,
"OMPI_MCA_orte_tmpdir_base" => nil,
"OMPI_MCA_plm" => nil,
"OMPI_MCA_pubsub" => nil,
"OMPI_MCA_shmem_RUNTIME_QUERY_hint" => nil,
"OMPI_NUM_APP_CTX" => nil,
"OMPI_UNIVERSE_SIZE" => nil,
"PMI_RANK" => nil,
"PMI_FD" => nil,
"PMI_SIZE" => nil,
}

Instance Method Summary collapse

Constructor Details

#initialize(selector, dir_class, id, option) ⇒ Executor

Returns a new instance of Executor.



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
# File 'lib/pwrake/worker/executor.rb', line 42

def initialize(selector,dir_class,id,option)
  @selector = selector
  @id = id
  @option = option
  @out = Writer.instance
  @log = LogExecutor.instance
  @queue = FiberQueue.new(@log)
  @rd_list = []
  @dir = dir_class.new
  @dir.open
  @dir.open_messages.each{|m| @log.info(m)}
  @out.puts "#{@id}:open"

  r,w = IO.pipe
  @command_pipe_r = NBIO::Reader.new(@selector,r)
  @command_pipe_w = NBIO::Writer.new(@selector,w)
  @start_process_fiber = Fiber.new do
    while line = @queue.deq
      cmd = line
      while /\\$/ =~ line  # line continues
        line = @queue.deq
        break if !line
        cmd += line
      end
      break if @stopped
      cmd.chomp!
      if !cmd.empty?
        start_process(cmd)
      end
      Fiber.yield
    end
  end
end

Instance Method Details

#callback(rd, mode) ⇒ Object



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
# File 'lib/pwrake/worker/executor.rb', line 165

def callback(rd,mode)
  while s = rd.gets
    s.chomp!
    @out.puts "#{@id}:#{mode}:#{s}"
  end
  if rd.eof?
    @rd_list.delete(rd)
    if @rd_list.empty?  # process_end
      @thread = @pid = nil
      @log.info inspect_status
      if @option[:gnu_time]
        if gt = @sh_gtm.gets
          @out.puts "#{@id}:t:#{gt.chomp}"
        end
        @sh_gtm.close
      end
      @out.puts "#{@id}:z:#{exit_status}"
      @sh_in.close
      @sh_out.close
      @sh_err.close
    end
  end
rescue => exc
  @log.error(([exc.to_s]+exc.backtrace).join("\n"))
  stop
end

#closeObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pwrake/worker/executor.rb', line 81

def close
  if @thread
    @thread.join(15)
    sleep 0.1
  end
  @thread = Thread.new do
    @dir.close_messages.each{|m| @log.info(m)}
    @dir.close
  end
rescue => exc
  @log.error(([exc.to_s]+exc.backtrace).join("\n"))
end

#execute(cmd) ⇒ Object



102
103
104
105
106
# File 'lib/pwrake/worker/executor.rb', line 102

def execute(cmd)
  return if @stopped
  @queue.enq(cmd)
  @start_process_fiber.resume
end

#exit_statusObject



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

def exit_status
  s = @status
  case
  when s.signaled?
    if s.coredump?
      "core_dumped"
    else
      "killed:#{s.termsig}"
    end
  when s.stopped?
    "stopped:#{s.stopsig}"
  when s.exited?
    "#{s.exitstatus}"
  else
    "unknown:%#x" % s.to_i
  end
end

#inspect_statusObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/pwrake/worker/executor.rb', line 192

def inspect_status
  s = @status
  case
  when s.signaled?
    if s.coredump?
      "pid=#{s.pid} dumped core."
    else
      "pid=#{s.pid} was killed by signal #{s.termsig}"
    end
  when s.stopped?
    "pid=#{s.pid} was stopped by signal #{s.stopsig}"
  when s.exited?
    "pid=#{s.pid} exited normally. status=#{s.exitstatus}"
  else
    "unknown status %#x" % s.to_i
  end
end

#joinObject



94
95
96
97
98
99
100
# File 'lib/pwrake/worker/executor.rb', line 94

def join
  if @thread
    @thread.join(15)
  end
rescue => exc
  @log.error(([exc.to_s]+exc.backtrace).join("\n"))
end

#kill(sig) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/pwrake/worker/executor.rb', line 228

def kill(sig)
  stop
  if @pid
    Process.kill(sig,-@pid)
    @log.warn "Executor(id=#{@id})#kill pid=#{@pid} sig=#{sig}"
  end
end

#start_process(command) ⇒ Object



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
# File 'lib/pwrake/worker/executor.rb', line 108

def start_process(command)
  return if @thread      # running
  return if !command     # empty queue
  @spawn_in, @sh_in = IO.pipe
  @sh_out, @spawn_out = IO.pipe
  @sh_err, @spawn_err = IO.pipe
  if @option[:gnu_time]
    @sh_gtm, @spawn_gtm = IO.pipe
    @pid = Kernel.spawn(ENV, wrap_gnu_time(command),
                        in:@spawn_in,
                        out:@spawn_out,
                        err:@spawn_err,
                        3=>@spawn_gtm,
                        chdir:@dir.current,
                        pgroup:true,
                       )
    @thread = Thread.new do
      @pid2,@status = Process.waitpid2(@pid)
      @spawn_in.close
      @spawn_out.close
      @spawn_err.close
      @spawn_gtm.close
    end
  else
    @pid = Kernel.spawn(ENV, command,
                        in:@spawn_in,
                        out:@spawn_out,
                        err:@spawn_err,
                        chdir:@dir.current,
                        pgroup:true
                       )
    @thread = Thread.new do
      @pid2,@status = Process.waitpid2(@pid)
      @spawn_in.close
      @spawn_out.close
      @spawn_err.close
    end
  end
  @log.info "pid=#{@pid} started. command=#{command.inspect}"

  @rd_out = NBIO::Reader.new(@selector,@sh_out)
  @rd_err = NBIO::Reader.new(@selector,@sh_err)
  @rd_list = [@rd_out,@rd_err]

  Fiber.new{callback(@rd_err,"e")}.resume
  Fiber.new{callback(@rd_out,"o")}.resume
end

#stopObject



76
77
78
79
# File 'lib/pwrake/worker/executor.rb', line 76

def stop
  @stopped = true
  @queue.finish
end

#wrap_gnu_time(cmd) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/pwrake/worker/executor.rb', line 156

def wrap_gnu_time(cmd)
  if /\[|\]|<|>|\(|\)|\&|\||\\|\$|;|`|'|"|\n/ =~ cmd
    cmd = cmd.gsub(/'/,"'\"'\"'")
    cmd = (ENV['SHELL']||"sh")+" -c '#{cmd}'"
  end
  f = "%e,%S,%U,%M,%t,%K,%D,%p,%X,%Z,%F,%R,%W,%c,%w,%I,%O,%r,%s,%k"
  "/usr/bin/time -o /dev/fd/3 -f '#{f}' #{cmd}"
end