111 lines
5.9 KiB
Python
111 lines
5.9 KiB
Python
# Times the compilers at their work, in turns, through `count`, as docs/performance.md
|
|
# measures them: each checking and building the rebuild's sources of its day, checking a
|
|
# one-line program, and running the rebuild's compiler on its VM.
|
|
#
|
|
# cc -O2 -o count count.c
|
|
# python3 -I measure.py WORK [GROUP...]
|
|
#
|
|
# WORK is a directory holding what's measured, laid out as below, and GROUP any of
|
|
# `hello`, `check`, `build`, `vm` and `threads` (all by default). Prints a line for
|
|
# each command and writes them to WORK/results.json.
|
|
#
|
|
# count the counter, built from count.c
|
|
# c_start/sakura C as it was, and c_start/std its std
|
|
# r_start/ the rebuild's sources and std as they were
|
|
# r_start_c the rebuild as they were, built by c_start/sakura
|
|
# r_start_s2 the same, its own fixed point
|
|
# c_now/sakura C as it is, and c_now/std its std
|
|
# r_now/ the rebuild's sources and std as they are, with `std::json` and
|
|
# `std::args`, packages since 2026-10-24, copied back into its std
|
|
# as `std/json` and `std/args`, since C can't take `--lib std::json`
|
|
# r_now_c the rebuild as it is, built by c_now/sakura
|
|
# r_now_s2 the same, its own fixed point
|
|
#
|
|
# Each group's commands are pinned to CPU (2 by default, or $CPU), but for `threads`,
|
|
# which lets the rebuild run its threads where it will.
|
|
import glob, json, os, statistics, subprocess, sys
|
|
|
|
W = os.path.abspath(sys.argv[1])
|
|
CPU = int(os.environ.get("CPU", "2"))
|
|
|
|
def sources(root, dirs):
|
|
found = ["entry.sakura"]
|
|
for d in dirs:
|
|
found += sorted(os.path.relpath(f, root) for f in glob.glob(f"{root}/{d}/**/*.sakura", recursive=True))
|
|
return found
|
|
|
|
ST, NW = W + "/r_start", W + "/r_now"
|
|
SS = sources(ST, ["analyzer", "compiler", "machine", "native"])
|
|
SN = sources(NW, ["analyzer", "compiler", "machine", "native", "server"])
|
|
STDS, STDN = ST + "/std", NW + "/std"
|
|
CS, CN = W + "/c_start/sakura", W + "/c_now/sakura"
|
|
RS, RSC = W + "/r_start_s2", W + "/r_start_c"
|
|
RN, RNC = W + "/r_now_s2", W + "/r_now_c"
|
|
HS, HN, OUT = W + "/hello_start.sakura", W + "/hello_now.sakura", W + "/out.bin"
|
|
with open(HS, "w") as f: f.write("import std::io;\n\ndef main(): void { io::println(\"hi\"); }\n")
|
|
with open(HN, "w") as f: f.write("import std::io;\n\ndef main(): void { io::println(\"hi\"); }\n")
|
|
|
|
groups = {
|
|
"hello": (21, [
|
|
("start C checks one line", W, [CS, "check", "--std", W + "/c_start/std", HS]),
|
|
("start rebuild checks one line", W, [RS, "--std", STDS, HS]),
|
|
("now C checks one line", W, [CN, "check", "--std", W + "/c_now/std", HN]),
|
|
("now rebuild checks one line -j1", W, [RN, "check", "-j", "1", "--std", STDN, HN]),
|
|
]),
|
|
"check": (9, [
|
|
("start C checks rebuild", ST, [CS, "check", "--std", STDS] + SS),
|
|
("start rebuild checks itself", ST, [RS, "--std", STDS] + SS),
|
|
("now C checks rebuild", NW, [CN, "check", "--std", STDN] + SN),
|
|
("now rebuild checks itself -j1", NW, [RN, "check", "-j", "1", "--std", STDN] + SN),
|
|
]),
|
|
"build": (5, [
|
|
("start C builds rebuild", ST, [CS, "build", "--std", STDS, "-O2", "-o", OUT] + SS),
|
|
("start rebuild (self-built) builds itself", ST, [RS, "build", "--std", STDS, "-O2", "-o", OUT] + SS),
|
|
("start rebuild (C-built) builds itself", ST, [RSC, "build", "--std", STDS, "-O2", "-o", OUT] + SS),
|
|
("now C builds rebuild", NW, [CN, "build", "--std", STDN, "-O2", "-o", OUT] + SN),
|
|
("now rebuild (self-built) builds itself -j1", NW, [RN, "build", "-j", "1", "--std", STDN, "-O2", "-o", OUT] + SN),
|
|
("now rebuild (C-built) builds itself -j1", NW, [RNC, "build", "-j", "1", "--std", STDN, "-O2", "-o", OUT] + SN),
|
|
]),
|
|
"vm": (3, [
|
|
("start C VM runs rebuild checking one line", ST, [CS, "run", "--std", STDS] + SS + ["--", "--std", STDS, HS]),
|
|
("start rebuild VM runs itself checking one line", ST, [RS, "run", "--std", STDS] + SS + ["--", "--std", STDS, HS]),
|
|
("now C VM runs rebuild checking one line", NW, [CN, "run", "--std", STDN] + SN + ["--", "check", "--std", STDN, HN]),
|
|
("now rebuild VM runs itself checking one line -j1", NW, [RN, "run", "-j", "1", "--std", STDN] + SN + ["--", "check", "-j", "1", "--std", STDN, HN]),
|
|
]),
|
|
"threads": (5, [
|
|
("now rebuild builds itself, default threads", NW, [RN, "build", "--std", STDN, "-O2", "-o", OUT] + SN),
|
|
("now rebuild builds itself -j1 (unpinned)", NW, [RN, "build", "-j", "1", "--std", STDN, "-O2", "-o", OUT] + SN),
|
|
("now rebuild checks itself, default threads", NW, [RN, "check", "--std", STDN] + SN),
|
|
]),
|
|
}
|
|
|
|
def once(cpu, cwd, argv):
|
|
run = subprocess.run([W + "/count", str(cpu)] + argv, cwd=cwd, stderr=subprocess.PIPE, text=True)
|
|
wall, rss, ins, cyc, status = run.stderr.strip().splitlines()[-1].split()
|
|
if status != "0":
|
|
print("FAILED", " ".join(argv[:3]), run.stderr[-400:], file=sys.stderr)
|
|
return float(wall), int(rss), int(ins), int(cyc)
|
|
|
|
results = {}
|
|
for group in sys.argv[2:] or list(groups):
|
|
rounds, commands = groups[group]
|
|
cpu = -1 if group == "threads" else CPU
|
|
runs = {name: [] for name, _, _ in commands}
|
|
for _ in range(rounds):
|
|
for name, cwd, argv in commands:
|
|
runs[name].append(once(cpu, cwd, argv))
|
|
for name, rs in runs.items():
|
|
walls = [r[0] for r in rs]
|
|
results[name] = {
|
|
"best": min(walls), "median": statistics.median(walls),
|
|
"rss_mb": max(r[1] for r in rs) / 1024,
|
|
"instructions": statistics.median(r[2] for r in rs),
|
|
"cycles": statistics.median(r[3] for r in rs), "runs": rounds,
|
|
}
|
|
r = results[name]
|
|
print(f"{name:50s} best {r['best']:7.3f}s median {r['median']:7.3f}s "
|
|
f"rss {r['rss_mb']:6.1f}MB instr {r['instructions'] / 1e9:6.3f}G "
|
|
f"cycles {r['cycles'] / 1e9:6.3f}G", flush=True)
|
|
with open(W + "/results.json", "w") as f: json.dump(results, f, indent=1)
|
|
if os.path.exists(OUT): os.remove(OUT)
|