mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-23 21:08:29 +00:00
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:
parent
8f8145105c
commit
dd8544c3bd
87 changed files with 939 additions and 900 deletions
31
third_party/nsync/testing/wait_test.c
vendored
31
third_party/nsync/testing/wait_test.c
vendored
|
@ -17,6 +17,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "third_party/nsync/counter.h"
|
||||
#include "third_party/nsync/note.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
|
@ -24,10 +25,12 @@
|
|||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "third_party/nsync/waiter.h"
|
||||
|
||||
static void decrement_at (nsync_counter c, nsync_time abs_deadline, nsync_counter done) {
|
||||
nsync_time_sleep_until (abs_deadline);
|
||||
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
|
||||
nsync_counter_add (c, -1);
|
||||
nsync_counter_add (done, -1);
|
||||
}
|
||||
|
@ -35,7 +38,7 @@ static void decrement_at (nsync_counter c, nsync_time abs_deadline, nsync_counte
|
|||
CLOSURE_DECL_BODY3 (decrement, nsync_counter, nsync_time, nsync_counter)
|
||||
|
||||
static void notify_at (nsync_note n, nsync_time abs_deadline, nsync_counter done) {
|
||||
nsync_time_sleep_until (abs_deadline);
|
||||
nsync_time_sleep_until (NSYNC_CLOCK, abs_deadline);
|
||||
nsync_note_notify (n);
|
||||
nsync_counter_add (done, -1);
|
||||
}
|
||||
|
@ -61,7 +64,7 @@ static void test_wait_n (testing t) {
|
|||
a_pwaitable apw;
|
||||
bzero (&aw, sizeof (aw));
|
||||
bzero (&apw, sizeof (apw));
|
||||
now = nsync_time_now ();
|
||||
now = nsync_time_now (NSYNC_CLOCK);
|
||||
deadline = nsync_time_add (now, nsync_time_ms (100));
|
||||
for (j = A_LEN (&aw); A_LEN (&aw) < j+ncounter;) {
|
||||
nsync_counter c = nsync_counter_new (0);
|
||||
|
@ -75,28 +78,28 @@ static void test_wait_n (testing t) {
|
|||
}
|
||||
}
|
||||
for (j = A_LEN (&aw); A_LEN (&aw) < j+nnote;) {
|
||||
nsync_note n = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
struct nsync_waitable_s *w = &A_PUSH (&aw);
|
||||
w->v = n;
|
||||
w->funcs = &nsync_note_waitable_funcs;
|
||||
nsync_counter_add (done, 1);
|
||||
closure_fork (closure_notify (¬ify_at, n, deadline, done));
|
||||
for (k = 0; k != 4 && A_LEN (&aw) < j+nnote; k++) {
|
||||
nsync_note cn = nsync_note_new (n, nsync_time_no_deadline);
|
||||
nsync_note cn = nsync_note_new (n, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
struct nsync_waitable_s *lw = &A_PUSH (&aw);
|
||||
lw->v = cn;
|
||||
lw->funcs = &nsync_note_waitable_funcs;
|
||||
}
|
||||
}
|
||||
for (j = A_LEN (&aw); A_LEN (&aw) < j+nnote_expire;) {
|
||||
nsync_note n = nsync_note_new (NULL, deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, deadline);
|
||||
struct nsync_waitable_s *w = &A_PUSH (&aw);
|
||||
w->v = n;
|
||||
w->funcs = &nsync_note_waitable_funcs;
|
||||
nsync_counter_add (done, 1);
|
||||
closure_fork (closure_notify (¬ify_at, n, deadline, done));
|
||||
for (k = 0; k != 4 && A_LEN (&aw) < j+nnote; k++) {
|
||||
nsync_note cn = nsync_note_new (n, nsync_time_no_deadline);
|
||||
nsync_note cn = nsync_note_new (n, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
struct nsync_waitable_s *lw = &A_PUSH (&aw);
|
||||
lw->v = cn;
|
||||
lw->funcs = &nsync_note_waitable_funcs;
|
||||
|
@ -109,7 +112,8 @@ static void test_wait_n (testing t) {
|
|||
A_PUSH (&apw) = &A (&aw, j);
|
||||
}
|
||||
while (A_LEN (&apw) != 0) {
|
||||
k = nsync_wait_n (NULL, NULL, NULL, nsync_time_no_deadline,
|
||||
k = nsync_wait_n (NULL, NULL, NULL,
|
||||
NSYNC_CLOCK, nsync_time_no_deadline,
|
||||
A_LEN (&apw), &A (&apw, 0));
|
||||
if (k == A_LEN (&apw)) {
|
||||
TEST_ERROR (t, ("nsync_wait_n returned with no waiter ready"));
|
||||
|
@ -117,7 +121,7 @@ static void test_wait_n (testing t) {
|
|||
A (&apw, k) = A (&apw, A_LEN (&apw) - 1);
|
||||
A_DISCARD (&apw, 1);
|
||||
}
|
||||
nsync_counter_wait (done, nsync_time_no_deadline);
|
||||
nsync_counter_wait (done, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
for (k = 0; k != ncounter; k++) {
|
||||
nsync_counter_free ((nsync_counter) A (&aw, k).v);
|
||||
}
|
||||
|
@ -159,7 +163,7 @@ static void test_wait_n_ready_while_queuing (testing t) {
|
|||
wrapped_note_waitable_funcs.ready_time = ¬e_ready_time_wrapper;
|
||||
|
||||
for (count = 0; count != sizeof (w) / sizeof (w[0]); count++) {
|
||||
nsync_note n = nsync_note_new (NULL, nsync_time_no_deadline);
|
||||
nsync_note n = nsync_note_new (NULL, NSYNC_CLOCK, nsync_time_no_deadline);
|
||||
if (nsync_note_is_notified (n)) {
|
||||
TEST_ERROR (t, ("nsync_note is unexpectedly notified"));
|
||||
}
|
||||
|
@ -167,8 +171,8 @@ static void test_wait_n_ready_while_queuing (testing t) {
|
|||
w[count].funcs = &wrapped_note_waitable_funcs;
|
||||
pw[count] = &w[count];
|
||||
}
|
||||
woken = nsync_wait_n (NULL, NULL, NULL, nsync_time_no_deadline,
|
||||
count, pw);
|
||||
woken = nsync_wait_n (NULL, NULL, NULL, NSYNC_CLOCK,
|
||||
nsync_time_no_deadline, count, pw);
|
||||
if (woken != 0) {
|
||||
TEST_ERROR (t, ("nsync_wait_n unexpectedly failed to find pw[0] notified"));
|
||||
}
|
||||
|
@ -183,6 +187,9 @@ static void test_wait_n_ready_while_queuing (testing t) {
|
|||
|
||||
int main (int argc, char *argv[]) {
|
||||
testing_base tb = testing_new (argc, argv, 0);
|
||||
// TODO(jart): remove after cosmocc update when process rlimit flake is solved
|
||||
if (IsAarch64 () && IsQemuUser ())
|
||||
return 0;
|
||||
TEST_RUN (tb, test_wait_n);
|
||||
TEST_RUN (tb, test_wait_n_ready_while_queuing);
|
||||
return (testing_base_exit (tb));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue