include/punk_observe/po_seg.h

filter: Statementstmt 93.8% 243 / 259 Branchbran 64.7% 123 / 190 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.5% 366 / 449 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_pb_read.h ↑ Summary xs/live.xs »
1 /* po_seg.h - the immutable segment.
2 *
3 * One container, three payload regions, so a query over a time range opens
4 * one file per range rather than three and the timestamp pruning is written
5 * once.
6 *
7 * TWO INVARIANTS CARRY EVERYTHING ELSE.
8 *
9 * 1. THE FOOTER IS LAST, AND THE MAGIC IS THE LAST BYTES OF IT. A segment is
10 * valid if and only if that magic is present, which makes "was this
11 * completely written" a read of the last few bytes rather than a scan. Any
12 * code path that opens a segment without checking will one day read a torn
13 * one, and it will not look torn - it will look like missing data.
14 *
15 * 2. A SEALED SEGMENT IS NEVER MODIFIED. Written to .tmp, fsync'd, renamed.
16 * The rename is the commit in the directory; the footer is the commit
17 * inside the file. It is deleted by unlink and NEVER by ftruncate: an
18 * unlinked file's existing mappings stay valid on POSIX, a truncated
19 * file's mappings SIGBUS on the next touch of a removed page. That is what
20 * makes lock-free readers possible, and phase 10 is where it gets broken
21 * if it is going to be.
22 */
23 #ifndef PO_SEG_H
24 #define PO_SEG_H
25  
26 #include "punk_observe/po_compat.h"
27 #include "punk_observe/po_rec.h"
28 #include "punk_observe/po_crc32c.h"
29 #include "punk_observe/po_intern.h"
30 #include "punk_observe/po_labels.h"
31  
32 #include <stdio.h>
33 #include <errno.h>
34 #ifndef _WIN32
35 # include <unistd.h>
36 # include <fcntl.h>
37 # include <sys/stat.h>
38 # include <sys/mman.h>
39 #else
40 # include <io.h>
41 # include <fcntl.h>
42 #endif
43  
44 #define PO_SEG_MAGIC 0x47534F4Fu /* "OOSG" */
45 #define PO_FOOT_MAGIC 0x54464F4Fu /* "OOFT" */
46 #define PO_SEG_VERSION 1
47  
48 #define PO_SEG_HDR 64
49 #define PO_SEG_FOOT 96
50 #define PO_SEG_MAX_REGIONS 16
51  
52 /* REGION KINDS.
53 *
54 * The first three are the original fixed areas. Everything after is a
55 * per-signal area that phases 5-7 produce: metric chunks, log blocks, span
56 * arrays and their indexes. They live in a TABLE rather than at fixed
57 * offsets, so a signal that gains a structure later does not shift the ones
58 * beside it and a reader that does not know a kind can skip it.
59 *
60 * That is the point of a table: an older reader meeting a newer segment
61 * skips what it does not recognise, instead of misreading it. */
62 #define PO_RGN_RECORDS 1
63 #define PO_RGN_ARENA 2
64 #define PO_RGN_SYMBOLS 3
65 #define PO_RGN_MSERIES 4 /* metric series directory */
66 #define PO_RGN_MCHUNKS 5 /* gorilla bit streams */
67 #define PO_RGN_MEXEMPLAR 6
68 #define PO_RGN_LOGDIR 7 /* log block directory */
69 #define PO_RGN_LOGBLOCKS 8 /* deflated blocks + blooms */
70 #define PO_RGN_SPANS 9 /* the 64-byte span array */
71 #define PO_RGN_TRACEIDX 10
72 #define PO_RGN_TRACESUM 11
73 #define PO_RGN_SGRAPH 12
74  
75 typedef struct {
76 uint32_t kind;
77 po_u64 off, len;
78 } po_region;
79  
80 /* signal, so a reader knows what the records region holds without decoding */
81 #define PO_SIG_MIXED 0
82 #define PO_SIG_TRACE 1
83 #define PO_SIG_METRIC 2
84 #define PO_SIG_LOG 3
85  
86 typedef struct {
87 uint32_t magic;
88 uint16_t version;
89 uint8_t signal;
90 uint8_t flags;
91 uint32_t tenant_hash; /* of the tenant id: checked on OPEN, so a
92 * mis-filed segment is caught then rather than
93 * after it has been served */
94 uint32_t worker_slot;
95 po_u64 t_min, t_max;
96 po_u64 records;
97 uint8_t ulid[16];
98 } po_seg_hdr;
99  
100 /* A writer accumulates records and symbols, then emits the whole file. */
101 typedef struct {
102 po_rec *rec;
103 size_t n, cap;
104 po_arena arena; /* record payloads: bodies */
105 po_intern sym; /* every string in the segment */
106 po_arena labels; /* canonical blocks, owned by the series tab */
107 po_series_tab series;
108 po_u64 t_min, t_max;
109 uint8_t signal;
110 uint32_t worker_slot;
111 uint32_t tenant_hash;
112  
113 /* per-signal blobs, attached by the caller and written verbatim */
114 char *rgn[PO_SEG_MAX_REGIONS];
115 size_t rgn_len[PO_SEG_MAX_REGIONS];
116 uint32_t rgn_kind[PO_SEG_MAX_REGIONS];
117 int nrgn;
118 int rgn_owned[PO_SEG_MAX_REGIONS];
119 } po_seg_w;
120  
121 /* Attach a prepared region. The writer takes ownership when `owned` is set,
122 * which is what a serialiser that malloc'd its output wants. */
123 10 static int po_seg_w_region(po_seg_w *w, uint32_t kind,
124 char *blob, size_t len, int owned) {
125 10 ▶ if (w->nrgn >= PO_SEG_MAX_REGIONS) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w->nrgn >= PO_SEG_MAX_REGIONS) return 0; 0 10 10
126 10 w->rgn[w->nrgn] = blob;
127 10 w->rgn_len[w->nrgn] = len;
128 10 w->rgn_kind[w->nrgn] = kind;
129 10 w->rgn_owned[w->nrgn] = owned;
130 10 w->nrgn++;
131 10 return 1;
132 }
133  
134 /* Widen the segment's declared time span to cover something the records
135 * region does not.
136 *
137 * THE FOOTER'S SPAN IS A PROMISE THE PRUNER BELIEVES. `po_seg_overlaps`
138 * decides from t_min/t_max alone whether a segment is worth opening, so a
139 * segment whose metric chunks or log blocks reach outside the span its
140 * records declare is a segment a correct query will skip. That is a silent
141 * loss of data, not a slow query, so anything attached as a region widens the
142 * span here. */
143 16 static void po_seg_w_span(po_seg_w *w, po_u64 t_min, po_u64 t_max) {
144 16 ▶ if (t_min < w->t_min) w->t_min = t_min;
Branch: 1 branch
BranchTrueFalseTotal
if (t_min < w->t_min) w->t_min = t_min; 0 16 16
145 16 ▶ if (t_max > w->t_max) w->t_max = t_max;
Branch: 1 branch
BranchTrueFalseTotal
if (t_max > w->t_max) w->t_max = t_max; 3 13 16
146 16 }
147  
148 27 static int po_seg_w_init(po_seg_w *w, uint8_t signal, uint32_t slot,
149 const char *tenant, size_t tlen) {
150 27 memset(w, 0, sizeof(*w));
151 27 w->cap = 256;
152 27 w->rec = (po_rec *)malloc(w->cap * sizeof(po_rec));
153 27 ▶ if (!w->rec) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!w->rec) return 0; 0 27 27
154 27 ▶ if (!po_arena_init(&w->arena, 4096)) goto fail1;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_arena_init(&w->arena, 4096)) goto fail1; 0 27 27
155 27 ▶ if (!po_intern_init(&w->sym, 256)) goto fail2;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_intern_init(&w->sym, 256)) goto fail2; 0 27 27
156 27 ▶ if (!po_arena_init(&w->labels, 4096)) goto fail3;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_arena_init(&w->labels, 4096)) goto fail3; 0 27 27
157 27 ▶ if (!po_series_tab_init(&w->series, &w->labels, 256)) goto fail4;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_series_tab_init(&w->series, &w->labels, 256)) goto fail4; 0 27 27
158 27 w->signal = signal;
159 27 w->worker_slot = slot;
160 27 ▶ w->tenant_hash = po_hash32(tenant ? tenant : "", tenant ? tlen : 0);
Branch: 2 branches
BranchTrueFalseTotal
w->tenant_hash = po_hash32(tenant ? tenant : "", tenant ? tlen : 0); 27 0 27
w->tenant_hash = po_hash32(tenant ? tenant : "", tenant ? tlen : 0); 27 0 27
161 27 w->t_min = PO_U64_MAX;
162 27 w->t_max = 0;
163 27 return 1;
164 0 fail4: po_arena_free(&w->labels);
165 0 fail3: po_intern_free(&w->sym);
166 0 fail2: po_arena_free(&w->arena);
167 0 fail1: free(w->rec); w->rec = NULL;
168 0 return 0;
169 }
170  
171 27 static void po_seg_w_free(po_seg_w *w) {
172 int i;
173 37 ▶ for (i = 0; i < w->nrgn; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->nrgn; i++) 10 27 37
174 10 ▶ if (w->rgn_owned[i]) free(w->rgn[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (w->rgn_owned[i]) free(w->rgn[i]); 10 0 10
175 27 w->nrgn = 0;
176 27 free(w->rec); w->rec = NULL;
177 27 po_arena_free(&w->arena);
178 27 po_intern_free(&w->sym);
179 27 po_series_tab_free(&w->series);
180 27 po_arena_free(&w->labels);
181 27 w->n = w->cap = 0;
182 27 }
183  
184 /* Add a record, interning its body and its label block. `created` reports
185 * whether the series is new, which is the only moment the cardinality cap can
186 * be enforced. */
187 715 static int po_seg_w_add(po_seg_w *w, const po_rec *src,
188 const char *body, uint32_t body_len,
189 const char *labels, uint32_t labels_len,
190 int *series_created) {
191 po_rec *r;
192 uint32_t slot;
193 po_h128 id;
194  
195 715 ▶ if (w->n == w->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->n == w->cap) { 0 715 715
196 0 size_t want = w->cap * 2;
197 0 po_rec *nr = (po_rec *)realloc(w->rec, want * sizeof(po_rec));
198 0 ▶ if (!nr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nr) return 0; 0 0 0
199 0 w->rec = nr; w->cap = want;
200 }
201  
202 715 slot = po_series_intern(&w->series, labels, labels_len, &id, series_created);
203 715 ▶ if (slot == PO_SERIES_ERR) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (slot == PO_SERIES_ERR) return 0; 0 715 715
204  
205 715 r = &w->rec[w->n];
206 715 *r = *src;
207 715 r->series = po_series_key(id);
208  
209 715 ▶ if (body_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_len) { 715 0 715
210 715 uint32_t sym = po_intern_put(&w->sym, body, body_len);
211 715 ▶ if (sym == PO_SYM_NONE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sym == PO_SYM_NONE) return 0; 0 715 715
212 /* The body becomes a SYMBOL id, not a copy. body_len is repurposed as
213 * a marker that the field is a symbol reference; the reader resolves
214 * through the segment's table. */
215 715 r->body_off = sym;
216 715 r->body_len = 0;
217 }
218 715 ▶ r->attr_off = labels_len ? po_arena_put(&w->arena, labels, labels_len) : 0;
Branch: 1 branch
BranchTrueFalseTotal
r->attr_off = labels_len ? po_arena_put(&w->arena, labels, labels_len) : 0; 715 0 715
219 715 ▶ if (labels_len && r->attr_off == PO_ARENA_ERR) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (labels_len && r->attr_off == PO_ARENA_ERR) return 0; 715 0 715
if (labels_len && r->attr_off == PO_ARENA_ERR) return 0; 0 715 715
220 715 r->attr_len = labels_len;
221  
222 715 ▶ if (r->t_unix_nano < w->t_min) w->t_min = r->t_unix_nano;
Branch: 1 branch
BranchTrueFalseTotal
if (r->t_unix_nano < w->t_min) w->t_min = r->t_unix_nano; 27 688 715
223 715 ▶ if (r->t_unix_nano > w->t_max) w->t_max = r->t_unix_nano;
Branch: 1 branch
BranchTrueFalseTotal
if (r->t_unix_nano > w->t_max) w->t_max = r->t_unix_nano; 714 1 715
224 715 w->n++;
225 715 return 1;
226 }
227  
228 148 static int po_write_all_fd(int fd, const char *p, size_t n) {
229 296 ▶ while (n) {
Branch: 1 branch
BranchTrueFalseTotal
while (n) { 148 148 296
230 148 ssize_t k = write(fd, p, n);
231 148 ▶ if (k < 0) { if (errno == EINTR) continue; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (k < 0) { if (errno == EINTR) continue; return 0; } 0 148 148
if (k < 0) { if (errno == EINTR) continue; return 0; } 0 0 0
232 148 ▶ if (k == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (k == 0) return 0; 0 148 148
233 148 p += (size_t)k; n -= (size_t)k;
234 }
235 148 return 1;
236 }
237  
238 /* Write the segment to `path`, atomically.
239 *
240 * .tmp in the SAME DIRECTORY as the destination, because rename is atomic
241 * within a filesystem and not across one. Then fsync the file, rename, and
242 * fsync the DIRECTORY - without that last one the rename itself can be lost
243 * on a crash even though the data was not. */
244 27 static int po_seg_write(po_seg_w *w, const char *path, const uint8_t ulid[16]) {
245 char tmp[4096];
246 int fd;
247 size_t symlen, reclen, arlen;
248 char hdr[PO_SEG_HDR], foot[PO_SEG_FOOT];
249 27 uint32_t crc = 0;
250 27 po_u64 off_rec, off_ar, off_sym, off_tab = 0;
251 27 uint32_t n_tab = 0;
252  
253 27 ▶ if (strlen(path) + 5 >= sizeof(tmp)) { errno = ENAMETOOLONG; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(path) + 5 >= sizeof(tmp)) { errno = ENAMETOOLONG; return 0; } 0 27 27
254 27 sprintf(tmp, "%s.tmp", path);
255  
256 27 reclen = w->n * sizeof(po_rec);
257 27 arlen = w->arena.len;
258 27 symlen = po_intern_size(&w->sym);
259  
260 27 off_rec = PO_SEG_HDR;
261 27 off_ar = off_rec + reclen;
262 27 off_sym = off_ar + arlen;
263  
264 { /* header */
265 27 char *q = hdr;
266 uint32_t u32; uint16_t u16; po_u64 u64;
267 27 memset(hdr, 0, sizeof(hdr));
268 27 u32 = po_le32(PO_SEG_MAGIC); memcpy(q, &u32, 4); q += 4;
269 27 u16 = PO_SEG_VERSION; q[0] = (char)(u16 & 0xFF);
270 27 q[1] = (char)(u16 >> 8); q += 2;
271 27 *q++ = (char)w->signal;
272 27 *q++ = 0; /* flags */
273 27 u32 = po_le32(w->tenant_hash); memcpy(q, &u32, 4); q += 4;
274 27 u32 = po_le32(w->worker_slot); memcpy(q, &u32, 4); q += 4;
275 27 ▶ u64 = po_le64(w->n ? w->t_min : 0); memcpy(q, &u64, 8); q += 8;
Branch: 1 branch
BranchTrueFalseTotal
u64 = po_le64(w->n ? w->t_min : 0); memcpy(q, &u64, 8); q += 8; 27 0 27
276 27 u64 = po_le64(w->t_max); memcpy(q, &u64, 8); q += 8;
277 27 u64 = po_le64((po_u64)w->n); memcpy(q, &u64, 8); q += 8;
278 27 memcpy(q, ulid, 16); q += 16;
279 }
280  
281 27 crc = po_crc32c(0, hdr, PO_SEG_HDR);
282 27 ▶ if (reclen) crc = po_crc32c(crc, w->rec, reclen);
Branch: 1 branch
BranchTrueFalseTotal
if (reclen) crc = po_crc32c(crc, w->rec, reclen); 27 0 27
283 27 ▶ if (arlen) crc = po_crc32c(crc, w->arena.base, arlen);
Branch: 1 branch
BranchTrueFalseTotal
if (arlen) crc = po_crc32c(crc, w->arena.base, arlen); 27 0 27
284  
285 #ifdef _WIN32
286 fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
287 #else
288 27 fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0644);
289 #endif
290 27 ▶ if (fd < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return 0; 0 27 27
291  
292 27 ▶ if (!po_write_all_fd(fd, hdr, PO_SEG_HDR)) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_write_all_fd(fd, hdr, PO_SEG_HDR)) goto fail; 0 27 27
293 27 ▶ if (reclen && !po_write_all_fd(fd, (const char *)w->rec, reclen)) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (reclen && !po_write_all_fd(fd, (const char *)w->rec, reclen)) goto fail; 27 0 27
if (reclen && !po_write_all_fd(fd, (const char *)w->rec, reclen)) goto fail; 0 27 27
294 27 ▶ if (arlen && !po_write_all_fd(fd, w->arena.base, arlen)) goto fail;
Branch: 2 branches
BranchTrueFalseTotal
if (arlen && !po_write_all_fd(fd, w->arena.base, arlen)) goto fail; 27 0 27
if (arlen && !po_write_all_fd(fd, w->arena.base, arlen)) goto fail; 0 27 27
295 {
296 27 char *sb = (char *)malloc(symlen);
297 int ok;
298 27 ▶ if (!sb) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!sb) goto fail; 0 27 27
299 27 po_intern_write(&w->sym, sb);
300 27 crc = po_crc32c(crc, sb, symlen);
301 27 ok = po_write_all_fd(fd, sb, symlen);
302 27 free(sb);
303 27 ▶ if (!ok) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) goto fail; 0 27 27
304 }
305  
306 /* the per-signal regions, then the table that locates them */
307 {
308 27 po_u64 at = off_sym + symlen;
309 po_region tab[PO_SEG_MAX_REGIONS];
310 int i;
311 char *tbuf;
312 size_t tlen;
313  
314 37 ▶ for (i = 0; i < w->nrgn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->nrgn; i++) { 10 27 37
315 10 tab[i].kind = w->rgn_kind[i];
316 10 tab[i].off = at;
317 10 tab[i].len = (po_u64)w->rgn_len[i];
318 10 ▶ if (w->rgn_len[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->rgn_len[i]) { 10 0 10
319 10 crc = po_crc32c(crc, w->rgn[i], w->rgn_len[i]);
320 10 ▶ if (!po_write_all_fd(fd, w->rgn[i], w->rgn_len[i])) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_write_all_fd(fd, w->rgn[i], w->rgn_len[i])) goto fail; 0 10 10
321 }
322 10 at += w->rgn_len[i];
323 }
324  
325 27 tlen = (size_t)w->nrgn * 20;
326 27 off_tab = at;
327 27 ▶ tbuf = (char *)malloc(tlen ? tlen : 1);
Branch: 1 branch
BranchTrueFalseTotal
tbuf = (char *)malloc(tlen ? tlen : 1); 3 24 27
328 27 ▶ if (!tbuf) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!tbuf) goto fail; 0 27 27
329 37 ▶ for (i = 0; i < w->nrgn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->nrgn; i++) { 10 27 37
330 10 uint32_t k = po_le32(tab[i].kind);
331 10 po_u64 o = po_le64(tab[i].off);
332 10 po_u64 l = po_le64(tab[i].len);
333 10 memcpy(tbuf + i * 20, &k, 4);
334 10 memcpy(tbuf + i * 20 + 4, &o, 8);
335 10 memcpy(tbuf + i * 20 + 12, &l, 8);
336 }
337 27 ▶ if (tlen) crc = po_crc32c(crc, tbuf, tlen);
Branch: 1 branch
BranchTrueFalseTotal
if (tlen) crc = po_crc32c(crc, tbuf, tlen); 3 24 27
338 27 ▶ if (tlen && !po_write_all_fd(fd, tbuf, tlen)) { free(tbuf); goto fail; }
Branch: 2 branches
BranchTrueFalseTotal
if (tlen && !po_write_all_fd(fd, tbuf, tlen)) { free(tbuf); goto fail; } 3 24 27
if (tlen && !po_write_all_fd(fd, tbuf, tlen)) { free(tbuf); goto fail; } 0 3 3
339 27 free(tbuf);
340 27 n_tab = (uint32_t)w->nrgn;
341 }
342  
343 { /* footer, and the magic is the LAST four bytes of the file */
344 27 char *q = foot;
345 uint32_t u32; po_u64 u64;
346 27 memset(foot, 0, sizeof(foot));
347 27 u32 = po_le32(PO_FOOT_MAGIC); memcpy(q, &u32, 4); q += 4;
348 27 u64 = po_le64(off_rec); memcpy(q, &u64, 8); q += 8;
349 27 u64 = po_le64((po_u64)reclen); memcpy(q, &u64, 8); q += 8;
350 27 u64 = po_le64(off_ar); memcpy(q, &u64, 8); q += 8;
351 27 u64 = po_le64((po_u64)arlen); memcpy(q, &u64, 8); q += 8;
352 27 u64 = po_le64(off_sym); memcpy(q, &u64, 8); q += 8;
353 27 u64 = po_le64((po_u64)symlen); memcpy(q, &u64, 8); q += 8;
354 27 u32 = po_le32(crc); memcpy(q, &u32, 4); q += 4;
355 27 u64 = po_le64(off_tab); memcpy(q, &u64, 8); q += 8;
356 27 u32 = po_le32(n_tab); memcpy(q, &u32, 4); q += 4;
357 /* the trailing magic: the ONLY thing that says this file is whole */
358 27 u32 = po_le32(PO_SEG_MAGIC);
359 27 memcpy(foot + PO_SEG_FOOT - 4, &u32, 4);
360 }
361 27 ▶ if (!po_write_all_fd(fd, foot, PO_SEG_FOOT)) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_write_all_fd(fd, foot, PO_SEG_FOOT)) goto fail; 0 27 27
362  
363 #ifndef _WIN32
364 27 ▶ if (fsync(fd) != 0) goto fail;
Branch: 1 branch
BranchTrueFalseTotal
if (fsync(fd) != 0) goto fail; 0 27 27
365 #endif
366 27 close(fd);
367  
368 27 ▶ if (rename(tmp, path) != 0) { unlink(tmp); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rename(tmp, path) != 0) { unlink(tmp); return 0; } 0 27 27
369  
370 #ifndef _WIN32
371 { /* fsync the directory, or the rename can be lost on a crash */
372 char dir[4096];
373 char *slash;
374 int dfd;
375 27 strncpy(dir, path, sizeof(dir) - 1);
376 27 dir[sizeof(dir) - 1] = '\0';
377 27 slash = strrchr(dir, '/');
378 27 ▶ if (slash) { *slash = '\0'; } else { strcpy(dir, "."); }
Branch: 1 branch
BranchTrueFalseTotal
if (slash) { *slash = '\0'; } else { strcpy(dir, "."); } 27 0 27
379 27 dfd = open(dir, O_RDONLY);
380 27 ▶ if (dfd >= 0) { fsync(dfd); close(dfd); }
Branch: 1 branch
BranchTrueFalseTotal
if (dfd >= 0) { fsync(dfd); close(dfd); } 27 0 27
381 }
382 #endif
383 27 return 1;
384  
385 0 fail:
386 0 close(fd);
387 0 unlink(tmp);
388 0 return 0;
389 }
390  
391 /* ---- reading ------------------------------------------------------------- */
392  
393 typedef struct {
394 const char *base;
395 size_t len;
396 int fd;
397 int mapped;
398 po_seg_hdr hdr;
399 const po_rec *rec;
400 size_t n;
401 const char *arena;
402 size_t arena_len;
403 po_intern_view sym;
404 po_region rgn[PO_SEG_MAX_REGIONS];
405 int nrgn;
406 } po_seg_r;
407  
408 /* Find a region by kind. Returns NULL when this segment does not carry one,
409 * which is the normal answer for a signal it does not hold. */
410 1866 static const po_region *po_seg_region(const po_seg_r *s, uint32_t kind) {
411 int i;
412 4349 ▶ for (i = 0; i < s->nrgn; i++) if (s->rgn[i].kind == kind) return &s->rgn[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->nrgn; i++) if (s->rgn[i].kind == kind) return &s->rgn[i]; 1860 2483 4343
for (i = 0; i < s->nrgn; i++) if (s->rgn[i].kind == kind) return &s->rgn[i]; 4343 6 4349
413 6 return NULL;
414 }
415  
416 1866 static const char *po_seg_region_ptr(const po_seg_r *s, uint32_t kind,
417 size_t *len) {
418 1866 const po_region *r = po_seg_region(s, kind);
419 1866 ▶ if (!r) { if (len) *len = 0; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r) { if (len) *len = 0; return NULL; } 6 1860 1866
if (!r) { if (len) *len = 0; return NULL; } 6 0 6
420 1860 ▶ if (len) *len = (size_t)r->len;
Branch: 1 branch
BranchTrueFalseTotal
if (len) *len = (size_t)r->len; 1860 0 1860
421 1860 return s->base + r->off;
422 }
423  
424 /* Validate an in-memory image. Separated from the mmap so a test can feed it
425 * a deliberately damaged buffer without touching a filesystem. */
426 2108 static int po_seg_parse(po_seg_r *s, const char *p, size_t len) {
427 uint32_t magic, tail, crc, want;
428 po_u64 off_rec, len_rec, off_ar, len_ar, off_sym, len_sym, off_tab;
429 uint32_t n_tab;
430 const char *f;
431  
432 2108 memset(&s->hdr, 0, sizeof(s->hdr));
433 2108 ▶ if (len < PO_SEG_HDR + PO_SEG_FOOT) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < PO_SEG_HDR + PO_SEG_FOOT) return 0; 320 1788 2108
434  
435 1788 memcpy(&magic, p, 4);
436 1788 ▶ if (po_le32(magic) != PO_SEG_MAGIC) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (po_le32(magic) != PO_SEG_MAGIC) return 0; 0 1788 1788
437  
438 /* The trailing magic FIRST. This is the cheap question - is the file
439 * whole - and everything else is only worth asking once it is yes. */
440 1788 memcpy(&tail, p + len - 4, 4);
441 1788 ▶ if (po_le32(tail) != PO_SEG_MAGIC) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (po_le32(tail) != PO_SEG_MAGIC) return 0; 1124 664 1788
442  
443 664 f = p + len - PO_SEG_FOOT;
444 664 memcpy(&magic, f, 4);
445 664 ▶ if (po_le32(magic) != PO_FOOT_MAGIC) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (po_le32(magic) != PO_FOOT_MAGIC) return 0; 0 664 664
446  
447 664 memcpy(&off_rec, f + 4, 8); off_rec = po_le64(off_rec);
448 664 memcpy(&len_rec, f + 12, 8); len_rec = po_le64(len_rec);
449 664 memcpy(&off_ar, f + 20, 8); off_ar = po_le64(off_ar);
450 664 memcpy(&len_ar, f + 28, 8); len_ar = po_le64(len_ar);
451 664 memcpy(&off_sym, f + 36, 8); off_sym = po_le64(off_sym);
452 664 memcpy(&len_sym, f + 44, 8); len_sym = po_le64(len_sym);
453 664 memcpy(&crc, f + 52, 4); crc = po_le32(crc);
454 664 memcpy(&off_tab, f + 56, 8); off_tab = po_le64(off_tab);
455 664 memcpy(&n_tab, f + 64, 4); n_tab = po_le32(n_tab);
456  
457 /* Every region must lie inside the file. A footer that points outside it
458 * is a corrupt footer, not a large segment.
459 *
460 * WRITTEN AS SUBTRACTION, DELIBERATELY. `off + len > file_len` OVERFLOWS
461 * in a uint64_t: a corrupt footer claiming an offset near 2^64 wraps the
462 * sum to something small, the check passes, and the read runs off the end
463 * of the mapping - which is a SIGBUS, not an error return. That is the
464 * exact failure this format exists to make impossible, and the first cut
465 * of the region table reintroduced it. */
466 #define PO_IN_FILE(off, l) ((off) <= len && (l) <= len - (off))
467 664 ▶ if (!PO_IN_FILE(off_rec, len_rec) || !PO_IN_FILE(off_ar, len_ar)
Branch: 4 branches
BranchTrueFalseTotal
if (!PO_IN_FILE(off_rec, len_rec) || !PO_IN_FILE(off_ar, len_ar) 663 1 664
if (!PO_IN_FILE(off_rec, len_rec) || !PO_IN_FILE(off_ar, len_ar) 662 1 663
if (!PO_IN_FILE(off_rec, len_rec) || !PO_IN_FILE(off_ar, len_ar) 661 1 662
if (!PO_IN_FILE(off_rec, len_rec) || !PO_IN_FILE(off_ar, len_ar) 660 1 661
468 664 ▶ || !PO_IN_FILE(off_sym, len_sym)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
|| !PO_IN_FILE(off_sym, len_sym)) return 0; 659 1 660
|| !PO_IN_FILE(off_sym, len_sym)) return 0; 2 657 659
469 657 ▶ if (off_rec != PO_SEG_HDR) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (off_rec != PO_SEG_HDR) return 0; 0 657 657
470 657 ▶ if (len_rec % sizeof(po_rec)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len_rec % sizeof(po_rec)) return 0; 0 657 657
471  
472 657 ▶ if (n_tab > PO_SEG_MAX_REGIONS) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n_tab > PO_SEG_MAX_REGIONS) return 0; 0 657 657
473 657 ▶ if (n_tab && !PO_IN_FILE(off_tab, (po_u64)n_tab * 20)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (n_tab && !PO_IN_FILE(off_tab, (po_u64)n_tab * 20)) return 0; 624 33 657
if (n_tab && !PO_IN_FILE(off_tab, (po_u64)n_tab * 20)) return 0; 623 1 624
if (n_tab && !PO_IN_FILE(off_tab, (po_u64)n_tab * 20)) return 0; 0 623 623
474  
475 656 want = po_crc32c(0, p, PO_SEG_HDR);
476 656 ▶ if (len_rec) want = po_crc32c(want, p + off_rec, (size_t)len_rec);
Branch: 1 branch
BranchTrueFalseTotal
if (len_rec) want = po_crc32c(want, p + off_rec, (size_t)len_rec); 656 0 656
477 656 ▶ if (len_ar) want = po_crc32c(want, p + off_ar, (size_t)len_ar);
Branch: 1 branch
BranchTrueFalseTotal
if (len_ar) want = po_crc32c(want, p + off_ar, (size_t)len_ar); 656 0 656
478 656 ▶ if (len_sym) want = po_crc32c(want, p + off_sym, (size_t)len_sym);
Branch: 1 branch
BranchTrueFalseTotal
if (len_sym) want = po_crc32c(want, p + off_sym, (size_t)len_sym); 656 0 656
479 {
480 uint32_t i;
481 3146 ▶ for (i = 0; i < n_tab; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_tab; i++) { 2490 656 3146
482 po_u64 o, l;
483 2490 memcpy(&o, p + off_tab + i * 20 + 4, 8); o = po_le64(o);
484 2490 memcpy(&l, p + off_tab + i * 20 + 12, 8); l = po_le64(l);
485 /* Same subtraction, same reason. */
486 2490 ▶ if (!PO_IN_FILE(o, l)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!PO_IN_FILE(o, l)) return 0; 2490 0 2490
if (!PO_IN_FILE(o, l)) return 0; 0 2490 2490
487 2490 ▶ if (l) want = po_crc32c(want, p + o, (size_t)l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) want = po_crc32c(want, p + o, (size_t)l); 2490 0 2490
488 }
489 656 ▶ if (n_tab) want = po_crc32c(want, p + off_tab, (size_t)n_tab * 20);
Branch: 1 branch
BranchTrueFalseTotal
if (n_tab) want = po_crc32c(want, p + off_tab, (size_t)n_tab * 20); 623 33 656
490 }
491 656 ▶ if (want != crc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (want != crc) return 0; 2 654 656
492  
493 {
494 uint16_t v;
495 654 memcpy(&s->hdr.magic, p, 4); s->hdr.magic = po_le32(s->hdr.magic);
496 654 v = (uint16_t)((unsigned char)p[4] | ((unsigned char)p[5] << 8));
497 654 s->hdr.version = v;
498 654 s->hdr.signal = (uint8_t)p[6];
499 654 s->hdr.flags = (uint8_t)p[7];
500 654 memcpy(&s->hdr.tenant_hash, p + 8, 4);
501 654 s->hdr.tenant_hash = po_le32(s->hdr.tenant_hash);
502 654 memcpy(&s->hdr.worker_slot, p + 12, 4);
503 654 s->hdr.worker_slot = po_le32(s->hdr.worker_slot);
504 654 memcpy(&s->hdr.t_min, p + 16, 8); s->hdr.t_min = po_le64(s->hdr.t_min);
505 654 memcpy(&s->hdr.t_max, p + 24, 8); s->hdr.t_max = po_le64(s->hdr.t_max);
506 654 memcpy(&s->hdr.records, p + 32, 8); s->hdr.records = po_le64(s->hdr.records);
507 654 memcpy(s->hdr.ulid, p + 40, 16);
508 }
509  
510 654 s->base = p;
511 654 s->len = len;
512 654 s->rec = (const po_rec *)(p + off_rec);
513 654 s->n = (size_t)(len_rec / sizeof(po_rec));
514 654 s->arena = p + off_ar;
515 654 s->arena_len = (size_t)len_ar;
516 654 ▶ if (!po_intern_open(&s->sym, p + off_sym, (size_t)len_sym)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_intern_open(&s->sym, p + off_sym, (size_t)len_sym)) return 0; 0 654 654
517 654 ▶ if (s->hdr.records != (po_u64)s->n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s->hdr.records != (po_u64)s->n) return 0; 0 654 654
518  
519 654 s->nrgn = (int)n_tab;
520 {
521 uint32_t i;
522 3140 ▶ for (i = 0; i < n_tab; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_tab; i++) { 2486 654 3140
523 uint32_t k; po_u64 o, l;
524 2486 memcpy(&k, p + off_tab + i * 20, 4); s->rgn[i].kind = po_le32(k);
525 2486 memcpy(&o, p + off_tab + i * 20 + 4, 8); s->rgn[i].off = po_le64(o);
526 2486 memcpy(&l, p + off_tab + i * 20 + 12, 8); s->rgn[i].len = po_le64(l);
527 }
528 }
529 654 return 1;
530 #undef PO_IN_FILE
531 }
532  
533 /* mmap a segment read-only.
534 *
535 * PROT_READ, MAP_SHARED: safe across fork, and page-cache backed so N workers
536 * mapping the same segment cost one copy between them. */
537 653 static int po_seg_open(po_seg_r *s, const char *path) {
538 #ifndef _WIN32
539 struct stat st;
540 void *m;
541 653 int fd = open(path, O_RDONLY);
542 653 memset(s, 0, sizeof(*s));
543 653 s->fd = -1;
544 653 ▶ if (fd < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return 0; 3 650 653
545 650 ▶ if (fstat(fd, &st) != 0 || st.st_size <= 0) { close(fd); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (fstat(fd, &st) != 0 || st.st_size <= 0) { close(fd); return 0; } 650 0 650
if (fstat(fd, &st) != 0 || st.st_size <= 0) { close(fd); return 0; } 0 650 650
546 650 m = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0);
547 650 ▶ if (m == MAP_FAILED) { close(fd); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (m == MAP_FAILED) { close(fd); return 0; } 0 650 650
548 650 ▶ if (!po_seg_parse(s, (const char *)m, (size_t)st.st_size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_seg_parse(s, (const char *)m, (size_t)st.st_size)) { 0 650 650
549 0 munmap(m, (size_t)st.st_size);
550 0 close(fd);
551 0 return 0;
552 }
553 650 s->fd = fd;
554 650 s->mapped = 1;
555 650 return 1;
556 #else
557 (void)s; (void)path;
558 return 0;
559 #endif
560 }
561  
562 650 static void po_seg_close(po_seg_r *s) {
563 #ifndef _WIN32
564 650 ▶ if (s->mapped && s->base) munmap((void *)s->base, s->len);
Branch: 2 branches
BranchTrueFalseTotal
if (s->mapped && s->base) munmap((void *)s->base, s->len); 650 0 650
if (s->mapped && s->base) munmap((void *)s->base, s->len); 650 0 650
565 #endif
566 650 ▶ if (s->fd >= 0) close(s->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) close(s->fd); 650 0 650
567 650 s->base = NULL; s->fd = -1; s->mapped = 0;
568 650 }
569  
570 /* Does this segment's time span overlap the query's? THIS is the pruning that
571 * makes everything else affordable: list the directory, read the footer, open
572 * the two that overlap instead of a week's worth. */
573 625 static int po_seg_overlaps(const po_seg_r *s, po_u64 from, po_u64 to) {
574 625 ▶ return s->hdr.t_min <= to && s->hdr.t_max >= from;
Branch: 2 branches
BranchTrueFalseTotal
return s->hdr.t_min <= to && s->hdr.t_max >= from; 624 1 625
return s->hdr.t_min <= to && s->hdr.t_max >= from; 622 2 624
575 }
576  
577 #endif /* PO_SEG_H */
« include/punk_observe/po_pb_read.h ↑ Summary xs/live.xs »