GeezerVison is showing github.com — Running perf stat and ap -e cycles --total in parallel, make them to disagree · Issue #1585 · async-profiler/async-profiler
Running perf stat and ap -e cycles --total in parallel, make them to disagree #1585DescriptionDescribe the bug
Running perf stat with async profiler -e cycles --total against the same application in parallel produce a different number of total cycles.
Running them separately just work (and both agree on the numbers).
Expected vs. actual behavior
perf stat and async profiler -e cycles --total shouldn't disagree if run in parallel
Reproduction Steps
# git clone https://github.com/franz1981/quarkus-profiling-workshop
# cd quarkus-profiling-workshop
# mvn clean package -DskipTests
# cd scripts
# ./benchmark.sh -e cycles -p -g -u time/cached
It should produce an output like
----- Install ap-loader -----
[jbang] A script with name 'ap-loader' already exists, use '--force' to install anyway.
----- Install Hyperfoil -----
[jbang] A script with name 'wrk2' already exists, use '--force' to install anyway.
[jbang] A script with name 'wrk' already exists, use '--force' to install anyway.
----- Benchmarking endpoint http://localhost:8080/time/cached
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2025-11-10 05:28:44,678 INFO [io.quarkus] (main) hello-world-app 1.0.0-SNAPSHOT on JVM (powered by Quarkus 3.29.0) started in 0.392s. Listening on: http://0.0.0.0:8080
2025-11-10 05:28:44,679 INFO [io.quarkus] (main) Profile prod activated.
2025-11-10 05:28:44,679 INFO [io.quarkus] (main) Installed features: [cdi, rest, rest-jackson, scheduler, smallrye-context-propagation, vertx]
----- Quarkus running at pid 215978 using 2 I/O threads
----- Start all-out test and profiling
----- Waiting 16 seconds before profiling for 20 seconds
Running 40s test @ http://localhost:8080/time/cached
2 threads and 10 connections
----- Starting async-profiler on quarkus application (215978)
----- Collecting perf stat on 215978
----- Showing stats for 16 seconds
Linux 6.17.6-300.fc43.x86_64 (fedora.fritz.box) 11/10/2025 _x86_64_ (32 CPU)
05:29:02 AM UID PID %usr %system %guest %wait %CPU CPU Command
05:29:03 AM 1000 215978 57.00 113.00 0.00 0.00 170.00 9 java
05:29:04 AM 1000 215978 51.00 95.00 0.00 0.00 146.00 9 java
05:29:05 AM 1000 215978 50.00 95.00 0.00 0.00 145.00 9 java
05:29:06 AM 1000 215978 51.00 96.00 0.00 0.00 147.00 9 java
05:29:07 AM 1000 215978 51.00 95.00 0.00 0.00 146.00 9 java
05:29:08 AM 1000 215978 51.00 96.00 0.00 0.00 147.00 9 java
05:29:09 AM 1000 215978 54.00 92.00 0.00 0.00 146.00 9 java
05:29:10 AM 1000 215978 52.00 94.00 0.00 0.00 146.00 9 java
05:29:11 AM 1000 215978 52.00 95.00 0.00 0.00 147.00 9 java
05:29:12 AM 1000 215978 52.00 94.00 0.00 0.00 146.00 9 java
05:29:13 AM 1000 215978 53.00 94.00 0.00 0.00 147.00 9 java
05:29:14 AM 1000 215978 51.00 95.00 0.00 0.00 146.00 9 java
05:29:15 AM 1000 215978 48.00 98.00 0.00 0.00 146.00 9 java
05:29:16 AM 1000 215978 56.00 91.00 0.00 0.00 147.00 9 java
05:29:17 AM 1000 215978 51.00 95.00 0.00 0.00 146.00 9 java
----- Stopped stats, waiting load to complete
Average: 1000 215978 52.00 95.87 0.00 0.00 147.87 - java
Performance counter stats for process id '215978':
40,236,050,879 task-clock # 2.012 CPUs utilized
10,282 context-switches # 255.542 /sec
2,214 cpu-migrations # 55.025 /sec
47,526 page-faults # 1.181 K/sec
202,055,500,111 instructions # 1.20 insn per cycle
# 0.37 stalled cycles per insn (62.75%)
167,974,986,839 cycles # 4.175 GHz (62.80%)
74,158,886,491 stalled-cycles-frontend # 44.15% frontend cycles idle (62.86%)
44,429,978,871 branches # 1.104 G/sec (62.89%)
1,801,125,001 branch-misses # 4.05% of all branches (62.95%)
68,919,441,368 L1-dcache-loads # 1.713 G/sec (62.85%)
6,080,016,322 L1-dcache-load-misses # 8.82% of all L1-dcache accesses (62.79%)
20.002513669 seconds time elapsed
Profiling for 20 seconds
Done
Thread Stats Avg Stdev Max +/- Stdev
Latency 53.11μs 28.06μs 5.80ms 90.07%
Req/Sec 173822.63 28459.32 191163.00 97.56
7126728 requests in 40.002s, 1.29GB read
Requests/sec: 178159.29
Transfer/sec: 32.96MB
----- Profiling and workload completed: killing server
cleaning up quarkus process
and the produced flamegraph html file in the scripts folder eg
251110_05_29_02_cycles.html
should reports a total number of cycles lower than what perf stat has reported.
Running again the same test, but disabling perf stat:
# ./benchmark.sh -e cycles -g -u time/cached
would produce, instead, a flamegraph with the right number of cycles as
251110_05_50_52_cycles.html
shows i.e. ~160 Gcycles
I've created a jbang script to perform some manual testing on something simpler:
writing:
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
public class VolatileSpin {
static volatile boolean running = true;
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: jbang VolatileSpin.java <numThreads>");
System.exit(1);
}
int numThreads = Integer.parseInt(args[0]);
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
threads[i] = new Thread(() -> {
while (running) {
// Tight loop reading volatile field
if (!running) break;
}
}, "spinner-" + i);
threads[i].start();
}
System.out.println("Started " + numThreads + " spinning threads.");
System.out.println("Press Enter to stop...");
System.in.read();
running = false;
for (Thread t : threads) t.join();
System.out.println("All threads stopped.");
}
}
and runnable it via
# jbang VolatileSpin.java 2
then, while it runs, in another console:
perf stat -d -p 392212 sleep 10 & ./asprof --total -e cycles -t -f with_perf.html -d 10 392212 & wait
and
./asprof --total -e cycles -t -f alone.html -d 10 392212 & wait
The behaviour should be the same as in the Quarkus profiling script.
Additional Information/Context
No response
Async-profiler version
4.2
Environment details
The machine is a Ryzen 7950X with Fedora 43.
I've disabled turbo boost, setting the performance governor on all cores forcing min/max frequencies to 4.3 GHz while disabling frequency scaling Activityfranz1981 commented on Nov 10, 2025 Funny thing: pidstat is totally drunk here and perf stat is right, so please ignore what pidstat says ^^ changed the title [-]perf stat and -e cycles --total disagree[/-] [+]perf stat and -e cycles --total in parallel, make them to disagree[/+] on Nov 10, 2025changed the title [-]perf stat and -e cycles --total in parallel, make them to disagree[/-] [+]Running perf stat and ap -e cycles --total in parallel, make them to disagree[/+] on Nov 10, 2025apangin commented on Nov 10, 2025 Thank you for the report. The problem is understood: it happens when multiple perf_events share the same PMU counter slot - in particular, when two or more profilers measure the same HW counter, cycles in your case.
In this case, multiplexing happens, and each profiler counts only a portion of all events.
perf_event can be configured to report time_enabled and time_running in addition to the counter value, so that profilers can scale the obtained number of samples to estimate the total value. perf uses these settings, but async-profiler does not.
We will fix this, but I can't give you an exact timeline. Reacted by Francesco Nigro franz1981 commented on Jan 20, 2026 @apangin this can still happen in case of oversubscription while using async profiler alone on an application with many context switches and more threads than actual core?
To be fair, what I am observing seems different:
- 4 core constrained application via taskset
- 4 busy platform threads (half are from the FJ pool w Loom)
- 10 seconds workload (causing +1M context/switches)
- doubled total cycles than what's physically possible
- under the same scenario Linux perf stat work as expected
apangin commented on Jan 20, 2026 @franz1981 No, I realized my previous comment was misleading, sorry about that. Multiplexing happens when more hardware events are measured at the same time than the number of available hardware counters (typically 7 on Intel CPUs: 3 fixed + 4 programmable). Async-profiler only uses 1 hardware counter at a time, so the number of reported events should be accurate no matter how many threads are running. franz1981 commented on Jan 20, 2026
so the number of reported events should be accurate no matter how many threads are running.
Got it, thanks Andrei!
I will try to narrow this issue down to the simplest/cleanest possible reproducer 🙏 apangin commented on Jan 20, 2026 What I hear is that you now observe the opposite of what you originally noticed: async-profiler now reports more events than it should (i.e. more cycles per core than the maximum CPU frequency), is that correct? This could be a different issue then.
For troubleshooting purpose, try --record-cpu option that groups samples by CPU where they have occurred. Reacted by Francesco Nigro franz1981 commented on Jan 20, 2026
This could be a different issue then
Exactly; I posted here, because the original complex code which led to fill this issue, once running the described "oversubscribed" scenario, was hitting this other new one.
I will debug a bit more and distill it in a new issue 🙏 franz1981 commented on Jan 20, 2026 Sorry @apangin for the previous comment: I've discovered what i made wrong..and is funny and stupid at the same time...
Instead of running the profiler as
$asprof -d 10s ....
my script was running it via
$asprof start -d 10s ....
and this was causing the profiler to have an extended duration of profiling (the whole test was 20 seconds, and the profiling should have captured the first 10) - that's why i was always observing twice the number of cycles 😢 apangin commented on Jan 20, 2026 Ah I see. Thanks for the heads up.
asprof should probably detect incorrect usage of -d and warn user. Noted. Reacted by Francesco Nigro apangin commented on Jan 23, 2026 Thanks @vish-chan for pushing the fix for the counter scaling issue.
@franz1981 Please let us know if this works as expected now. Reacted by Francesco Nigro Reacted by Francesco Nigro franz1981 commented on Jan 23, 2026 Any chance async profiler could report the scaling factor to understand how much trusty is? apangin commented on Jan 24, 2026
Any chance async profiler could report the scaling factor to understand how much trusty is?
To begin with, there is no a single number to measure this. Each (per-thread) perf counter has its own scaling factor which changes over time. Averaging these will result in as misleading value as any other statistical average. OK, let's say we get this number, where and how to display it? Furthermore, to compute this number, we'll likely add some constant overhead even when no multiplexing happens. To proceed with that, I'd like to understand how important it is. What are your thoughts? franz1981 commented on Feb 5, 2026
@franz1981 Please let us know if this works as expected now.
Yep now it works fine for me, thanks!
I'm having another problem while comparing context-switches with perf stat vs async profiler, but I'll open a separate issue for it!
Re
OK, let's say we get this number, where and how to display it?
I wasn't thinking to have a visual rep yet, but more the info in the JFR event(s) as additional data, similarly to raw perf events which contains additional values which could be used by some processing scripts to "estimate" which part of the data should be trusted more e.g. the ones which doesn't exhibit any scaling (or a lower factor) franz1981 commented on Feb 5, 2026 I think this issue could be closed @apangin wdyt? 🙏 apangin commented on Feb 5, 2026 Thanks for confirming. Yes, I'll close it.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|