xref: /linux/tools/perf/bench/inject-buildid.c (revision 059e9100d82aae2254f1b06835a55755936b1417)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <ftw.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <pthread.h>
9 #include <sys/mman.h>
10 #include <sys/wait.h>
11 #include <linux/kernel.h>
12 #include <linux/time64.h>
13 #include <linux/list.h>
14 #include <linux/err.h>
15 #include <linux/zalloc.h>
16 #include <internal/lib.h>
17 #include <subcmd/parse-options.h>
18 
19 #include "bench.h"
20 #include "util/data.h"
21 #include "util/stat.h"
22 #include "util/debug.h"
23 #include "util/symbol.h"
24 #include "util/session.h"
25 #include "util/build-id.h"
26 #include "util/sample.h"
27 #include "util/synthetic-events.h"
28 
29 #define MMAP_DEV_MAJOR  8
30 #define DSO_MMAP_RATIO  4
31 
32 static unsigned int iterations = 100;
33 static unsigned int nr_mmaps   = 100;
34 static unsigned int nr_samples = 100;  /* samples per mmap */
35 
36 static u64 bench_sample_type;
37 static u16 bench_id_hdr_size;
38 
39 struct bench_data {
40 	int			pid;
41 	int			input_pipe[2];
42 	int			output_pipe[2];
43 	pthread_t		th;
44 };
45 
46 struct bench_dso {
47 	struct list_head	list;
48 	char			*name;
49 	int			ino;
50 };
51 
52 static int nr_dsos;
53 static struct bench_dso *dsos;
54 
55 extern int main(int argc, const char **argv);
56 
57 static const struct option options[] = {
58 	OPT_UINTEGER('i', "iterations", &iterations,
59 		     "Number of iterations used to compute average (default: 100)"),
60 	OPT_UINTEGER('m', "nr-mmaps", &nr_mmaps,
61 		     "Number of mmap events for each iteration (default: 100)"),
62 	OPT_UINTEGER('n', "nr-samples", &nr_samples,
63 		     "Number of sample events per mmap event (default: 100)"),
64 	OPT_INCR('v', "verbose", &verbose,
65 		 "be more verbose (show iteration count, DSO name, etc)"),
66 	OPT_END()
67 };
68 
69 static const char *const bench_usage[] = {
70 	"perf bench internals inject-build-id <options>",
71 	NULL
72 };
73 
74 /*
75  * Helper for collect_dso that adds the given file as a dso to dso_list
76  * if it contains a build-id.  Stops after collecting 4 times more than
77  * we need (for MMAP2 events).
78  */
79 static int add_dso(const char *fpath, const struct stat *sb __maybe_unused,
80 		   int typeflag, struct FTW *ftwbuf __maybe_unused)
81 {
82 	struct bench_dso *dso = &dsos[nr_dsos];
83 	struct build_id bid = { .size = 0, };
84 
85 	if (typeflag == FTW_D || typeflag == FTW_SL)
86 		return 0;
87 
88 	if (filename__read_build_id(fpath, &bid) < 0)
89 		return 0;
90 
91 	dso->name = realpath(fpath, NULL);
92 	if (dso->name == NULL)
93 		return -1;
94 
95 	dso->ino = nr_dsos++;
96 	pr_debug2("  Adding DSO: %s\n", fpath);
97 
98 	/* stop if we collected enough DSOs */
99 	if ((unsigned int)nr_dsos == DSO_MMAP_RATIO * nr_mmaps)
100 		return 1;
101 
102 	return 0;
103 }
104 
105 static void collect_dso(void)
106 {
107 	dsos = calloc(nr_mmaps * DSO_MMAP_RATIO, sizeof(*dsos));
108 	if (dsos == NULL) {
109 		printf("  Memory allocation failed\n");
110 		exit(1);
111 	}
112 
113 	if (nftw("/usr/lib/", add_dso, 10, FTW_PHYS) < 0)
114 		return;
115 
116 	pr_debug("  Collected %d DSOs\n", nr_dsos);
117 }
118 
119 static void release_dso(void)
120 {
121 	int i;
122 
123 	for (i = 0; i < nr_dsos; i++) {
124 		struct bench_dso *dso = &dsos[i];
125 
126 		zfree(&dso->name);
127 	}
128 	free(dsos);
129 }
130 
131 /* Fake address used by mmap and sample events */
132 static u64 dso_map_addr(struct bench_dso *dso)
133 {
134 	return 0x400000ULL + dso->ino * 8192ULL;
135 }
136 
137 static ssize_t synthesize_attr(struct bench_data *data)
138 {
139 	union perf_event event;
140 
141 	memset(&event, 0, sizeof(event.attr) + sizeof(u64));
142 
143 	event.header.type = PERF_RECORD_HEADER_ATTR;
144 	event.header.size = sizeof(event.attr) + sizeof(u64);
145 
146 	event.attr.attr.type = PERF_TYPE_SOFTWARE;
147 	event.attr.attr.config = PERF_COUNT_SW_TASK_CLOCK;
148 	event.attr.attr.exclude_kernel = 1;
149 	event.attr.attr.sample_id_all = 1;
150 	event.attr.attr.sample_type = bench_sample_type;
151 
152 	return writen(data->input_pipe[1], &event, event.header.size);
153 }
154 
155 static ssize_t synthesize_fork(struct bench_data *data)
156 {
157 	union perf_event event;
158 
159 	memset(&event, 0, sizeof(event.fork) + bench_id_hdr_size);
160 
161 	event.header.type = PERF_RECORD_FORK;
162 	event.header.misc = PERF_RECORD_MISC_FORK_EXEC;
163 	event.header.size = sizeof(event.fork) + bench_id_hdr_size;
164 
165 	event.fork.ppid = 1;
166 	event.fork.ptid = 1;
167 	event.fork.pid = data->pid;
168 	event.fork.tid = data->pid;
169 
170 	return writen(data->input_pipe[1], &event, event.header.size);
171 }
172 
173 static ssize_t synthesize_mmap(struct bench_data *data, struct bench_dso *dso, u64 timestamp)
174 {
175 	union perf_event event;
176 	size_t len = offsetof(struct perf_record_mmap2, filename);
177 	u64 *id_hdr_ptr = (void *)&event;
178 	int ts_idx;
179 
180 	len += roundup(strlen(dso->name) + 1, 8) + bench_id_hdr_size;
181 
182 	memset(&event, 0, min(len, sizeof(event.mmap2)));
183 
184 	event.header.type = PERF_RECORD_MMAP2;
185 	event.header.misc = PERF_RECORD_MISC_USER;
186 	event.header.size = len;
187 
188 	event.mmap2.pid = data->pid;
189 	event.mmap2.tid = data->pid;
190 	event.mmap2.maj = MMAP_DEV_MAJOR;
191 	event.mmap2.ino = dso->ino;
192 
193 	strcpy(event.mmap2.filename, dso->name);
194 
195 	event.mmap2.start = dso_map_addr(dso);
196 	event.mmap2.len = 4096;
197 	event.mmap2.prot = PROT_EXEC;
198 
199 	if (len > sizeof(event.mmap2)) {
200 		/* write mmap2 event first */
201 		if (writen(data->input_pipe[1], &event, len - bench_id_hdr_size) < 0)
202 			return -1;
203 		/* zero-fill sample id header */
204 		memset(id_hdr_ptr, 0, bench_id_hdr_size);
205 		/* put timestamp in the right position */
206 		ts_idx = (bench_id_hdr_size / sizeof(u64)) - 2;
207 		id_hdr_ptr[ts_idx] = timestamp;
208 		if (writen(data->input_pipe[1], id_hdr_ptr, bench_id_hdr_size) < 0)
209 			return -1;
210 
211 		return len;
212 	}
213 
214 	ts_idx = (len / sizeof(u64)) - 2;
215 	id_hdr_ptr[ts_idx] = timestamp;
216 	return writen(data->input_pipe[1], &event, len);
217 }
218 
219 static ssize_t synthesize_sample(struct bench_data *data, struct bench_dso *dso, u64 timestamp)
220 {
221 	union perf_event event;
222 	struct perf_sample sample = {
223 		.tid = data->pid,
224 		.pid = data->pid,
225 		.ip = dso_map_addr(dso),
226 		.time = timestamp,
227 	};
228 
229 	event.header.type = PERF_RECORD_SAMPLE;
230 	event.header.misc = PERF_RECORD_MISC_USER;
231 	event.header.size = perf_event__sample_event_size(&sample, bench_sample_type,
232 							   /*read_format=*/0,
233 							   /*branch_sample_type=*/0);
234 	perf_event__synthesize_sample(&event, bench_sample_type,
235 				      /*read_format=*/0,
236 				      /*branch_sample_type=*/0, &sample);
237 
238 	return writen(data->input_pipe[1], &event, event.header.size);
239 }
240 
241 static ssize_t synthesize_flush(struct bench_data *data)
242 {
243 	struct perf_event_header header = {
244 		.size = sizeof(header),
245 		.type = PERF_RECORD_FINISHED_ROUND,
246 	};
247 
248 	return writen(data->input_pipe[1], &header, header.size);
249 }
250 
251 static void *data_reader(void *arg)
252 {
253 	struct bench_data *data = arg;
254 	char buf[8192];
255 	int flag;
256 	int n;
257 
258 	flag = fcntl(data->output_pipe[0], F_GETFL);
259 	fcntl(data->output_pipe[0], F_SETFL, flag | O_NONBLOCK);
260 
261 	/* read out data from child */
262 	while (true) {
263 		n = read(data->output_pipe[0], buf, sizeof(buf));
264 		if (n > 0)
265 			continue;
266 		if (n == 0)
267 			break;
268 
269 		if (errno != EINTR && errno != EAGAIN)
270 			break;
271 
272 		usleep(100);
273 	}
274 
275 	close(data->output_pipe[0]);
276 	return NULL;
277 }
278 
279 static int setup_injection(struct bench_data *data, bool build_id_all)
280 {
281 	int ready_pipe[2];
282 	int dev_null_fd;
283 	char buf;
284 
285 	if (pipe(ready_pipe) < 0)
286 		return -1;
287 
288 	if (pipe(data->input_pipe) < 0)
289 		return -1;
290 
291 	if (pipe(data->output_pipe) < 0)
292 		return -1;
293 
294 	data->pid = fork();
295 	if (data->pid < 0)
296 		return -1;
297 
298 	if (data->pid == 0) {
299 		const char **inject_argv;
300 		int inject_argc = 3;
301 
302 		close(data->input_pipe[1]);
303 		close(data->output_pipe[0]);
304 		close(ready_pipe[0]);
305 
306 		dup2(data->input_pipe[0], STDIN_FILENO);
307 		close(data->input_pipe[0]);
308 		dup2(data->output_pipe[1], STDOUT_FILENO);
309 		close(data->output_pipe[1]);
310 
311 		dev_null_fd = open("/dev/null", O_WRONLY);
312 		if (dev_null_fd < 0)
313 			exit(1);
314 
315 		dup2(dev_null_fd, STDERR_FILENO);
316 
317 		if (build_id_all)
318 			inject_argc++;
319 
320 		inject_argv = calloc(inject_argc + 1, sizeof(*inject_argv));
321 		if (inject_argv == NULL)
322 			exit(1);
323 
324 		inject_argv[0] = strdup("perf");
325 		inject_argv[1] = strdup("inject");
326 		inject_argv[2] = strdup("-b");
327 		if (build_id_all)
328 			inject_argv[3] = strdup("--buildid-all");
329 
330 		/* signal that we're ready to go */
331 		close(ready_pipe[1]);
332 
333 		main(inject_argc, inject_argv);
334 
335 		exit(0);
336 	}
337 
338 	pthread_create(&data->th, NULL, data_reader, data);
339 
340 	close(ready_pipe[1]);
341 	close(data->input_pipe[0]);
342 	close(data->output_pipe[1]);
343 
344 	/* wait for child ready */
345 	if (read(ready_pipe[0], &buf, 1) < 0)
346 		return -1;
347 	close(ready_pipe[0]);
348 
349 	return 0;
350 }
351 
352 static int inject_build_id(struct bench_data *data, u64 *max_rss)
353 {
354 	int status;
355 	unsigned int i, k;
356 	struct rusage rusage;
357 
358 	/* this makes the child to run */
359 	if (perf_header__write_pipe(data->input_pipe[1]) < 0)
360 		return -1;
361 
362 	if (synthesize_attr(data) < 0)
363 		return -1;
364 
365 	if (synthesize_fork(data) < 0)
366 		return -1;
367 
368 	for (i = 0; i < nr_mmaps; i++) {
369 		int idx = rand() % nr_dsos;
370 		struct bench_dso *dso = &dsos[idx];
371 		u64 timestamp = rand() % 1000000;
372 
373 		pr_debug2("   [%d] injecting: %s\n", i+1, dso->name);
374 		if (synthesize_mmap(data, dso, timestamp) < 0)
375 			return -1;
376 
377 		for (k = 0; k < nr_samples; k++) {
378 			if (synthesize_sample(data, dso, timestamp + k * 1000) < 0)
379 				return -1;
380 		}
381 
382 		if ((i + 1) % 10 == 0) {
383 			if (synthesize_flush(data) < 0)
384 				return -1;
385 		}
386 	}
387 
388 	/* this makes the child to finish */
389 	close(data->input_pipe[1]);
390 
391 	wait4(data->pid, &status, 0, &rusage);
392 	*max_rss = rusage.ru_maxrss;
393 
394 	pr_debug("   Child %d exited with %d\n", data->pid, status);
395 
396 	return 0;
397 }
398 
399 static void do_inject_loop(struct bench_data *data, bool build_id_all)
400 {
401 	unsigned int i;
402 	struct stats time_stats, mem_stats;
403 	double time_average, time_stddev;
404 	double mem_average, mem_stddev;
405 
406 	init_stats(&time_stats);
407 	init_stats(&mem_stats);
408 
409 	pr_debug("  Build-id%s injection benchmark\n", build_id_all ? "-all" : "");
410 
411 	for (i = 0; i < iterations; i++) {
412 		struct timeval start, end, diff;
413 		u64 runtime_us, max_rss;
414 
415 		pr_debug("  Iteration #%d\n", i+1);
416 
417 		if (setup_injection(data, build_id_all) < 0) {
418 			printf("  Build-id injection setup failed\n");
419 			break;
420 		}
421 
422 		gettimeofday(&start, NULL);
423 		if (inject_build_id(data, &max_rss) < 0) {
424 			printf("  Build-id injection failed\n");
425 			break;
426 		}
427 
428 		gettimeofday(&end, NULL);
429 		timersub(&end, &start, &diff);
430 		runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
431 		update_stats(&time_stats, runtime_us);
432 		update_stats(&mem_stats, max_rss);
433 
434 		pthread_join(data->th, NULL);
435 	}
436 
437 	time_average = avg_stats(&time_stats) / USEC_PER_MSEC;
438 	time_stddev = stddev_stats(&time_stats) / USEC_PER_MSEC;
439 	printf("  Average build-id%s injection took: %.3f msec (+- %.3f msec)\n",
440 	       build_id_all ? "-all" : "", time_average, time_stddev);
441 
442 	/* each iteration, it processes MMAP2 + BUILD_ID + nr_samples * SAMPLE */
443 	time_average = avg_stats(&time_stats) / (nr_mmaps * (nr_samples + 2));
444 	time_stddev = stddev_stats(&time_stats) / (nr_mmaps * (nr_samples + 2));
445 	printf("  Average time per event: %.3f usec (+- %.3f usec)\n",
446 		time_average, time_stddev);
447 
448 	mem_average = avg_stats(&mem_stats);
449 	mem_stddev = stddev_stats(&mem_stats);
450 	printf("  Average memory usage: %.0f KB (+- %.0f KB)\n",
451 		mem_average, mem_stddev);
452 }
453 
454 static int do_inject_loops(struct bench_data *data)
455 {
456 
457 	srand(time(NULL));
458 	symbol__init(NULL);
459 
460 	bench_sample_type  = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP;
461 	bench_sample_type |= PERF_SAMPLE_TID | PERF_SAMPLE_TIME;
462 	bench_id_hdr_size  = 32;
463 
464 	collect_dso();
465 	if (nr_dsos == 0) {
466 		printf("  Cannot collect DSOs for injection\n");
467 		return -1;
468 	}
469 
470 	do_inject_loop(data, false);
471 	do_inject_loop(data, true);
472 
473 	release_dso();
474 	return 0;
475 }
476 
477 int bench_inject_build_id(int argc, const char **argv)
478 {
479 	struct bench_data data;
480 
481 	argc = parse_options(argc, argv, options, bench_usage, 0);
482 	if (argc) {
483 		usage_with_options(bench_usage, options);
484 		exit(EXIT_FAILURE);
485 	}
486 
487 	return do_inject_loops(&data);
488 }
489 
490