-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgengraph.py
executable file
·192 lines (148 loc) · 7.35 KB
/
gengraph.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
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
#!/usr/bin/env python3
import os
import json
from os import listdir
from os.path import isfile, join
import matplotlib
import matplotlib.pyplot as plt; plt.rcdefaults()
import re
import argparse
from collections import OrderedDict, defaultdict
from ccc.ccc import get_cc_from_callgrind_file
def parse_ciphersuite_names_from_file(ciphers_file_path):
CIPHER_ID_NAME_REGEX = r'(?P<id>\d+) (?P<name>[^ ]*?)( |$|\r?\n)'
pattern = re.compile(CIPHER_ID_NAME_REGEX)
ciphersuite_name = OrderedDict()
with open(ciphers_file_path, 'r') as ciphers_file:
lines = ciphers_file.readlines()
print(f'{ciphers_file_path} has {len(lines)} lines')
for line in lines:
if line is os.linesep:
continue
res = pattern.search(line)
id, name = int(res.group('id')), res.group('name')
ciphersuite_name[id] = name
return ciphersuite_name
def parse_callgrind_cli_srv_file_names(path):
CLI_FILE_REGEX = r'.*?callgrind\.out\.client\.(?P<cipher_id>\d+)'
SRV_FILE_REGEX = r'.*?callgrind\.out\.server\.(?P<cipher_id>\d+)'
cli_pattern = re.compile(CLI_FILE_REGEX)
srv_pattern = re.compile(SRV_FILE_REGEX)
cli_files = {}
srv_files = {}
all_files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
for file_name in all_files:
res = cli_pattern.search(file_name)
if res is not None:
cipher_id = int(res.group('cipher_id'))
cli_files[cipher_id] = file_name
continue
res = srv_pattern.search(file_name)
if res is not None:
cipher_id = int(res.group('cipher_id'))
srv_files[cipher_id] = file_name
return cli_files, srv_files
def parse_callgrind_cpu_cycles_from_files(funcs, cipher_id_file, entity):
profiling = defaultdict(dict) # {function_name : {ciphersuite_id : num_cycles}}
num_funcs = len(funcs)
funcs_parsed = 0
for func in funcs:
funcs_parsed = 1
print(f'Parsing for {entity} {func} {funcs_parsed}/{num_funcs}]...')
for cipher_id, file_name in cipher_id_file.items():
print(f'\tparsing for ciphersuite {cipher_id}...')
cipher_id = int(cipher_id)
num_cycles = get_cc_from_callgrind_file(file_name, func)
profiling[func][cipher_id] = num_cycles
print(f'\t\t num cycles: {num_cycles}')
return profiling
def dump_json_ids(cli_profiling, srv_profiling, file_name):
obj = {'client': cli_profiling, 'server': srv_profiling}
res = json.dumps(obj)
with open(file_name, 'w') as out_file:
out_file.write(res)
def print_total_ciphers_profiled_stats(cli_prof, srv_prof, cli_funcs, srv_funcs):
print('Total client ciphersuites profiled: ', end='')
num_cli_ciphers = 0
if len(cli_funcs) > 0:
first_func = cli_funcs[0]
num_cli_ciphers = len(cli_prof[first_func])
print(num_cli_ciphers)
print('Total server ciphersuites profiled: ', end='')
num_srv_ciphers = 0
if len(srv_funcs) > 0:
first_func = srv_funcs[0]
num_srv_ciphers = len(cli_prof[first_func])
print(num_srv_ciphers)
def dump_json_ids_if_needed(json_ids_file, cli_prof, srv_prof):
if json_ids_file is not None:
print(f'Dumping profiling resutls to {json_ids_file}...')
dump_json_ids(cli_prof, srv_prof, json_ids_file)
def plot_from_profiling(func_names, profiling, ciphersuites_ids_to_graph, ciphersuite_id_to_name, entity):
for func_name in func_names:
func_profiling = profiling[func_name]
values = []
labels = []
ciphersuite_names = []
num_ciphersuites_graphed = 0
for ciphersuite_id in ciphersuites_ids_to_graph:
profiling_res = func_profiling.get(ciphersuite_id, None)
if profiling_res is None:
continue
values.append(profiling_res)
labels.append(ciphersuite_id)
ciphersuite_names.append(ciphersuite_id_to_name[ciphersuite_id])
total_ciphers = len(values)
suffix = f'{entity} (Total: {total_ciphers})'
show_plot(values, labels, func_name, ciphersuite_names, suffix)
def show_plot(values, labels, func_name, ciphersuite_names, suffix):
fig, ax = plt.subplots()
ax.get_yaxis().get_major_formatter().set_scientific(False)
y_pos = range(len(labels))
plt.bar(y_pos, values, align='center', alpha=0.5)
plt.xticks(y_pos, labels, rotation='vertical')
plt.yticks()
plt.ylabel(f'CPU Cycles For {func_name}')
plt.title(f'Ciphersuite Comparison For {suffix}')
plt.margins(0) # graph bars start at y = 0, no margins on the right
plt.subplots_adjust(left=0.05, right=0.999)
max_val = max(values)
label_pos = max_val/2 + max_val/3
for i, v in enumerate(values):
ax.text(i - 0.25, label_pos, f'{v} | {ciphersuite_names[i]}', rotation='vertical')
#plt.tight_layout()
plt.show()
def run(ciphers_file_path, path, cli_funcs, srv_funcs, json_ids_file):
ciphersuite_name = parse_ciphersuite_names_from_file(ciphers_file_path)
ciphersuite_order = ciphersuite_name.keys() # follow the ordre of the file
cipher_id_file_cli, cipher_id_file_srv = parse_callgrind_cli_srv_file_names(path)
total_ciphersuites_requested = len(ciphersuite_order)
cli_ciphersuites_skipped = 0
cli_ciphersuties_evaluated = 0
# {function_name : {ciphersuite_id : num_cycles}}
CLI_FUNCS_PROFILING = parse_callgrind_cpu_cycles_from_files(cli_funcs, cipher_id_file_cli, 'client')
SRV_FUNCS_PROFILING = parse_callgrind_cpu_cycles_from_files(srv_funcs, cipher_id_file_srv, 'server')
print_total_ciphers_profiled_stats(CLI_FUNCS_PROFILING, SRV_FUNCS_PROFILING, cli_funcs, srv_funcs)
dump_json_ids_if_needed(json_ids_file, CLI_FUNCS_PROFILING, SRV_FUNCS_PROFILING)
plot_from_profiling(cli_funcs, CLI_FUNCS_PROFILING, ciphersuite_order, ciphersuite_name, 'Client')
plot_from_profiling(srv_funcs, SRV_FUNCS_PROFILING, ciphersuite_order, ciphersuite_name, 'Server')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate graph from existing callgrind ouput files.'
'The file name format file must be: callgrind.out.[client||server].<ciphersuite_id>.'
'If you need to change the output graph, edit the code directly.')
parser.add_argument('ciphers', type=str, help='file containing a list of ciphersuite ids and their respective names.'
'Each line of the file must have the format: <ciphersuite_id> <ciphersuite_name> [arbitrary_info, ...]')
parser.add_argument('-p', '--path', type=str, default='./', help='path of the callgrind output files')
parser.add_argument('--sf', nargs='*', default=[], help='name of server functions to profile')
parser.add_argument('--cf', nargs='*', default=[], help='name of client functions to profile')
parser.add_argument('--json-ids', type=str, default=None, help='output JSON file with the profiling results. The keys of the ciphersuites are its ids')
#TODO: json-cipher-names
args = parser.parse_args()
if args.path[-1] is not '/':
args.path += '/'
font = {
'family' : 'normal',
'size' : 7
}
matplotlib.rc('font', **font)
run(args.ciphers, args.path, args.cf, args.sf, args.json_ids)