Delve into clock rabbit hole

The worst issue I had with consts.sh for clock_gettime is how it defined
too many clocks. So I looked into these clocks all day to figure out how
how they overlap in functionality. I discovered counter-intuitive things
such as how CLOCK_MONOTONIC should be CLOCK_UPTIME on MacOS and BSD, and
that CLOCK_BOOTTIME should be CLOCK_MONOTONIC on MacOS / BSD. Windows 10
also has some incredible new APIs, that let us simplify clock_gettime().

  - Linux CLOCK_REALTIME         -> GetSystemTimePreciseAsFileTime()
  - Linux CLOCK_MONOTONIC        -> QueryUnbiasedInterruptTimePrecise()
  - Linux CLOCK_MONOTONIC_RAW    -> QueryUnbiasedInterruptTimePrecise()
  - Linux CLOCK_REALTIME_COARSE  -> GetSystemTimeAsFileTime()
  - Linux CLOCK_MONOTONIC_COARSE -> QueryUnbiasedInterruptTime()
  - Linux CLOCK_BOOTTIME         -> QueryInterruptTimePrecise()

Documentation on the clock crew has been added to clock_gettime() in the
docstring and in redbean's documentation too. You can read that to learn
interesting facts about eight essential clocks that survived this purge.
This is original research you will not find on Google, OpenAI, or Claude

I've tested this change by porting *NSYNC to become fully clock agnostic
since it has extensive tests for spotting irregularities in time. I have
also included these tests in the default build so they no longer need to
be run manually. Both CLOCK_REALTIME and CLOCK_MONOTONIC are good across
the entire amd64 and arm64 test fleets.
This commit is contained in:
Justine Tunney 2024-09-04 00:38:44 -07:00
parent 8f8145105c
commit dd8544c3bd
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
87 changed files with 939 additions and 900 deletions

View file

