perf bench: Add atomic operations benchmark
PATCH v2 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 configurable thread count and iteration count 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:
- 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 u64 counter, incrementing it from 0 to iterations.
- The shared counter is cache-line aligned (64 bytes) to isolate contention to the target cache line and avoid false sharing.
- The wall-clock time is measured as the max of all per-thread runtimes (the time for the slowest thread to finish).
- 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, iterations/thread: 100000000, repeats: 10 (warmup: 1)
Avg wall-clock time: 3286.297 msec (stddev 6.820 msec)
Total ops: 200,000,000
Throughput total: 60,858,769 ops/sec
Per-thread times and throughput (last repeat):
fastest: 3304.312 msec (30263489 ops/sec)
slowest: 3325.518 msec (30070504 ops/sec)
avg: 3314.915 msec (30166688 ops/sec)# Running atomic/inc benchmark...
Threads: 2, iterations/thread: 100000000, repeats: 10 (warmup: 1) Avg wall-clock time: 2170.716 msec (stddev 7.306 msec) Total ops: 200,000,000
Throughput total: 92,135,503 ops/sec
Per-thread times and throughput (last repeat):
fastest: 2196.458 msec (45527849 ops/sec)
slowest: 2208.764 msec (45274198 ops/sec)
avg: 2202.611 msec (45400669 ops/sec)Output fields explained:
- Threads: number of contending threads
- iterations/thread: atomic operations each thread performs
- repeats: number of test runs (first is warmup)
- Avg wall-clock time: mean time for all threads to complete
- stddev: standard deviation across repeats
- Total ops: threads x iterations/thread
- Throughput total: aggregate ops/sec across all threads
- Per-thread times: fastest/slowest/avg thread completion time
- Per-thread throughput: per-thread ops/sec (shows scheduling imbalance)
---
Changes since v1:
- Add an 'atomic inc' subcommand benchmarking contended fetch-add (__atomic_fetch_add), split the entry points into bench_atomic_cas() and bench_atomic_inc(), and document it.
- Hoist the plain load out of the compare-and-swap retry loop; the failed __atomic_compare_exchange_n already returns the current value via *expected, so the loop measures pure CAS throughput.
- Cast the seconds delta to u64 before multiplying by NSEC_PER_SEC to avoid 32-bit signed overflow in the runtime calculation.
--- tools/perf/Documentation/perf-bench.txt | 32 +++ tools/perf/bench/Build | 1 + tools/perf/bench/atomic.c | 268 ++++++++++++++++++++++++ tools/perf/bench/bench.h | 2 + tools/perf/builtin-bench.c | 9 + 5 files changed, 312 insertions(+) create mode 100644 tools/perf/bench/atomic.c
tools/perf/Documentation/perf-bench.txt +32 -0
@@ -58,6 +58,9 @@ SUBSYSTEM 'numa':: NUMA scheduling and MM benchmarks. +'atomic':: + Atomic operation benchmarks. + 'futex':: Futex stressing benchmarks.
@@ -283,6 +286,35 @@ 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). + +-i:: +--iterations=:: +Number of iterations per thread (default: 100000000). + +The per-thread fastest/slowest/avg summary is only printed when +running with more than one thread. + +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 +268 -0
268 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 reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] perf: 64-bit atomic builtins on u64 cause link errors on 32-bit - [Low] perf: Missing explicit…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] perf: 64-bit atomic builtins on u64 cause link errors on 32-bit
- [Low] perf: Missing explicit inclusion of <stdint.h> for UINT64_MAX
--
commit 1f5903d48b4529edce94275c5756879218402332
Author: Changbin Du <[email protected]>
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.
Quoted 5 lines
tools/perf/bench/atomic.c new file +0
-0
[ ... ]
Quoted 5 lines
tools/perf/bench/atomic.c new file +5
-0+static void *worker (void *arg) +{ +struct worker_stats *ws = (struct worker_stats *)arg; +struct timespec tstart, tend; +u64 i;
tools/perf/bench/atomic.c line 92
Quoted 10 lines
tools/perf/bench/atomic.c new file +10
-0+ +pthread_barrier_wait (&start_barrier); + +clock_gettime (CLOCK_MONOTONIC , &tstart); +switch (bench_op) { +case OP_CAS : +for (i =0 ; i < iterations; i++) { +u64 old_val, new_val; + + old_val =__atomic_load_n (&shared_counter, __ATOMIC_RELAXED);
[Severity: High]
Does the use of 64-bit atomic builtins on a u64 variable in worker() in tools/perf/bench/atomic.c break compilation on 32-bit architectures?
Without 32-bit fallback definitions or omitting 64-bit atomics entirely, calls like __atomic_load_n, __atomic_compare_exchange_n, and __atomic_fetch_add on the shared_counter might generate unresolved __atomic_*_8 library calls on 32-bit systems.
Quoted 16 lines
tools/perf/bench/atomic.c new file +16
-0+do { + new_val = old_val +1 ; + }while (!__atomic_compare_exchange_n (&shared_counter, + &old_val, new_val, + false, + __ATOMIC_RELAXED, + __ATOMIC_RELAXED)); + } +break ; +case OP_INC : +for (i =0 ; i < iterations; i++) +__atomic_fetch_add (&shared_counter,1 , __ATOMIC_RELAXED); +break ; +default : +break ; + }
[ ... ]
Quoted 3 lines
tools/perf/bench/atomic.c new file +3
-0+static void print_default_format (struct worker_stats *wstats, +struct stats *time_stats) +{
[ ... ]
tools/perf/bench/atomic.c line 142
Quoted 6 lines
tools/perf/bench/atomic.c new file +6
-0+/* +* Per-thread statistics from the last measured repeat show +* fairness / imbalance in CPU scheduling. +*/ +if (threads >1 ) { +u64 min_ns =UINT64_MAX , max_ns =0 , total_ns =0 ;
[Severity: Low]
Does the usage of UINT64_MAX in print_default_format() in tools/perf/bench/atomic.c require the explicit inclusion of <stdint.h>?
This might violate strict musl compatibility header guidelines, which require explicitly including <stdint.h> for this constant.
Quoted 2 lines
tools/perf/bench/atomic.c new file +2
-0+double min_thru, max_thru, avg_thru; +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerQuoted 39 lines · On Wed, Oct 07, 2026 at 05:39:22AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] perf: 64-bit atomic builtins on u64 cause link errors on 32-bit
- [Low] perf: Missing explicit inclusion of <stdint.h> forUINT64_MAX
--commit
1f5903d48b4529edce94275c5756879218402332
Author: Changbin Du <[email protected]>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.
Quoted 5 lines
tools/perf/bench/atomic.c new file +0
-0[ ... ]
Quoted 5 lines
tools/perf/bench/atomic.c new file +5
-0+static void *worker (void *arg) +{ +struct worker_stats *ws = (struct worker_stats *)arg; +struct timespec tstart, tend; +u64 i;tools/perf/bench/atomic.c line 92
Quoted 10 lines
tools/perf/bench/atomic.c new file +10
-0+ +pthread_barrier_wait (&start_barrier); + +clock_gettime (CLOCK_MONOTONIC , &tstart); +switch (bench_op) { +case OP_CAS : +for (i =0 ; i < iterations; i++) { +u64 old_val, new_val; + + old_val =__atomic_load_n (&shared_counter, __ATOMIC_RELAXED);[Severity: High]
Does the use of 64-bit atomic builtins on a u64 variable inworker()intools/perf/bench/atomic.cbreak compilation on 32-bit architectures?
Quoted 4 lines
Without 32-bit fallback definitions or omitting 64-bit atomics entirely, calls like
__atomic_load_n,__atomic_compare_exchange_n, and__atomic_fetch_addon theshared_countermight generate unresolved __atomic_*_8 library calls on 32-bit systems.
Probably it's ok to go with unsigned long.
Thanks,
Namhyung
Quoted 41 lines
Quoted 17 lines
tools/perf/bench/atomic.c new file +16
-0+do { + new_val = old_val +1 ; + }while (!__atomic_compare_exchange_n (&shared_counter, + &old_val, new_val, + false, + __ATOMIC_RELAXED, + __ATOMIC_RELAXED)); + } +break ; +case OP_INC : +for (i =0 ; i < iterations; i++) +__atomic_fetch_add (&shared_counter,1 , __ATOMIC_RELAXED); +break ; +default : +break ; + }[ ... ]
Quoted 3 lines
tools/perf/bench/atomic.c new file +3
-0+static void print_default_format (struct worker_stats *wstats, +struct stats *time_stats) +{[ ... ]
tools/perf/bench/atomic.c line 142
Quoted 6 lines
tools/perf/bench/atomic.c new file +6
-0+/* +* Per-thread statistics from the last measured repeat show +* fairness / imbalance in CPU scheduling. +*/ +if (threads >1 ) { +u64 min_ns =UINT64_MAX , max_ns =0 , total_ns =0 ;[Severity: Low]
Does the usage ofUINT64_MAXinprint_default_format()intools/perf/bench/atomic.crequire the explicit inclusion of <stdint.h>?This might violate strict musl compatibility header guidelines, which require explicitly including <
stdint.h> for this constant.Quoted 2 lines
tools/perf/bench/atomic.c new file +2
-0+double min_thru, max_thru, avg_thru; +Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 40 lines · Namhyung Kim
Quoted 39 lines · On Wed, Oct 07, 2026 at 05:39:22AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] perf: 64-bit atomic builtins on u64 cause link errors on 32-bit
- [Low] perf: Missing explicit inclusion of <stdint.h> forUINT64_MAX
--commit
1f5903d48b4529edce94275c5756879218402332
Author: Changbin Du <[email protected]>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.
Quoted 5 lines
tools/perf/bench/atomic.c new file +0
-0[ ... ]
Quoted 5 lines
tools/perf/bench/atomic.c new file +5
-0+static void *worker (void *arg) +{ +struct worker_stats *ws = (struct worker_stats *)arg; +struct timespec tstart, tend; +u64 i;tools/perf/bench/atomic.c line 92
Quoted 10 lines
tools/perf/bench/atomic.c new file +10
-0+ +pthread_barrier_wait (&start_barrier); + +clock_gettime (CLOCK_MONOTONIC , &tstart); +switch (bench_op) { +case OP_CAS : +for (i =0 ; i < iterations; i++) { +u64 old_val, new_val; + + old_val =__atomic_load_n (&shared_counter, __ATOMIC_RELAXED);[Severity: High]
Does the use of 64-bit atomic builtins on a u64 variable inworker()intools/perf/bench/atomic.cbreak compilation on 32-bit architectures?
Quoted 7 lines
Quoted 4 lines
Without 32-bit fallback definitions or omitting 64-bit atomics entirely, calls like
__atomic_load_n,__atomic_compare_exchange_n, and__atomic_fetch_addon theshared_countermight generate unresolved __atomic_*_8 library calls on 32-bit systems.Probably it's ok to go with unsigned long.
Yes, just fixed it by using the native word size for the shared counter — unsigned long instead of u64.
Quoted 44 lines
Thanks,
NamhyungQuoted 41 lines
Quoted 17 lines
tools/perf/bench/atomic.c new file +16
-0+do { + new_val = old_val +1 ; + }while (!__atomic_compare_exchange_n (&shared_counter, + &old_val, new_val, + false, + __ATOMIC_RELAXED, + __ATOMIC_RELAXED)); + } +break ; +case OP_INC : +for (i =0 ; i < iterations; i++) +__atomic_fetch_add (&shared_counter,1 , __ATOMIC_RELAXED); +break ; +default : +break ; + }[ ... ]
Quoted 3 lines
tools/perf/bench/atomic.c new file +3
-0+static void print_default_format (struct worker_stats *wstats, +struct stats *time_stats) +{[ ... ]
tools/perf/bench/atomic.c line 142
Quoted 6 lines
tools/perf/bench/atomic.c new file +6
-0+/* +* Per-thread statistics from the last measured repeat show +* fairness / imbalance in CPU scheduling. +*/ +if (threads >1 ) { +u64 min_ns =UINT64_MAX , max_ns =0 , total_ns =0 ;[Severity: Low]
Does the usage ofUINT64_MAXinprint_default_format()intools/perf/bench/atomic.crequire the explicit inclusion of <stdint.h>?This might violate strict musl compatibility header guidelines, which require explicitly including <
stdint.h> for this constant.Quoted 2 lines
tools/perf/bench/atomic.c new file +2
-0+double min_thru, max_thru, avg_thru; +Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Signature · 2 lines
Cheers, Changbin Du