Project

General

Profile

Bug #13990 » grep.py

Kefu Chai, 01/05/2016 11:11 AM

 
from __future__ import print_function
import re
import sys
from collections import defaultdict

def is_new_seg(line):
return ' -1 osdmap: ' in line


def get_epoch(line, epoch):
incs = {'<': +1,
'+': +1,
'-': -1,
'=': -1}
pattern = '.* -1 osdmap: (.)#%s (\d+)' % epoch
m = re.match(pattern, line)
inc = 0
refcount = 0
if m:
tag, refcount = m.groups()
inc = incs[tag]
pattern = '.* => #%s (\d+)$' % epoch
m = re.match(pattern, line)
if m:
inc += 1
return inc, m.group(1)
elif inc:
return inc, refcount
return None


# '1: (OSDService::_add_map(OSDMap*)+0x45a) [0x7f18fd1b769a]'
# =>
# '1: (OSDService::_add_map(OSDMap*) 0x0) [0x00000000]'
def normalize_f0(line):
return re.sub('(.*)\+0x([0-9a-f]+\) \[0x[0-9a-f]+\])',
r'\1 0x0) [0x00000000]',
line)


def main(log_file, epoch):
f = open(log_file)
in_my_seg = False
segs = defaultdict(lambda: 0)
refs = 0
bt = []
total = 0
inc = 0
for line in f:
if line == '\n':
if in_my_seg:
# the innest frames are always different for the
# inc_ref and dec_ref, so normalize them
bt[1] = normalize_f0(bt[1])
# remove the version line
segs[''.join(bt[1:])] += inc
bt = []
in_my_seg = False
elif is_new_seg(line):
found = get_epoch(line, epoch)
in_my_seg = found is not None
if in_my_seg:
inc, refs = found
elif in_my_seg:
bt.append(line)
else:
pass

for bt, cnt in segs.items():
if cnt == 0:
continue
print(cnt, '\n====\n', bt)
total += cnt
print('total =', total)


if __name__ == '__main__':
main(sys.argv[1], sys.argv[2])
(1-1/9)