from pathlib import Path import hashlib,json,os,shutil,subprocess,sys,time R=Path('');D=R/'pgo-c6';D.mkdir(exist_ok=True) os.sched_setaffinity(0,{10}) sys.path.insert(0,str(R));import bench_prebuilt as B T=Path('');h=(R/'pgo-standard.rs').read_bytes() binary,meta=B.build(T,h,R/'bench-cache',True) shutil.copytree(T/'src',D/'src',dirs_exist_ok=True);(D/'benchmarks').mkdir(exist_ok=True) # Apply the same deterministic constructor fixture as the paired runner. p=D/'src/random.rs';s=p.read_text();s=s.replace('use std::{collections::hash_map::RandomState as SeedSource, hash::BuildHasher};','use std::sync::atomic::{AtomicU64, Ordering};\nstatic SEED: AtomicU64 = AtomicU64::new(1);\npub fn reset_benchmark_seed() { SEED.store(1, Ordering::Relaxed); }');s=s.replace('let source = SeedSource::new();\n Self::from_seed(std::array::from_fn(|i| source.hash_one(i)))','let n=SEED.fetch_add(1, Ordering::Relaxed); Self::from_seed([n, n.wrapping_mul(0x9e3779b97f4a7c15), n ^ 0xdeadbeef, n.wrapping_add(42)])');p.write_text(s) (D/'benchmarks/standard.rs').write_bytes((R/'bench-cache'/meta['cache_key']/'measured-harness.rs').read_bytes()) shutil.copy2(binary,D/'before');shutil.copy2(R/'bench-cache'/meta['cache_key']/'source.patch',D/'source.patch') raw=D/'raw';raw.mkdir(exist_ok=True) flags=B.BENCH.FLAGS subprocess.run(['rustc',*flags,'--crate-name','standard','-C',f'profile-generate={raw}','benchmarks/standard.rs','-o',str(D/'instrumented')],cwd=D,check=True) names=subprocess.check_output([D/'instrumented','--list'],text=True).splitlines();training=[n for n in names if not n.startswith('holdout-')] rows=[] for n in training: print('Training',n,flush=True);start=time.time();out=subprocess.check_output([D/'instrumented','48000','256','4',n,'10'],text=True);rows.append(dict(case=n,stdout=out,seconds=time.time()-start)) profdata=Path(subprocess.check_output(['rustc','--print','sysroot'],text=True).strip())/'lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata' subprocess.run([profdata,'merge','-o',str(D/'profile.profdata'),*[str(p) for p in sorted(raw.glob('*.profraw'))]],check=True) subprocess.run(['rustc',*flags,'--crate-name','standard','-C',f'profile-use={D}/profile.profdata','-C','llvm-args=-pgo-warn-missing-function','benchmarks/standard.rs','-o',str(D/'after')],cwd=D,check=True) metadata=dict(implementation=meta,flags=flags,training_cases=training,training=rows,holdout_cases=[n for n in names if n.startswith('holdout-')],profile_sha256=hashlib.sha256((D/'profile.profdata').read_bytes()).hexdigest(),before_sha256=hashlib.sha256((D/'before').read_bytes()).hexdigest(),after_sha256=hashlib.sha256((D/'after').read_bytes()).hexdigest(),rustc=B.BENCH.capture('rustc','--version','--verbose')) (D/'metadata.json').write_text(json.dumps(metadata,indent=2)+'\n') print('Prepared PGO comparison',flush=True)