include/sa/sa_time.h

filter: Statementstmt 73.3% 11 / 15 Branchbran 33.3% 2 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 61.9% 13 / 21 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/xop.xs ↑ Summary xs/lease.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 * ---- CLOCK_MONOTONIC where the build found one, gettimeofday where not ------
20 *
21 * clock_gettime lives in librt on glibc before 2.17, which means a probe, a
22 * link flag, and a build that fails on the one machine that has neither. So
23 * Makefile.PL probes for CLOCK_MONOTONIC by RUNNING it, bare and then with
24 * -lrt, and defines SA_HAVE_MONOTONIC_CLOCK when it answers. gettimeofday is in
25 * libc everywhere this builds and stays as the fallback.
26 *
27 * The fallback is not monotonic: a clock stepped backwards during a wait makes
28 * that wait longer. Every caller therefore keeps a generous iteration backstop
29 * as well, so the worst a stepped clock can do is turn a bounded wait into a
30 * differently bounded one. It cannot turn it into a hang, which is the
31 * property rule 5 is actually about.
32 *
33 * It matters for more than a wait. The rate limiter STORES this clock's reading
34 * in the arena beside the tokens, so a step in the clock is a step in every
35 * bucket's clock at once. A Linux smoker's wall clock went back about 0.6s
36 * between a bucket being emptied and being read, and the bucket read as
37 * refilling nothing for 0.6s - correctly, by its own arithmetic, since no time
38 * had passed. A monotonic clock cannot be stepped. It is one clock for every
39 * process on the machine, and an arena is a shared-memory segment that does
40 * not outlive a boot, so a reading one process stores is on the clock the next
41 * one reads.
42 */
43  
44 #ifdef _WIN32
45 # include <windows.h>
46 #else
47 # include <sys/time.h>
48 # include <time.h>
49 #endif
50  
51 #include <stdint.h>
52  
53 /* Microseconds from some fixed point - boot, where the clock is monotonic.
54 * Only differences are ever used. */
55 9891 static uint64_t sa_now_us(void) {
56 #ifdef _WIN32
57 return (uint64_t)GetTickCount64() * 1000u;
58 #else
59 struct timeval tv;
60 # ifdef SA_HAVE_MONOTONIC_CLOCK
61 struct timespec ts;
62 9891 ▶ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) 9891 0 9891
63 9891 return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)(ts.tv_nsec / 1000);
64 # endif
65 0 ▶ if (gettimeofday(&tv, NULL) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gettimeofday(&tv, NULL) != 0) return 0; 0 0 0
66 0 return (uint64_t)tv.tv_sec * 1000000u + (uint64_t)tv.tv_usec;
67 #endif
68 }
69  
70 /* Milliseconds since the epoch. A WALL clock, deliberately, and a different
71 * one from sa_now_us above: a deadline is a moment, set by one process, read by
72 * another and shown to an operator, and the epoch is the one origin every
73 * reader of it agrees on. sa_now_us is a tick for measuring intervals; this is
74 * a timestamp for comparing deadlines. A step in it moves every deadline
75 * together, which costs at worst one round of early or late expiry - the cache
76 * and the map's TTL both accept that, and their tests measure their sleeps on
77 * this same clock because of it. */
78 1037473 static uint64_t sa_now_ms(void) {
79 #ifdef _WIN32
80 FILETIME ft;
81 ULARGE_INTEGER u;
82 GetSystemTimeAsFileTime(&ft);
83 u.LowPart = ft.dwLowDateTime;
84 u.HighPart = ft.dwHighDateTime;
85 /* 100ns units since 1601; the offset is dropped because only differences
86 * and comparisons against each other matter here. */
87 return (uint64_t)(u.QuadPart / 10000ULL);
88 #else
89 struct timeval tv;
90 # ifdef SA_HAVE_COARSE_CLOCK
91 /* A millisecond deadline needs a millisecond clock, and on Linux the
92 * coarse one is the kernel's last tick read out of the vDSO with no
93 * counter access: about a quarter of gettimeofday's cost, which on a map
94 * hit that carries a deadline was most of the hit. Its granularity is a
95 * tick, up to four milliseconds, which is one round of early or late
96 * expiry - the tolerance every deadline here already carries. Makefile.PL
97 * probes for it by running it; the fallback is the clock below. */
98 struct timespec ts;
99 1037473 ▶ if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0) 1037473 0 1037473
100 1037473 return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000);
101 # endif
102 0 gettimeofday(&tv, NULL);
103 0 return (uint64_t)tv.tv_sec * 1000ULL + (uint64_t)(tv.tv_usec / 1000);
104 #endif
105 }
106  
107 /* Sleep, in microseconds. A real sleep and not a spin: a process waiting on
108 * somebody else's commit has nothing to do, and burning a core to find that out
109 * helps nobody. */
110 1779 static void sa_stall(uint32_t us) {
111 #ifdef _WIN32
112 Sleep((DWORD)((us + 999) / 1000));
113 #else
114 struct timespec ts;
115 1779 ts.tv_sec = (time_t)(us / 1000000u);
116 1779 ts.tv_nsec = (long)((us % 1000000u) * 1000u);
117 1779 nanosleep(&ts, NULL);
118 #endif
119 1779 }
120  
121 /* How long a process waits for another one to finish publishing a header it has
122 * already begun. Long enough that an ordinary preemption is invisible, short
123 * enough that a creator which died mid-header does not hold anybody up. */
124 #define SA_CREATE_WAIT_US 2000000 /* two seconds */
125 #define SA_CREATE_POLL_US 200 /* between looks */
126  
127 #endif /* SA_TIME_H */
« xs/xop.xs ↑ Summary xs/lease.xs »