aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c
blob: 43f109d9313029c4e68afc166fee1f075f7739e9 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */

#include <argp.h>

#include <sys/prctl.h>
#include "local_storage_rcu_tasks_trace_bench.skel.h"
#include "bench.h"

#include <signal.h>

static struct {
	__u32 nr_procs;
	__u32 kthread_pid;
	bool quiet;
} args = {
	.nr_procs = 1000,
	.kthread_pid = 0,
	.quiet = false,
};

enum {
	ARG_NR_PROCS = 7000,
	ARG_KTHREAD_PID = 7001,
	ARG_QUIET = 7002,
};

static const struct argp_option opts[] = {
	{ "nr_procs", ARG_NR_PROCS, "NR_PROCS", 0,
		"Set number of user processes to spin up"},
	{ "kthread_pid", ARG_KTHREAD_PID, "PID", 0,
		"Pid of rcu_tasks_trace kthread for ticks tracking"},
	{ "quiet", ARG_QUIET, "{0,1}", 0,
		"If true, don't report progress"},
	{},
};

static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
	long ret;

	switch (key) {
	case ARG_NR_PROCS:
		ret = strtol(arg, NULL, 10);
		if (ret < 1 || ret > UINT_MAX) {
			fprintf(stderr, "invalid nr_procs\n");
			argp_usage(state);
		}
		args.nr_procs = ret;
		break;
	case ARG_KTHREAD_PID:
		ret = strtol(arg, NULL, 10);
		if (ret < 1) {
			fprintf(stderr, "invalid kthread_pid\n");
			argp_usage(state);
		}
		args.kthread_pid = ret;
		break;
	case ARG_QUIET:
		ret = strtol(arg, NULL, 10);
		if (ret < 0 || ret > 1) {
			fprintf(stderr, "invalid quiet %ld\n", ret);
			argp_usage(state);
		}
		args.quiet = ret;
		break;
break;
	default:
		return ARGP_ERR_UNKNOWN;
	}

	return 0;
}

const struct argp bench_local_storage_rcu_tasks_trace_argp = {
	.options = opts,
	.parser = parse_arg,
};

#define MAX_SLEEP_PROCS 150000

static void validate(void)
{
	if (env.producer_cnt != 1) {
		fprintf(stderr, "benchmark doesn't support multi-producer!\n");
		exit(1);
	}
	if (env.consumer_cnt != 1) {
		fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
		exit(1);
	}

	if (args.nr_procs > MAX_SLEEP_PROCS) {
		fprintf(stderr, "benchmark supports up to %u sleeper procs!\n",
			MAX_SLEEP_PROCS);
		exit(1);
	}
}

