Class: Jkr::Blktrace

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_basename) ⇒ Blktrace

Returns a new instance of Blktrace.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jkr/blktrace.rb', line 14

def initialize(input_basename)
  input_basename = Pathname.new(input_basename).relative_path_from(Pathname.new(`pwd`.strip)).to_s
  if Dir.glob(input_basename + ".blktrace.*").size > 0
    @input_basename = input_basename
    puts "multi file: #{input_basename}"
  else
    if File.exists?(input_basename)
      @input_singlefile = input_basename
      puts "single file: #{input_basename}"
    else
      raise ArgumentError.new("No such blktrace data: #{input_basename}")
    end
  end
end

Class Method Details

.each(input_basename, &block) ⇒ Object



9
10
11
# File 'lib/jkr/blktrace.rb', line 9

def each(input_basename, &block)
  self.new(input_basename).each(&block)
end

.open(input_basename) ⇒ Object



5
6
7
# File 'lib/jkr/blktrace.rb', line 5

def open(input_basename)
  self.new(input_basename)
end

Instance Method Details

#each(option = {}, &block) ⇒ Object



55
56
57
58
# File 'lib/jkr/blktrace.rb', line 55

def each(option = {}, &block)
  self.map(option, &block)
  true
end

#map(option = {}, &block) ⇒ Object



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jkr/blktrace.rb', line 60

def map(option = {}, &block)
  if ! option.include?(:cache)
    option[:cache] = true
  end
  if option[:limit]
    limit = "| head -n #{option[:limit]}"
  else
    limit = ""
  end

  if @input_basename
    cache_file_path = @input_basename + ".cache"
  else
    cache_file_path = @input_singlefile + ".cache"
  end

  if option[:cache] && File.exists?(cache_file_path)
    records = Marshal.load(File.open(cache_file_path))
  else
    records = []
    issues = []
    if @input_basename
      cmd = "blkparse -f \"bt\\t%c\\t%s\\t%T.%t\\t%a\\t%d\\t%S\\t%n\\n\" -i #{@input_basename} #{limit} | grep '^bt'|sort -k4,4"
    elsif
      cmd = "cat #{@input_singlefile}|blkparse -f \"bt\\t%c\\t%s\\t%T.%t\\t%a\\t%d\\t%S\\t%n\\n\" -i - #{limit} | grep '^bt'|sort -k4,4"
    end
    IO.popen(cmd, "r") do |io|
      while line = io.gets
        _, cpu, seqno, time, action, rwbs, pos_sec, sz_sec = line.split("\t")
        cpu = cpu.to_i
        seqno = seqno.to_i
        time = time.to_f
        pos_sec = pos_sec.to_i
        sz_sec = sz_sec.to_i
        
        record = [cpu, seqno, time, action, rwbs, pos_sec, sz_sec]
        if action == "D"
          issues.push(record)
        elsif action == "C"
          # check pos and sz
          del_idx = nil
          issues.each_with_index do |rec, idx|
            if pos_sec == rec[5] && sz_sec == rec[6]
              del_idx = idx
              rt = time - rec[2]
              record.push(rt) # append response time
              records.push(record)
              break
            end
          end
          if del_idx.nil?
            puts("Unmatched complete record: #{record.inspect}")
            # raise StandardError.new("Unmatched complete record: #{record.inspect}")
            next
          end
          issues.delete_at(del_idx)
        else
          raise NotImplementedError.new("Action #{action} handler is not implemented.")
        end
      end
    end
    File.open(cache_file_path, "w") do |file|
      Marshal.dump(records, file)
    end
  end

  records.map do |*record|
    block.call(*record)
  end
end

#raw_each(option = {}, &block) ⇒ Object



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
# File 'lib/jkr/blktrace.rb', line 29

def raw_each(option = {}, &block)
  if option[:limit]
    limit = "| head -n #{option[:limit]}"
  else
    limit = ""
  end
  if @input_basename
    cmd = "blkparse -f \"bt\\t%c\\t%s\\t%T.%t\\t%a\\t%d\\t%S\\t%n\\n\" -i #{@input_basename} #{limit} | grep '^bt'|sort -k4,4"
  elsif @input_singlefile
    cmd = "cat #{@input_singlefile}|blkparse -f \"bt\\t%c\\t%s\\t%T.%t\\t%a\\t%d\\t%S\\t%n\\n\" -i - #{limit} | grep '^bt'|sort -k4,4"
  end
  IO.popen(cmd, "r") do |io|
    while line = io.gets
      _, cpu, seqno, time, action, rwbs, pos_sec, sz_sec = line.split("\t")
      cpu = cpu.to_i
      seqno = seqno.to_i
      time = time.to_f
      pos_sec = pos_sec.to_i
      sz_sec = sz_sec.to_i
      record = [cpu, seqno, time, action, rwbs, pos_sec, sz_sec]

      block.call(record)
    end
  end
end