From: Marc Gonzalez <marc.w.gonzalez@free.fr>
To: John Ogness <john.ogness@linutronix.de>,
Leon Woestenberg <leon@sidebranch.com>,
Daniel Wagner <dwagner@suse.de>
Cc: linux-rt-users@vger.kernel.org,
Steven Rostedt <rostedt@goodmis.org>,
Thomas Gleixner <tglx@linutronix.de>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Daniel Wagner <daniel.wagner@suse.com>,
Clark Williams <williams@redhat.com>,
Pavel Machek <pavel@denx.de>,
Luis Goncalves <lgoncalv@redhat.com>,
John McCalpin <mccalpin@tacc.utexas.edu>
Subject: Re: Large(ish) variance induced by SCHED_FIFO
Date: Mon, 8 Sep 2025 17:42:58 +0200 [thread overview]
Message-ID: <3fe55967-b4fa-4bc1-ae2b-58b5d1bbf3ea@free.fr> (raw)
In-Reply-To: <84ldmp8ewv.fsf@jogness.linutronix.de>
On 08/09/2025 11:36, John Ogness wrote:
> There are still reasons why CLOCK_MONOTONIC_RAW might be
> interesting. For example, if you want a very stable time source to
> compare intervals, but do not care so much about the real world time
> lengths of those intervals (i.e. where is the greatest latency vs. what
> is the value of the greatest latency). Although even here, I doubt
> CLOCK_MONOTONIC_RAW has a practical advantage over CLOCK_MONOTONIC.
In fact, I'm just trying to compare the run-time of 2 minor
variations of the same program (testing micro-optimizations).
Absolute run-time is not really important, what I really want
to know is: does v2 run faster or slower than v1?
This is the framework I'm using at this point:
#include <stdio.h>
#include <time.h>
extern void my_code(void);
static long loop(int log2)
{
int n = 1 << log2;
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
for (int i = 0; i < n; ++i) my_code();
clock_gettime(CLOCK_MONOTONIC, &t1);
long d = (t1.tv_sec - t0.tv_sec)*1000000000L + (t1.tv_nsec - t0.tv_nsec);
long t = d >> log2;
return t;
}
int main(void)
{
long t, min = loop(4);
for (int i = 0; i < 20; ++i)
if ((t = loop(8)) < min) min = t;
printf("MIN=%ld\n", min);
return 0;
}
Basically:
- warm up the caches
- run my_code() 256 times && compute average run-time
- repeat 20 times to find MINIMUM average run-time
When my_code() is a trivial computational loop such as:
mov $(1<<12), %eax
1: dec %ecx
dec %ecx
dec %eax
jnz 1b
ret
Then running the benchmark 1000 times returns the same value 1000 times:
MIN=2737
Obviously, the program I'm working on is a bit more complex, but barely:
- no system calls, no library calls
- just simple bit twiddling
- tiny code, tiny data structures, everything fits in L1
$ size a.out
text data bss dec hex filename
8549 632 1072 10253 280d a.out
When I run the benchmark 1000 times, there are some large outliers:
MIN_MIN=2502
MAX_MIN=2774
NOTE: 95% of the results are below 2536.
But the top 1% (worst 10) are really bad (2646-2774)
How to get repeatable results?
Random 10% outliers break the ability to measure the impact
of micro-optimizations expected to provide 0-3% improvements :(
For reference, the script launching the benchmark does:
echo -1 > /proc/sys/kernel/sched_rt_runtime_us
for I in 0 1 2 3; do echo userspace > /sys/devices/system/cpu/cpu$I/cpufreq/scaling_governor; done
sleep 0.25
for I in 0 1 2 3; do echo 3000000 > /sys/devices/system/cpu/cpu$I/cpufreq/scaling_setspeed; done
sleep 0.25
for I in $(seq 1 1000); do
chrt -f 99 taskset -c 2 ./a.out < $1
done
for I in 0 1 2 3; do
echo schedutil > /sys/devices/system/cpu/cpu$I/cpufreq/scaling_governor
done
echo 950000 > /proc/sys/kernel/sched_rt_runtime_us
I've run out of ideas to identify other sources of variance.
(I ran everything in single user mode with nothing else running.)
Perhaps with perf I could identify the source of stalls or bubbles?
Hoping someone can point me in the right direction.
Regards
next prev parent reply other threads:[~2025-09-08 15:43 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-04 23:45 Large(ish) variance induced by SCHED_FIFO Marc Gonzalez
2025-09-05 8:25 ` Daniel Wagner
2025-09-05 13:23 ` Marc Gonzalez
2025-09-05 14:03 ` John Ogness
2025-09-05 15:34 ` Marc Gonzalez
2025-09-05 16:07 ` John Ogness
2025-09-05 17:40 ` Marc Gonzalez
2025-09-08 9:36 ` John Ogness
2025-09-08 15:42 ` Marc Gonzalez [this message]
2025-09-08 16:02 ` Daniel Wagner
2025-09-08 18:40 ` Marc Gonzalez
2025-09-09 11:08 ` Unexplained variance in run-time of trivial program Marc Gonzalez
2025-09-09 11:21 ` Daniel Wagner
2025-09-09 12:42 ` Marc Gonzalez
2025-09-09 14:23 ` Steven Rostedt
2025-09-09 12:34 ` John Ogness
2025-09-09 14:08 ` Marc Gonzalez
2025-09-09 18:33 ` Marc Gonzalez
[not found] ` <CAMjhiJjMOQN-nWd+KP4JBBNHf20M+J2fXAuTTvowXctJgvGOcQ@mail.gmail.com>
2025-09-09 20:30 ` Marc Gonzalez
2025-09-10 7:59 ` Daniel Wagner
2025-09-11 21:29 ` Marc Gonzalez
2025-09-11 22:15 ` Marc Gonzalez
2025-09-12 7:44 ` Daniel Wagner
2025-09-13 10:09 ` Marc Gonzalez
2025-09-15 16:30 ` Marc Gonzalez
2025-09-16 7:50 ` Ahmed S. Darwish
2025-09-25 11:38 ` Marc Gonzalez
2025-09-08 16:52 ` [EXT] Re: Large(ish) variance induced by SCHED_FIFO Rui Sousa
2025-09-08 17:03 ` Marc Gonzalez
[not found] ` <CAMjhiJiVDmnx+pgpvtAy=KarHexjsYs+T9tSkpGvppjqFdtmiw@mail.gmail.com>
2025-09-05 21:05 ` 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=3fe55967-b4fa-4bc1-ae2b-58b5d1bbf3ea@free.fr \
--to=marc.w.gonzalez@free.fr \
--cc=bigeasy@linutronix.de \
--cc=daniel.wagner@suse.com \
--cc=dwagner@suse.de \
--cc=john.ogness@linutronix.de \
--cc=leon@sidebranch.com \
--cc=lgoncalv@redhat.com \
--cc=linux-rt-users@vger.kernel.org \
--cc=mccalpin@tacc.utexas.edu \
--cc=pavel@denx.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