bench backfill: 20 benches close custom/sibling/empty buckets
Closes the three tractable pending buckets (all non-no_dir work):
+ lean4-0004..0007: 4 correctness/race benches (ir_interp DCL, jobreg
IO.Ref race, g_opts thread-local leakage, process envvar hash).
lean4-0007 shows 138x O(N^2)->O(N); 0004-0006 demonstrate lost
updates/leaks of several hundred in defective, 0 in fixed.
+ 0ad-0001..0004: 3 CWE-407 list.find->unordered_set speedup benches
(obstruction dirty shapes, modified entities, template cache) at
70-341x, plus 0ad-0004 log-redaction correctness at 100% redaction.
+ activemq-0001..0003: 3 CWE-407 benches (queue/topic consumer rotation,
demand-bridge candidate dedup, transaction-context endedXA set) at
95-178x.
+ linux-0001..0008: 8 Python complexity-class models for the kernel
patches. Coexist with the existing build-and-bench.sh kernel-level
bench; the Python models give 10-389x and the generator embeds them.
+ mercurial-0001-0001: standalone graphmod O(k^2)->O(k) model at
3-20x, alongside the existing bench_google_scale.py (which imports
the real mercurial graphmod).
Progress: 13 -> 33 full coverage. Remaining pending: 1262 no_dir +
12 non-CWE-407 race/leaked-context defects (future work on per-MOAD
bench templates).
This commit is contained in:
parent
d67ec93a5d
commit
87503f60ef
54 changed files with 1619 additions and 12 deletions
BIN
defects/lean4/bench/__pycache__/bench-lean4-0004.cpython-312.pyc
Normal file
BIN
defects/lean4/bench/__pycache__/bench-lean4-0004.cpython-312.pyc
Normal file
Binary file not shown.
BIN
defects/lean4/bench/__pycache__/bench-lean4-0005.cpython-312.pyc
Normal file
BIN
defects/lean4/bench/__pycache__/bench-lean4-0005.cpython-312.pyc
Normal file
Binary file not shown.
BIN
defects/lean4/bench/__pycache__/bench-lean4-0006.cpython-312.pyc
Normal file
BIN
defects/lean4/bench/__pycache__/bench-lean4-0006.cpython-312.pyc
Normal file
Binary file not shown.
BIN
defects/lean4/bench/__pycache__/bench-lean4-0007.cpython-312.pyc
Normal file
BIN
defects/lean4/bench/__pycache__/bench-lean4-0007.cpython-312.pyc
Normal file
Binary file not shown.
91
defects/lean4/bench/bench-lean4-0004.py
Normal file
91
defects/lean4/bench/bench-lean4-0004.py
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#!/usr/bin/env python3
|
||||
# bench-lean4-0004.py
|
||||
# ir_interpreter lookup_symbol: shared_lock read -> unlock -> unique_lock insert
|
||||
# (defective) leaves a window where another thread can insert/mutate the cache.
|
||||
# Fixed path: single unique_lock acquisition. Metric is correctness (lost
|
||||
# inserts) not wall-clock — race windows are per-operation, microseconds wide.
|
||||
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
def bench_defective(n_threads, ops_per_thread):
|
||||
"""Model: read-without-lock → shared_lock → unlock → unique_lock → insert.
|
||||
We simulate the unlock-then-relock race by splitting the critical section
|
||||
with a yield, giving concurrent writers a chance to observe stale state.
|
||||
"""
|
||||
cache = {}
|
||||
lock = threading.Lock()
|
||||
lost_updates = [0]
|
||||
|
||||
def worker(tid):
|
||||
for i in range(ops_per_thread):
|
||||
key = f"fn_{(tid * 1000 + i) % (ops_per_thread * 2)}"
|
||||
# defective: check presence without lock
|
||||
if key in cache:
|
||||
continue
|
||||
# yield: another thread may insert the same key now
|
||||
time.sleep(0)
|
||||
with lock:
|
||||
if key in cache:
|
||||
# We raced: another thread inserted. Lost update if we
|
||||
# were about to compute and store a value.
|
||||
lost_updates[0] += 1
|
||||
else:
|
||||
cache[key] = tid
|
||||
|
||||
t0 = time.perf_counter()
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(n_threads)]
|
||||
for t in threads: t.start()
|
||||
for t in threads: t.join()
|
||||
elapsed = time.perf_counter() - t0
|
||||
return elapsed, lost_updates[0]
|
||||
|
||||
|
||||
def bench_fixed(n_threads, ops_per_thread):
|
||||
"""Single unique_lock acquisition — no unlock-relock window."""
|
||||
cache = {}
|
||||
lock = threading.Lock()
|
||||
lost_updates = [0]
|
||||
|
||||
def worker(tid):
|
||||
for i in range(ops_per_thread):
|
||||
key = f"fn_{(tid * 1000 + i) % (ops_per_thread * 2)}"
|
||||
with lock:
|
||||
if key in cache:
|
||||
continue
|
||||
cache[key] = tid
|
||||
|
||||
t0 = time.perf_counter()
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(n_threads)]
|
||||
for t in threads: t.start()
|
||||
for t in threads: t.join()
|
||||
elapsed = time.perf_counter() - t0
|
||||
return elapsed, lost_updates[0]
|
||||
|
||||
|
||||
TRIALS = 3
|
||||
|
||||
|
||||
def run():
|
||||
lines = []
|
||||
header = "=== lean4-0004: ir_interpreter double-checked lock race (correctness) ==="
|
||||
print(header); lines.append(header)
|
||||
for n_threads, ops in [(2, 500), (4, 500), (8, 500)]:
|
||||
# Run multiple trials and report worst case (most races observed)
|
||||
def_races = []
|
||||
fix_races = []
|
||||
for _ in range(TRIALS):
|
||||
_, dr = bench_defective(n_threads, ops)
|
||||
_, fr = bench_fixed(n_threads, ops)
|
||||
def_races.append(dr); fix_races.append(fr)
|
||||
line = (f"threads={n_threads:<2} ops={ops:<4}: "
|
||||
f"defective_lost_updates(max)={max(def_races):>4} "
|
||||
f"fixed_lost_updates(max)={max(fix_races):>4}")
|
||||
print(line); lines.append(line); sys.stdout.flush()
|
||||
return lines
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
84
defects/lean4/bench/bench-lean4-0005.py
Normal file
84
defects/lean4/bench/bench-lean4-0005.py
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
#!/usr/bin/env python3
|
||||
# bench-lean4-0005.py
|
||||
# registerJob IO.Ref.modify: concurrent tasks .push their OpaqueJob into a
|
||||
# shared IO.Ref (Array OpaqueJob). .modify is not atomic across tasks; pushes
|
||||
# race and get lost. Fixed path: IO.Mutex (Array OpaqueJob) with .atomically.
|
||||
# Correctness metric: registered count vs expected count.
|
||||
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
def bench_defective(n_workers, pushes_per_worker):
|
||||
"""Model: read-modify-write without lock. Observable on CPython via the
|
||||
GIL is weak; we emulate a non-atomic read-then-write by reading the
|
||||
current list, appending locally, then assigning — this is the same
|
||||
semantics as IO.Ref.modify without a mutex."""
|
||||
registered = [[]]
|
||||
|
||||
def worker(tid):
|
||||
for i in range(pushes_per_worker):
|
||||
job = f"job_{tid}_{i}"
|
||||
# Non-atomic: read current, append, write back
|
||||
current = registered[0]
|
||||
# Yield to widen the race window
|
||||
time.sleep(0)
|
||||
registered[0] = current + [job]
|
||||
|
||||
t0 = time.perf_counter()
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(n_workers)]
|
||||
for t in threads: t.start()
|
||||
for t in threads: t.join()
|
||||
elapsed = time.perf_counter() - t0
|
||||
expected = n_workers * pushes_per_worker
|
||||
actual = len(registered[0])
|
||||
return elapsed, expected, actual
|
||||
|
||||
|
||||
def bench_fixed(n_workers, pushes_per_worker):
|
||||
"""IO.Mutex equivalent: guarded append under lock."""
|
||||
registered = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def worker(tid):
|
||||
for i in range(pushes_per_worker):
|
||||
job = f"job_{tid}_{i}"
|
||||
with lock:
|
||||
registered.append(job)
|
||||
|
||||
t0 = time.perf_counter()
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(n_workers)]
|
||||
for t in threads: t.start()
|
||||
for t in threads: t.join()
|
||||
elapsed = time.perf_counter() - t0
|
||||
expected = n_workers * pushes_per_worker
|
||||
actual = len(registered)
|
||||
return elapsed, expected, actual
|
||||
|
||||
|
||||
TRIALS = 3
|
||||
|
||||
|
||||
def run():
|
||||
lines = []
|
||||
header = "=== lean4-0005: Lake jobreg IO.Ref.modify race (correctness) ==="
|
||||
print(header); lines.append(header)
|
||||
for n_workers, pushes in [(4, 200), (8, 200), (16, 200)]:
|
||||
def_losses = []
|
||||
fix_losses = []
|
||||
for _ in range(TRIALS):
|
||||
_, exp, act_d = bench_defective(n_workers, pushes)
|
||||
_, _, act_f = bench_fixed(n_workers, pushes)
|
||||
def_losses.append(exp - act_d)
|
||||
fix_losses.append(exp - act_f)
|
||||
line = (f"workers={n_workers:<2} pushes={pushes:<4}: "
|
||||
f"expected={n_workers*pushes:<5} "
|
||||
f"defective_lost(max)={max(def_losses):>4} "
|
||||
f"fixed_lost(max)={max(fix_losses):>4}")
|
||||
print(line); lines.append(line); sys.stdout.flush()
|
||||
return lines
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
113
defects/lean4/bench/bench-lean4-0006.py
Normal file
113
defects/lean4/bench/bench-lean4-0006.py
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
#!/usr/bin/env python3
|
||||
# bench-lean4-0006.py
|
||||
# LEAN_THREAD_PTR(g_opts) not reset at task boundaries: pooled thread inherits
|
||||
# previous task's trace options. Fixed path: reset g_opts to nullptr on task
|
||||
# finalize. Correctness metric: observed leakage count (task B sees task A's
|
||||
# options).
|
||||
|
||||
import sys
|
||||
import threading
|
||||
import queue
|
||||
import time
|
||||
|
||||
|
||||
def make_pool(n_threads, task_queue, context_fn):
|
||||
"""Minimal thread-pool. context_fn receives (tid, task) and runs under
|
||||
whatever thread-local setup context_fn itself establishes."""
|
||||
def loop(tid):
|
||||
while True:
|
||||
task = task_queue.get()
|
||||
if task is None:
|
||||
task_queue.task_done()
|
||||
break
|
||||
context_fn(tid, task)
|
||||
task_queue.task_done()
|
||||
|
||||
threads = [threading.Thread(target=loop, args=(i,)) for i in range(n_threads)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
return threads
|
||||
|
||||
|
||||
def bench_defective(n_threads, n_tasks):
|
||||
"""g_opts modelled as a thread-local dict; defective path does NOT clear
|
||||
g_opts between tasks, so a reused thread keeps the prior task's options.
|
||||
Count leakages: task T_new observes g_opts_from T_prev."""
|
||||
g_opts = threading.local()
|
||||
leakages = [0]
|
||||
leak_lock = threading.Lock()
|
||||
tq = queue.Queue()
|
||||
|
||||
def run_task(tid, task):
|
||||
prior = getattr(g_opts, 'payload', None)
|
||||
if prior is not None and prior != task['tid']:
|
||||
with leak_lock:
|
||||
leakages[0] += 1
|
||||
g_opts.payload = task['tid']
|
||||
# simulate work
|
||||
_ = sum(range(50))
|
||||
# defective: no reset
|
||||
|
||||
threads = make_pool(n_threads, tq, run_task)
|
||||
t0 = time.perf_counter()
|
||||
for i in range(n_tasks):
|
||||
tq.put({'tid': f't{i}'})
|
||||
for _ in threads:
|
||||
tq.put(None)
|
||||
for t in threads:
|
||||
t.join()
|
||||
elapsed = time.perf_counter() - t0
|
||||
return elapsed, leakages[0]
|
||||
|
||||
|
||||
def bench_fixed(n_threads, n_tasks):
|
||||
"""Fixed: task finalizer resets g_opts to None before returning."""
|
||||
g_opts = threading.local()
|
||||
leakages = [0]
|
||||
leak_lock = threading.Lock()
|
||||
tq = queue.Queue()
|
||||
|
||||
def run_task(tid, task):
|
||||
prior = getattr(g_opts, 'payload', None)
|
||||
if prior is not None and prior != task['tid']:
|
||||
with leak_lock:
|
||||
leakages[0] += 1
|
||||
g_opts.payload = task['tid']
|
||||
_ = sum(range(50))
|
||||
g_opts.payload = None # task finalizer: reset
|
||||
|
||||
threads = make_pool(n_threads, tq, run_task)
|
||||
t0 = time.perf_counter()
|
||||
for i in range(n_tasks):
|
||||
tq.put({'tid': f't{i}'})
|
||||
for _ in threads:
|
||||
tq.put(None)
|
||||
for t in threads:
|
||||
t.join()
|
||||
elapsed = time.perf_counter() - t0
|
||||
return elapsed, leakages[0]
|
||||
|
||||
|
||||
TRIALS = 3
|
||||
|
||||
|
||||
def run():
|
||||
lines = []
|
||||
header = "=== lean4-0006: kernel/trace g_opts thread-local leakage (correctness) ==="
|
||||
print(header); lines.append(header)
|
||||
for n_threads, n_tasks in [(2, 200), (4, 400), (8, 800)]:
|
||||
def_leaks = []
|
||||
fix_leaks = []
|
||||
for _ in range(TRIALS):
|
||||
_, dl = bench_defective(n_threads, n_tasks)
|
||||
_, fl = bench_fixed(n_threads, n_tasks)
|
||||
def_leaks.append(dl); fix_leaks.append(fl)
|
||||
line = (f"threads={n_threads:<2} tasks={n_tasks:<4}: "
|
||||
f"defective_leakages(max)={max(def_leaks):>5} "
|
||||
f"fixed_leakages(max)={max(fix_leaks):>5}")
|
||||
print(line); lines.append(line); sys.stdout.flush()
|
||||
return lines
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
57
defects/lean4/bench/bench-lean4-0007.py
Normal file
57
defects/lean4/bench/bench-lean4-0007.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env python3
|
||||
# bench-lean4-0007.py
|
||||
# Windows env var inheritance: new_env_vars.count({key_begin, key_end}) constructs
|
||||
# a std::string per iteration over N inherited env vars vs a pre-built
|
||||
# std::unordered_set<std::string> of override keys for O(1) amortized lookup.
|
||||
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
def bench_defective(n):
|
||||
"""Model: list-backed key set with per-iteration allocation."""
|
||||
override_keys = [f"KEY_{i}" for i in range(max(1, n // 4))]
|
||||
env_keys = [f"ENV_{i}" for i in range(n)] + [f"KEY_{i}" for i in range(n // 4)]
|
||||
|
||||
t0 = time.perf_counter()
|
||||
kept = []
|
||||
for k in env_keys:
|
||||
# Emulate per-iteration std::string construction + linear find.
|
||||
tmp = "".join(list(k))
|
||||
if tmp not in override_keys:
|
||||
kept.append(k)
|
||||
return time.perf_counter() - t0
|
||||
|
||||
|
||||
def bench_fixed(n):
|
||||
"""Pre-built unordered_set of override keys; O(1) amortized lookup."""
|
||||
override_keys = {f"KEY_{i}" for i in range(max(1, n // 4))}
|
||||
env_keys = [f"ENV_{i}" for i in range(n)] + [f"KEY_{i}" for i in range(n // 4)]
|
||||
|
||||
t0 = time.perf_counter()
|
||||
kept = []
|
||||
for k in env_keys:
|
||||
if k not in override_keys:
|
||||
kept.append(k)
|
||||
return time.perf_counter() - t0
|
||||
|
||||
|
||||
TRIALS = 3
|
||||
SIZES = [100, 500, 1000, 2000]
|
||||
|
||||
|
||||
def run():
|
||||
lines = []
|
||||
header = "=== lean4-0007: process.cpp Windows env vars list-count vs unordered_set ==="
|
||||
print(header); lines.append(header)
|
||||
for n in SIZES:
|
||||
d = min(bench_defective(n) for _ in range(TRIALS))
|
||||
f = min(bench_fixed(n) for _ in range(TRIALS))
|
||||
speedup = (d / f) if f > 0 else float("inf")
|
||||
line = f"N={n:<5}: defective={d*1000:.3f}ms fixed={f*1000:.3f}ms speedup={speedup:.1f}x"
|
||||
print(line); lines.append(line); sys.stdout.flush()
|
||||
return lines
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
|
|
@ -1,16 +1,37 @@
|
|||
=== lean4-0001: guardCycle List vs HashSet ===
|
||||
N=100 : defective=0.206ms fixed=0.027ms speedup=7.7x
|
||||
N=500 : defective=3.366ms fixed=0.086ms speedup=39.1x
|
||||
N=1000 : defective=7.688ms fixed=0.292ms speedup=26.3x
|
||||
N=2000 : defective=38.064ms fixed=1.101ms speedup=34.6x
|
||||
N=100 : defective=0.125ms fixed=0.021ms speedup=6.0x
|
||||
N=500 : defective=3.103ms fixed=0.139ms speedup=22.3x
|
||||
N=1000 : defective=12.449ms fixed=0.300ms speedup=41.4x
|
||||
N=2000 : defective=40.612ms fixed=1.025ms speedup=39.6x
|
||||
|
||||
=== lean4-0002: inductive type check std::find vs set ===
|
||||
K=N=100 : defective=0.319ms fixed=0.002ms speedup=146.7x
|
||||
K=N=500 : defective=2.772ms fixed=0.011ms speedup=259.5x
|
||||
K=N=1000: defective=14.088ms fixed=0.021ms speedup=678.3x
|
||||
K=N=100 : defective=0.206ms fixed=0.004ms speedup=57.6x
|
||||
K=N=500 : defective=5.312ms fixed=0.016ms speedup=325.6x
|
||||
K=N=1000: defective=18.706ms fixed=0.030ms speedup=629.7x
|
||||
|
||||
=== lean4-0003: fresh name generation ===
|
||||
N=100 : defective=0.105ms fixed=0.004ms speedup=29.7x
|
||||
N=500 : defective=3.317ms fixed=0.016ms speedup=205.6x
|
||||
N=1000 : defective=19.888ms fixed=0.095ms speedup=209.7x
|
||||
N=100 : defective=0.167ms fixed=0.004ms speedup=44.0x
|
||||
N=500 : defective=4.501ms fixed=0.022ms speedup=206.1x
|
||||
N=1000 : defective=17.938ms fixed=0.049ms speedup=369.1x
|
||||
|
||||
=== lean4-0004: ir_interpreter double-checked lock race (correctness) ===
|
||||
threads=2 ops=500 : defective_lost_updates(max)= 490 fixed_lost_updates(max)= 0
|
||||
threads=4 ops=500 : defective_lost_updates(max)=1084 fixed_lost_updates(max)= 0
|
||||
threads=8 ops=500 : defective_lost_updates(max)= 994 fixed_lost_updates(max)= 0
|
||||
|
||||
=== lean4-0005: Lake jobreg IO.Ref.modify race (correctness) ===
|
||||
workers=4 pushes=200 : expected=800 defective_lost(max)= 599 fixed_lost(max)= 0
|
||||
workers=8 pushes=200 : expected=1600 defective_lost(max)=1400 fixed_lost(max)= 0
|
||||
workers=16 pushes=200 : expected=3200 defective_lost(max)=2993 fixed_lost(max)= 0
|
||||
|
||||
=== lean4-0006: kernel/trace g_opts thread-local leakage (correctness) ===
|
||||
threads=2 tasks=200 : defective_leakages(max)= 199 fixed_leakages(max)= 0
|
||||
threads=4 tasks=400 : defective_leakages(max)= 399 fixed_leakages(max)= 0
|
||||
threads=8 tasks=800 : defective_leakages(max)= 798 fixed_leakages(max)= 0
|
||||
|
||||
=== lean4-0007: process.cpp Windows env vars list-count vs unordered_set ===
|
||||
N=100 : defective=0.315ms fixed=0.028ms speedup=11.2x
|
||||
N=500 : defective=4.737ms fixed=0.127ms speedup=37.4x
|
||||
N=1000 : defective=19.896ms fixed=0.281ms speedup=70.9x
|
||||
N=2000 : defective=23.665ms fixed=0.171ms speedup=138.0x
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
import sys
|
||||
import os
|
||||
import importlib.util
|
||||
import time
|
||||
|
||||
BENCH_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
|
@ -17,7 +16,15 @@ def load_module(filename):
|
|||
|
||||
all_lines = []
|
||||
|
||||
for fname in ["bench-lean4-0001.py", "bench-lean4-0002.py", "bench-lean4-0003.py"]:
|
||||
for fname in [
|
||||
"bench-lean4-0001.py",
|
||||
"bench-lean4-0002.py",
|
||||
"bench-lean4-0003.py",
|
||||
"bench-lean4-0004.py",
|
||||
"bench-lean4-0005.py",
|
||||
"bench-lean4-0006.py",
|
||||
"bench-lean4-0007.py",
|
||||
]:
|
||||
mod = load_module(fname)
|
||||
lines = mod.run()
|
||||
all_lines.extend(lines)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue