1 // SPDX-License-Identifier: GPL-2.0 2 #include "evlist.h" 3 #include "evsel.h" 4 #include "target.h" 5 #include "thread_map.h" 6 #include "cpumap.h" 7 #include "tests.h" 8 9 #include <errno.h> 10 #include <signal.h> 11 #include <perf/evlist.h> 12 13 static int exited; 14 static int nr_exit; 15 16 static void sig_handler(int sig __maybe_unused) 17 { 18 exited = 1; 19 } 20 21 /* 22 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since 23 * we asked by setting its exec_error to this handler. 24 */ 25 static void workload_exec_failed_signal(int signo __maybe_unused, 26 siginfo_t *info __maybe_unused, 27 void *ucontext __maybe_unused) 28 { 29 exited = 1; 30 nr_exit = -1; 31 } 32 33 /* 34 * This test will start a workload that does nothing then it checks 35 * if the number of exit event reported by the kernel is 1 or not 36 * in order to check the kernel returns correct number of event. 37 */ 38 int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused) 39 { 40 int err = -1; 41 union perf_event *event; 42 struct evsel *evsel; 43 struct evlist *evlist; 44 struct target target = { 45 .uid = UINT_MAX, 46 .uses_mmap = true, 47 }; 48 const char *argv[] = { "true", NULL }; 49 char sbuf[STRERR_BUFSIZE]; 50 struct perf_cpu_map *cpus; 51 struct perf_thread_map *threads; 52 struct perf_mmap *md; 53 54 signal(SIGCHLD, sig_handler); 55 56 evlist = perf_evlist__new_default(); 57 if (evlist == NULL) { 58 pr_debug("perf_evlist__new_default\n"); 59 return -1; 60 } 61 62 /* 63 * Create maps of threads and cpus to monitor. In this case 64 * we start with all threads and cpus (-1, -1) but then in 65 * perf_evlist__prepare_workload we'll fill in the only thread 66 * we're monitoring, the one forked there. 67 */ 68 cpus = perf_cpu_map__dummy_new(); 69 threads = thread_map__new_by_tid(-1); 70 if (!cpus || !threads) { 71 err = -ENOMEM; 72 pr_debug("Not enough memory to create thread/cpu maps\n"); 73 goto out_free_maps; 74 } 75 76 perf_evlist__set_maps(&evlist->core, cpus, threads); 77 78 cpus = NULL; 79 threads = NULL; 80 81 err = perf_evlist__prepare_workload(evlist, &target, argv, false, 82 workload_exec_failed_signal); 83 if (err < 0) { 84 pr_debug("Couldn't run the workload!\n"); 85 goto out_delete_evlist; 86 } 87 88 evsel = perf_evlist__first(evlist); 89 evsel->core.attr.task = 1; 90 #ifdef __s390x__ 91 evsel->core.attr.sample_freq = 1000000; 92 #else 93 evsel->core.attr.sample_freq = 1; 94 #endif 95 evsel->core.attr.inherit = 0; 96 evsel->core.attr.watermark = 0; 97 evsel->core.attr.wakeup_events = 1; 98 evsel->core.attr.exclude_kernel = 1; 99 100 err = evlist__open(evlist); 101 if (err < 0) { 102 pr_debug("Couldn't open the evlist: %s\n", 103 str_error_r(-err, sbuf, sizeof(sbuf))); 104 goto out_delete_evlist; 105 } 106 107 if (perf_evlist__mmap(evlist, 128) < 0) { 108 pr_debug("failed to mmap events: %d (%s)\n", errno, 109 str_error_r(errno, sbuf, sizeof(sbuf))); 110 goto out_delete_evlist; 111 } 112 113 perf_evlist__start_workload(evlist); 114 115 retry: 116 md = &evlist->mmap[0]; 117 if (perf_mmap__read_init(md) < 0) 118 goto out_init; 119 120 while ((event = perf_mmap__read_event(md)) != NULL) { 121 if (event->header.type == PERF_RECORD_EXIT) 122 nr_exit++; 123 124 perf_mmap__consume(md); 125 } 126 perf_mmap__read_done(md); 127 128 out_init: 129 if (!exited || !nr_exit) { 130 perf_evlist__poll(evlist, -1); 131 goto retry; 132 } 133 134 if (nr_exit != 1) { 135 pr_debug("received %d EXIT records\n", nr_exit); 136 err = -1; 137 } 138 139 out_free_maps: 140 perf_cpu_map__put(cpus); 141 perf_thread_map__put(threads); 142 out_delete_evlist: 143 evlist__delete(evlist); 144 return err; 145 } 146