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



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

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

Class Method Details

.format_time(t) ⇒ Object



82
83
84
# File 'lib/pwrake/branch/shell_profiler.rb', line 82

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

Instance Method Details

#_puts(s) ⇒ Object



47
48
49
50
51
# File 'lib/pwrake/branch/shell_profiler.rb', line 47

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

#closeObject



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

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 #`


77
78
79
80
# File 'lib/pwrake/branch/shell_profiler.rb', line 77

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

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



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

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



86
87
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
# File 'lib/pwrake/branch/shell_profiler.rb', line 86

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



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

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