Class: Procview::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/procview/runner.rb

Constant Summary collapse

FORMAT =
"%7s"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



18
19
20
# File 'lib/procview/runner.rb', line 18

def initialize(argv)
  @opts = Options.new(argv)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



14
15
16
# File 'lib/procview/runner.rb', line 14

def opts
  @opts
end

Instance Method Details

#mkTitleFormatObject



109
110
111
112
113
114
# File 'lib/procview/runner.rb', line 109

def mkTitleFormat
  format = FORMAT.dup
  format.prepend " " * 7 if @opts.counts
  format += " " * 7 if @opts.variances
  Array.new(4, format).join(' ') + " %s\n"
end

#printCalls(pva) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/procview/runner.rb', line 158

def printCalls pva
  puts "Duration by Call Type"
  totalType = 0.0
  pva.calls.keys.sort{|a,b| pva.calls[b][1] <=> pva.calls[a][1]}.each do |k|
    v = pva.calls[k]
    printf "#{FORMAT} #{k}: #{v[0]}\n", v[1].to_duration(@opts.digits)
    totalType += v[1]
  end
  printf "#{FORMAT} Total duration \n\n", totalType.to_duration(@opts.digits)

  if pva.sigmap.size > 0
    puts "Signals"
    pva.sigmap.keys.sort{|a,b| pva.sigmap[b] <=> pva.sigmap[a]}.each do |k|
      puts "\t#{k}: #{pva.sigmap[k]}"
    end
  end
end

#printLine(name, stat) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/procview/runner.rb', line 116

def printLine name, stat
      (0..3).each do |ix|
        printf "%5s @", stat.counts(1)[ix] if @opts.counts
        printf "#{FORMAT}", stat.durations(@opts.digits)[ix]
        printf "±#{FORMAT}", stat.deviations(@opts.digits)[ix] if @opts.variances
        printf " "
      end
      puts name # as it may contain %
end

#printReport(pva) ⇒ Object



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
# File 'lib/procview/runner.rb', line 126

def printReport pva

  titleformat = mkTitleFormat

  puts "Duration by Source"
  printf titleformat, 'OTHER', 'WAIT', 'READ', 'WRITE', 'Name'
  totalSource = Procview::Stats.new
  pva.iomap.keys.sort{|a,b| pva.iomap[b].sum <=> pva.iomap[a].sum}.each do |k|
    binned = false
    @opts.buckets.values.each do |v|
      binned = k =~ v[0]
      if binned
        v[1].add! pva.iomap[k]
        break
      end
    end
    unless binned
      printLine k, pva.iomap[k]
    end
    totalSource.add! pva.iomap[k]
  end
  @opts.buckets.each do |k,v|
    name = k.is_a?(Symbol) ? k.to_s: "Sum: #{k}"
    printLine name, v[1]
  end
  printLine "Total Duration", totalSource
  printf titleformat, pva.dregs.to_duration(@opts.digits),"","","", 'Unassigned'

  return totalSource.sum

end

#runObject



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/procview/runner.rb', line 22

def run
  if @opts.debug
    @opts.debug = File.open("/tmp/procview.debug.#{$$}", "w")
    $stderr.puts "Writing debug output to file #{@opts.debug.path}"
  end

  unless @opts.filename
    Procview.checklsof @opts.quiet
    Procview.checkstrace @opts.quiet
  end

  pva = Analyzer.new @opts.to_h

  if @opts.filename
    file = File.open(@opts.filename, "r")
    unless file
      $stderr.puts "Can't open input file #{@opts.filename}"
      exit 2
    end
    if @opts.raw
      pva.pwd = FileUtils.pwd
      pva.inithandles = { 0 => :stdin, 1 => :stdout, 2 => :stderr }
    end
    pva.load(file)
  else
    fifoname = "/tmp/procview.fifo.#{$$}"
    res = `mkfifo #{fifoname}`
    begin
      cmdargs = Procview::STRACE + [ "-o", fifoname ]
      if @opts.pid
        puts "Tracing PID #{@opts.pid} (Ctrl-C to disconnect)"
        cmdargs += [ "-p", @opts.pid.to_s ]
      else
        cmdargs += ARGV
      end
      stpid = spawn(*cmdargs)
      trap 'INT' do
        Process.kill("INT", stpid)
        puts "Sent #{stpid} SIGINT"
      end
      if @opts.pid
        IO.popen(Procview::LSOF + ["-p", @opts.pid.to_s]) do |lsof|
          while line = lsof.gets
            fields = line.split(/\s+/)
            next unless fields[3] =~ /(\d+\w|cwd)/
            @opts.debug.puts "LSOF #{line}\n" if @opts.debug
            pva.digest "LSOF #{line}"
          end
        end
      else
        pva.pwd = FileUtils.pwd
        pva.inithandles = { 0 => :stdin, 1 => :stdout, 2 => :stderr }
        # We don't know the actual traced process PID so put -1
        # and let the analyzer work things out
        @opts.debug.puts "          LSOF bash    -1 root  cwd    DIR  253,0     4096 655363 \#{pva.pwd}\n          LSOF bash    -1 root    0u   CHR  136,0      0t0      3 stdin\n          LSOF bash    -1 root    1u   CHR  136,0      0t0      3 stdout\n          LSOF bash    -1 root    2u   CHR  136,0      0t0      3 stderr\n          FAKELSOF\n      end\n      File.open(fifoname, \"r\") do |fifo|\n        pva.load(fifo)\n      end\n      Process.wait(stpid)\n    ensure\n      FileUtils.rm_f fifoname\n    end\n  end\n\n  totalBySource = printReport pva\n  totalByCall   = pva.calls.values.inject(0.0){|s,v| s + v[1]}\n\n  unless @opts.quiet\n    $stderr.puts \"Warnings (\#{pva.warnings.size})\\n\" if pva.warnings.size > 0\n    pva.warnings.each { |w| $stderr.puts w }\n  end\n\n  unless totalByCall.near? totalBySource + pva.dregs, 0.001\n    puts \"WARNING IO and call totals don't add up!\"\n    printf \"\\tDelta %s (The dregs were %s)\\n\", (totalByCall - totalBySource).to_duration(@opts.digits), pva.dregs.to_duration(@opts.digits)\n  end\n\n  printCalls(pva) if @opts.calls\n\nend\n".gsub(/^\s+/,'')  if @opts.debug