@ -25,7 +25,7 @@
/* Verify the properties of a prenotified note. */
static void test_note_prenotified (testing t) {
int i;
nsync_note n = nsync_note_new (NULL, nsync_time_zero /* prenotified */);
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_zero /* prenotified */);
nsync_time expiry;
expiry = nsync_note_expiry (n);
if (nsync_time_cmp (expiry, nsync_time_zero) != 0) {
@ -55,7 +55,7 @@ static void test_note_unnotified (testing t) {
nsync_time start;
nsync_time waited;
nsync_time deadline;
nsync_note n = nsync_note_new (NULL, nsync_time_no_deadline);
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
nsync_time expiry;
expiry = nsync_note_expiry (n);
if (nsync_time_cmp (expiry, nsync_time_no_deadline) != 0) {
@ -68,12 +68,12 @@ static void test_note_unnotified (testing t) {
if (nsync_note_wait (n, nsync_time_zero)) {
TEST_ERROR (t, ("notified note is notified (poll)"));
}
start = nsync_time_now ();
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
start = nsync_time_now (NSYNC_CLOCK);
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
if (nsync_note_wait (n, deadline)) {
TEST_ERROR (t, ("unnotified note is notified (1s wait)"));
}
waited = nsync_time_sub (nsync_time_now (), start);
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
TEST_ERROR (t, ("timed wait on unnotified note returned too quickly (1s wait took %s)",
nsync_time_str (waited, 2)));
@ -110,13 +110,13 @@ static void test_note_expiry (testing t) {
nsync_time deadline;
nsync_note n;
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
n = nsync_note_new (NULL, deadline);
start = nsync_time_now ();
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
start = nsync_time_now (NSYNC_CLOCK);
if (!nsync_note_wait (n, nsync_time_no_deadline)) {
TEST_ERROR (t, ("expired note is not notified"));
}
waited = nsync_time_sub (nsync_time_now (), start);
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
nsync_time_str (waited, 2)));
@ -136,13 +136,13 @@ static void test_note_expiry (testing t) {
}
nsync_note_free (n);
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
n = nsync_note_new (NULL, deadline);
start = nsync_time_now ();
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
start = nsync_time_now (NSYNC_CLOCK);
while (!nsync_note_is_notified (n)) {
nsync_time_sleep (nsync_time_ms (10));
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (10));
}
waited = nsync_time_sub (nsync_time_now (), start);
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
nsync_time_str (waited, 2)));
@ -164,7 +164,7 @@ static void test_note_expiry (testing t) {
}
static void notify_at (nsync_note n, nsync_time abs_deadline) {
nsync_time_sleep_until (abs_deadline);
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
nsync_note_notify (n);
}
@ -177,14 +177,14 @@ static void test_note_notify (testing t) {
nsync_time deadline;
nsync_note n;
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (10000));
n = nsync_note_new (NULL, deadline);
closure_fork (closure_notify (&notify_at, n, nsync_time_add (nsync_time_now (), nsync_time_ms (1000))));
start = nsync_time_now ();
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (10000));
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
closure_fork (closure_notify (&notify_at, n, nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000))));
start = nsync_time_now (NSYNC_CLOCK);
if (!nsync_note_wait (n, nsync_time_no_deadline)) {
TEST_ERROR (t, ("expired note is not notified"));
}
waited = nsync_time_sub (nsync_time_now (), start);
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
nsync_time_str (waited, 2)));
@ -204,14 +204,14 @@ static void test_note_notify (testing t) {
}
nsync_note_free (n);
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (10000));
n = nsync_note_new (NULL, deadline);
closure_fork (closure_notify (&notify_at, n, nsync_time_add (nsync_time_now (), nsync_time_ms (1000))));
start = nsync_time_now ();
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (10000));
n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
closure_fork (closure_notify (&notify_at, n, nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000))));
start = nsync_time_now (NSYNC_CLOCK);
while (!nsync_note_is_notified (n)) {
nsync_time_sleep (nsync_time_ms (10));
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (10));
}
waited = nsync_time_sub (nsync_time_now (), start);
waited = nsync_time_sub (nsync_time_now (NSYNC_CLOCK), start);
if (nsync_time_cmp (waited, nsync_time_ms (900)) < 0) {
TEST_ERROR (t, ("note expired too quickly (1s expiry took %s)",
nsync_time_str (waited, 2)));
@ -253,9 +253,9 @@ static void test_note_in_tree (testing t) {
nsync_note node[count_i];
/* Initialize heap structure in the nodes. No deadlines. */
node[0] = nsync_note_new (NULL, nsync_time_no_deadline);
node[0] = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
for (i = 1; i != count_i; i++) {
node[i] = nsync_note_new (node[(i-1)/2], nsync_time_no_deadline);
node[i] = nsync_note_new (node[(i-1)/2], NSYNC_CLOCK, nsync_time_no_deadline);
}
/* check that the nodes are not yet notified. */
@ -285,14 +285,14 @@ static void test_note_in_tree (testing t) {
}
/* Initialize heap structure in the nodes. The focus node has a 1s deadline. */
node[0] = nsync_note_new (NULL, nsync_time_no_deadline);
node[0] = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
for (i = 1; i != count_i; i++) {
nsync_time deadline;
deadline = nsync_time_add (nsync_time_now (), nsync_time_ms (1000));
deadline = nsync_time_add (nsync_time_now (NSYNC_CLOCK), nsync_time_ms (1000));
if (i != focus_i) {
deadline = nsync_time_no_deadline;
}
node[i] = nsync_note_new (node[(i - 1) / 2], deadline);
node[i] = nsync_note_new (node[(i - 1) / 2], NSYNC_CLOCK, deadline);
}
/* check that the nodes are not yet notified. */
@ -303,7 +303,7 @@ static void test_note_in_tree (testing t) {
}
/* Wait for timer to go off. */
nsync_time_sleep (nsync_time_ms (1100));
nsync_time_sleep (NSYNC_CLOCK, nsync_time_ms (1100));
/* Check that the right nodes have been notified. */
for (i = 0; i != count_i; i++) {