Class: StraceLog::Stat

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

Defined Under Namespace

Classes: CallCounter, Counter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sum: false, table: '/etc/mtab', column: 2) ⇒ Stat

Returns a new instance of Stat.



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/strace_log.rb', line 145

def initialize(sum:false,table:'/etc/mtab',column:2)
  @sum = sum
  @stat = {}
  @total = CallCounter.new('*')
  if @sum
    a = open(table,'r').each_line.map do |line|
      line.split(/\s+/)[column-1]
    end.sort.reverse
    @paths = Hash[ a.map{|x| [x, /^#{x.sub(/\/$/,'')}($|\/)/] } ]
  end
end

Instance Attribute Details

#statObject (readonly)

Returns the value of attribute stat.



157
158
159
# File 'lib/strace_log.rb', line 157

def stat
  @stat
end

#totalObject (readonly)

Returns the value of attribute total.



157
158
159
# File 'lib/strace_log.rb', line 157

def total
  @total
end

Instance Method Details

#get_fd(arg) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/strace_log.rb', line 166

def get_fd(arg)
  if /([-\d]+)(<.*>)?/ =~ arg
    x = $1.to_i
    return x
  else
    raise
  end
end

#parse(a) ⇒ Object



159
160
161
162
163
164
# File 'lib/strace_log.rb', line 159

def parse(a)
  @fd2path = ["stdin", "stdout", "stderr"]
  a.each do |line|
    stat_call( ParsedCall.new(line) )
  end
end

#rename(call) ⇒ Object



246
247
248
249
250
251
252
253
254
# File 'lib/strace_log.rb', line 246

def rename(call)
  if call.ret != "-1"
    oldpath = call.args[0]
    newpath = call.args[1]
    ioc = @stat[newpath] = (@stat[oldpath] ||= CallCounter.new(oldpath))
    ioc.rename_as(newpath)
    @stat.delete(oldpath)
  end
end

#stat_call(call) ⇒ Object



175
176
177
178
179
180
181
182
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
# File 'lib/strace_log.rb', line 175

def stat_call(call)
  path = nil

  case call.func

  when /^(execve|l?stat|(read|un)?link|getc?wd|access|mkdir|mknod|chmod|chown)$/
    path = call.args[0]

  when /^open$/
    path = call.args[0]
    if call.ret != "-1"
      fd = call.ret.to_i
      @fd2path[fd] = path
    end

  when /^connect$/
    fd = get_fd(call.args[0])
    @fd2path[fd] = path = "socket"

  when /^close$/
    fd = get_fd(call.args[0])
    path = @fd2path[fd]
    if call.ret != "-1"
      @fd2path[fd] = nil
    end

  when /^(readv?|writev?)$/
    call.set_size
    fd = get_fd(call.args[0])
    path = @fd2path[fd]

  when /^(fstat|fchmod|[fl]chown|lseek|ioctl|fcntl|getdents|sendto|recvmsg)$/
    fd = get_fd(call.args[0])
    path = @fd2path[fd]

  when /^rename$/
    rename(call)
    path = call.args[1]

  when /^dup[23]?$/
    fd = get_fd(call.args[0])
    path = @fd2path[fd]
    if call.ret != "-1"
      fd2 = call.ret.to_i
      @fd2path[fd2] = @fd2path[fd]
    end

  when /^mmap$/
    fd = get_fd(call.args[4])
    path = @fd2path[fd] if fd >= 0

  when NilClass
    return
  end

  @total.count(call)

  if path
    if @sum
      realpath = File.exist?(path) ? Pathname.new(path).realdirpath.to_s : path
      @paths.each do |mp,re|
        if re =~ realpath
          (@stat[mp] ||= CallCounter.new(mp)).count(call)
          return
        end
      end
    end
    (@stat[path] ||= CallCounter.new(path)).count(call)
  end
end

#write(file = nil) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/strace_log.rb', line 256

def write(file=nil)
  block = proc do |w|
    w << ["path","syscall","calls","errors","time","size"]
    @total.each{|item| w << item}
    @stat.each do |path,cntr|
      cntr.each{|item| w << item}
    end
  end
  if file
    CSV.open(file,'w',&block)
  else
    CSV.instance(&block)
  end
end