From: Willy Tarreau Date: Fri, 8 Oct 2021 13:09:17 +0000 (+0200) Subject: MINOR: clock: move the clock_ids to clock.c X-Git-Tag: v2.5-dev9~22 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2169498941254eef0589d7c078f9955959d4baf5;p=thirdparty%2Fhaproxy.git MINOR: clock: move the clock_ids to clock.c This removes the knowledge of clockid_t from anywhere but clock.c, thus eliminating a source of includes burden. The unused clock_id field was removed from thread_info, and the definition setting of clockid_t was removed from compat.h. The most visible change is that the function now_cpu_time_thread() now takes the thread number instead of a tinfo pointer. --- diff --git a/include/haproxy/clock.h b/include/haproxy/clock.h index 2cee76da4e..dedcacb248 100644 --- a/include/haproxy/clock.h +++ b/include/haproxy/clock.h @@ -24,7 +24,6 @@ #include #include -#include extern struct timeval start_date; /* the process's start date in wall-clock time */ extern volatile ullong global_now; /* common monotonic date between all threads (32:32) */ @@ -32,7 +31,7 @@ extern volatile ullong global_now; /* common monotonic date betwe extern THREAD_LOCAL struct timeval now; /* internal monotonic date derived from real clock */ extern THREAD_LOCAL struct timeval date; /* the real current date (wall-clock time) */ -uint64_t now_cpu_time_thread(const struct thread_info *thr); +uint64_t now_cpu_time_thread(int thr); uint64_t now_mono_time(void); uint64_t now_cpu_time(void); void clock_set_local_source(void); diff --git a/include/haproxy/compat.h b/include/haproxy/compat.h index 18eadcc565..be361b31ec 100644 --- a/include/haproxy/compat.h +++ b/include/haproxy/compat.h @@ -162,8 +162,6 @@ typedef struct { } empty_t; /* systems without such defines do not know clockid_t or timer_t */ #if !(_POSIX_TIMERS > 0) -#undef clockid_t -#define clockid_t empty_t #undef timer_t #define timer_t empty_t #endif diff --git a/include/haproxy/tinfo-t.h b/include/haproxy/tinfo-t.h index 6c09fc4567..4d3ff5b4d1 100644 --- a/include/haproxy/tinfo-t.h +++ b/include/haproxy/tinfo-t.h @@ -33,7 +33,6 @@ * the pthread identifier which does not exist). */ struct thread_info { - clockid_t clock_id; timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */ uint64_t prev_cpu_time; /* previous per thread CPU time */ uint64_t prev_mono_time; /* previous system wide monotonic time */ diff --git a/src/clock.c b/src/clock.c index 517f0a3a53..33d7b4dee5 100644 --- a/src/clock.c +++ b/src/clock.c @@ -43,6 +43,10 @@ static THREAD_LOCAL unsigned int idle_time; /* total idle time over curren static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */ static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */ +#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) +static clockid_t per_thread_clock_id[MAX_THREADS]; +#endif + /* returns the system's monotonic time in nanoseconds if supported, otherwise zero */ uint64_t now_mono_time(void) { @@ -68,12 +72,12 @@ uint64_t now_cpu_time(void) } /* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */ -uint64_t now_cpu_time_thread(const struct thread_info *thr) +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(thr->clock_id, &ts); + clock_gettime(per_thread_clock_id[thr], &ts); ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; #endif return ret; @@ -84,9 +88,9 @@ void clock_set_local_source(void) { #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) #ifdef USE_THREAD - pthread_getcpuclockid(pthread_self(), &ti->clock_id); + pthread_getcpuclockid(pthread_self(), &per_thread_clock_id[tid]); #else - ti->clock_id = CLOCK_THREAD_CPUTIME_ID; + per_thread_clock_id[tid] = CLOCK_THREAD_CPUTIME_ID; #endif #endif } @@ -115,7 +119,7 @@ int clock_setup_signal_timer(void *tmr, int sig, int val) sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = sig; sev.sigev_value.sival_int = val; - if (timer_create(ti->clock_id, &sev, timer) != -1 || + if (timer_create(per_thread_clock_id[tid], &sev, timer) != -1 || timer_create(CLOCK_REALTIME, &sev, timer) != -1) ret = 1; #endif diff --git a/src/debug.c b/src/debug.c index c5b78edc7b..ae3d9aebd4 100644 --- a/src/debug.c +++ b/src/debug.c @@ -151,7 +151,7 @@ void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) { unsigned long thr_bit = 1UL << thr; unsigned long long p = ha_thread_info[thr].prev_cpu_time; - unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]); + unsigned long long n = now_cpu_time_thread(thr); int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK); chunk_appendf(buf, diff --git a/src/wdt.c b/src/wdt.c index a893b5d896..87d0cda542 100644 --- a/src/wdt.c +++ b/src/wdt.c @@ -65,7 +65,7 @@ void wdt_handler(int sig, siginfo_t *si, void *arg) break; p = ha_thread_info[thr].prev_cpu_time; - n = now_cpu_time_thread(&ha_thread_info[thr]); + n = now_cpu_time_thread(thr); /* not yet reached the deadline of 1 sec */ if (n - p < 1000000000UL)