1 // SPDX-License-Identifier: GPL-2.0
4 #include "thread_map.h"
10 #include <perf/evlist.h>
15 static void sig_handler(int sig __maybe_unused)
21 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
22 * we asked by setting its exec_error to this handler.
24 static void workload_exec_failed_signal(int signo __maybe_unused,
25 siginfo_t *info __maybe_unused,
26 void *ucontext __maybe_unused)
33 * This test will start a workload that does nothing then it checks
34 * if the number of exit event reported by the kernel is 1 or not
35 * in order to check the kernel returns correct number of event.
37 int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
40 union perf_event *event;
42 struct evlist *evlist;
43 struct target target = {
47 const char *argv[] = { "true", NULL };
48 char sbuf[STRERR_BUFSIZE];
49 struct perf_cpu_map *cpus;
50 struct perf_thread_map *threads;
53 signal(SIGCHLD, sig_handler);
55 evlist = perf_evlist__new_default();
57 pr_debug("perf_evlist__new_default\n");
62 * Create maps of threads and cpus to monitor. In this case
63 * we start with all threads and cpus (-1, -1) but then in
64 * perf_evlist__prepare_workload we'll fill in the only thread
65 * we're monitoring, the one forked there.
67 cpus = perf_cpu_map__dummy_new();
68 threads = thread_map__new_by_tid(-1);
69 if (!cpus || !threads) {
71 pr_debug("Not enough memory to create thread/cpu maps\n");
75 perf_evlist__set_maps(&evlist->core, cpus, threads);
80 err = perf_evlist__prepare_workload(evlist, &target, argv, false,
81 workload_exec_failed_signal);
83 pr_debug("Couldn't run the workload!\n");
84 goto out_delete_evlist;
87 evsel = perf_evlist__first(evlist);
88 evsel->core.attr.task = 1;
90 evsel->core.attr.sample_freq = 1000000;
92 evsel->core.attr.sample_freq = 1;
94 evsel->core.attr.inherit = 0;
95 evsel->core.attr.watermark = 0;
96 evsel->core.attr.wakeup_events = 1;
97 evsel->core.attr.exclude_kernel = 1;
99 err = evlist__open(evlist);
101 pr_debug("Couldn't open the evlist: %s\n",
102 str_error_r(-err, sbuf, sizeof(sbuf)));
103 goto out_delete_evlist;
106 if (perf_evlist__mmap(evlist, 128) < 0) {
107 pr_debug("failed to mmap events: %d (%s)\n", errno,
108 str_error_r(errno, sbuf, sizeof(sbuf)));
109 goto out_delete_evlist;
112 perf_evlist__start_workload(evlist);
115 md = &evlist->mmap[0];
116 if (perf_mmap__read_init(md) < 0)
119 while ((event = perf_mmap__read_event(md)) != NULL) {
120 if (event->header.type == PERF_RECORD_EXIT)
123 perf_mmap__consume(md);
125 perf_mmap__read_done(md);
128 if (!exited || !nr_exit) {
129 perf_evlist__poll(evlist, -1);
134 pr_debug("received %d EXIT records\n", nr_exit);
139 perf_cpu_map__put(cpus);
140 perf_thread_map__put(threads);
142 evlist__delete(evlist);