Skip to content

Commit 90e8ccd

Browse files
committed
MINOR: sample: add new sample fetch functions reporting current CPU usage
Some features can automatically turn on or off depending on CPU usage, but it's not easy to measure it. Let's provide 3 new sample fetch functions reporting the CPU usage as measured inside haproxy during the previous polling loop, and reported in "idle" stats header / "show info", or used by tune.glitches.kill.cpu-usage, or maxcompcpuusage: - cpu_usage_thr: CPU usage between 0 and 100 of the current thread, used by functions above - cpu_usage_grp: CPU usage between 0 and 100, averaged over all threads of the same group as the current one. - cpu_usage_proc: CPU usage between 0 and 100, averaged over all threads of the current process Note that the value will fluctuate since it only covers a few tens to hundreds of requests of the last polling loop, but it reports what is being used to take decisions. It could also be used to disable some non-essential debugging/processing under too high loads for example.
1 parent 630ef96 commit 90e8ccd

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

doc/configuration.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23672,6 +23672,30 @@ cpu_ns_tot : integer
2367223672
high cpu_calls count, for example when processing many HTTP chunks, and for
2367323673
this reason it is often preferred to log cpu_ns_avg instead.
2367423674

23675+
cpu_usage_grp : integer
23676+
Returns the measured CPU usage over the last polling loop, between 0 and 100,
23677+
averaged over all threads of the current thread group. This can be used for
23678+
troubleshooting and for logging. The measure is extremely volatile but will
23679+
remain accurate for sustained loads as each thread measures it over a few
23680+
tens to hundreds of requests.
23681+
23682+
cpu_usage_proc : integer
23683+
Returns the measured CPU usage over the last polling loop, between 0 and 100,
23684+
averaged over all running threads. This can be used for troubleshooting and
23685+
for logging. The measure is extremely volatile but will remain accurate for
23686+
sustained loads as each thread measures it over a few tens to hundreds of
23687+
requests. This is 100 minus the value reported in the idle ratio in the stats
23688+
page and in "show info".
23689+
23690+
cpu_usage_thr : integer
23691+
Returns the measured CPU usage over the last polling loop, between 0 and 100,
23692+
for the calling thread. This can be used for troubleshooting and for logging.
23693+
The measure is extremely volatile but will remain accurate for sustained
23694+
loads as it is measured over a few tens to hundreds of requests. This is the
23695+
same value as used to decide to enable connection killing on too high
23696+
glitches, or to disable compression. See also "tune.glitches.kill.cpu-usage"
23697+
and "maxcomcpuusage".
23698+
2367523699
date([<offset>[,<unit>]]) : integer
2367623700
Returns the current date as the epoch (number of seconds since 01/01/1970).
2367723701

src/sample.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,6 +5089,43 @@ smp_fetch_tgroup(const struct arg *args, struct sample *smp, const char *kw, voi
50895089
return 1;
50905090
}
50915091

5092+
/* returns the last known CPU usage of the current thread */
5093+
static int
5094+
smp_fetch_cpu_usage_thr(const struct arg *args, struct sample *smp, const char *kw, void *private)
5095+
{
5096+
smp->data.type = SMP_T_SINT;
5097+
smp->data.u.sint = 100 - th_ctx->idle_pct;
5098+
return 1;
5099+
}
5100+
5101+
/* returns the last known CPU usage of the current thread group */
5102+
static int
5103+
smp_fetch_cpu_usage_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
5104+
{
5105+
uint thr, tot = 0;
5106+
5107+
for (thr = 0; thr < ha_tgroup_info[tgid - 1].count; thr++)
5108+
tot += 100 - ha_thread_ctx[ha_tgroup_info[tgid - 1].base + thr].idle_pct;
5109+
5110+
smp->data.type = SMP_T_SINT;
5111+
smp->data.u.sint = (tot + thr / 2) / thr;
5112+
return 1;
5113+
}
5114+
5115+
/* returns the last known CPU usage of the whole process */
5116+
static int
5117+
smp_fetch_cpu_usage_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
5118+
{
5119+
int thr, tot = 0;
5120+
5121+
for (thr = 0; thr < global.nbthread; thr++)
5122+
tot += 100 - ha_thread_ctx[thr].idle_pct;
5123+
5124+
smp->data.type = SMP_T_SINT;
5125+
smp->data.u.sint = (tot + thr / 2) / thr;
5126+
return 1;
5127+
}
5128+
50925129
/* generate a random 32-bit integer for whatever purpose, with an optional
50935130
* range specified in argument.
50945131
*/
@@ -5677,6 +5714,9 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
56775714
{ "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
56785715
{ "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
56795716
{ "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
5717+
{ "cpu_usage_grp", smp_fetch_cpu_usage_grp, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
5718+
{ "cpu_usage_proc",smp_fetch_cpu_usage_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
5719+
{ "cpu_usage_thr", smp_fetch_cpu_usage_thr, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
56805720
{ "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
56815721
{ "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
56825722

0 commit comments

Comments
 (0)