Class: ProcView

Inherits:
Object
  • Object
show all
Defined in:
lib/procview.rb,
lib/procview/stats.rb,
lib/procview/version.rb

Defined Under Namespace

Classes: PSTATS, Stats

Constant Summary collapse

STRACE =
%w{ /usr/bin/env strace -Tfq -e desc,network,file,process }
LSOF =
%w{ /usr/bin/env lsof }
VERSION =
'0.8.5'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = { quiet: true, debug: nil, stat: true }) ⇒ ProcView

Returns a new instance of ProcView.



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

def initialize opts = { quiet: true, debug: nil, stat: true }
    @calls = Hash.new {|h,k| h[k] = [0,0.0]}
    @unk = [] # record handles found that were not in the map
    @iomap = Hash.new{|h,k| h[k] = ProcView::Stats.new}
    @pstats = Hash.new{|h,k| h[k] = initPstats }
    @pid    = nil # used to pass the current pid around
    @sigmap = Hash.new(0)
    @dregs  = 0.0 # store unaccounted for time here
    @pwd    = nil
    @inithandles = nil
    @opts   = opts
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



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

def calls
  @calls
end

#dregsObject (readonly)

Returns the value of attribute dregs.



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

def dregs
  @dregs
end

#inithandles=(value) ⇒ Object (writeonly)

Sets the attribute inithandles

Parameters:

  • the value to set the attribute inithandles to.



20
21
22
# File 'lib/procview.rb', line 20

def inithandles=(value)
  @inithandles = value
end

#iomapObject (readonly)

Returns the value of attribute iomap.



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

def iomap
  @iomap
end

#pidObject (readonly)

Returns the value of attribute pid.



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

def pid
  @pid
end

#pstatsObject (readonly)

Returns the value of attribute pstats.



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

def pstats
  @pstats
end

#pwd=(value) ⇒ Object (writeonly)

Sets the attribute pwd

Parameters:

  • the value to set the attribute pwd to.



20
21
22
# File 'lib/procview.rb', line 20

def pwd=(value)
  @pwd = value
end

#sigmapObject (readonly)

Returns the value of attribute sigmap.



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

def sigmap
  @sigmap
end

Class Method Details

.checklsof(quiet = false) ⇒ Object



52
53
54
55
56
57
# File 'lib/procview.rb', line 52

def self.checklsof quiet = false
    lsof = `#{(ProcView::LSOF+%w{ -v 2>&1 }).join(' ')}`
    /\s+revision:\s+(?<rev>[.\d]+)\s+/ =~ lsof
    raise "'lsof' not found! Check that it is installed and on your PATH" if rev.nil?
    STDERR.puts "Using lsof #{rev}" unless quiet
end

.checkstrace(quiet = false) ⇒ Object



59
60
61
62
63
64
# File 'lib/procview.rb', line 59

def self.checkstrace quiet = false
    strace = `#{(ProcView::STRACE+%w{ -V 2>&1 }).join(' ')}`
    /^strace\s+--\s+version\s+(?<rev>[.\d]+)$/ =~ strace
    raise "'strace' not found! Check that it is installed and on your PATH" if rev.nil?
    STDERR.puts "Using strace #{rev}" unless quiet
end

Instance Method Details

#digest(line) ⇒ 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
120
121
122
123
124
125
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
# File 'lib/procview.rb', line 86

