From: Cyril Hrubis <chrubis@suse.cz>
To: Piotr Kubaj <piotr.kubaj@intel.com>
Cc: daniel.niestepski@intel.com, tomasz.ossowski@intel.com,
helena.anna.dubel@intel.com, rafael.j.wysocki@intel.com,
ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v5 3/3] pmc_core: add ltr_counter test
Date: Fri, 27 Nov 2026 12:02:31 +0100 [thread overview]
Message-ID: <awljR7i361DNAOS1@yuki.lan> (raw)
In-Reply-To: <20261021085932.20557-4-piotr.kubaj@intel.com>
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
prev parent reply other threads:[~2026-11-27 11:02 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-21 8:59 [LTP] [PATCH v5 0/3] pmc_core: cover the PkgC LTR blocker counters Piotr Kubaj
2026-10-21 8:59 ` [LTP] [PATCH v5 1/3] lib: add tst_test.needs_debugfs flag Piotr Kubaj
2026-10-21 13:39 ` [LTP] " linuxtestproject.agent
2026-11-27 10:04 ` [LTP] [PATCH v5 1/3] " Cyril Hrubis
2026-11-30 8:06 ` Kubaj, Piotr
2026-10-21 8:59 ` [LTP] [PATCH v5 2/3] pmc_core: add test for pkgc_ltr_blocker_show Piotr Kubaj
2026-11-27 10:34 ` Cyril Hrubis
2026-10-21 8:59 ` [LTP] [PATCH v5 3/3] pmc_core: add ltr_counter test Piotr Kubaj
2026-11-27 11:02 ` Cyril Hrubis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awljR7i361DNAOS1@yuki.lan \
--to=chrubis@suse.cz \
--cc=daniel.niestepski@intel.com \
--cc=helena.anna.dubel@intel.com \
--cc=ltp@lists.linux.it \
--cc=piotr.kubaj@intel.com \
--cc=rafael.j.wysocki@intel.com \
--cc=tomasz.ossowski@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox