-
Notifications
You must be signed in to change notification settings - Fork 38
/
size.py
executable file
·439 lines (360 loc) · 11.9 KB
/
size.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env python
'''
size
====
Get the binary sizes from executables using lexical. By default,
this uses binutils `size` command to probe binary sizes: if `size`
is not on the path, like on Windows, you can set the `SIZE` environment
variable to manually specify the `size` executable. Likewise, the `strip`
command can be overrided by the `STRIP` environment variable.
'''
import argparse
import json
import subprocess
import os
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
plt.style.use('ggplot')
if os.name == 'nt':
from winmagic import magic
else:
import magic
scripts = os.path.dirname(os.path.realpath(__file__))
home = os.path.dirname(scripts)
size_command = os.environ.get('SIZE', 'size')
strip_command = os.environ.get('STRIP', 'strip')
LEVELS = {
'0': 'debug',
'1': 'debug',
'2': 'release',
'3': 'release',
's': 'release',
'z': 'release',
}
DEBUG = '''
[profile.dev]
opt-level = {level}
debug = true
debug-assertions = true
lto = false
'''
RELEASE = '''
[profile.release]
opt-level = {level}
debug = false
debug-assertions = false
lto = true
'''
def parse_args(argv=None):
'''Create and parse our command line arguments.'''
parser = argparse.ArgumentParser(description='Get lexical binary sizes.')
parser.add_argument(
'--opt-levels',
help='''optimization levels to test''',
default='0,1,2,3,s,z',
)
parser.add_argument(
'--features',
help='''optional features to add''',
default='',
)
parser.add_argument(
'--no-default-features',
help='''disable default features''',
action='store_true',
)
parser.add_argument(
'--plot',
help='''plot graphs''',
action='store_true',
)
parser.add_argument(
'--run',
help='''generate size data''',
action='store_true',
)
return parser.parse_args(argv)
def filename(basename, args):
'''Get a resilient name for the benchmark data.'''
name = basename
if args.no_default_features:
name = f'{name}_nodefault'
if args.features:
name = f'{name}_features={args.features}'
return name
def plot_bar(
xlabel=None,
data=None,
path=None,
title=None,
key=None
):
'''Plot a generic bar chart.'''
keys = [i.split('_') for i in data.keys()]
xticks = sorted({i[1] for i in keys})
libraries = sorted({i[0] for i in keys})
def plot_ax(ax, xticks):
'''Plot an axis with various subfigures.'''
length = len(xticks)
width = 0.4 / len(libraries)
x = np.arange(length)
for index, library in enumerate(libraries):
xi = x + width * index
yi = [data[f'{library}_{i}'] for i in xticks]
plt.bar(xi, yi, width, label=library, alpha=.7)
ax.grid(color='white', linestyle='solid')
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel('Size (B)')
ax.set_yscale('log')
ax.set_xticks(x + width * len(libraries) / 4)
ax.set_xticklabels(xticks, rotation=-45)
ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, p: prettyify(x)))
ax.legend(libraries, fancybox=True, framealpha=1, shadow=True, borderpad=1)
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)
plot_ax(ax, xticks)
fig.savefig(path, format='svg')
fig.clf()
def clean():
'''Clean the project'''
os.chdir(f'{home}/lexical-size')
subprocess.check_call(
'cargo +nightly clean',
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def write_manifest(level):
'''Write the manifest for the given optimization level.'''
manifest = f'{home}/lexical-size/Cargo.toml'
with open(f'{manifest}.in') as file:
contents = file.read()
toml_level = level
if toml_level.isalpha():
toml_level = f'"{level}"'
if LEVELS[level] == 'debug':
contents += DEBUG.format(level=toml_level)
else:
contents += RELEASE.format(level=toml_level)
with open(manifest, 'w') as file:
file.write(contents)
def build(args, level, is_lexical):
'''Build the project.'''
os.chdir(f'{home}/lexical-size')
command = 'cargo +nightly build'
if args.no_default_features:
command = f'{command} --no-default-features'
features = args.features
if is_lexical:
if features:
features = f'{features},lexical'
else:
features = 'lexical'
if features:
command = f'{command} --features={features}'
if LEVELS[level] == 'release':
command = f'{command} --release'
subprocess.check_call(
# Use shell for faster performance.
# Spawning a new process is a **lot** slower, gives misleading info.
command,
shell=True,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
def is_executable(path):
'''Determine if a file is a binary executable.'''
if os.name == 'nt':
return magic.from_file(path, mime=True) == 'application/x-dosexec'
else:
return magic.from_file(path, mime=True) == 'application/x-pie-executable'
def prettyify(size):
'''Get the human readable filesize from bytes.'''
suffixes = ['KB', 'MB', 'GB', 'TB']
if size < 1024:
return f'{size}B'
size /= 1024
for suffix in suffixes:
if size < 1024:
return f'{size:0.1f}{suffix}'
size /= 1024
return f'{size:0.1f}PB'
def get_file_size(path):
'''Read the file size of a given binary.'''
# Use the size utility, and grep for 2 sections.
# We can't use `stat`, or `file`, or any other
# utility that isn't aware of padding. We have
# 3 sections that matter: .rodata, .text, and .data.
# .text: Compiled code
# .rodata: Read-only data (on Windows, this is `.rdata`)
# .data: Other data (often empty).
cmd = [size_command, '-A', '-d', path]
stdout = subprocess.run(cmd, check=True, stdout=subprocess.PIPE).stdout
stdout = stdout.decode('utf-8')
lines = [i.strip() for i in stdout.splitlines()[2:] if i.strip()]
sections = dict([i.split()[:2] for i in lines])
text = int(sections['.text'])
data = int(sections['.data'])
if os.name == 'nt':
rodata = int(sections['.rdata'])
else:
rodata = int(sections['.rodata'])
return text + data + rodata
def get_sizes(level):
'''Get the binary sizes for all targets.'''
data = {}
build_type = LEVELS[level]
target = f'{home}/lexical-size/target/{build_type}'
for filename in os.listdir(target):
path = os.path.join(target, filename)
if os.path.isfile(path) and is_executable(path):
exe_name = filename
if os.name == 'nt':
exe_name = filename[:-len('.exe')]
data[exe_name] = get_file_size(path)
empty = data.pop('empty')
return {k: v - empty for k, v in data.items()}
def strip(level):
'''Strip all the binaries'''
if os.name == 'nt':
# The Portable Executable format uses PDB for debugging info.
return
build_type = LEVELS[level]
target = f'{home}/lexical-size/target/{build_type}'
for filename in os.listdir(target):
path = os.path.join(target, filename)
if os.path.isfile(path) and is_executable(path):
subprocess.check_call(
[strip_command, path],
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
def plot_level(args, data, level):
'''Print markdown-based report for the file sizes.'''
print(f'Plotting binary sizes for optimization level {level}.')
def sort_key(x):
split = x.split('-')
ctype = split[-1]
return (split[0], split[1], ctype[0], int(ctype[1:]))
def flatten(lib, key, filter):
subdata = {k: v for k, v in data[lib][key].items() if filter in k}
return {f'{lib}_{k.split("-")[2]}': v for k, v in subdata.items()}
# Create the bar graphs
assets = f'{home}/assets'
bar_kwds = {
'xlabel': 'Binary Sizes',
'key': sort_key,
}
if os.name == 'nt':
pe = {
**flatten('core', 'pe', 'parse'),
**flatten('lexical', 'pe', 'parse'),
}
file = filename(f'size_parse_pe_opt{level}_{os.name}', args)
plot_bar(
**bar_kwds,
data=pe,
path=f'{assets}/{file}.svg',
title=f'Parse Data -- Optimization Level "{level}"',
)
pe = {
**flatten('core', 'pe', 'write'),
**flatten('lexical', 'pe', 'write'),
}
file = filename(f'size_write_pe_opt{level}_{os.name}', args)
plot_bar(
**bar_kwds,
data=pe,
path=f'{assets}/{file}.svg',
title=f'Write Data -- Optimization Level "{level}"',
)
else:
unstripped = {
**flatten('core', 'unstripped', 'parse'),
**flatten('lexical', 'unstripped', 'parse'),
}
file = filename(f'size_parse_unstripped_opt{level}_{os.name}', args)
plot_bar(
**bar_kwds,
data=unstripped,
path=f'{assets}/{file}.svg',
title=f'Parse Unstripped Data -- Optimization Level "{level}"',
)
unstripped = {
**flatten('core', 'unstripped', 'write'),
**flatten('lexical', 'unstripped', 'write'),
}
file = filename(f'size_write_unstripped_opt{level}_{os.name}', args)
plot_bar(
**bar_kwds,
data=unstripped,
path=f'{assets}/{file}.svg',
title=f'Write Unstripped Data -- Optimization Level "{level}"',
)
stripped = {
**flatten('core', 'stripped', 'parse'),
**flatten('lexical', 'stripped', 'parse'),
}
file = filename(f'size_parse_stripped_opt{level}_{os.name}', args)
plot_bar(
**bar_kwds,
data=stripped,
path=f'{assets}/{file}.svg',
title=f'Parse Stripped Data -- Optimization Level "{level}"',
)
stripped = {
**flatten('core', 'stripped', 'write'),
**flatten('lexical', 'stripped', 'write'),
}
file = filename(f'size_write_stripped_opt{level}_{os.name}', args)
plot_bar(
**bar_kwds,
data=stripped,
path=f'{assets}/{file}.svg',
title=f'Write Stripped Data -- Optimization Level "{level}"',
)
def run_level(args, level, is_lexical):
'''Generate the size data for a given build configuration.'''
print(f'Calculating binary sizes for optimization level {level}.')
write_manifest(level)
clean()
build(args, level, is_lexical)
data = {}
if os.name == 'nt':
data['pe'] = get_sizes(level)
else:
data['unstripped'] = get_sizes(level)
strip(level)
data['stripped'] = get_sizes(level)
return data
def run(args):
'''Run the size calculations.'''
assets = f'{home}/assets'
opt_levels = args.opt_levels.split(',')
for level in opt_levels:
data = {}
data['core'] = run_level(args, level, False)
data['lexical'] = run_level(args, level, True)
file = filename(f'size{level}_{os.name}', args)
with open(f'{assets}/{file}.json', 'w') as file:
json.dump(data, file)
def plot(args):
'''Plot the size calculations.'''
assets = f'{home}/assets'
opt_levels = args.opt_levels.split(',')
for level in opt_levels:
file = filename(f'size{level}_{os.name}', args)
with open(f'{assets}/{file}.json', 'r') as file:
data = json.load(file)
plot_level(args, data, level)
def main(argv=None):
'''Entry point.'''
args = parse_args(argv)
if args.run:
run(args)
if args.plot:
plot(args)
if __name__ == '__main__':
main()