Class: Pwrake::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/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]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProfiler

Returns a new instance of Profiler.



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

def initialize
  @lock = Mutex.new
  @separator = ","
  @re_escape = /\s#{Regexp.escape(@separator)}/
  @gnu_time = false
  @id = 0
  @io = nil
end

Instance Attribute Details

#gnu_timeObject

Returns the value of attribute gnu_time.



23
24
25
# File 'lib/pwrake/profiler.rb', line 23

def gnu_time
  @gnu_time
end

#separatorObject

Returns the value of attribute separator.



23
24
25
# File 'lib/pwrake/profiler.rb', line 23

def separator
  @separator
end

Class Method Details

.format_time(t) ⇒ Object



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

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

Instance Method Details

#_puts(s) ⇒ Object



51
52
53
54
55
# File 'lib/pwrake/profiler.rb', line 51

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

#closeObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pwrake/profiler.rb', line 38

def close
  t = Time.now
  profile(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

#command(cmd, terminator) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pwrake/profiler.rb', line 65

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

#format_time(t) ⇒ Object



79
80
81
82
# File 'lib/pwrake/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



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

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 = File.open(file,"w")
  end
  _puts table_header
  t = Time.now
  profile(nil,'pwrake_profile_start',t,t)
end

#profile(task, cmd, start_time, end_time, host = "", 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
# File 'lib/pwrake/profiler.rb', line 88

def profile(task, cmd, start_time, end_time, host="", status=nil)
  status = "" if status.nil?
  id = @lock.synchronize do
    id = @id
    @id += 1
    id
  end
  if @io
    if task.kind_of? Rake::Task
      tname = task.name.inspect
      task_id = task.task_id
    else
      tname = ""
      task_id = ""
    end
    host = '"'+host+'"' if @re_escape =~ host
    _puts [id, task_id, tname, cmd.inspect,
           format_time(start_time),
           format_time(end_time),
           "%.3f" % (end_time-start_time),
           host, status].join(@separator)
  end
  if status==""
    1
  elsif @gnu_time
    /^([^,]*),/ =~ status
    Integer($1)
  else
    Integer(status)
  end
end

#table_headerObject



57
58
59
60
61
62
63
# File 'lib/pwrake/profiler.rb', line 57

def table_header
  a = HEADER_FOR_PROFILE
  if @gnu_time
    a += HEADER_FOR_GNU_TIME
  end
  a.join(@separator)
end