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
|
#!/usr/bin/env python3
""" sum_debug_alloc
Sum lines reported from codec2-dev/src/debug_alloc.h and report
Lines like:
CALLOC: run_ldpc_decoder(112, 32)
MALLOC: other_func(238)
"""
import fileinput
by_func = {}
by_addr = {}
def new_func():
rec = {}
rec['cur'] = 0
rec['max'] = 0
return(rec)
def new_addr():
rec = {}
rec['func'] = 0 # Where allocated, may not be same as where free'd!
rec['size'] = 0
return(rec)
for line in fileinput.input():
if ((line.startswith("MALLOC:")) or (line.startswith("CALLOC:"))):
if (line.startswith("MALLOC:")):
words = line.translate( str.maketrans("()", " ") ).strip().split()
func = words[1]
addr = words[2]
size = int(words[3])
elif (line.startswith("CALLOC:")):
words = line.translate( str.maketrans("(,)", " ") ).strip().split()
func = words[1]
addr = words[2]
size = int(words[3]) * int(words[4])
#print("Alloc: {} to {} in {}".format(size, addr, func))
if (not (func in by_func)): by_func[func] = new_func()
data = by_func[func]
data['cur'] += size
if (data['cur'] > data['max']):
data['max'] = data['cur']
if (addr in by_addr):
print("Error: duplicate allocation to {} in {}".format(addr, func))
else:
by_addr[addr] = new_addr()
by_addr[addr]['func'] = func
by_addr[addr]['size'] = size
elif (line.startswith("FREE:")):
words = line.translate( str.maketrans("(,)", " ") ).strip().split()
func = words[1]
addr = words[2]
if (addr in by_addr):
func_a = by_addr[addr]['func']
by_func[func_a]['cur'] -= by_addr[addr]['size']
del(by_addr[addr])
else:
print("Error: free of unallocated location {} in {}".format(addr, func))
#print("Free: {}:{} in {} to {}".format(func_a, addr, func, by_func[func_a]['cur']))
#####
total = 0
for func, sum in by_func.items():
max = by_func[func]['max']
print("{} = {}".format(func, max))
total += max
print("Total = {}".format(total))
|