include/sa/sa_time.h

filter: Statementstmt 84.6% 11 / 13 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.4% 13 / 17 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/cms.xs ↑ Summary xs/frozen.xs »
1 #ifndef SA_TIME_H
2 #define SA_TIME_H
3  
4 /* sa_time.h - how long to wait, measured in time. Perl-free.
5 *
6 * ---- an iteration count is not a bound anybody wants ------------------------
7 *
8 * Rule 5 of this dist says every wait is bounded. The waits WERE bounded, by a
9 * spin count, and that is the wrong quantity in both directions at once. Two
10 * hundred thousand iterations of a tight loop is under a millisecond on a fast
11 * machine - so a process that was merely descheduled at the wrong moment gets
12 * abandoned - and two hundred thousand iterations of a loop containing an fstat
13 * is two hundred thousand SYSCALLS, which is a core burned to discover that
14 * somebody else has not finished yet.
15 *
16 * A deadline says what was actually meant: wait this long, sleeping rather than
17 * spinning, and give up when the time is gone however many turns that took.
18 *
19 * ---- gettimeofday, and not clock_gettime -----------------------------------
20 *
21 * clock_gettime is the better clock and it lives in librt on glibc before 2.17,
22 * which means a probe, a link flag, and a build that fails on the one machine
23 * that has neither. gettimeofday is in libc everywhere this builds and needs
24 * none of that.
25 *
26 * The price is that it is not monotonic: a clock stepped backwards during a
27 * wait makes that wait longer. Every caller therefore keeps a generous
28 * iteration backstop as well, so the worst a stepped clock can do is turn a
29 * bounded wait into a differently bounded one. It cannot turn it into a hang,
30 * which is the property rule 5 is actually about.
31 */
32  
33 #ifdef _WIN32
34 # include <windows.h>
35 #else
36 # include <sys/time.h>
37 # include <time.h>
38 #endif
39  
40 #include <stdint.h>
41  
42 /* Microseconds from some fixed point. Only differences are ever used. */
43 10253 static uint64_t sa_now_us(void) {
44 #ifdef _WIN32
45 return (uint64_t)GetTickCount64() * 1000u;
46 #else
47 struct timeval tv;
48 10253 ▶ if (gettimeofday(&tv, NULL) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gettimeofday(&tv, NULL) != 0) return 0; 0 10253 10253
49 10253 return (uint64_t)tv.tv_sec * 1000000u + (uint64_t)tv.tv_usec;
50 #endif
51 }
52  
53 /* Milliseconds since the epoch. A WALL clock, deliberately, and a different
54 * one from sa_now_us above: a deadline is set by one process and read by
55 * another, and the only clock two processes certainly agree on is the one the
56 * machine keeps. sa_now_us is a monotonic tick for measuring intervals within
57 * a process; this is a shared timestamp for comparing deadlines across them.
58 * A step in it moves every deadline together, which costs at worst one round of
59 * early or late expiry - the cache and the map's TTL both rely on that. */
60 1113673 static uint64_t sa_now_ms(void) {
61 #ifdef _WIN32
62 FILETIME ft;
63 ULARGE_INTEGER u;
64 GetSystemTimeAsFileTime(&ft);
65 u.LowPart = ft.dwLowDateTime;
66 u.HighPart = ft.dwHighDateTime;
67 /* 100ns units since 1601; the offset is dropped because only differences
68 * and comparisons against each other matter here. */
69 return (uint64_t)(u.QuadPart / 10000ULL);
70 #else
71 struct timeval tv;
72 # ifdef SA_HAVE_COARSE_CLOCK
73 /* A millisecond deadline needs a millisecond clock, and on Linux the
74 * coarse one is the kernel's last tick read out of the vDSO with no
75 * counter access: about a quarter of gettimeofday's cost, which on a map
76 * hit that carries a deadline was most of the hit. Its granularity is a
77 * tick, up to four milliseconds, which is one round of early or late
78 * expiry - the tolerance every deadline here already carries. Makefile.PL
79 * probes for it by running it; the fallback is the clock below. */
80 struct timespec ts;
81 1113673 ▶ if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0) 1113673 0 1113673
82 1113673 return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000);
83 # endif
84 0 gettimeofday(&tv, NULL);
85 0 return (uint64_t)tv.tv_sec * 1000ULL + (uint64_t)(tv.tv_usec / 1000);
86 #endif
87 }
88  
89 /* Sleep, in microseconds. A real sleep and not a spin: a process waiting on
90 * somebody else's commit has nothing to do, and burning a core to find that out
91 * helps nobody. */
92 1780 static void sa_stall(uint32_t us) {
93 #ifdef _WIN32
94 Sleep((DWORD)((us + 999) / 1000));
95 #else
96 struct timespec ts;
97 1780 ts.tv_sec = (time_t)(us / 1000000u);
98 1780 ts.tv_nsec = (long)((us % 1000000u) * 1000u);
99 1780 nanosleep(&ts, NULL);
100 #endif
101 1780 }
102  
103 /* How long a process waits for another one to finish publishing a header it has
104 * already begun. Long enough that an ordinary preemption is invisible, short
105 * enough that a creator which died mid-header does not hold anybody up. */
106 #define SA_CREATE_WAIT_US 2000000 /* two seconds */
107 #define SA_CREATE_POLL_US 200 /* between looks */
108  
109 #endif /* SA_TIME_H */
« xs/cms.xs ↑ Summary xs/frozen.xs »