From: Willy Tarreau Date: Mon, 9 Sep 2024 07:12:07 +0000 (+0200) Subject: MINOR: clock: test all clock_gettime() return values X-Git-Tag: v3.1-dev8~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=42e699903e7eca155cb3f4c407a8a1ac88e08cf1;p=thirdparty%2Fhaproxy.git MINOR: clock: test all clock_gettime() return values Till now we were only using clock_gettime() for profiling, so if it would fail it was no big deal. We intend to use it as the main clock as well now, so we need to more reliably detect its absence or failure and gracefully fall back to other options. Without the test we would return anything present in the stack, which is neither clean nor easy to detect. --- diff --git a/src/clock.c b/src/clock.c index 9a33ee6428..9665dfc0ac 100644 --- a/src/clock.c +++ b/src/clock.c @@ -53,10 +53,10 @@ static clockid_t per_thread_clock_id[MAX_THREADS]; uint64_t now_mono_time(void) { uint64_t ret = 0; -#if defined(_POSIX_TIMERS) && defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) +#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; + if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) + ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; #endif return ret; } @@ -72,14 +72,13 @@ uint64_t now_mono_time_fast(void) #if defined(CLOCK_MONOTONIC_COARSE) struct timespec ts; - clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); - return (ts.tv_sec * 1000000000ULL + ts.tv_nsec); -#else + if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) + return (ts.tv_sec * 1000000000ULL + ts.tv_nsec); +#endif /* fallback to regular mono time, * returns 0 if not supported */ return now_mono_time(); -#endif } /* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */ @@ -88,8 +87,8 @@ uint64_t now_cpu_time(void) uint64_t ret = 0; #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) struct timespec ts; - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); - ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; + if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) + ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; #endif return ret; } @@ -126,8 +125,8 @@ uint64_t now_cpu_time_thread(int thr) uint64_t ret = 0; #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) struct timespec ts; - clock_gettime(per_thread_clock_id[thr], &ts); - ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; + if (clock_gettime(per_thread_clock_id[thr], &ts) == 0) + ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; #endif return ret; }