1 // SPDX-License-Identifier: GPL-2.0 2 #include "debug.h" 3 #include "evlist.h" 4 #include "evsel.h" 5 #include "target.h" 6 #include "thread_map.h" 7 #include "cpumap.h" 8 #include "tests.h" 9 10 #include <errno.h> 11 #include <signal.h> 12 #include <linux/string.h> 13 #include <perf/evlist.h> 14 15 static int exited; 16 static int nr_exit; 17 18 static void sig_handler(int sig __maybe_unused) 19 { 20 exited = 1; 21 } 22 23 /* 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. 26 */ 27 static void workload_exec_failed_signal(int signo __maybe_unused, 28 siginfo_t *info __maybe_unused, 29 void *ucontext __maybe_unused) 30 { 31 exited = 1; 32 nr_exit = -1; 33 } 34 35 /* 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. 39 */ 40 int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused) 41 { 42 int err = -1; 43 union perf_event *event; 44 struct evsel *evsel; 45 struct evlist *evlist; 46 struct target target = { 47 .uid = UINT_MAX, 48 .uses_mmap = true, 49 }; 50 const char *argv[] = { "true", NULL }; 51 char sbuf[STRERR_BUFSIZE]; 52 struct perf_cpu_map *cpus; 53 struct perf_thread_map *threads; 54 struct perf_mmap *md; 55 56 signal(SIGCHLD, sig_handler); 57 58 evlist = perf_evlist__new_default(); 59 if (evlist == NULL) { 60 pr_debug("perf_evlist__new_default\n"); 61 return -1; 62 } 63 64 /* 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. 69 */ 70 cpus = perf_cpu_map__dummy_new(); 71 threads = thread_map__new_by_tid(-1); 72 if (!cpus || !threads) { 73 err = -ENOMEM; 74 pr_debug("Not enough memory to create thread/cpu maps\n"); 75 goto out_free_maps; 76 } 77 78 perf_evlist__set_maps(&evlist->core, cpus, threads); 79 80 cpus = NULL; 81 threads = NULL; 82 83 err = perf_evlist__prepare_workload(evlist, &target, argv, false, 84 workload_exec_failed_signal); 85 if (err < 0) { 86 pr_debug("Couldn't run the workload!\n"); 87 goto out_delete_evlist; 88 } 89 90 evsel = perf_evlist__first(evlist); 91 evsel->core.attr.task = 1; 92 #ifdef __s390x__ 93 evsel->core.attr.sample_freq = 1000000; 94 #else 95 evsel->core.attr.sample_freq = 1; 96 #endif 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; 101 102 err = evlist__open(evlist); 103 if (err < 0) { 104 pr_debug("Couldn't open the evlist: %s\n", 105 str_error_r(-err, sbuf, sizeof(sbuf))); 106 goto out_delete_evlist; 107 } 108 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; 113 } 114 115 perf_evlist__start_workload(evlist); 116 117 retry: 118 md = &evlist->mmap[0]; 119 if (perf_mmap__read_init(md) < 0) 120 goto out_init; 121 122 while ((event = perf_mmap__read_event(md)) != NULL) { 123 if (event->header.type == PERF_RECORD_EXIT) 124 nr_exit++; 125 126 perf_mmap__consume(md); 127 } 128 perf_mmap__read_done(md); 129 130 out_init: 131 if (!exited || !nr_exit) { 132 perf_evlist__poll(evlist, -1); 133 goto retry; 134 } 135 136 if (nr_exit != 1) { 137 pr_debug("received %d EXIT records\n", nr_exit); 138 err = -1; 139 } 140 141 out_free_maps: 142 perf_cpu_map__put(cpus); 143 perf_thread_map__put(threads); 144 out_delete_evlist: 145 evlist__delete(evlist); 146 return err; 147 } 148