[LTP] [PATCH v2] getrusage03: account for percpu RSS counter batching
Cyril Hrubis
chrubis@suse.cz
Thu Sep 24 13:26:48 CEST 2026
Hi!
> diff --git a/testcases/kernel/syscalls/getrusage/getrusage03.c b/testcases/kernel/syscalls/getrusage/getrusage03.c
> index a2cdd6158..38a100576 100644
> --- a/testcases/kernel/syscalls/getrusage/getrusage03.c
> +++ b/testcases/kernel/syscalls/getrusage/getrusage03.c
> @@ -13,9 +13,13 @@
> * this program.
> */
>
> +#define _GNU_SOURCE
> #include <stdlib.h>
> #include <stdio.h>
>
> +#include "lapi/cpuset.h"
> +#include "tst_safe_stdio.h"
> +#include "tst_cpu.h"
> #include "tst_test.h"
> #include "getrusage03.h"
>
> @@ -23,12 +27,99 @@
>
> static struct rusage ru;
> static long maxrss_init;
> +static long lower_allowance;
>
> static const char *const resource[] = {
> TESTBIN,
> NULL,
> };
>
> +static long count_online_cpus(void)
> +{
> + FILE *fp = SAFE_FOPEN("/proc/stat", "r");
> + char line[BUFSIZ];
> + long count = 0;
> +
> + while (fgets(line, sizeof(line), fp)) {
> + if (line[0] == 'c' && line[1] == 'p' && line[2] == 'u' &&
> + line[3] >= '0' && line[3] <= '9')
> + count++;
> + }
> +
> + if (ferror(fp))
> + tst_brk(TBROK | TERRNO, "fgets(/proc/stat)");
> +
> + SAFE_FCLOSE(fp);
> +
> + if (!count)
> + tst_brk(TBROK, "No online CPUs found in /proc/stat");
> +
> + return count;
> +}
We do have tst_ncpus() in the test library, please use that instead.
> +static void pin_to_cpu(void)
> +{
> + long ncpus = tst_ncpus_max();
> + size_t size = CPU_ALLOC_SIZE(ncpus);
> + cpu_set_t *mask = CPU_ALLOC(ncpus);
> + int cpu = -1;
> +
> + if (!mask)
> + tst_brk(TBROK | TERRNO, "CPU_ALLOC()");
> +
> + CPU_ZERO_S(size, mask);
> + if (sched_getaffinity(0, size, mask) < 0) {
> + CPU_FREE(mask);
> + tst_brk(TBROK | TERRNO, "sched_getaffinity()");
> + }
> +
> + for (long i = 0; i < ncpus; i++) {
> + if (CPU_ISSET_S((int)i, size, mask)) {
> + cpu = (int)i;
> + break;
> + }
> + }
> +
> + if (cpu < 0) {
> + CPU_FREE(mask);
> + tst_brk(TBROK, "sched_getaffinity() returned an empty CPU mask");
> + }
> +
> + CPU_ZERO_S(size, mask);
> + CPU_SET_S(cpu, size, mask);
> + if (sched_setaffinity(0, size, mask) < 0) {
> + CPU_FREE(mask);
> + tst_brk(TBROK | TERRNO, "sched_setaffinity()");
> + }
> +
> + CPU_FREE(mask);
> +}
I like the funciton from Jan that pins the test to the current CPU
slightly better.
The rest looks good.
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list