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

#printCalls(pva) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/procview/runner.rb', line 142

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

#printReport(pva) ⇒ Object



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

def printReport pva

  ioformat = Array.new(4, FORMAT).join(' ')

  puts "Duration by Source"
  printf "#{ioformat} %s\n", '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
      printf "#{ioformat} ", *(pva.iomap[k].map{|m|m.to_duration(@opts.digits)})
      puts k # as it may contain %
    end
    totalSource.add! pva.iomap[k]
  end
  @opts.buckets.each do |k,v|
    name = k.is_a?(Symbol) ? k.to_s: "Sum: #{k}"
    printf "#{ioformat} #{name}\n", *(v[1].map{|m|m.to_duration(@opts.digits)})
  end
  printf "#{ioformat} Total duration\n", *(totalSource.map{|m|m.to_duration(@opts.digits)})
  printf "#{FORMAT} %34s\n", 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 <<-FAKELSOF.gsub(/^\s+/,'')  if @opts.debug
          LSOF bash    -1 root  cwd    DIR  253,0     4096 655363 #{pva.pwd}
          LSOF bash    -1 root    0u   CHR  136,0      0t0      3 stdin
          LSOF bash    -1 root    1u   CHR  136,0      0t0      3 stdout
          LSOF bash    -1 root    2u   CHR  136,0      0t0      3 stderr
          FAKELSOF
      end
      File.open(fifoname, "r") do |fifo|
        pva.load(fifo)
      end
      Process.wait(stpid)
    ensure
      FileUtils.rm_f fifoname
    end
  end

  totalBySource = printReport pva
  totalByCall   = pva.calls.values.inject(0.0){|s,v| s + v[1]}

  unless @opts.quiet
    $stderr.puts "Warnings (#{pva.warnings.size})\n" if pva.warnings.size > 0
    pva.warnings.each { |w| $stderr.puts w }
  end

  unless totalByCall.near? totalBySource + pva.dregs, 0.001
    puts "WARNING IO and call totals don't add up!"
    printf "\tDelta %s (The dregs were %s)\n", (totalByCall - totalBySource).to_duration(@opts.digits), pva.dregs.to_duration(@opts.digits)
  end

  printCalls(pva) if @opts.calls

end