()
| 166 | |
| 167 | |
| 168 | def main() -> int: |
| 169 | ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 170 | ap.add_argument("--corpus", required=True, help="a real source tree to replicate") |
| 171 | ap.add_argument("--replicas", type=int, default=2, help="K: legs index K and 2K replicas") |
| 172 | ap.add_argument("--threshold", type=float, default=2.3, help="2K/K counter ratio that is super-linear") |
| 173 | ap.add_argument("--min-excess", type=int, default=10_000_000, |
| 174 | help="ignore functions whose work beyond linear is below this many counter increments") |
| 175 | ap.add_argument("--top", type=int, default=30) |
| 176 | ap.add_argument("--out", help="work directory (default: a new temp dir)") |
| 177 | ap.add_argument("--no-build", action="store_true") |
| 178 | ap.add_argument("--cc", default="clang") |
| 179 | ap.add_argument("--json", action="store_true") |
| 180 | args = ap.parse_args() |
| 181 | |
| 182 | corpus = os.path.abspath(os.path.expanduser(args.corpus)) |
| 183 | if not os.path.isdir(corpus): |
| 184 | sys.exit(f"memwaste-scaling: {corpus} is not a directory") |
| 185 | binary = os.path.join(ROOT, BUILD_DIR, "codebase-memory-mcp") |
| 186 | if not args.no_build: |
| 187 | cxx = "clang++" if args.cc == "clang" else args.cc.replace("clang", "clang++") |
| 188 | subprocess.run([os.path.join(ROOT, "scripts", "build.sh"), f"BUILD_DIR={BUILD_DIR}", |
| 189 | "MEMWASTE_PROFILE=1", f"CC={args.cc}", f"CXX={cxx}"], |
| 190 | check=True, stdout=subprocess.DEVNULL) |
| 191 | if not os.access(binary, os.X_OK): |
| 192 | sys.exit(f"memwaste-scaling: {binary} is missing (build it, or drop --no-build)") |
| 193 | profdata = find_profdata(args.cc) |
| 194 | work = os.path.abspath(args.out) if args.out else tempfile.mkdtemp(prefix="cbm-scaling.") |
| 195 | os.makedirs(work, exist_ok=True) |
| 196 | for sub in os.listdir(work): |
| 197 | if sub.startswith("leg-"): |
| 198 | shutil.rmtree(os.path.join(work, sub), ignore_errors=True) |
| 199 | |
| 200 | k = args.replicas |
| 201 | small, info_k = run_leg(binary, profdata, corpus, work, k) |
| 202 | large, info_2k = run_leg(binary, profdata, corpus, work, 2 * k) |
| 203 | |
| 204 | total_k = sum(small.values()) |
| 205 | total_2k = sum(large.values()) |
| 206 | rows = [] |
| 207 | for name, c2 in large.items(): |
| 208 | c1 = small.get(name, 0) |
| 209 | excess = c2 - 2 * c1 |
| 210 | ratio = (c2 / c1) if c1 else float("inf") |
| 211 | rows.append({"function": name, "k": c1, "2k": c2, "ratio": ratio, "excess": excess}) |
| 212 | flagged = [r for r in rows if r["ratio"] > args.threshold and r["excess"] >= args.min_excess] |
| 213 | flagged.sort(key=lambda r: -r["excess"]) |
| 214 | heaviest = sorted(rows, key=lambda r: -r["2k"])[: args.top] |
| 215 | |
| 216 | for info in (info_k, info_2k): |
| 217 | other = info.pop("other", {}) or {} |
| 218 | info["other_total"] = sum(other.values()) |
| 219 | info["other_top"] = sorted(other.items(), key=lambda kv: -kv[1])[:10] |
| 220 | report = { |
| 221 | "corpus": corpus, "legs": [info_k, info_2k], "threshold": args.threshold, |
| 222 | "min_excess": args.min_excess, "total_counts": {"k": total_k, "2k": total_2k}, |
| 223 | "total_ratio": (total_2k / total_k) if total_k else None, |
| 224 | "superlinear": flagged, |
| 225 | "work_dir": work, |
no test coverage detected