| 84 | |
| 85 | |
| 86 | def run_leg(binary: str, profdata: str, corpus: str, work: str, copies: int) -> tuple[dict, dict]: |
| 87 | leg = os.path.join(work, f"leg-{copies}") |
| 88 | tree = os.path.join(leg, "corpus") |
| 89 | os.makedirs(tree, exist_ok=True) |
| 90 | nfiles = replicate(corpus, tree, copies) |
| 91 | raw_dir = os.path.join(leg, "profraw") |
| 92 | os.makedirs(raw_dir, exist_ok=True) |
| 93 | runtime = tempfile.mkdtemp(prefix="cbm-sc.", dir="/tmp") |
| 94 | os.chmod(runtime, 0o700) |
| 95 | env = dict(os.environ) |
| 96 | env.update({ |
| 97 | "LLVM_PROFILE_FILE": os.path.join(raw_dir, "cbm-%p.profraw"), |
| 98 | "CBM_PROFILE_DIR": raw_dir, # the index worker names its own: worker-<pid>.profraw |
| 99 | "CBM_CACHE_DIR": os.path.join(leg, "cache"), |
| 100 | "CBM_RUNTIME_DIR": runtime, |
| 101 | "CBM_LOG_LEVEL": "error", |
| 102 | }) |
| 103 | env.pop("CBM_MEMWASTE", None) # production code paths: the event layer stays dormant |
| 104 | t0 = time.monotonic() |
| 105 | try: |
| 106 | res = subprocess.run([binary, "cli", "index_repository", "--repo-path", tree, "--mode", "full"], |
| 107 | env=env, capture_output=True, text=True, check=False) |
| 108 | finally: |
| 109 | shutil.rmtree(runtime, ignore_errors=True) |
| 110 | wall = time.monotonic() - t0 |
| 111 | if res.returncode != 0: |
| 112 | sys.exit(f"memwaste-scaling: index of leg {copies} failed (rc={res.returncode}):\n{res.stderr[-2000:]}") |
| 113 | raws = sorted(os.path.join(raw_dir, f) for f in os.listdir(raw_dir) if f.endswith(".profraw")) |
| 114 | workers = [r for r in raws if os.path.basename(r).startswith("worker-")] |
| 115 | if not workers: |
| 116 | sys.exit("memwaste-scaling: the index worker wrote no profile -- is the binary built with " |
| 117 | "MEMWASTE_PROFILE=1, and does it call cbm_memev_process_exit before _Exit?") |
| 118 | # The comparison is the WORKER's work: the daemon and CLI processes differ in |
| 119 | # number between legs (and hash the binary at start-up), which is not scaling. |
| 120 | info = {"copies": copies, "files": nfiles, "wall_s": round(wall, 1), "profiles": len(raws)} |
| 121 | return merge_counts(profdata, workers, os.path.join(leg, "worker.profdata")), \ |
| 122 | dict(info, other=merge_counts(profdata, [r for r in raws if r not in workers], |
| 123 | os.path.join(leg, "other.profdata"))) |
| 124 | |
| 125 | |
| 126 | def merge_counts(profdata: str, raws: list[str], merged: str) -> dict[str, int]: |