From: Marc Gonzalez <marc.w.gonzalez@free.fr>
To: "John D. McCalpin" <john@mccalpin.com>
Cc: linux-rt-users@vger.kernel.org, Daniel Wagner <dwagner@suse.de>,
Leon Woestenberg <leon@sidebranch.com>,
John Ogness <john.ogness@linutronix.de>,
Steven Rostedt <rostedt@goodmis.org>,
Thomas Gleixner <tglx@linutronix.de>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <williams@redhat.com>,
Pavel Machek <pavel@denx.de>,
Luis Goncalves <lgoncalv@redhat.com>,
Frederic Weisbecker <fweisbec@gmail.com>,
Ingo Molnar <mingo@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
"Ahmed S. Darwish" <darwi@linutronix.de>,
Agner Fog <agner@agner.org>, Dirk Beyer <dirk.beyer@lmu.de>,
Philipp Wendler <philipp.wendler@lmu.de>,
Matt Godbolt <matt@godbolt.org>
Subject: Re: Unexplained variance in run-time of simple program (part 2)
Date: Tue, 7 Apr 2026 15:52:37 +0200 [thread overview]
Message-ID: <b73d91c0-2fc8-45fc-959e-5fc2b484e0fe@free.fr> (raw)
In-Reply-To: <17537284-FA52-40E5-A70F-1120FCEB8BC6@mccalpin.com>
Hello Doctor Bandwidth,
I was secretly hoping you would chime in! :)
You've moved to Spain if I understand correctly?
On 07/04/2026 10:37, John D. McCalpin wrote:
> Going back to the example discussed at [in 2025-09], it looks like
> the benchmark takes ~2500 ns and that you are executing the
> benchmark 2^16 times. Does that mean you are launching a new
> process 2^16 times? If so, exactly what mechanism are you using?
> The variation you are reporting of 0.1 to 0.3 microseconds per
> execution seems very small to me. Are you sure that the variation
> is in the "execution time", or could it be associated with the
> launching of the execution? If the benchmark code is being
> executed 2^16 times in a loop in a single execution, then the
> situation quite different and performance counters should be very
> effective at determining the cause of the variability.
I understand that process creation is a costly operation.
I do run the benchmark several times from the same process.
(I plan on revisiting your above remarks after I digest them.)
> For the performance counter side of things, I would recommend
> programming the performance counters externally to the benchmark
> process and using inline RDPMC instructions to get the counter
> values (inside the benchmark executable).
Why do you suggest programming the PMCs from outside the benchmark process?
(Is it perhaps because it's simpler than using the kernel API?)
It never occurred to me that I could read the PMCs from user-space!
This is what I've been doing until now:
int open_event(u64 type, u64 config, int fd)
{
struct perf_event_attr attr = {
.type = type,
.size = sizeof(attr),
.config = config,
.read_format = PERF_FORMAT_GROUP,
};
return syscall(SYS_perf_event_open, &attr, 0, 3, fd, 0);
}
int main_fd = open_event(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, -1);
open_event(PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS, main_fd);
open_event(PERF_TYPE_RAW, UOPS_EXECUTED, main_fd);
open_event(PERF_TYPE_RAW, EXEC_STALLS, main_fd);
for (int i = 0; i < 1000000; ++i)
{
ioctl(main_fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP);
for (int i = 0; i < N; ++i) spin(ctx);
if (read(main_fd, v, sizeof v) < sizeof v) return 2;
printf("%lu %lu %lu %lu\n", v[1]/N, v[2]/N, v[3]/N, v[4]/N);
}
The problem with that technique is that the PMCs are "polluted" by
the exit from ioctl & the entry into read.
Reading the PMCs from user-space with RDPMC solves this issue!
Found this interesting suggestion from 10 years ago:
https://community.intel.com/t5/Software-Tuning-Performance/How-to-read-performance-counters-by-rdpmc-instruction/m-p/1009043
I think you might be familiar with the responder ;)
> I am not certain what the lowest overhead mechanism might be for
> getting those performance counter values out of the program — I
> would probably try attaching to a persistent System V shared memory
> segment and simply storing the values in memory for later post-processing.
Are you implying that simply using printf might disturb the caches
from the write calls? (I redirect the output to a tmpfs.)
> On Intel SKX processors I measured the overhead of an RDPMC
> instruction at as low as ~25 cycles, with RDTSCP instructions taking
> a little bit longer (~40 cycles), but most importantly inline RDPMC
> does not involve any uncontrolled code paths or any hardware
> accesses outside the core. RDTSCP is similar, but might be
> accessing off-core resources — the timing depends on both the core
> and uncore clock frequencies.
Thanks for the great suggestion. I'll be reading up on RDPMC.
Why do you think I would need RDTSCP?
At the moment, I just pin all cores at 2 GHz & count cycles using PMCs.
Reference for myself:
https://www.codestudy.net/blog/difference-between-rdtscp-rdtsc-memory-and-cpuid-rdtsc/
Regards
next prev parent reply other threads:[~2026-04-07 13:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 15:24 Unexplained variance in run-time of simple program (part 2) Marc Gonzalez
2026-03-26 19:09 ` Marc Gonzalez
2026-04-07 0:38 ` Marc Gonzalez
[not found] ` <17537284-FA52-40E5-A70F-1120FCEB8BC6@mccalpin.com>
2026-04-07 13:52 ` Marc Gonzalez [this message]
2026-04-08 9:29 ` John D. McCalpin
2026-04-10 17:16 ` Marc Gonzalez
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=b73d91c0-2fc8-45fc-959e-5fc2b484e0fe@free.fr \
--to=marc.w.gonzalez@free.fr \
--cc=agner@agner.org \
--cc=bigeasy@linutronix.de \
--cc=darwi@linutronix.de \
--cc=dirk.beyer@lmu.de \
--cc=dwagner@suse.de \
--cc=fweisbec@gmail.com \
--cc=john.ogness@linutronix.de \
--cc=john@mccalpin.com \
--cc=leon@sidebranch.com \
--cc=lgoncalv@redhat.com \
--cc=linux-rt-users@vger.kernel.org \
--cc=matt@godbolt.org \
--cc=mhiramat@kernel.org \
--cc=mingo@kernel.org \
--cc=pavel@denx.de \
--cc=philipp.wendler@lmu.de \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=williams@redhat.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