1 // SPDX-License-Identifier: GPL-2.0
6 #include "thread_map.h"
12 #include <linux/string.h>
13 #include <perf/evlist.h>
18 static void sig_handler(int sig __maybe_unused)
24 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
25 * we asked by setting its exec_error to this handler.
27 static void workload_exec_failed_signal(int signo __maybe_unused,
28 siginfo_t *info __maybe_unused,
29 void *ucontext __maybe_unused)
36 * This test will start a workload that does nothing then it checks
37 * if the number of exit event reported by the kernel is 1 or not
38 * in order to check the kernel returns correct number of event.
40 int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
43 union perf_event *event;
45 struct evlist *evlist;
46 struct target target = {
50 const char *argv[] = { "true", NULL };
51 char sbuf[STRERR_BUFSIZE];
52 struct perf_cpu_map *cpus;
53 struct perf_thread_map *threads;
56 signal(SIGCHLD, sig_handler);
58 evlist = perf_evlist__new_default();
60 pr_debug("perf_evlist__new_default\n");
65 * Create maps of threads and cpus to monitor. In this case
66 * we start with all threads and cpus (-1, -1) but then in
67 * perf_evlist__prepare_workload we'll fill in the only thread
68 * we're monitoring, the one forked there.
70 cpus = perf_cpu_map__dummy_new();
71 threads = thread_map__new_by_tid(-1);
72 if (!cpus || !threads) {
74 pr_debug("Not enough memory to create thread/cpu maps\n");
78 perf_evlist__set_maps(&evlist->core, cpus, threads);
83 err = perf_evlist__prepare_workload(evlist, &target, argv, false,
84 workload_exec_failed_signal);
86 pr_debug("Couldn't run the workload!\n");
87 goto out_delete_evlist;
90 evsel = perf_evlist__first(evlist);
91 evsel->core.attr.task = 1;
93 evsel->core.attr.sample_freq = 1000000;
95 evsel->core.attr.sample_freq = 1;
97 evsel->core.attr.inherit = 0;
98 evsel->core.attr.watermark = 0;
99 evsel->core.attr.wakeup_events = 1;
100 evsel->core.attr.exclude_kernel = 1;
102 err = evlist__open(evlist);
104 pr_debug("Couldn't open the evlist: %s\n",
105 str_error_r(-err, sbuf, sizeof(sbuf)));
106 goto out_delete_evlist;
109 if (perf_evlist__mmap(evlist, 128) < 0) {
110 pr_debug("failed to mmap events: %d (%s)\n", errno,
111 str_error_r(errno, sbuf, sizeof(sbuf)));
112 goto out_delete_evlist;
115 perf_evlist__start_workload(evlist);
118 md = &evlist->mmap[0];
119 if (perf_mmap__read_init(md) < 0)
122 while ((event = perf_mmap__read_event(md)) != NULL) {
123 if (event->header.type == PERF_RECORD_EXIT)
126 perf_mmap__consume(md);
128 perf_mmap__read_done(md);
131 if (!exited || !nr_exit) {
132 perf_evlist__poll(evlist, -1);
137 pr_debug("received %d EXIT records\n", nr_exit);
142 perf_cpu_map__put(cpus);
143 perf_thread_map__put(threads);
145 evlist__delete(evlist);