diff --git a/include/haproxy/time.h b/include/haproxy/time.h index 4a3feb5df..c8358e00c 100644 --- a/include/haproxy/time.h +++ b/include/haproxy/time.h @@ -577,10 +577,26 @@ static inline void measure_idle() if (samp_time < 500000) return; - ti->idle_pct = (100ULL * idle_time + samp_time / 2) / samp_time; + HA_ATOMIC_STORE(&ti->idle_pct, (100ULL * idle_time + samp_time / 2) / samp_time); idle_time = samp_time = 0; } +/* report the average CPU idle percentage over all running threads, between 0 and 100 */ +static inline uint report_idle() +{ + uint total = 0; + uint rthr = 0; + uint thr; + + for (thr = 0; thr < MAX_THREADS; thr++) { + if (!(all_threads_mask & (1UL << thr))) + continue; + total += HA_ATOMIC_LOAD(&ha_thread_info[thr].idle_pct); + rthr++; + } + return rthr ? total / rthr : 0; +} + /* 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. */ diff --git a/src/stats.c b/src/stats.c index 35415b309..11198f1b9 100644 --- a/src/stats.c +++ b/src/stats.c @@ -3400,7 +3400,7 @@ static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *u actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec), bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0), bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k', - total_run_queues(), total_allocated_tasks(), ti->idle_pct + total_run_queues(), total_allocated_tasks(), report_idle() ); /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ @@ -4433,7 +4433,7 @@ int stats_fill_info(struct field *info, int len, uint flags) #endif info[INF_TASKS] = mkf_u32(0, total_allocated_tasks()); info[INF_RUN_QUEUE] = mkf_u32(0, total_run_queues()); - info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct); + info[INF_IDLE_PCT] = mkf_u32(FN_AVG, report_idle()); info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node); if (global.desc) info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);