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.
This commit is contained in:
Willy Tarreau 2021-10-08 15:09:17 +02:00
parent 6cb0c391e7
commit 2169498941
6 changed files with 12 additions and 12 deletions

View File

@ -24,7 +24,6 @@
#include <sys/time.h>
#include <haproxy/api.h>
#include <haproxy/tinfo-t.h>
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);

View File

@ -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

View File

@ -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 */

View File

@ -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

View File

@ -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,

View File

@ -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)