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
"""Measure sequential offline builds from source snapshots and fresh Zig caches."""
import argparse
import hashlib
import json
import os
import platform
import shutil
import struct
import subprocess
import tempfile
import time
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument('output', type=Path)
args = parser.parse_args()
args.output = args.output.resolve()
args.output.mkdir(parents=True, exist_ok=True)
repo = Path(__file__).resolve().parents[2]
flags = ['-Dbackend=zio', '-Doptimize=ReleaseSafe', '-Dtarget=x86_64-linux-gnu', '-j2']
report = {'host': platform.platform(), 'zig': subprocess.check_output(['zig', 'version'], text=True).strip(),
'flags': flags, 'network': 'sandbox-exec deny network*', 'measurements': []}
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def snapshot(source, target):
target.mkdir(parents=True)
for name in ['src', 'build.zig', 'build.zig.zon']:
origin = source / name
if origin.is_dir():
shutil.copytree(origin, target / name)
else:
shutil.copy2(origin, target / name)
(target / 'zig-pkg').symlink_to(source / 'zig-pkg', target_is_directory=True)
def elf(path):
data = path.read_bytes()
assert data[:6] == b'\x7fELF\x02\x01'
offset = struct.unpack_from('<Q', data, 32)[0]
size, count = struct.unpack_from('<HH', data, 54)
segments = [offset + i * size for i in range(count)
if struct.unpack_from('<I', data, offset + i * size)[0] == 1]
return {'bytes': len(data), 'sha256': digest(path),
'load_file_bytes': sum(struct.unpack_from('<Q', data, p + 32)[0] for p in segments),
'load_memory_bytes': sum(struct.unpack_from('<Q', data, p + 40)[0] for p in segments)}
with tempfile.TemporaryDirectory(prefix='zlay-build-audit-') as temporary:
root = Path(temporary)
core = repo.parent.parent / 'zzstoatzz.io/zkv'
report['zkv_revision'] = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=core, text=True).strip()
report['zkv_source_hashes'] = {str(p.relative_to(core)): digest(p) for p in sorted(core.glob('src/**/*.zig')) + [core / 'build.zig', core / 'build.zig.zon']}
snapshot(core, root / 'zzstoatzz.io/zkv')
for label, source in [('zkv', repo), ('rocks', repo.parent / 'zlay-zkv-pilot')]:
target = root / 'zat.dev' / label
snapshot(source, target)
revision = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=source, text=True).strip()
short = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=source, text=True).strip()
build = target / 'build.zig'
text = build.read_text()
start = text.index(' build_options.addOption([]const u8, "git_sha", git_sha: {')
end = text.index('\n build_options.addOption([]const u8, "optimize"', start)
text = text[:start] + f' build_options.addOption([]const u8, "git_sha", "{short}");' + text[end:]
text = text.replace('.root_source_file = b.path("src/main.zig"),', '.root_source_file = b.path("src/main.zig"),\n .strip = b.option(bool, "audit-strip", "strip relay") orelse false,')
build.write_text(text)
item = {'engine': label, 'revision': revision,
'source_hashes': {str(p.relative_to(source)): digest(p) for p in
sorted(source.glob('src/**/*.zig')) + [source / 'build.zig', source / 'build.zig.zon']},
'snapshot_change': 'Freeze identical short SHA; add audit-strip option to relay module only.',
'runs': []}
report['measurements'].append(item)
env = dict(os.environ, ZIG_GLOBAL_CACHE_DIR=str(root / (label + '-global-cache')))
for phase in ['fresh-cache', 'unchanged', 'strip-rebuild']:
log = args.output / f'{label}-{phase}.log'
command = ['/usr/bin/time', '-l', '/usr/bin/sandbox-exec', '-p',
'(version 1)(allow default)(deny network*)', 'zig', 'build', *flags]
if phase == 'strip-rebuild':
command.append('-Daudit-strip=true')
started = time.monotonic()
with log.open('w') as output:
completed = subprocess.run(command, cwd=target, env=env, stdout=output, stderr=subprocess.STDOUT)
item['runs'].append({'phase': phase, 'seconds': time.monotonic() - started,
'exit_code': completed.returncode, 'log': log.name})
(args.output / 'results.json').write_text(json.dumps(report, indent=2) + '\n')
completed.check_returncode()
binary = target / 'zig-out/bin/zlay'
item['stripped_elf' if phase == 'strip-rebuild' else 'elf'] = elf(binary)
(args.output / 'results.json').write_text(json.dumps(report, indent=2) + '\n')