perf bench: Add atomic operations benchmark
PATCH v5 1/1 perf bench: Add atomic operations benchmark
Add a new 'atomic' collection to perf bench for benchmarking atomic read-modify-write operations with multi-threaded contention testing. It provides two subcommands:
- atomic cas: contended compare-and-swap operations (__atomic_compare_exchange_n)
- atomic inc: contended fetch-add operations (__atomic_fetch_add)
Both support a configurable thread count and runtime to measure atomic contention effects.
Why this benchmark is needed:
- Atomic read-modify-write operations are fundamental to lock-free algorithms and data structures. Understanding their performance characteristics under contention is critical for designing high-performance concurrent applications.
- The benchmark helps identify atomic operation latency and scalability issues across different thread counts, revealing contention patterns that are not visible in single-threaded tests.
- Useful for evaluating atomic implementation quality on different architectures and for regression testing after changes to atomic primitives or memory ordering.
Measurement methodology:
- By default two threads contend for the counter, enough to expose the contention effect; use -t to scale the count.
- Each thread starts a private timer (clock_gettime CLOCK_MONOTONIC) after synchronizing on a pthread_barrier, ensuring all threads begin simultaneously.
- Each thread performs a hot loop of the selected atomic operation (compare-and-swap or fetch-add) on a shared unsigned long counter for a fixed runtime. It counts its own completed operations in a local variable, and the loop runs in batches so the termination flag is only polled between batches.
- The main thread sleeps for the runtime and then sets the flag; the throughput of a repeat is the total operation count divided by the measured wall time (the max per-thread runtime).
- The shared counter is cache-line aligned (64 bytes) to isolate contention to the target cache line and avoid false sharing.
- The per-thread operation counts of the last repeat show whether any thread failed to make progress under contention.
- The first repeat is excluded from statistics as a warmup phase to avoid cache-cold effects.
Example usage:
$ perf bench atomic all
# Running atomic/cas benchmark...
Threads: 2, runtime: 2 sec, repeats: 10 (warmup: 1)
Avg throughput: 68,599,954 ops/sec (+- 0.13%)
Per-thread ops (last repeat):
fastest: 74,842,112 ops
slowest: 63,176,704 ops
avg: 69,009,408 ops# Running atomic/inc benchmark...
Threads: 2, runtime: 2 sec, repeats: 10 (warmup: 1)
Avg throughput: 128,428,772 ops/sec (+- 0.54%)
Per-thread ops (last repeat):
fastest: 146,230,272 ops
slowest: 112,825,344 ops
avg: 129,527,808 opsOutput fields explained:
- Threads: number of contending threads
- runtime: seconds each thread runs per repeat
- repeats: number of test runs (first is warmup)
- Avg throughput: mean aggregate ops/sec across repeats
- +- %: relative standard error of the mean throughput across repeats
- Per-thread ops: fastest/slowest/avg operations per thread in the last repeat (shows scheduling imbalance and lack of progress)
---
Changes in v5:
- Use -T/--threads and -R/--runtime so the subcommand options do not shadow perf bench's -r/--repeat option.
Changes in v4:
- Default to 2 threads (1 on a single-CPU machine).
- Replace the iteration count with a fixed runtime per repeat.
- Report per-thread operation counts to show lack of progress.
Changes in v3:
- Use native word size (unsigned long) for the shared counter instead of u64 to avoid libatomic on 32-bit targets.
- Include <stdint.h> explicitly for UINT64_MAX.
Changes in v2:
- Add an 'atomic inc' subcommand for contended __atomic_fetch_add.
- Hoist the load out of the CAS retry loop; the failed __atomic_compare_exchange_n already returns the current value.
- Cast the seconds delta to u64 before multiplying by NSEC_PER_SEC to avoid 32-bit overflow.
--- tools/perf/Documentation/perf-bench.txt | 38 +++ tools/perf/bench/Build | 1 + tools/perf/bench/atomic.c | 299 ++++++++++++++++++++++++ tools/perf/bench/bench.h | 2 + tools/perf/builtin-bench.c | 9 + 5 files changed, 349 insertions(+) create mode 100644 tools/perf/bench/atomic.c
tools/perf/Documentation/perf-bench.txt +38 -0
@@ -58,6 +58,9 @@ SUBSYSTEM 'numa':: NUMA scheduling and MM benchmarks. +'atomic':: + Atomic operation benchmarks. + 'futex':: Futex stressing benchmarks.
@@ -283,6 +286,41 @@ SUITES FOR 'numa' *mem*:: Suite for evaluating NUMA workloads. +SUITES FOR 'atomic' +~~~~~~~~~~~~~~~~~~~ +*cas*:: +Suite for evaluating compare-and-swap (CAS) atomic operations under +multi-threaded contention. + +*inc*:: +Suite for evaluating atomic fetch-add (increment) operations under +multi-threaded contention. + +Options of *cas* and *inc* +^^^^^^^^^^^^^^^^^^^^^^^^^^ +-T:: +--threads=:: +Number of threads contending for the shared counter (default: 2, or +1 on a single-CPU machine). + +-R:: +--runtime=:: +Runtime of each repeat in seconds (default: 2). + +The per-thread fastest/slowest/avg ops summary is only printed when +running with more than one thread. + +The average throughput is annotated with its relative deviation +("+- x%"), i.e. the standard error of the mean over the measured +repeats. A small value means the average is reproducible; it does not +reflect per-thread fairness, which the per-thread ops summary shows. + +Note that the cas benchmark counts successful compare-and-swap +operations only; retries and the loads between them are additional +atomic instructions not included in the ops/sec count, so cas and +inc throughputs are not directly comparable at the instruction +level. + SUITES FOR 'futex' ~~~~~~~~~~~~~~~~~~ *hash*::
tools/perf/bench/Build +1 -0
@@ -3,6 +3,7 @@ perf-bench-y += sched-pipe.o perf-bench-y += sched-seccomp-notify.o perf-bench-y += syscall.o perf-bench-y += mem-functions.o +perf-bench-y += atomic.o perf-bench-y += futex.o perf-bench-y += futex-hash.o perf-bench-y += futex-wake.o
tools/perf/bench/atomic.c new file +299 -0
299 lines not on this page · raw message
tools/perf/bench/bench.h +2 -0
@@ -30,6 +30,8 @@ int bench_mem_memcpy(int argc, const char **argv);int bench_mem_memset (int argc,const char **argv);int bench_mem_mmap (int argc,const char **argv);int bench_mem_find_bit (int argc,const char **argv); +int bench_atomic_cas (int argc,const char **argv); +int bench_atomic_inc (int argc,const char **argv);int bench_futex_hash (int argc,const char **argv);int bench_futex_wake (int argc,const char **argv);int bench_futex_wake_parallel (int argc,const char **argv);
tools/perf/builtin-bench.c +9 -0
@@ -14,6 +14,7 @@* syscall ... System call performance * mem ... memory access performance * numa ... NUMA scheduling and MM performance +* atomic ... Atomic operation performance * futex ... Futex performance * epoll ... Event poll performance */
@@ -115,6 +116,13 @@ static const struct bench uprobe_benchmarks[] = { { NULL, NULL, NULL }, }; +static const struct bench atomic_benchmarks[] = { + {"cas" ,"Benchmark CAS (compare-and-swap) operations" , bench_atomic_cas }, + {"inc" ,"Benchmark atomic fetch-add operations" , bench_atomic_inc }, + {"all" ,"Run all atomic benchmarks" ,NULL }, + {NULL ,NULL ,NULL } +}; + struct collection { const char *name; const char *summary;
@@ -128,6 +136,7 @@ static const struct collection collections[] = { #ifdef HAVE_LIBNUMA_SUPPORT { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks }, #endif + { "atomic", "Atomic operation benchmarks", atomic_benchmarks }, {"futex", "Futex stressing benchmarks", futex_benchmarks }, #ifdef HAVE_EVENTFD_SUPPORT {"epoll", "Epoll stressing benchmarks", epoll_benchmarks },
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines
Add a new 'atomic' collection to perf bench for benchmarking atomic read-modify-write operations with multi-threaded contention testing. It provides two subcommands:
- atomic cas: contended compare-and-swap operations (
__atomic_compare_exchange_n)
- atomic inc: contended fetch-add operations (__atomic_fetch_add)Both support a configurable thread count and runtime to measure atomic contention effects.
Why this benchmark is needed:
- Atomic read-modify-write operations are fundamental to lock-free algorithms and data structures. Understanding their performance characteristics under contention is critical for designing
[ ... ]
- Per-thread ops: fastest/slowest/avg operations per thread in the last repeat (shows scheduling imbalance and lack of progress)
Quoted 2 lines
Assisted-by opencode:DeepSeek-V4-ProSigned-off-by Changbin Du <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 71 lines · Changbin Du
Add a new 'atomic' collection to perf bench for benchmarking atomic read-modify-write operations with multi-threaded contention testing. It provides two subcommands:
- atomic cas: contended compare-and-swap operations (
__atomic_compare_exchange_n)
- atomic inc: contended fetch-add operations (__atomic_fetch_add)Both support a configurable thread count and runtime to measure atomic contention effects.
Why this benchmark is needed:
- Atomic read-modify-write operations are fundamental to lock-free algorithms and data structures. Understanding their performance characteristics under contention is critical for designing high-performance concurrent applications.
- The benchmark helps identify atomic operation latency and scalability issues across different thread counts, revealing contention patterns that are not visible in single-threaded tests.
- Useful for evaluating atomic implementation quality on different architectures and for regression testing after changes to atomic primitives or memory ordering.Measurement methodology:
- By default two threads contend for the counter, enough to expose the contention effect; use -t to scale the count.
- Each thread starts a private timer (clock_gettimeCLOCK_MONOTONIC) after synchronizing on apthread_barrier, ensuring all threads begin simultaneously.
- Each thread performs a hot loop of the selected atomic operation (compare-and-swap or fetch-add) on a shared unsigned long counter for a fixed runtime. It counts its own completed operations in a local variable, and the loop runs in batches so the termination flag is only polled between batches.
- The main thread sleeps for the runtime and then sets the flag; the throughput of a repeat is the total operation count divided by the measured wall time (the max per-thread runtime).
- The shared counter is cache-line aligned (64 bytes) to isolate contention to the target cache line and avoid false sharing.
- The per-thread operation counts of the last repeat show whether any thread failed to make progress under contention.
- The first repeat is excluded from statistics as a warmup phase to avoid cache-cold effects.Example usage:
$ perf bench atomic all # Running atomic/cas benchmark... Threads: 2, runtime: 2 sec, repeats: 10 (warmup: 1) Avg throughput: 68,599,954 ops/sec (+- 0.13%) Per-thread ops (last repeat): fastest: 74,842,112 ops slowest: 63,176,704 ops avg: 69,009,408 ops# Running atomic/inc benchmark...
Threads: 2, runtime: 2 sec, repeats: 10 (warmup: 1) Avg throughput: 128,428,772 ops/sec (+- 0.54%) Per-thread ops (last repeat): fastest: 146,230,272 ops slowest: 112,825,344 ops avg: 129,527,808 opsOutput fields explained:
- Threads: number of contending threads
- runtime: seconds each thread runs per repeat
- repeats: number of test runs (first is warmup)
- Avg throughput: mean aggregate ops/sec across repeats
- +- %: relative standard error of the mean throughput across repeats
- Per-thread ops: fastest/slowest/avg operations per thread in the last repeat (shows scheduling imbalance and lack of progress)
Quoted 2 lines
Assisted-by opencode:DeepSeek-V4-ProSigned-off-by Changbin Du <[email protected]>
Thanks,
Namhyung
Quoted 2 lines · Namhyung Kim
Quoted 1 line · Changbin Du
Assisted-by opencode:DeepSeek-V4-Pro
Just a nit, the recommendation is for:
And even that will be removed, i.e. no need for an Assisted-by: LLM tag as per what was discussed in Prague recently.
Quoted 3 lines
Quoted 1 line
Signed-off-by Changbin Du <[email protected]>Reviewed-by Namhyung Kim <[email protected]>
Thanks, applied to perf-tools-next, for v7.4.
- Arnaldo