[LTP] [PATCH v5 3/3] pmc_core: add ltr_counter test
Cyril Hrubis
chrubis@suse.cz
Fri Nov 27 12:02:31 CET 2026
Hi!
> +/*
> + * Read the counter values, checking that the counter set itself has not changed
> + * since setup(). Only the values are of interest here - the format of the file
> + * is what the pkgc_ltr_blocker_show test covers - so anything unexpected about
> + * the lines makes this test unable to run rather than failing it.
> + */
> +static void read_counters(uint32_t *values)
> +{
> + char line[256];
> + FILE *fp;
> + int n = 0;
> +
> + fp = SAFE_FOPEN(PATH, "r");
> +
> + while (fgets(line, sizeof(line), fp)) {
> + char name[NAME_LEN];
> + unsigned int value;
> +
> + line[strcspn(line, "\n")] = '\0';
This shouldn't be needed here, the \n should be ingored since we ask for
%u at the end of the line here.
> + if (n == MAX_COUNTERS)
> + tst_brk(TBROK, "more than %d counters reported", MAX_COUNTERS);
This should be at the end of the loop just after n++, or we can even do:
if (++n == MAX_COUNTERS)
...
> + if (sscanf(line, "%63s %u", name, &value) != 2)
> + tst_brk(TBROK, "unexpected line format: '%s'", line);
> +
> + values[n] = value;
> +
> + if (cnt && strcmp(name, names[n])) {
> + tst_brk(TBROK, "counter %d renamed during the test: %s -> %s",
> + n, names[n], name);
> + }
> +
> + if (!cnt)
> + strcpy(names[n], name);
> +
> + n++;
> + }
> +
> + /*
> + * A telemetry read that fails in the driver aborts the seq_file read
> + * instead of ending it at EOF, so without this the counters lost to the
> + * error would look like counters that stopped advancing.
> + */
> + if (ferror(fp))
> + tst_brk(TBROK, "reading " PATH " failed after %d counters", n);
> +
> + SAFE_FCLOSE(fp);
> +
> + if (cnt && n != cnt)
> + tst_brk(TBROK, "counter count changed during the test: %d -> %d", cnt, n);
> +
> + cnt = n;
> +}
> +
> +static void spin(void)
> +{
> + /* volatile so that the loop survives optimization */
This comment AFAIK states the obvious.
> + for (;;)
> + burn++;
> +}
> +
> +/*
> + * Keep every CPU the test may run on busy, so that the package cannot go idle
> + * and no package C-state entry can be attempted.
> + *
> + * The spinners are pinned one per CPU taken from the test's own affinity mask,
> + * rather than left to the load balancer, so that a CPU is not left idle by a
> + * scheduling decision. Offline CPUs and a restricted mask are handled by
> + * construction.
This paragraph again AFAIK states the obvious.
> + */
> +static void start_load(void)
> +{
> + cpu_set_t mask;
> + int nr_cpus;
> + int cpu = 0;
> +
> + if (sched_getaffinity(0, sizeof(mask), &mask))
> + tst_brk(TBROK | TERRNO, "sched_getaffinity() failed");
> +
> + nr_cpus = CPU_COUNT(&mask);
> + spinners = SAFE_MALLOC(nr_cpus * sizeof(*spinners));
> +
> + for (int i = 0; i < nr_cpus; i++) {
> + pid_t pid;
> +
> + while (!CPU_ISSET(cpu, &mask))
> + cpu++;
> +
> + pid = SAFE_FORK();
> +
> + if (!pid) {
> + cpu_set_t one;
> +
> + CPU_ZERO(&one);
> + CPU_SET(cpu, &one);
> +
> + if (sched_setaffinity(0, sizeof(one), &one)) {
> + tst_brk(TBROK | TERRNO,
> + "sched_setaffinity(CPU %d) failed", cpu);
> + }
It would be nicer if we added SAFE_SCHED_SETAFFINITY() and
SAFE_SCHED_GETAFFINITY().
> + spin();
> + }
> +
> + /*
> + * Counted only once the child exists, so that a fork() that
> + * fails half way through does not leave stop_load() killing
> + * pids that were never written. A zeroed entry would mean
> + * kill() signalling the whole process group.
> + */
SAFE_FORK() cannot fail, this comment is wrong.
> + spinners[nr_spinners++] = pid;
> + cpu++;
> + }
> +
> + tst_res(TINFO, "keeping %d CPUs busy", nr_spinners);
> +}
> +
> +static void stop_load(void)
> +{
> + if (!spinners)
> + return;
> +
> + for (int i = 0; i < nr_spinners; i++) {
> + SAFE_KILL(spinners[i], SIGKILL);
> + SAFE_WAITPID(spinners[i], NULL, 0);
> + }
> +
> + free(spinners);
> + spinners = NULL;
> + nr_spinners = 0;
> +}
> +
> +/*
> + * Wait for a counter to advance while the system idles, reporting how many did.
> + *
> + * This is what tells a counter that stops under load from one that never moves.
> + * On an idle system most counters read 0 permanently, because nothing posts an
> + * LTR that would block a package C-state entry, so a single counter advancing
> + * is enough.
> + */
> +static int wait_for_advance(const uint32_t *baseline)
> +{
> + uint32_t values[MAX_COUNTERS];
> + struct timespec start, now;
> + int advanced = 0;
> +
> + tst_clock_gettime(CLOCK_MONOTONIC, &start);
> +
> + for (;;) {
> + read_counters(values);
> +
> + for (int i = 0; i < cnt; i++) {
> + if (values[i] == baseline[i])
> + continue;
> +
> + tst_res(TINFO, "%s advanced by %u while idle", names[i],
> + values[i] - baseline[i]);
> + advanced++;
> + }
> +
> + if (advanced)
> + return advanced;
> +
> + tst_clock_gettime(CLOCK_MONOTONIC, &now);
> +
> + if (tst_timespec_diff_ms(now, start) >= IDLE_WINDOW * 1000)
> + return 0;
> +
> + usleep(POLL_MS * 1000);
> + }
The IDLE_WINDOW shouldn't be hardcoded, it should be controlled by
.runtime and .min_runtime and this loop should act on
tst_remanining_runtime()
> +}
> +
> +/*
> + * Require the counters to hold still for QUIET_WINDOW seconds of full load.
> + */
> +static void check_counters_stop(void)
> +{
> + uint32_t first[MAX_COUNTERS], prev[MAX_COUNTERS], values[MAX_COUNTERS];
> + struct timespec changed_at[MAX_COUNTERS];
> + struct timespec start, last_change, now;
> +
> + read_counters(first);
> + memcpy(prev, first, sizeof(prev));
> +
> + tst_clock_gettime(CLOCK_MONOTONIC, &start);
> + last_change = start;
> +
> + for (int i = 0; i < cnt; i++)
> + changed_at[i] = start;
> +
> + for (;;) {
> + usleep(POLL_MS * 1000);
> +
> + read_counters(values);
> + tst_clock_gettime(CLOCK_MONOTONIC, &now);
> +
> + for (int i = 0; i < cnt; i++) {
> + if (values[i] == prev[i])
> + continue;
> +
> + changed_at[i] = now;
> + last_change = now;
> + }
> +
> + memcpy(prev, values, sizeof(prev));
> +
> + if (tst_timespec_diff_ms(now, last_change) >= QUIET_WINDOW * 1000) {
> + tst_res(TPASS,
> + "%d counters held still over %ds with every CPU busy",
> + cnt, QUIET_WINDOW);
> + return;
> + }
> +
> + if (tst_timespec_diff_ms(now, start) < LOAD_WINDOW * 1000)
> + continue;
> +
> + /*
> + * A counter that last changed a quiet window ago or more was
> + * only draining entries blocked before the load took hold. The
> + * ones that changed since are the ones that never stopped.
> + */
> + for (int i = 0; i < cnt; i++) {
> + if (tst_timespec_diff_ms(now, changed_at[i]) >=
> + QUIET_WINDOW * 1000)
> + continue;
> +
> + tst_res(TFAIL,
> + "%s kept advancing over %ds with every CPU busy: %u -> %u",
> + names[i], LOAD_WINDOW, first[i], values[i]);
> + }
> +
> + return;
> + }
> +}
> +
> +static void setup(void)
> +{
> + uint32_t values[MAX_COUNTERS];
> +
> + if (access(PATH, R_OK))
> + tst_brk(TCONF | TERRNO, "%s not available", PATH);
> +
> + /* Learns the counter set the rest of the test compares against */
> + read_counters(values);
> +
> + if (!cnt)
> + tst_brk(TBROK, "no LTR blocker counters reported");
> +}
> +
> +static void cleanup(void)
> +{
> + stop_load();
> +}
> +
> +static void run(void)
> +{
> + uint32_t baseline[MAX_COUNTERS];
> +
> + read_counters(baseline);
> +
> + /*
> + * Without a counter that moves while idle there is nothing to stop
> + * under load, a stuck counter would look exactly the same.
> + */
> + if (!wait_for_advance(baseline)) {
> + tst_brk(TCONF,
> + "no LTR blocked a package C-state entry over %ds of idling",
> + IDLE_WINDOW);
> + }
> +
> + start_load();
> + check_counters_stop();
> + stop_load();
> +}
> +
> +static struct tst_test test = {
> + .min_kver = "7.2",
> + .timeout = 90,
> + .needs_debugfs = 1,
> + .forks_child = 1,
> + .needs_kconfigs = (const char *const []) {
> + "CONFIG_DEBUG_FS",
> + "CONFIG_INTEL_PMC_CORE",
> + NULL
> + },
> + .supported_archs = (const char *const []) {
> + "x86",
> + "x86_64",
> + NULL
> + },
> + .tags = (const struct tst_tag[]) {
> + {"linux-git", "38c79dd63b72e36919ef097d4e5025ca0fa17f34"},
> + {}
> + },
Again tags are only for regression tests and point to kernel fixes.
> + .setup = setup,
> + .cleanup = cleanup,
> + .test_all = run
> +};
What happens when the test is run in a KVM VM where only subset of CPUs
is exposed, will the counters in the file match the set of the CPUs
exposed to the VM?
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list