test/wcc/995_self_rebuild: fork-parallel the 5-tool rebuild loop

Was sequential `for (i = 0; i < 5; i++) rebuild_one(...)` at ~3:15
wall. Split into spawn_build (fork+system ww_ww build) phase and
waitpid+slurp_eq collect phase. The five tool builds now run
concurrently; wall drops from sum(builds) to max(builds).

Per-tool workdir already pid-keyed (/tmp/wwsr_<pid>_<tool>); add
build.err per workdir so concurrent diagnostics don't merge —
collect phase replays the err file on failure. 180s timeout still
inside the cd && ... && ww_ww build string.

All five tools' failure status collected before reporting, so a
multi-divergence run names every tool (not just the first).

make test wall: 4m31s → 3m17s on 8 cores. 132/132 green.
This commit is contained in:
2026-05-21 16:41:03 +09:00
parent 65a6cac6a0
commit 31594e4cae

View File

@@ -10,6 +10,10 @@
* This is stricter than `make bootstrap`: that loop only proves the
* wwdump cgen self-stabilises; this proves every wwstage tool round-
* trips through the wwstage pipeline.
*
* The five tool builds run concurrently: fork() per tool, parent
* collects each pid with waitpid() and then byte-compares. Wall drops
* from sum(builds) to max(builds).
*/
#include <stdio.h>
#include <stdlib.h>
@@ -67,47 +71,93 @@ slurp_eq(const char *a, const char *b)
* Some tools have a local module dir (w6a, w6l with sibling .ww files).
* inc_local is "" for tools without one (w6c, ww, wwdump).
*/
static int
rebuild_one(const char *bin, const char *cwd, const char *tool,
const char *src_rel, const char *inc_local)
{
struct buildjob {
const char *tool;
const char *src_rel;
const char *inc_local;
char workdir[64];
snprintf(workdir, sizeof workdir, "/tmp/wwsr_%d_%s", getpid(), tool);
char cmd[4096];
snprintf(cmd, sizeof cmd, "rm -rf %s && mkdir -p %s", workdir, workdir);
if (runwait(cmd) != 0) return -1;
pid_t pid;
};
if (inc_local && inc_local[0]) {
/* Fork a child that runs the `ww_ww build` for this tool. The child
* inherits no concurrent siblings — system() spawns a fresh /bin/sh -c.
* stderr lands in <workdir>/build.err so concurrent builds don't merge
* their diagnostics. */
static int
spawn_build(const char *bin, const char *cwd, struct buildjob *j)
{
snprintf(j->workdir, sizeof j->workdir, "/tmp/wwsr_%d_%s",
getpid(), j->tool);
char setup[256];
snprintf(setup, sizeof setup, "rm -rf %s && mkdir -p %s",
j->workdir, j->workdir);
if (runwait(setup) != 0) return -1;
char cmd[4096];
if (j->inc_local && j->inc_local[0]) {
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s/ww_ww build -I %s/%s -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc "
"%s/%s >/dev/null 2>&1",
workdir, bin, cwd, inc_local, cwd, cwd, cwd, cwd, cwd, src_rel);
"cd %s && timeout 180 %s/ww_ww build -I %s/%s "
"-I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse "
"-I %s/selfhost/cmd/wcc %s/%s >/dev/null 2>%s/build.err",
j->workdir, bin, cwd, j->inc_local,
cwd, cwd, cwd, cwd, cwd, j->src_rel, j->workdir);
} else {
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s/ww_ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc "
"%s/%s >/dev/null 2>&1",
workdir, bin, cwd, cwd, cwd, cwd, cwd, src_rel);
}
if (runwait(cmd) != 0) {
fprintf(stderr, "self-rebuild FAIL: ww_ww build errored on %s\n", tool);
return -1;
"cd %s && timeout 180 %s/ww_ww build "
"-I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse "
"-I %s/selfhost/cmd/wcc %s/%s >/dev/null 2>%s/build.err",
j->workdir, bin, cwd, cwd, cwd, cwd, cwd, j->src_rel, j->workdir);
}
char rebuilt[256], canonical[256];
snprintf(rebuilt, sizeof rebuilt, "%s/main", workdir);
snprintf(canonical, sizeof canonical, "%s/%s_ww", bin, tool);
pid_t p = fork();
if (p < 0) return -1;
if (p == 0) {
int rc = system(cmd);
if (rc == -1) _exit(1);
_exit(WIFEXITED(rc) ? WEXITSTATUS(rc) : 1);
}
j->pid = p;
return 0;
}
int rc = slurp_eq(rebuilt, canonical);
if (rc != 0) {
fprintf(stderr, "self-rebuild FAIL: %s rebuilt != cstage %s\n",
tool, canonical);
/* Reap one job, byte-diff rebuilt vs canonical, replay captured stderr
* on build failure, rm workdir. Returns 0 on byte-match, -1 otherwise. */
static int
collect_build(const char *bin, struct buildjob *j)
{
int rc = 0;
int status;
if (waitpid(j->pid, &status, 0) < 0) {
fprintf(stderr, "self-rebuild FAIL: waitpid %s\n", j->tool);
rc = -1;
} else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
fprintf(stderr, "self-rebuild FAIL: ww_ww build errored on %s\n",
j->tool);
/* Replay the captured stderr so the failure is diagnosable. */
char errpath[256];
snprintf(errpath, sizeof errpath, "%s/build.err", j->workdir);
FILE *f = fopen(errpath, "r");
if (f) {
char buf[1024];
while (fgets(buf, sizeof buf, f))
fputs(buf, stderr);
fclose(f);
}
rc = -1;
} else {
char rebuilt[256], canonical[256];
snprintf(rebuilt, sizeof rebuilt, "%s/main", j->workdir);
snprintf(canonical, sizeof canonical, "%s/%s_ww", bin, j->tool);
rc = slurp_eq(rebuilt, canonical);
if (rc != 0) {
fprintf(stderr, "self-rebuild FAIL: %s rebuilt != cstage %s\n",
j->tool, canonical);
}
}
/* Leave the driver's intermediates (.s/.o/.combined.ww) next to the
* source — 991/992/994 read those fixtures, and `make wwstage` had
* already produced byte-identical copies anyway. */
snprintf(cmd, sizeof cmd, "rm -rf %s", workdir);
runwait(cmd);
char cleanup[128];
snprintf(cleanup, sizeof cleanup, "rm -rf %s", j->workdir);
runwait(cleanup);
return rc;
}
@@ -119,26 +169,32 @@ main(void)
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
struct {
const char *tool;
const char *src;
const char *inc_local;
} tools[] = {
{ "w6c", "selfhost/cmd/w6c/main.ww", "" },
{ "w6a", "selfhost/cmd/w6a/main.ww", "selfhost/cmd/w6a" },
{ "w6l", "selfhost/cmd/w6l/main.ww", "selfhost/cmd/w6l" },
{ "ww", "selfhost/cmd/ww/main.ww", "" },
{ "wwdump", "selfhost/cmd/wwdump/main.ww", "" },
{ NULL, NULL, NULL },
struct buildjob jobs[] = {
{ "w6c", "selfhost/cmd/w6c/main.ww", "", {0}, 0 },
{ "w6a", "selfhost/cmd/w6a/main.ww", "selfhost/cmd/w6a", {0}, 0 },
{ "w6l", "selfhost/cmd/w6l/main.ww", "selfhost/cmd/w6l", {0}, 0 },
{ "ww", "selfhost/cmd/ww/main.ww", "", {0}, 0 },
{ "wwdump", "selfhost/cmd/wwdump/main.ww", "", {0}, 0 },
};
const int n = (int)(sizeof jobs / sizeof jobs[0]);
int fail = 0, n = 0;
for (int i = 0; tools[i].tool; i++) {
if (rebuild_one(bin, cwd, tools[i].tool, tools[i].src,
tools[i].inc_local) != 0)
fail++;
n++;
int spawned = 0;
for (int i = 0; i < n; i++) {
if (spawn_build(bin, cwd, &jobs[i]) != 0) {
fprintf(stderr, "self-rebuild FAIL: spawn %s\n", jobs[i].tool);
jobs[i].pid = 0;
} else {
spawned++;
}
}
int fail = n - spawned;
for (int i = 0; i < n; i++) {
if (jobs[i].pid == 0) continue;
if (collect_build(bin, &jobs[i]) != 0)
fail++;
}
if (fail) {
fprintf(stderr, "self-rebuild: %d/%d tool(s) diverged\n", fail, n);
return 1;