Created
April 6, 2018 06:40
-
-
Save siddontang/6ca422aad494bf2968787e9a9d559fa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
def parse(): | |
if len(sys.argv) != 2: | |
print 'must pass memory trace file' | |
sys.exit(2) | |
file_name = sys.argv[1] | |
# [addr, size, stack] | |
mem_info = [0, 0, ""] | |
mem_stats = {} | |
with open(file_name) as f: | |
for line in f: | |
line = line.strip() | |
if line.startswith("malloc"): | |
if mem_info[0] > 0: | |
addr = mem_info[0] | |
mem_stats[addr] = mem_info | |
line2 = line.split(",") | |
v1 = line2[0].split(":") | |
v2 = line2[1].split(":") | |
addr = int(v1[1].strip(), 0) | |
size = int(v2[1].strip(), 0) | |
mem_info = [addr, size, ""] | |
elif line.startswith("free"): | |
line2 = line.split(":") | |
addr = int(line2[1].strip(), 0) | |
if addr in mem_stats: | |
del mem_stats[addr] | |
else: | |
mem_info[2] += line + "\n" | |
stack_stats = {} | |
for m in mem_stats.values(): | |
stack = m[2] | |
if stack not in stack_stats: | |
# [size, count, stack] | |
stack_stats[stack] = [m[1], 1, stack] | |
else: | |
info = stack_stats[stack] | |
info[0] += m[1] | |
info[1] += 1 | |
for s in sorted(stack_stats.values(), reverse=True): | |
print "size: ", s[0], " count: ", s[1], "\n", s[2] | |
if __name__ == "__main__": | |
parse() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment