xref: /linux/tools/perf/tests/task-exit.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 #include "evlist.h"
2 #include "evsel.h"
3 #include "thread_map.h"
4 #include "cpumap.h"
5 #include "tests.h"
6 
7 #include <signal.h>
8 
9 static int exited;
10 static int nr_exit;
11 
12 static void sig_handler(int sig)
13 {
14 	exited = 1;
15 
16 	if (sig == SIGUSR1)
17 		nr_exit = -1;
18 }
19 
20 /*
21  * This test will start a workload that does nothing then it checks
22  * if the number of exit event reported by the kernel is 1 or not
23  * in order to check the kernel returns correct number of event.
24  */
25 int test__task_exit(void)
26 {
27 	int err = -1;
28 	union perf_event *event;
29 	struct perf_evsel *evsel;
30 	struct perf_evlist *evlist;
31 	struct target target = {
32 		.uid		= UINT_MAX,
33 		.uses_mmap	= true,
34 	};
35 	const char *argv[] = { "true", NULL };
36 
37 	signal(SIGCHLD, sig_handler);
38 	signal(SIGUSR1, sig_handler);
39 
40 	evlist = perf_evlist__new_default();
41 	if (evlist == NULL) {
42 		pr_debug("perf_evlist__new_default\n");
43 		return -1;
44 	}
45 
46 	/*
47 	 * Create maps of threads and cpus to monitor. In this case
48 	 * we start with all threads and cpus (-1, -1) but then in
49 	 * perf_evlist__prepare_workload we'll fill in the only thread
50 	 * we're monitoring, the one forked there.
51 	 */
52 	evlist->cpus = cpu_map__dummy_new();
53 	evlist->threads = thread_map__new_by_tid(-1);
54 	if (!evlist->cpus || !evlist->threads) {
55 		err = -ENOMEM;
56 		pr_debug("Not enough memory to create thread/cpu maps\n");
57 		goto out_delete_maps;
58 	}
59 
60 	err = perf_evlist__prepare_workload(evlist, &target, argv, false, true);
61 	if (err < 0) {
62 		pr_debug("Couldn't run the workload!\n");
63 		goto out_delete_maps;
64 	}
65 
66 	evsel = perf_evlist__first(evlist);
67 	evsel->attr.task = 1;
68 	evsel->attr.sample_freq = 0;
69 	evsel->attr.inherit = 0;
70 	evsel->attr.watermark = 0;
71 	evsel->attr.wakeup_events = 1;
72 	evsel->attr.exclude_kernel = 1;
73 
74 	err = perf_evlist__open(evlist);
75 	if (err < 0) {
76 		pr_debug("Couldn't open the evlist: %s\n", strerror(-err));
77 		goto out_delete_maps;
78 	}
79 
80 	if (perf_evlist__mmap(evlist, 128, true) < 0) {
81 		pr_debug("failed to mmap events: %d (%s)\n", errno,
82 			 strerror(errno));
83 		goto out_close_evlist;
84 	}
85 
86 	perf_evlist__start_workload(evlist);
87 
88 retry:
89 	while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
90 		if (event->header.type == PERF_RECORD_EXIT)
91 			nr_exit++;
92 
93 		perf_evlist__mmap_consume(evlist, 0);
94 	}
95 
96 	if (!exited || !nr_exit) {
97 		poll(evlist->pollfd, evlist->nr_fds, -1);
98 		goto retry;
99 	}
100 
101 	if (nr_exit != 1) {
102 		pr_debug("received %d EXIT records\n", nr_exit);
103 		err = -1;
104 	}
105 
106 	perf_evlist__munmap(evlist);
107 out_close_evlist:
108 	perf_evlist__close(evlist);
109 out_delete_maps:
110 	perf_evlist__delete_maps(evlist);
111 	perf_evlist__delete(evlist);
112 	return err;
113 }
114