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