1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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
|
#!/usr/bin/env ruby
require 'time'
require 'optparse'
unless Array.instance_methods.include? :to_h
class Array
def to_h
h = {}
each { |k,v| h[k]=v }
h
end
end
end
# statedump.c:gf_proc_dump_mempool_info uses a five-dash record separator,
# client.c:client_fd_lk_ctx_dump uses a six-dash record separator.
ARRSEP = /^(-{5,6}=-{5,6})?$/
HEAD = /^\[(.*)\]$/
INPUT_FORMATS = %w[statedump json]
format = 'json'
input_format = 'statedump'
tz = '+0000'
memstat_select,memstat_reject = //,/\Z./
OptionParser.new do |op|
op.banner << " [<] <STATEDUMP>"
op.on("-f", "--format=F", "json/yaml/memstat(-[plain|human|json])") { |s| format = s }
op.on("--input-format=F", INPUT_FORMATS.join(?/)) { |s| input_format = s }
op.on("--timezone=T",
"time zone to apply to zoneless timestamps [default UTC]") { |s| tz = s }
op.on("--memstat-select=RX", "memstat: select memory types matching RX") { |s|
memstat_select = Regexp.new s
}
op.on("--memstat-reject=RX", "memstat: reject memory types matching RX") { |s|
memstat_reject = Regexp.new s
}
end.parse!
if format =~ /\Amemstat(?:-(.*))?/
memstat_type = $1 || 'plain'
unless %w[plain human json].include? memstat_type
raise "unknown memstat type #{memstat_type.dump}"
end
format = 'memstat'
end
repr, logsep = case format
when 'yaml'
require 'yaml'
[proc { |e| e.to_yaml }, "\n"]
when 'json', 'memstat'
require 'json'
[proc { |e| e.to_json }, " "]
else
raise "unkonwn format '#{format}'"
end
formatter = proc { |e| puts repr.call(e) }
INPUT_FORMATS.include? input_format or raise "unkwown input format '#{input_format}'"
dumpinfo = {}
# parse a statedump entry
elem_cbk = proc { |s,&cbk|
arraylike = false
s.grep(/\S/).empty? and next
head = nil
while s.last =~ /^\s*$/
s.pop
end
body = catch { |misc2|
s[0] =~ HEAD ? (head = $1) : (throw misc2)
body = [[]]
s[1..-1].each { |l|
if l =~ ARRSEP
arraylike = true
body << []
next
end
body.last << l
}
body.reject(&:empty?).map { |e|
ea = e.map { |l|
k,v = l.split("=",2)
m = /\A(0|-?[1-9]\d*)(\.\d+)?\Z/.match v
[k, m ? (m[2] ? Float(v) : Integer(v)) : v]
}
begin
ea.to_h
rescue
throw misc2
end
}
}
if body
cbk.call [head, arraylike ? body : (body.empty? ? {} : body[0])]
else
STDERR.puts ["WARNING: failed to parse record:", repr.call(s)].join(logsep)
end
}
# aggregator routine
aggr = case format
when 'memstat'
meminfo = {}
# commit memory-related entries to meminfo
proc { |k,r|
case k
when /memusage/
(meminfo["GF_MALLOC"]||={})[k] ||= r["size"] if k =~ memstat_select and k !~ memstat_reject
when "mempool"
r.each {|e|
kk = "mempool:#{e['pool-name']}"
(meminfo["mempool"]||={})[kk] ||= e["size"] if kk =~ memstat_select and kk !~ memstat_reject
}
end
}
else
# just format data, don't actually aggregate anything
proc { |pair| formatter.call pair }
end
# processing the data
case input_format
when 'statedump'
acc = []
$<.each { |l|
l = l.strip
if l =~ /^(DUMP-(?:START|END)-TIME):\s+(.*)/
dumpinfo["_meta"]||={}
(dumpinfo["_meta"]["date"]||={})[$1] = Time.parse([$2, tz].join " ")
next
end
if l =~ HEAD
elem_cbk.call(acc, &aggr)
acc = [l]
next
end
acc << l
}
elem_cbk.call(acc, &aggr)
when 'json'
$<.each { |l|
r = JSON.load l
case r
when Array
aggr[r]
when Hash
dumpinfo.merge! r
end
}
end
# final actions: output aggregated data
case format
when 'memstat'
ma = meminfo.values.map(&:to_a).inject(:+)
totals = meminfo.map { |coll,h| [coll, h.values.inject(:+)] }.to_h
tt = ma.transpose[1].inject(:+)
summary_sep,showm = case memstat_type
when 'json'
["", proc { |k,v| puts({type: k, value: v}.to_json) }]
when 'plain', 'human'
# human-friendly number representation
hr = proc { |n|
qa = %w[B kB MB GB]
q = ((1...qa.size).find {|i| n < (1 << i*10)} || qa.size) - 1
"%.2f%s" % [n.to_f / (1 << q*10), qa[q]]
}
templ = "%{val} %{key}"
tft = proc { |t| t }
nft = if memstat_type == 'human'
nw = [ma.transpose[1], totals.values, tt].flatten.map{|n| hr[n].size}.max
proc { |n|
hn = hr[n]
" " * (nw - hn.size) + hn
}
else
nw = tt.to_s.size
proc { |n| "%#{nw}d" % n }
end
## Alternative template, key first:
# templ = "%{key} %{val}"
# tw = ma.transpose[0].map(&:size).max
# tft = proc { |t| t + " " * [tw - t.size, 0].max }
# nft = (memstat_type == 'human') ? hr : proc { |n| n }
["\n", proc { |k,v| puts templ % {key: tft[k], val: nft[v]} }]
else
raise 'this should be impossible'
end
ma.sort_by { |k,v| v }.each(&showm)
print summary_sep
totals.each { |coll,t| showm.call "Total #{coll}", t }
showm.call "TOTAL", tt
else
formatter.call dumpinfo
end
|