-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallgrind_to_csv.py
executable file
·77 lines (62 loc) · 2.32 KB
/
callgrind_to_csv.py
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
#!/usr/bin/env python3.5
import subprocess
import re
import os
import ipdb
class Fingerprint:
exec_file = "/usr/bin/callgrind_annotate"
def __init__(self, filename):
self.functions = {}
self.filename = ""
p = subprocess.Popen([self.exec_file, filename],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.decode("ascii")
fmatches = re.search(r"target: .*\/([^\/]+\.pcap)", out, re.MULTILINE)
if fmatches:
self.filename = fmatches.groups(0)[0]
else:
return None
lines = out.split("\n")
for line in lines:
m = re.search('^\s*([\d,]+) .*:(\S+).*$', line)
if m:
match = m.groups(1)
count = match[0]
count = int(count.replace(",", ""))
self.functions[match[1]] = count
def compare_to(self, otherFingerprint):
diffs = {}
if len(self.functions) == 0:
return 1.0
for function in self.functions:
if function in otherFingerprint.functions:
if self.functions[function] > otherFingerprint.functions[function]:
high = self.functions[function]
low = otherFingerprint.functions[function]
else:
high = otherFingerprint.functions[function]
low = self.functions[function]
diffs[function] = 1. - float(low)/float(high)
else:
diffs[function] = 1.
for function in otherFingerprint.functions:
if function not in self.functions:
diffs[function] = 1.
sum = 0.
for value in diffs.values():
sum += value
return 1-(sum / len(diffs.keys()))
fingerprints = {}
for i in os.listdir("output"):
fprint = Fingerprint("output/" + i)
if fprint is None:
continue
highest_diff_print = 0
highest_diff = 0.0
for f in fingerprints.keys():
if highest_diff < fprint.compare_to(f):
highest_diff_print = f.filename
highest_diff = fprint.compare_to(f)
print("%s\t%f\t%s" % (fprint.filename, highest_diff, highest_diff_print))
fingerprints[fprint] = highest_diff