include/punk_observe/po_time.h

filter: Statementstmt 83.3% 20 / 24 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 24 / 30 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_row.h ↑ Summary include/punk_observe/po_retain.h »
1 /* po_time.h - clocks, and the seam that keeps later phases testable.
2 *
3 * Two clocks, and they are not interchangeable.
4 *
5 * The WALL clock produces a record's timestamp. It is what OTLP carries and
6 * what a chart's x axis means. It also steps: NTP corrects it, an operator
7 * sets it, a VM resumes with a stale one. So it is read for a timestamp and
8 * never for an interval.
9 *
10 * The MONOTONIC clock produces every interval this distribution measures -
11 * segment rotation, the head-flush timer, the fsync coalescer, a query's
12 * deadline. It cannot step. Using the wall clock for one of these is how a
13 * block ends up with min_ts > max_ts, which is a block no query will find.
14 *
15 * And one seam: po_now_ns is a FUNCTION POINTER. Every timer in this dist
16 * reads the clock through it, so a test moves time instead of sleeping.
17 * A fixed sleep before asserting that a timer fired FAILS on a smoker running
18 * forty parallel builds, which is how this ecosystem has lost releases before.
19 * The seam is here, in phase 0, so that no later phase has to invent one.
20 */
21 #ifndef PO_TIME_H
22 #define PO_TIME_H
23  
24 #include "punk_observe/po_compat.h"
25  
26 #include <time.h>
27 #ifndef _WIN32
28 # include <sys/time.h>
29 #endif
30 #ifdef _WIN32
31 # include <windows.h>
32 #endif
33  
34 #define PO_NS_PER_SEC ((po_u64)1000000000)
35 #define PO_NS_PER_MSEC ((po_u64)1000000)
36  
37 /* Wall clock, unix nanoseconds. */
38 120 static po_u64 po_wall_ns_real(void) {
39 #if defined(PO_HAVE_CLOCK_GETTIME)
40 struct timespec ts;
41 120 ▶ if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_REALTIME, &ts) == 0) 120 0 120
42 120 return (po_u64)ts.tv_sec * PO_NS_PER_SEC + (po_u64)ts.tv_nsec;
43 0 return 0;
44 #elif defined(_WIN32)
45 /* FILETIME is 100ns ticks since 1601-01-01; 11644473600 seconds to epoch. */
46 FILETIME ft; ULARGE_INTEGER u;
47 GetSystemTimeAsFileTime(&ft);
48 u.LowPart = ft.dwLowDateTime; u.HighPart = ft.dwHighDateTime;
49 return ((po_u64)u.QuadPart - (po_u64)116444736000000000ULL) * 100;
50 #else
51 struct timeval tv;
52 if (gettimeofday(&tv, NULL) == 0)
53 return (po_u64)tv.tv_sec * PO_NS_PER_SEC + (po_u64)tv.tv_usec * 1000;
54 return 0;
55 #endif
56 }
57  
58 /* Monotonic nanoseconds. The zero point is arbitrary and means nothing; only
59 * differences are defined. Where the platform has no monotonic clock this
60 * falls back to the wall clock, which is wrong in exactly the way described
61 * above - so po_have_monotonic() lets a caller say so rather than pretend. */
62 499 static po_u64 po_mono_ns(void) {
63 #if defined(PO_HAVE_CLOCK_MONOTONIC)
64 struct timespec ts;
65 499 ▶ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) 499 0 499
66 499 return (po_u64)ts.tv_sec * PO_NS_PER_SEC + (po_u64)ts.tv_nsec;
67 0 return 0;
68 #elif defined(_WIN32)
69 LARGE_INTEGER f, c;
70 if (QueryPerformanceFrequency(&f) && QueryPerformanceCounter(&c) && f.QuadPart)
71 return (po_u64)((double)c.QuadPart / (double)f.QuadPart * 1e9);
72 return po_wall_ns_real();
73 #else
74 return po_wall_ns_real();
75 #endif
76 }
77  
78 0 static int po_have_monotonic(void) {
79 #if defined(PO_HAVE_CLOCK_MONOTONIC) || defined(_WIN32)
80 0 return 1;
81 #else
82 return 0;
83 #endif
84 }
85  
86 /* The seam. Tests replace these; production never touches them. */
87 typedef po_u64 (*po_clock_fn)(void);
88  
89 static po_clock_fn po_now_ns = po_wall_ns_real; /* timestamps */
90 static po_clock_fn po_tick_ns = po_mono_ns; /* intervals */
91  
92 /* A test clock: set it, step it, and every timer in the dist moves with it. */
93 static po_u64 po_fake_now = 0;
94 525 static po_u64 po_fake_clock(void) { return po_fake_now; }
95  
96 218 static void po_clock_freeze(po_u64 at) {
97 218 po_fake_now = at;
98 218 po_now_ns = po_fake_clock;
99 218 po_tick_ns = po_fake_clock;
100 218 }
101 1 static void po_clock_step(po_u64 by) { po_fake_now += by; }
102 69 static void po_clock_real(void) {
103 69 po_now_ns = po_wall_ns_real;
104 69 po_tick_ns = po_mono_ns;
105 69 }
106  
107 /* Block alignment. Blocks are two hours, epoch-aligned, so that a block's
108 * identity is a pure function of a timestamp and two workers never disagree
109 * about which block a record belongs to. */
110 #define PO_BLOCK_NS ((po_u64)2 * 3600 * PO_NS_PER_SEC)
111  
112 4 static po_u64 po_block_start(po_u64 t_ns) { return t_ns - (t_ns % PO_BLOCK_NS); }
113  
114 /* An interval, clamped. A wall clock that stepped backwards mid-span gives
115 * end < start; in a uint64_t that subtraction is not a small negative number,
116 * it is about 1.8e19. Clamping to zero and counting it is the only safe
117 * reading, and phase 7 counts them. */
118 27 static po_u64 po_duration(po_u64 start_ns, po_u64 end_ns) {
119 27 ▶ return end_ns >= start_ns ? end_ns - start_ns : 0;
Branch: 1 branch
BranchTrueFalseTotal
return end_ns >= start_ns ? end_ns - start_ns : 0; 26 1 27
120 }
121  
122 #endif /* PO_TIME_H */
« include/punk_observe/po_row.h ↑ Summary include/punk_observe/po_retain.h »