From 20adfde9c848e5c7ef54e28bf0cc9578ed7f3171 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 8 Oct 2021 11:34:46 +0200 Subject: [PATCH] MINOR: activity: get the run_time from the clock updates Instead of fiddling with before_poll and after_poll in activity_count_runtime(), the function is now called by clock_entering_poll() which passes it the number of microseconds spent working. This allows to remove all calls to activity_count_runtime() from the pollers. --- include/haproxy/activity.h | 2 +- src/activity.c | 12 +++++------- src/clock.c | 6 ++++++ src/ev_epoll.c | 1 - src/ev_evports.c | 1 - src/ev_kqueue.c | 1 - src/ev_poll.c | 1 - src/ev_select.c | 1 - 8 files changed, 12 insertions(+), 13 deletions(-) diff --git a/include/haproxy/activity.h b/include/haproxy/activity.h index 42b0c8793..f0ca4eea1 100644 --- a/include/haproxy/activity.h +++ b/include/haproxy/activity.h @@ -31,7 +31,7 @@ extern struct activity activity[MAX_THREADS]; extern struct sched_activity sched_activity[256]; void report_stolen_time(uint64_t stolen); -void activity_count_runtime(); +void activity_count_runtime(uint32_t run_time); struct sched_activity *sched_activity_entry(struct sched_activity *array, const void *func); #endif /* _HAPROXY_ACTIVITY_H */ diff --git a/src/activity.c b/src/activity.c index a2d31898c..a4527f2db 100644 --- a/src/activity.c +++ b/src/activity.c @@ -349,14 +349,13 @@ void report_stolen_time(uint64_t stolen) update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen); } -/* Collect date and time information before calling poll(). This will be used - * to count the run time of the past loop and the sleep time of the next poll. - * It also makes use of the just updated before_poll timer to count the loop's - * run time and feed the average loop time metric (in microseconds). +/* Update avg_loop value for the current thread and possibly decide to enable + * task-level profiling on the current thread based on its average run time. + * The argument is the number of microseconds elapsed since the + * last time poll() returned. */ -void activity_count_runtime() +void activity_count_runtime(uint32_t run_time) { - uint32_t run_time; uint32_t up, down; /* 1 millisecond per loop on average over last 1024 iterations is @@ -365,7 +364,6 @@ void activity_count_runtime() up = 1000; down = up * 99 / 100; - run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec); run_time = swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time); /* In automatic mode, reaching the "up" threshold on average switches diff --git a/src/clock.c b/src/clock.c index f68830efb..cd5ead7b4 100644 --- a/src/clock.c +++ b/src/clock.c @@ -283,10 +283,13 @@ void clock_entering_poll(void) { uint64_t new_mono_time; uint64_t new_cpu_time; + uint32_t run_time; int64_t stolen; gettimeofday(&before_poll, NULL); + run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec); + new_cpu_time = now_cpu_time(); new_mono_time = now_mono_time(); @@ -302,6 +305,9 @@ void clock_entering_poll(void) report_stolen_time(stolen); } } + + /* update the average runtime */ + activity_count_runtime(run_time); } /* returns the current date as returned by gettimeofday() in ISO+microsecond diff --git a/src/ev_epoll.c b/src/ev_epoll.c index a9b572b2e..1babe56c2 100644 --- a/src/ev_epoll.c +++ b/src/ev_epoll.c @@ -190,7 +190,6 @@ static void _do_poll(struct poller *p, int exp, int wake) /* now let's wait for polled events */ wait_time = wake ? 0 : compute_poll_timeout(exp); clock_entering_poll(); - activity_count_runtime(); do { int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time; diff --git a/src/ev_evports.c b/src/ev_evports.c index dcc30a195..710d51236 100644 --- a/src/ev_evports.c +++ b/src/ev_evports.c @@ -160,7 +160,6 @@ static void _do_poll(struct poller *p, int exp, int wake) */ wait_time = wake ? 0 : compute_poll_timeout(exp); clock_entering_poll(); - activity_count_runtime(); do { int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time; diff --git a/src/ev_kqueue.c b/src/ev_kqueue.c index c9f355b93..1bd79fd69 100644 --- a/src/ev_kqueue.c +++ b/src/ev_kqueue.c @@ -147,7 +147,6 @@ static void _do_poll(struct poller *p, int exp, int wake) wait_time = wake ? 0 : compute_poll_timeout(exp); fd = global.tune.maxpollevents; clock_entering_poll(); - activity_count_runtime(); do { int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time; diff --git a/src/ev_poll.c b/src/ev_poll.c index dbfa26269..5cfdff112 100644 --- a/src/ev_poll.c +++ b/src/ev_poll.c @@ -203,7 +203,6 @@ static void _do_poll(struct poller *p, int exp, int wake) /* now let's wait for events */ wait_time = wake ? 0 : compute_poll_timeout(exp); clock_entering_poll(); - activity_count_runtime(); status = poll(poll_events, nbfd, wait_time); clock_update_date(wait_time, status); clock_leaving_poll(wait_time, status); diff --git a/src/ev_select.c b/src/ev_select.c index 87a926d84..3c8471fcd 100644 --- a/src/ev_select.c +++ b/src/ev_select.c @@ -174,7 +174,6 @@ static void _do_poll(struct poller *p, int exp, int wake) delta.tv_sec = (delta_ms / 1000); delta.tv_usec = (delta_ms % 1000) * 1000; clock_entering_poll(); - activity_count_runtime(); status = select(maxfd, readnotnull ? tmp_evts[DIR_RD] : NULL, writenotnull ? tmp_evts[DIR_WR] : NULL,