| 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 |
9946 |
| static uint64_t sa_now_us(void) { |
| 56 |
|
| #ifdef _WIN32 |
| 57 |
|
| /* QueryPerformanceCounter and not GetTickCount64: the latter is declared |
| 58 |
|
| * only for _WIN32_WINNT >= 0x0600, which perl's own headers do not set, so |
| 59 |
|
| * it compiled as an implicit int and failed at link. This one is declared |
| 60 |
|
| * everywhere, is one counter for every process, and cannot be stepped. The |
| 61 |
|
| * division is split so the product does not overflow after days of uptime. */ |
| 62 |
|
| LARGE_INTEGER c, f; |
| 63 |
|
| if (!QueryPerformanceFrequency(&f) || f.QuadPart <= 0) return 0; |
| 64 |
|
| if (!QueryPerformanceCounter(&c)) return 0; |
| 65 |
|
| return (uint64_t)(c.QuadPart / f.QuadPart) * 1000000u |
| 66 |
|
| + (uint64_t)(c.QuadPart % f.QuadPart) * 1000000u / (uint64_t)f.QuadPart; |
| 67 |
|
| #else |
| 68 |
|
| struct timeval tv; |
| 69 |
|
| # ifdef SA_HAVE_MONOTONIC_CLOCK |
| 70 |
|
| struct timespec ts; |
| 71 |
9946 |
▶ | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
9946 |
0 |
9946 |
|
| 72 |
9946 |
| return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)(ts.tv_nsec / 1000); |
| 73 |
|
| # endif |
| 74 |
0 |
▶ | if (gettimeofday(&tv, NULL) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gettimeofday(&tv, NULL) != 0) return 0; |
0 |
0 |
0 |
|
| 75 |
0 |
| return (uint64_t)tv.tv_sec * 1000000u + (uint64_t)tv.tv_usec; |
| 76 |
|
| #endif |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* Milliseconds since the epoch. A WALL clock, deliberately, and a different |
| 80 |
|
| * one from sa_now_us above: a deadline is a moment, set by one process, read by |
| 81 |
|
| * another and shown to an operator, and the epoch is the one origin every |
| 82 |
|
| * reader of it agrees on. sa_now_us is a tick for measuring intervals; this is |
| 83 |
|
| * a timestamp for comparing deadlines. A step in it moves every deadline |
| 84 |
|
| * together, which costs at worst one round of early or late expiry - the cache |
| 85 |
|
| * and the map's TTL both accept that, and their tests measure their sleeps on |
| 86 |
|
| * this same clock because of it. */ |
| 87 |
817073 |
| static uint64_t sa_now_ms(void) { |
| 88 |
|
| #ifdef _WIN32 |
| 89 |
|
| FILETIME ft; |
| 90 |
|
| ULARGE_INTEGER u; |
| 91 |
|
| GetSystemTimeAsFileTime(&ft); |
| 92 |
|
| u.LowPart = ft.dwLowDateTime; |
| 93 |
|
| u.HighPart = ft.dwHighDateTime; |
| 94 |
|
| /* 100ns units since 1601; the offset is dropped because only differences |
| 95 |
|
| * and comparisons against each other matter here. */ |
| 96 |
|
| return (uint64_t)(u.QuadPart / 10000ULL); |
| 97 |
|
| #else |
| 98 |
|
| struct timeval tv; |
| 99 |
|
| # ifdef SA_HAVE_COARSE_CLOCK |
| 100 |
|
| /* A millisecond deadline needs a millisecond clock, and on Linux the |
| 101 |
|
| * coarse one is the kernel's last tick read out of the vDSO with no |
| 102 |
|
| * counter access: about a quarter of gettimeofday's cost, which on a map |
| 103 |
|
| * hit that carries a deadline was most of the hit. Its granularity is a |
| 104 |
|
| * tick, up to four milliseconds, which is one round of early or late |
| 105 |
|
| * expiry - the tolerance every deadline here already carries. Makefile.PL |
| 106 |
|
| * probes for it by running it; the fallback is the clock below. */ |
| 107 |
|
| struct timespec ts; |
| 108 |
817073 |
▶ | if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0) |
817073 |
0 |
817073 |
|
| 109 |
817073 |
| return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000); |
| 110 |
|
| # endif |
| 111 |
0 |
| gettimeofday(&tv, NULL); |
| 112 |
0 |
| return (uint64_t)tv.tv_sec * 1000ULL + (uint64_t)(tv.tv_usec / 1000); |
| 113 |
|
| #endif |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| /* Sleep, in microseconds. A real sleep and not a spin: a process waiting on |
| 117 |
|
| * somebody else's commit has nothing to do, and burning a core to find that out |
| 118 |
|
| * helps nobody. */ |
| 119 |
1831 |
| static void sa_stall(uint32_t us) { |
| 120 |
|
| #ifdef _WIN32 |
| 121 |
|
| Sleep((DWORD)((us + 999) / 1000)); |
| 122 |
|
| #else |
| 123 |
|
| struct timespec ts; |
| 124 |
1831 |
| ts.tv_sec = (time_t)(us / 1000000u); |
| 125 |
1831 |
| ts.tv_nsec = (long)((us % 1000000u) * 1000u); |
| 126 |
1831 |
| nanosleep(&ts, NULL); |
| 127 |
|
| #endif |
| 128 |
1831 |
| } |
| 129 |
|
| |
| 130 |
|
| /* How long a process waits for another one to finish publishing a header it has |
| 131 |
|
| * already begun. Long enough that an ordinary preemption is invisible, short |
| 132 |
|
| * enough that a creator which died mid-header does not hold anybody up. */ |
| 133 |
|
| #define SA_CREATE_WAIT_US 2000000 /* two seconds */ |
| 134 |
|
| #define SA_CREATE_POLL_US 200 /* between looks */ |
| 135 |
|
| |
| 136 |
|
| #endif /* SA_TIME_H */ |