summaryrefslogtreecommitdiff
path: root/tools/perf/bench/atomic.c
blob: 8cad20c7e802cc79a48c56b6e660d1f4b56f726e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2026 Changbin Du <changbin.du@gmail.com>
 *
 * Benchmarks for atomic read-modify-write operations.
 *
 * Measures throughput (ops/sec) of contended atomic operations on a
 * single shared counter across multiple threads. Each thread performs
 * a hot loop of the selected operation -- compare-and-swap (cas) or
 * fetch-add (inc) -- competing with all other threads for the same
 * cache line. Threads run for a fixed runtime, so a system that
 * cannot make progress under heavy contention reports a low (or
 * zero) throughput instead of hanging.
 */
#include "bench.h"
#include <linux/compiler.h>
#include "../util/debug.h"

#ifndef HAVE_PTHREAD_BARRIER
int bench_atomic_cas(int argc __maybe_unused, const char **argv __maybe_unused)
{
	pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
	return 0;
}

int bench_atomic_inc(int argc __maybe_unused, const char **argv __maybe_unused)
{
	pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
	return 0;
}
#else /* HAVE_PTHREAD_BARRIER */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <inttypes.h>
#include <err.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/time64.h>
#include <perf/cpumap.h>
#include <subcmd/parse-options.h>
#include "../util/stat.h"

static unsigned int threads;		/* 0: default to min(2, online CPUs) */
static unsigned int runtime	= 2;	/* seconds per repeat */

static const struct option options[] = {
	OPT_UINTEGER('T', "threads", &threads,
		     "Number of contending threads (default: 2, or 1 on single CPU)"),
	OPT_UINTEGER('R', "runtime", &runtime,
		     "Runtime of each repeat in seconds (default: 2)"),
	OPT_END()
};

static const char * const cas_usage[] = {
	"perf bench atomic cas <options>",
	NULL
};

static const char * const inc_usage[] = {
	"perf bench atomic inc <options>",
	NULL
};

enum atomic_op {
	OP_CAS,
	OP_INC,
};

static int bench_op = OP_CAS;

/*
 * The termination flag is polled between batches only, so that the
 * check does not perturb the measured loop.
 */
#define BATCH_OPS	1024

static unsigned long shared_counter __aligned(64);
static pthread_barrier_t start_barrier __aligned(64);
static bool done;

struct worker_stats {
	u64	runtime_ns;
	u64	ops;
};

static void cas_batch(void)
{
	unsigned long i;

	/*
	 * Each iteration counts as one successful compare-and-swap;
	 * retries and the loads between them are not counted.
	 */
	for (i = 0; i < BATCH_OPS; i++) {
		unsigned long old_val, new_val;

		old_val = __atomic_load_n(&shared_counter, __ATOMIC_RELAXED);
		do {
			new_val = old_val + 1;
		} while (!__atomic_compare_exchange_n(&shared_counter, &old_val,
						      new_val, false,
						      __ATOMIC_RELAXED,
						      __ATOMIC_RELAXED));
	}
}

static void inc_batch(void)
{
	unsigned long i;

	for (i = 0; i < BATCH_OPS; i++)
		__atomic_fetch_add(&shared_counter, 1, __ATOMIC_RELAXED);
}

static void *worker(void *arg)
{
	struct worker_stats *ws = (struct worker_stats *)arg;
	struct timespec tstart, tend;
	u64 ops = 0;

	pthread_barrier_wait(&start_barrier);

	clock_gettime(CLOCK_MONOTONIC, &tstart);
	while (!__atomic_load_n(&done, __ATOMIC_RELAXED)) {
		switch (bench_op) {
		case OP_CAS:
			cas_batch();
			break;
		case OP_INC:
			inc_batch();
			break;
		default:
			BUG_ON(1);
			break;
		}
		ops += BATCH_OPS;
	}
	clock_gettime(CLOCK_MONOTONIC, &tend);

	ws->runtime_ns	= (u64)(tend.tv_sec - tstart.tv_sec) * NSEC_PER_SEC +
			  (tend.tv_nsec - tstart.tv_nsec);
	ws->ops		= ops;

	return NULL;
}

static void print_default_format(struct worker_stats *wstats,
				 struct stats *throughput_stats)
{
	double thru_avg, thru_stddev;

	printf("  Threads: %u, runtime: %u sec, repeats: %u (warmup: 1)\n",
	       threads, runtime, bench_repeat);

