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