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