Class: Pwrake::ShellProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/branch/shell_profiler.rb

Constant Summary collapse

HEADER_FOR_PROFILE =
%w[exec_id task_id task_name command
start_time end_time elap_time host status]
HEADER_FOR_GNU_TIME =
%w[realtime systime usrtime maxrss averss memsz
datasz stcksz textsz pagesz majflt minflt nswap ncswinv
ncswvol ninp nout msgrcv msgsnd signum]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShellProfiler

Returns a new instance of ShellProfiler.



16
17
18
19
20
21
# File 'lib/pwrake/branch/shell_profiler.rb', line 16

def initialize
  @lock = Mutex.new
  @gnu_time = false
  @id = 0
  @io = nil
end

Class Method Details

.format_time(t) ⇒ Object



84
85
86
# File 'lib/pwrake/branch/shell_profiler.rb', line 84

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

Instance Method Details

#_puts(s) ⇒ Object



49
50
51
52
53
# File 'lib/pwrake/branch/shell_profiler.rb', line 49

def _puts(s)
  @lock.synchronize do
    @io.puts(s) if @io
  end
end

#closeObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pwrake/branch/shell_profiler.rb', line 36

def close
  t = Time.now
  profile(nil,nil,'pwrake_profile_end',t,t)
  @lock.synchronize do
    @io.close if @io != nil
    @io = nil
  end
  if @plot
    require 'pwrake/report'
    Parallelism.plot_parallelism(@file)
  end
end

#format_time(t) ⇒ Object

def command(cmd,terminator)

  if @gnu_time
    if /\*|\?|\{|\}|\[|\]|<|>|\(|\)|\~|\&|\||\\|\$|;|`|\n/ =~ cmd
      cmd = cmd.gsub(/'/,"'\"'\"'")
      cmd = "sh -c '#{cmd}'"
    end
    f = %w[%x %e %S %U %M %t %K %D %p %X %Z %F %R %W %c %w %I %O %r
           %s %k].join(@separator)
    "/usr/bin/time -o /dev/stdout -f '#{terminator}:#{f}' #{cmd}"
  else
    "#{cmd}\necho '#{terminator}':$? "
  end
end #`


79
80
81
82
# File 'lib/pwrake/branch/shell_profiler.rb', line 79

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

#open(file, gnu_time = false, plot = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pwrake/branch/shell_profiler.rb', line 23

def open(file,gnu_time=false,plot=false)
  @file = file
  @gnu_time = gnu_time
  @plot = plot
  @lock.synchronize do
    @io.close if @io != nil
    @io = CSV.open(file,"w")
  end
  _puts table_header
  t = Time.now
  profile(nil,nil,'pwrake_profile_start',t,t)
end

#profile(task_id, task_name, cmd, start_time, end_time, host = nil, status = nil) ⇒ Object



88
89
90
91
92
93
94
95
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
# File 'lib/pwrake/branch/shell_profiler.rb', line 88

def profile(task_id, task_name, cmd, start_time, end_time, host=nil, status=nil)
  id = @lock.synchronize do
    id = @id
    @id += 1
    id
  end
  if @io
    _puts [ id, task_id, task_name, cmd,
           format_time(start_time),
           format_time(end_time),
           "%.3f" % (end_time-start_time),
           host, status ]
  end
  case status
  when ""
    1
  when Integer
    status
  when String
    if @gnu_time
      if /^([^,]*),/ =~ status
        Integer($1)
      else
        status
      end
    else
      if /^\d+$/ =~ status
        Integer(status)
      else
        status
      end
    end
  end
end

#table_headerObject



55
56
57
58
59
60
61
# File 'lib/pwrake/branch/shell_profiler.rb', line 55

def table_header
  a = HEADER_FOR_PROFILE
  if @gnu_time
    a += HEADER_FOR_GNU_TIME
  end
  a
end