	thru_avg    = avg_stats(throughput_stats);
	thru_stddev = stddev_stats(throughput_stats);

	printf("  Avg throughput: %'.0f ops/sec (+- %.2f%%)\n",
	       thru_avg, rel_stddev_stats(thru_stddev, thru_avg));

	/*
	 * Per-thread operation counts from the last measured repeat show
	 * whether any thread failed to make progress under contention.
	 */
	if (threads > 1) {
		u64 min_ops = UINT64_MAX, max_ops = 0, total_ops = 0;

		for (unsigned int t = 0; t < threads; t++) {
			if (wstats[t].ops < min_ops)
				min_ops = wstats[t].ops;
			if (wstats[t].ops > max_ops)
				max_ops = wstats[t].ops;
			total_ops += wstats[t].ops;
		}

		printf("  Per-thread ops (last repeat):\n");
		printf("    fastest:  %'" PRIu64 " ops\n", max_ops);
		printf("    slowest:  %'" PRIu64 " ops\n", min_ops);
		printf("    avg:      %'" PRIu64 " ops\n", total_ops / threads);
	}
}

static int run_benchmark(void)
{
	pthread_t *thread_ids;
	struct worker_stats *wstats;
	struct stats throughput_stats;
	unsigned int r, t;

	thread_ids = calloc(threads, sizeof(pthread_t));
	if (!thread_ids)
		err(EXIT_FAILURE, "calloc");

	wstats = calloc(threads, sizeof(struct worker_stats));
	if (!wstats)
		err(EXIT_FAILURE, "calloc");

	init_stats(&throughput_stats);

	for (r = 0; r < bench_repeat + 1; r++) {
		u64 max_runtime_ns = 0, total_ops = 0;
		double throughput;

		shared_counter = 0;
		__atomic_store_n(&done, false, __ATOMIC_RELAXED);
		pthread_barrier_init(&start_barrier, NULL, threads);

		for (t = 0; t < threads; t++) {
			if (pthread_create(&thread_ids[t], NULL,
					   worker, &wstats[t]))
				err(EXIT_FAILURE, "pthread_create");
		}

		sleep(runtime);
		__atomic_store_n(&done, true, __ATOMIC_RELAXED);

		for (t = 0; t < threads; t++)
			pthread_join(thread_ids[t], NULL);

		pthread_barrier_destroy(&start_barrier);

		for (t = 0; t < threads; t++) {
			total_ops += wstats[t].ops;
			if (wstats[t].runtime_ns > max_runtime_ns)
				max_runtime_ns = wstats[t].runtime_ns;
		}

		/*
		 * Exclude the first repeat (warmup) from statistics so
		 * that cache-cold and lazy-init effects are not counted.
		 */
		if (r > 0 && max_runtime_ns) {
			throughput = (double)total_ops /
				     ((double)max_runtime_ns / NSEC_PER_SEC);
			update_stats(&throughput_stats, (u64)throughput);
		}
	}

	switch (bench_format) {
	case BENCH_FORMAT_DEFAULT:
		print_default_format(wstats, &throughput_stats);
		break;

	case BENCH_FORMAT_SIMPLE:
		printf("%.0f\n", avg_stats(&throughput_stats));
		break;

	default:
		fprintf(stderr, "Unknown format: %d\n", bench_format);
		exit(EXIT_FAILURE);
	}

	free(thread_ids);
	free(wstats);

	return 0;
}

static int run_atomic_bench(int argc, const char **argv,
			    const char * const *usage)
{
	struct perf_cpu_map *cpu;

	if (parse_options(argc, argv, options, usage, 0)) {
		usage_with_options(usage, options);
		exit(EXIT_FAILURE);
	}

	if (!threads) {	/* default to 2 threads (1 on a single-CPU machine) */
		cpu = perf_cpu_map__new_online_cpus();
		threads = (cpu && perf_cpu_map__nr(cpu) > 1) ? 2 : 1;
		perf_cpu_map__put(cpu);
	}

	if (runtime < 1) {
		fprintf(stderr, "Invalid runtime: %u\n", runtime);
		return 1;
	}

	return run_benchmark();
}

int bench_atomic_cas(int argc, const char **argv)
{
	bench_op = OP_CAS;
	return run_atomic_bench(argc, argv, cas_usage);
}

int bench_atomic_inc(int argc, const char **argv)
{
	bench_op = OP_INC;
	return run_atomic_bench(argc, argv, inc_usage);
}
#endif /* HAVE_PTHREAD_BARRIER */