static long kthread_pid_ticks(void)
{
	char procfs_path[100];
	long stime;
	FILE *f;

	if (!args.kthread_pid)
		return -1;

	sprintf(procfs_path, "/proc/%u/stat", args.kthread_pid);
	f = fopen(procfs_path, "r");
	if (!f) {
		fprintf(stderr, "couldn't open %s, exiting\n", procfs_path);
		goto err_out;
	}
	if (fscanf(f, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %ld", &stime) != 1) {
		fprintf(stderr, "fscanf of %s failed, exiting\n", procfs_path);
		goto err_out;
	}
	fclose(f);
	return stime;

err_out:
	if (f)
		fclose(f);
	exit(1);
	return 0;
}

static struct {
	struct local_storage_rcu_tasks_trace_bench *skel;
	long prev_kthread_stime;
} ctx;

static void sleep_and_loop(void)
{
	while (true) {
		sleep(rand() % 4);
		syscall(__NR_getpgid);
	}
}

static void local_storage_tasks_trace_setup(void)
{
	int i, err, forkret, runner_pid;

	runner_pid = getpid();

	for (i = 0; i < args.nr_procs; i++) {
		forkret = fork();
		if (forkret < 0) {
			fprintf(stderr, "Error forking sleeper proc %u of %u, exiting\n", i,
				args.nr_procs);
			goto err_out;
		}

		if (!forkret) {
			err = prctl(PR_SET_PDEATHSIG, SIGKILL);
			if (err < 0) {
				fprintf(stderr, "prctl failed with err %d, exiting\n", errno);
				goto err_out;
			}

			if (getppid() != runner_pid) {
				fprintf(stderr, "Runner died while spinning up procs, exiting\n");
				goto err_out;
			}
			sleep_and_loop();
		}
	}
	printf("Spun up %u procs (our pid %d)\n", args.nr_procs, runner_pid);

	setup_libbpf();

	ctx.skel = local_storage_rcu_tasks_trace_bench__open_and_load();
	if (!ctx.skel) {
		fprintf(stderr, "Error doing open_and_load, exiting\n");
		goto err_out;
	}

	ctx.prev_kthread_stime = kthread_pid_ticks();

	if (!bpf_program__attach(ctx.skel->progs.get_local)) {
		fprintf(stderr, "Error attaching bpf program\n");
		goto err_out;
	}

	if (!bpf_program__attach(ctx.skel->progs.pregp_step)) {
		fprintf(stderr, "Error attaching bpf program\n");
		goto err_out;
	}

	if (!bpf_program__attach(ctx.skel->progs.postgp)) {
		fprintf(stderr, "Error attaching bpf program\n");
		goto err_out;
	}

	return;
err_out:
	exit(1);
}

static void measure(struct bench_res *res)
{
	long ticks;

	res->gp_ct = atomic_swap(&ctx.skel->bss->gp_hits, 0);
	res->gp_ns = atomic_swap(&ctx.skel->bss->gp_times, 0);
	ticks = kthread_pid_ticks();
	res->stime = ticks - ctx.prev_kthread_stime;
	ctx.prev_kthread_stime = ticks;
}

static void *consumer(void *input)
{
	return NULL;
}

static void *producer(void *input)
{
	while (true)
		syscall(__NR_getpgid);
	return NULL;
}

static void report_progress(int iter, struct bench_res *res, long delta_ns)
{
	if (ctx.skel->bss->unexpected) {
		fprintf(stderr, "Error: Unexpected order of bpf prog calls (postgp after pregp).");
		fprintf(stderr, "Data can't be trusted, exiting\n");
		exit(1);
	}

	if (args.quiet)
		return;

	printf("Iter %d\t avg tasks_trace grace period latency\t%lf ns\n",
	       iter, res->gp_ns / (double)res->gp_ct);
	printf("Iter %d\t avg ticks per tasks_trace grace period\t%lf\n",
	       iter, res->stime / (double)res->gp_ct);
}

static void report_final(struct bench_res res[], int res_cnt)
{
	struct basic_stats gp_stat;

	grace_period_latency_basic_stats(res, res_cnt, &gp_stat);
	printf("SUMMARY tasks_trace grace period latency");
	printf("\tavg %.3lf us\tstddev %.3lf us\n", gp_stat.mean, gp_stat.stddev);
	grace_period_ticks_basic_stats(res, res_cnt, &gp_stat);
	printf("SUMMARY ticks per tasks_trace grace period");
	printf("\tavg %.3lf\tstddev %.3lf\n", gp_stat.mean, gp_stat.stddev);
}

/* local-storage-tasks-trace: Benchmark performance of BPF local_storage's use
 * of RCU Tasks-Trace.
 *
 * Stress RCU Tasks Trace by forking many tasks, all of which do no work aside
 * from sleep() loop, and creating/destroying BPF task-local storage on wakeup.
 * The number of forked tasks is configurable.
 *
 * exercising code paths which call call_rcu_tasks_trace while there are many
 * thousands of tasks on the system should result in RCU Tasks-Trace having to
 * do a noticeable amount of work.
 *
 * This should be observable by measuring rcu_tasks_trace_kthread CPU usage
 * after the grace period has ended, or by measuring grace period latency.
 *
 * This benchmark uses both approaches, attaching to rcu_tasks_trace_pregp_step
 * and rcu_tasks_trace_postgp functions to measure grace period latency and
 * using /proc/PID/stat to measure rcu_tasks_trace_kthread kernel ticks
 */
const struct bench bench_local_storage_tasks_trace = {
	.name = "local-storage-tasks-trace",
	.validate = validate,
	.setup = local_storage_tasks_trace_setup,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = report_progress,
	.report_final = report_final,
};