From 8f3f3991023702d597728be7e37b1e46b3ad7e46 Mon Sep 17 00:00:00 2001 From: pirofti <> Date: Mon, 6 Jul 2020 13:33:06 +0000 Subject: Add support for timeconting in userland. This diff exposes parts of clock_gettime(2) and gettimeofday(2) to userland via libc eliberating processes from the need for a context switch everytime they want to count the passage of time. If a timecounter clock can be exposed to userland than it needs to set its tc_user member to a non-zero value. Tested with one or multiple counters per architecture. The timing data is shared through a pointer found in the new ELF auxiliary vector AUX_openbsd_timekeep containing timehands information that is frequently updated by the kernel. Timing differences between the last kernel update and the current time are adjusted in userland by the tc_get_timecount() function inside the MD usertc.c file. This permits a much more responsive environment, quite visible in browsers, office programs and gaming (apparently one is are able to fly in Minecraft now). Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others! OK from at least kettenis@, cheloha@, naddy@, sthen@ --- src/lib/libc/crypt/bcrypt.c | 6 +-- src/lib/libc/net/res_random.c | 6 +-- src/regress/lib/libc/timekeep/Makefile | 5 ++ src/regress/lib/libc/timekeep/test_clock_gettime.c | 43 +++++++++++++++++ src/regress/lib/libc/timekeep/test_gettimeofday.c | 37 +++++++++++++++ src/regress/lib/libc/timekeep/test_time_skew.c | 55 ++++++++++++++++++++++ 6 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 src/regress/lib/libc/timekeep/Makefile create mode 100644 src/regress/lib/libc/timekeep/test_clock_gettime.c create mode 100644 src/regress/lib/libc/timekeep/test_gettimeofday.c create mode 100644 src/regress/lib/libc/timekeep/test_time_skew.c diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c index 82de8fa33b..ba45b104ed 100644 --- a/src/lib/libc/crypt/bcrypt.c +++ b/src/lib/libc/crypt/bcrypt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcrypt.c,v 1.57 2016/08/26 08:25:02 guenther Exp $ */ +/* $OpenBSD: bcrypt.c,v 1.58 2020/07/06 13:33:05 pirofti Exp $ */ /* * Copyright (c) 2014 Ted Unangst @@ -248,9 +248,9 @@ _bcrypt_autorounds(void) char buf[_PASSWORD_LEN]; int duration; - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &before); + WRAP(clock_gettime)(CLOCK_THREAD_CPUTIME_ID, &before); bcrypt_newhash("testpassword", r, buf, sizeof(buf)); - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &after); + WRAP(clock_gettime)(CLOCK_THREAD_CPUTIME_ID, &after); duration = after.tv_sec - before.tv_sec; duration *= 1000000; diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c index 763e420bb8..b7036815c9 100644 --- a/src/lib/libc/net/res_random.c +++ b/src/lib/libc/net/res_random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_random.c,v 1.24 2016/04/05 04:29:21 guenther Exp $ */ +/* $OpenBSD: res_random.c,v 1.25 2020/07/06 13:33:06 pirofti Exp $ */ /* * Copyright 1997 Niels Provos @@ -219,7 +219,7 @@ res_initid(void) if (ru_prf != NULL) arc4random_buf(ru_prf, sizeof(*ru_prf)); - clock_gettime(CLOCK_MONOTONIC, &ts); + WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts); ru_reseed = ts.tv_sec + RU_OUT; ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; } @@ -232,7 +232,7 @@ __res_randomid(void) u_int r; static void *randomid_mutex; - clock_gettime(CLOCK_MONOTONIC, &ts); + WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts); pid = getpid(); _MUTEX_LOCK(&randomid_mutex); diff --git a/src/regress/lib/libc/timekeep/Makefile b/src/regress/lib/libc/timekeep/Makefile new file mode 100644 index 0000000000..ed7dc60379 --- /dev/null +++ b/src/regress/lib/libc/timekeep/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2020/07/06 13:33:06 pirofti Exp $ + +PROGS= test_clock_gettime test_time_skew test_gettimeofday + +.include diff --git a/src/regress/lib/libc/timekeep/test_clock_gettime.c b/src/regress/lib/libc/timekeep/test_clock_gettime.c new file mode 100644 index 0000000000..cc1d8e36e3 --- /dev/null +++ b/src/regress/lib/libc/timekeep/test_clock_gettime.c @@ -0,0 +1,43 @@ +/* $OpenBSD: test_clock_gettime.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */ +/* + * Copyright (c) 2020 Paul Irofti + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#define ASSERT_EQ(a, b) assert((a) == (b)) + +void +check() +{ + struct timespec tp = {0}; + + ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &tp)); + ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &tp)); + ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &tp)); + ASSERT_EQ(0, clock_gettime(CLOCK_UPTIME, &tp)); + + + ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp)); + ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp)); + +} + +int main() +{ + check(); + return 0; +} diff --git a/src/regress/lib/libc/timekeep/test_gettimeofday.c b/src/regress/lib/libc/timekeep/test_gettimeofday.c new file mode 100644 index 0000000000..914058505d --- /dev/null +++ b/src/regress/lib/libc/timekeep/test_gettimeofday.c @@ -0,0 +1,37 @@ +/* $OpenBSD: test_gettimeofday.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */ +/* + * Copyright (c) 2020 Paul Irofti + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#define ASSERT_EQ(a, b) assert((a) == (b)) + +void +check() +{ + struct timeval tv = {0}; + struct timezone tzp; + + ASSERT_EQ(0, gettimeofday(&tv, NULL)); + ASSERT_EQ(0, gettimeofday(&tv, &tzp)); +} + +int main() +{ + check(); + return 0; +} diff --git a/src/regress/lib/libc/timekeep/test_time_skew.c b/src/regress/lib/libc/timekeep/test_time_skew.c new file mode 100644 index 0000000000..7871795f06 --- /dev/null +++ b/src/regress/lib/libc/timekeep/test_time_skew.c @@ -0,0 +1,55 @@ +/* $OpenBSD: test_time_skew.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */ +/* + * Copyright (c) 2020 Paul Irofti + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include +#include + +#define ASSERT_EQ(a, b) assert((a) == (b)) +#define ASSERT_NE(a, b) assert((a) != (b)) + +void +check() +{ + struct timespec tp1, tp2, tout; + + tout.tv_sec = 0; + tout.tv_nsec = 100000; + + ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &tp1)); + + nanosleep(&tout, NULL); + + ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &tp2)); + + /* tp1 should never be larger than tp2 */ + ASSERT_NE(1, timespeccmp(&tp1, &tp2, >)); +} + +int +main(void) +{ + int i; + + for (i = 0; i < 1000; i++) + check(); + + return 0; +} -- cgit v1.2.3-55-g6feb