def digest line

    pid = (line.slice! /^(\d+)\s+/) ? $~[1].to_i: nil

    case line
    when /^TIME\s+/
        puts "TRACED on #{$'}" unless @opts[:quiet]

    when /^REQUEST\s+/
        puts $' unless @opts[:quiet]

    when /^LSOF/
        fields = line.split /\s+/
        unless fields.size > 9
            STDERR.print "Too few fields" unless @opts[:quiet]
            return 1
        end
        pid = fields[2].to_i
        fname = fields[9..-1].join(' ')
        case fields[4]
        when 'cwd'
            raise "PID set prior to cwd" if @pid
            @pid = pid
            @pstats[@pid].cwd = fname
        when /(\d+)\w/
            handle = $1.to_i
            @pstats[@pid].hmap[handle] = fname
        else
            STDERR.print "Invalid handle" unless @opts[:quiet]
            return 1
        end

    when /^(?<call>\w+)\((?<args>.*)\)\s+=\s+\?/x # process termination has ?
        return 0

    when /^(?<call>\w+)
            \((?<args>.*)\)\s+=\s+(?<rc>-?(\d+|\?))\s*
            (?<ret>.*)\s+
            <(?<duration>.*)>$
        /x
        return handleCall(pid, $1.to_sym, $2, $3.to_i, $4, $5.to_f)

    when / <unfinished ...>/
        @pstats[pid].resume.push $`

    when /<... (\w+) resumed> /
        call = $1.to_sym
        rest = $'.chomp
        prev = @pstats[pid].resume.find_index {|f| f =~ /^#{call}/}
        unless prev
            STDERR.print "Failed to find resumable call" unless @opts[:quiet]
            return 1
        end
        first = @pstats[pid].resume.delete_at(prev)
        return digest (pid.to_s+" "+first+rest)

    when /^---\s*(\w+)\s*/
        signal = $1.to_sym
        @sigmap[signal] += 1
    when /^+++.*+++$/
    else
        STDERR.print "Unknown" unless @opts[:quiet]
        return 1
    end
    return 0
end

#handleCall(pid, call, args, rc, ret, duration) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/procview.rb', line 183

def handleCall pid, call, args, rc, ret, duration
    pstat = @pstats[pid]
    case call

    when :accept, :accept4 
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :other, duration
        pstat.hmap[rc] = StraceArgs.sockaddr! args

    when :clone
        StraceArgs.nextcomma! args
        flags = StraceArgs.flags! args
        @pstats[rc] = initPstats @pstats[pid], flags.include?(:CLONE_FS), flags.include?(:CLONE_FILES)
        @dregs += duration

    when :stat, :lstat
        if @opts[:stat] and rc == -1
            @dregs += duration
        else
            file = StraceArgs.name! args
            @iomap[File.absolute_path(file, pstat.cwd)].acc! :other, duration
        end

    when :execve, :access, :readlink, :chmod
        file = StraceArgs.name! args
        @iomap[File.absolute_path(file, pstat.cwd)].acc! :other, duration

    when :open, :unlink
        file = StraceArgs.name! args
        pstat.hmap[rc] = File.absolute_path(file, pstat.cwd)
        accumulate pstat.hmap, rc, :other, duration

    when :mkdir, :rmdir, :getcwd, :statfs
        file = StraceArgs.name! args
        @iomap[File.absolute_path(file, pstat.cwd)].acc! :other, duration

    when :chdir
        file = StraceArgs.name! args
        pstat.cwd = File.absolute_path(file, pstat.cwd)
        @iomap[pstat.cwd].acc! :other, duration

    when :close
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :other, duration
        pstat.hmap.delete fd

    when :pipe, :pipe2
        fds = StraceArgs.afds! args
        pstat.hmap[fds[0]] = call
        accumulate pstat.hmap, fds[0], :other, duration/2.0
        pstat.hmap[fds[1]] = call
        accumulate pstat.hmap, fds[1], :other, duration/2.0

    when :socket
        pstat.hmap[rc] = Stats.new
        accumulate pstat.hmap, rc, :other, duration

    when :getsockopt, :setsockopt
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :other, duration

    when :connect, :getpeername
        fd = StraceArgs.fd! args
        name = StraceArgs.sockaddr! args
        accumulate pstat.hmap, fd, :other, duration
        movetoIOmap name, fd, pstat.hmap

    when :bind
        fd = StraceArgs.fd! args
        name = StraceArgs.sockaddr! args
        accumulate pstat.hmap, fd, :other, duration
        movetoIOmap name, fd, pstat.hmap

    when :listen
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :other, duration

    when :dup, :fsync
        fd = args.slice(/^\d+/).to_i
        pstat.hmap[rc] = pstat.hmap[fd]
        accumulate pstat.hmap, rc, :other, duration

    when :dup2, :dup3
        fda = StraceArgs.fd! args
        fdb = StraceArgs.fd! args
        pstat.hmap[fdb] = pstat.hmap[fda]
        accumulate pstat.hmap, fdb, :other, duration

    when :flock, :fstat, :ioctl, :fcntl, :getsockname, :shutdown
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :other, duration

    # Calls classed as 'wait'

    when :select
        max = StraceArgs.fd! args
        rfds = StraceArgs.afds!(args).to_set
        wfds = StraceArgs.afds!(args).to_set
        efds = StraceArgs.afds!(args).to_set
        fds = rfds+wfds+efds
        fds.each{|fd| accumulate pstat.hmap, fd, :wait, duration/fds.size}

    when :poll, :ppoll
        fds = StraceArgs.fds(args.slice(/^\[(.*?)\]/, 1))
        if (rc > 0)
            rds = StraceArgs.fds(ret.slice(/^\(\[(.*?)\]\)/, 1))
            rds.each{|rd| accumulate pstat.hmap, rd, :wait, duration/rds.size }
        else
            fds.each{|fd| accumulate pstat.hmap, fd, :wait, duration/fds.size }
        end

    when :epoll_ctl
        epfd = StraceArgs.fd! args
        act = StraceArgs.define! args
        fd  = StraceArgs.fd! args
        case act
        when :EPOLL_CTL_ADD, :EPOLL_CTL_MOD
            pstat.emap[epfd].add fd
        when :EPOLL_CTL_DEL
            pstat.emap[epfd].delete fd
        end

    when :epoll_wait, :epoll_pwait
        epfd = StraceArgs.fd! args
        if rc < 1
            pstat.emap[epfd].each do |fd|
                accumulate pstat.hmap, fd, :wait, duration/pstat.emap[epfd].size
            end
        else
            epfds = StraceArgs.epollevents! args
            epfds.each{|fd| accumulate pstat.hmap, fd, :wait, duration/epfds.size }
        end

    when :pread, :read, :recvfrom, :lseek, :recvmsg, :getdents
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :read, duration

    when :pwrite, :write, :writev, :sendto
        fd = StraceArgs.fd! args
        accumulate pstat.hmap, fd, :write, duration

    when :arch_prctl, :wait4
        @dregs += duration

    else
        STDERR.print "Unhandled call" unless @opts[:quiet]
        return 1
    end
    @calls[call][0] += 1
    @calls[call][1] += duration
    return 0
end

#initPstats(parent = nil, same_wd = false, same_fds = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/procview.rb', line 22

def initPstats parent = nil, same_wd = false, same_fds = false
    if parent
        cwd = same_wd ? parent.cwd : parent.cwd.clone
        hmap = same_fds ? parent.hmap : parent.hmap.clone
        emap = same_fds ? parent.emap : parent.emap.clone
        return PSTATS.new( cwd, hmap, emap, [] )
    else
        stats = PSTATS.new( @pwd,
               Hash.new{|h,k| @unk.push k; h[k] = "h#{k}".to_sym;},
               Hash.new{|h,k| h[k] = Set.new},
               []
        )
        @inithandles.each {|k,v| stats.hmap[k] = v} if @inithandles
        return stats
    end
end

#load(io) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/procview.rb', line 66

def load io
    badLines = 0
    while line = io.gets
        @opts[:debug].puts line if @opts[:debug]
        begin
            if digest(line) > 0
                puts ": #{line}" unless @opts[:quiet]
                badLines += 1
            end
        rescue StandardError => e
            STDERR.puts "#{e.message} (#{e.backtrace[0]}): #{line}" unless @opts[:quiet]
            badLines += 1
        end
    end
    unless @opts[:quiet]
        STDERR.puts "WARNING: Unknown handles: #{@unk.join(', ')}" if @unk.size > 0
        STDERR.puts "WARNING: #{badLines} bad lines" if badLines > 0
    end
end