Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | b420902 | 2019-08-29 15:56:40 -0300 | [diff] [blame] | 2 | #include "debug.h" |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 3 | #include "evlist.h" |
| 4 | #include "evsel.h" |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 5 | #include "target.h" |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 6 | #include "thread_map.h" |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 7 | #include "tests.h" |
Arnaldo Carvalho de Melo | e0fcfb0 | 2019-09-23 12:20:38 -0300 | [diff] [blame] | 8 | #include "util/mmap.h" |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 10 | #include <errno.h> |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 11 | #include <signal.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 12 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 87ffb6c | 2019-09-10 16:29:02 +0100 | [diff] [blame] | 13 | #include <perf/cpumap.h> |
Jiri Olsa | 453fa03 | 2019-07-21 13:24:43 +0200 | [diff] [blame] | 14 | #include <perf/evlist.h> |
Jiri Olsa | 7728fa0 | 2019-10-07 14:53:17 +0200 | [diff] [blame] | 15 | #include <perf/mmap.h> |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 16 | |
| 17 | static int exited; |
| 18 | static int nr_exit; |
| 19 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 20 | static void sig_handler(int sig __maybe_unused) |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 21 | { |
| 22 | exited = 1; |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 23 | } |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 24 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 25 | /* |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 26 | * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 27 | * we asked by setting its exec_error to this handler. |
| 28 | */ |
| 29 | static void workload_exec_failed_signal(int signo __maybe_unused, |
| 30 | siginfo_t *info __maybe_unused, |
| 31 | void *ucontext __maybe_unused) |
| 32 | { |
| 33 | exited = 1; |
| 34 | nr_exit = -1; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* |
| 38 | * This test will start a workload that does nothing then it checks |
| 39 | * if the number of exit event reported by the kernel is 1 or not |
| 40 | * in order to check the kernel returns correct number of event. |
| 41 | */ |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame^] | 42 | static int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused) |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 43 | { |
| 44 | int err = -1; |
| 45 | union perf_event *event; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 46 | struct evsel *evsel; |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 47 | struct evlist *evlist; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 48 | struct target target = { |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 49 | .uid = UINT_MAX, |
| 50 | .uses_mmap = true, |
| 51 | }; |
| 52 | const char *argv[] = { "true", NULL }; |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 53 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 54 | struct perf_cpu_map *cpus; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 55 | struct perf_thread_map *threads; |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 56 | struct mmap *md; |
Leo Yan | 791ce9c | 2019-10-11 17:19:42 +0800 | [diff] [blame] | 57 | int retry_count = 0; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 58 | |
| 59 | signal(SIGCHLD, sig_handler); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 60 | |
Arnaldo Carvalho de Melo | 606e2c2 | 2020-11-30 15:04:05 -0300 | [diff] [blame] | 61 | evlist = evlist__new_default(); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 62 | if (evlist == NULL) { |
Arnaldo Carvalho de Melo | 606e2c2 | 2020-11-30 15:04:05 -0300 | [diff] [blame] | 63 | pr_debug("evlist__new_default\n"); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 64 | return -1; |
| 65 | } |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Create maps of threads and cpus to monitor. In this case |
| 69 | * we start with all threads and cpus (-1, -1) but then in |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 70 | * evlist__prepare_workload we'll fill in the only thread |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 71 | * we're monitoring, the one forked there. |
| 72 | */ |
Jiri Olsa | 397721e | 2019-07-21 13:24:16 +0200 | [diff] [blame] | 73 | cpus = perf_cpu_map__dummy_new(); |
Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 74 | threads = thread_map__new_by_tid(-1); |
| 75 | if (!cpus || !threads) { |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 76 | err = -ENOMEM; |
| 77 | pr_debug("Not enough memory to create thread/cpu maps\n"); |
Namhyung Kim | 83d25cc | 2021-03-01 23:04:01 +0900 | [diff] [blame] | 78 | goto out_delete_evlist; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Jiri Olsa | 453fa03 | 2019-07-21 13:24:43 +0200 | [diff] [blame] | 81 | perf_evlist__set_maps(&evlist->core, cpus, threads); |
Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 82 | |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 83 | err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 84 | if (err < 0) { |
| 85 | pr_debug("Couldn't run the workload!\n"); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 86 | goto out_delete_evlist; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Jiri Olsa | 515dbe4 | 2019-09-03 10:39:52 +0200 | [diff] [blame] | 89 | evsel = evlist__first(evlist); |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 90 | evsel->core.attr.task = 1; |
Thomas Richter | 9965484 | 2017-11-23 12:46:11 +0100 | [diff] [blame] | 91 | #ifdef __s390x__ |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 92 | evsel->core.attr.sample_freq = 1000000; |
Thomas Richter | 9965484 | 2017-11-23 12:46:11 +0100 | [diff] [blame] | 93 | #else |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 94 | evsel->core.attr.sample_freq = 1; |
Thomas Richter | 9965484 | 2017-11-23 12:46:11 +0100 | [diff] [blame] | 95 | #endif |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 96 | evsel->core.attr.inherit = 0; |
| 97 | evsel->core.attr.watermark = 0; |
| 98 | evsel->core.attr.wakeup_events = 1; |
| 99 | evsel->core.attr.exclude_kernel = 1; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 100 | |
Jiri Olsa | 474ddc4 | 2019-07-21 13:24:06 +0200 | [diff] [blame] | 101 | err = evlist__open(evlist); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 102 | if (err < 0) { |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 103 | pr_debug("Couldn't open the evlist: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 104 | str_error_r(-err, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 105 | goto out_delete_evlist; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Jiri Olsa | 9521b5f | 2019-07-28 12:45:35 +0200 | [diff] [blame] | 108 | if (evlist__mmap(evlist, 128) < 0) { |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 109 | pr_debug("failed to mmap events: %d (%s)\n", errno, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 110 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Leo Yan | 6add129 | 2019-10-11 17:19:41 +0800 | [diff] [blame] | 111 | err = -1; |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 112 | goto out_delete_evlist; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 115 | evlist__start_workload(evlist); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 116 | |
| 117 | retry: |
Kan Liang | 7594873 | 2018-03-01 18:09:10 -0500 | [diff] [blame] | 118 | md = &evlist->mmap[0]; |
Jiri Olsa | 7c4d418 | 2019-10-07 14:53:18 +0200 | [diff] [blame] | 119 | if (perf_mmap__read_init(&md->core) < 0) |
Kan Liang | 7594873 | 2018-03-01 18:09:10 -0500 | [diff] [blame] | 120 | goto out_init; |
| 121 | |
Jiri Olsa | 151ed5d | 2019-10-07 14:53:20 +0200 | [diff] [blame] | 122 | while ((event = perf_mmap__read_event(&md->core)) != NULL) { |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 123 | if (event->header.type == PERF_RECORD_EXIT) |
| 124 | nr_exit++; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 125 | |
Jiri Olsa | 7728fa0 | 2019-10-07 14:53:17 +0200 | [diff] [blame] | 126 | perf_mmap__consume(&md->core); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 127 | } |
Jiri Olsa | 32fdc2c | 2019-10-07 14:53:19 +0200 | [diff] [blame] | 128 | perf_mmap__read_done(&md->core); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 129 | |
Kan Liang | 7594873 | 2018-03-01 18:09:10 -0500 | [diff] [blame] | 130 | out_init: |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 131 | if (!exited || !nr_exit) { |
Jiri Olsa | 80ab298 | 2019-08-31 22:48:33 +0200 | [diff] [blame] | 132 | evlist__poll(evlist, -1); |
Leo Yan | 791ce9c | 2019-10-11 17:19:42 +0800 | [diff] [blame] | 133 | |
| 134 | if (retry_count++ > 1000) { |
| 135 | pr_debug("Failed after retrying 1000 times\n"); |
| 136 | err = -1; |
Namhyung Kim | 83d25cc | 2021-03-01 23:04:01 +0900 | [diff] [blame] | 137 | goto out_delete_evlist; |
Leo Yan | 791ce9c | 2019-10-11 17:19:42 +0800 | [diff] [blame] | 138 | } |
| 139 | |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 140 | goto retry; |
| 141 | } |
| 142 | |
| 143 | if (nr_exit != 1) { |
| 144 | pr_debug("received %d EXIT records\n", nr_exit); |
| 145 | err = -1; |
| 146 | } |
| 147 | |
Namhyung Kim | 83d25cc | 2021-03-01 23:04:01 +0900 | [diff] [blame] | 148 | out_delete_evlist: |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 149 | perf_cpu_map__put(cpus); |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 150 | perf_thread_map__put(threads); |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 151 | evlist__delete(evlist); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 152 | return err; |
| 153 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame^] | 154 | |
| 155 | DEFINE_SUITE("Number of exit events of a simple workload", task_exit); |