Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 2 | #include <linux/types.h> |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 3 | #include <limits.h> |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <sys/prctl.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 6 | #include <perf/cpumap.h> |
Jiri Olsa | 453fa03 | 2019-07-21 13:24:43 +0200 | [diff] [blame] | 7 | #include <perf/evlist.h> |
Jiri Olsa | 7728fa0 | 2019-10-07 14:53:17 +0200 | [diff] [blame] | 8 | #include <perf/mmap.h> |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | b420902 | 2019-08-29 15:56:40 -0300 | [diff] [blame] | 10 | #include "debug.h" |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 11 | #include "parse-events.h" |
| 12 | #include "evlist.h" |
| 13 | #include "evsel.h" |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 14 | #include "record.h" |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 15 | #include "thread_map.h" |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 16 | #include "tests.h" |
Arnaldo Carvalho de Melo | e0fcfb0 | 2019-09-23 12:20:38 -0300 | [diff] [blame] | 17 | #include "util/mmap.h" |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 18 | |
| 19 | #define CHECK__(x) { \ |
| 20 | while ((x) < 0) { \ |
| 21 | pr_debug(#x " failed!\n"); \ |
| 22 | goto out_err; \ |
| 23 | } \ |
| 24 | } |
| 25 | |
| 26 | #define CHECK_NOT_NULL__(x) { \ |
| 27 | while ((x) == NULL) { \ |
| 28 | pr_debug(#x " failed!\n"); \ |
| 29 | goto out_err; \ |
| 30 | } \ |
| 31 | } |
| 32 | |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 33 | static int find_comm(struct evlist *evlist, const char *comm) |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 34 | { |
| 35 | union perf_event *event; |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 36 | struct mmap *md; |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 37 | int i, found; |
| 38 | |
| 39 | found = 0; |
Jiri Olsa | c976ee1 | 2019-07-30 13:04:59 +0200 | [diff] [blame] | 40 | for (i = 0; i < evlist->core.nr_mmaps; i++) { |
Kan Liang | 693d32a | 2018-03-01 18:09:03 -0500 | [diff] [blame] | 41 | md = &evlist->mmap[i]; |
Jiri Olsa | 7c4d418 | 2019-10-07 14:53:18 +0200 | [diff] [blame] | 42 | if (perf_mmap__read_init(&md->core) < 0) |
Kan Liang | 693d32a | 2018-03-01 18:09:03 -0500 | [diff] [blame] | 43 | continue; |
Jiri Olsa | 151ed5d | 2019-10-07 14:53:20 +0200 | [diff] [blame] | 44 | while ((event = perf_mmap__read_event(&md->core)) != NULL) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 45 | if (event->header.type == PERF_RECORD_COMM && |
| 46 | (pid_t)event->comm.pid == getpid() && |
| 47 | (pid_t)event->comm.tid == getpid() && |
| 48 | strcmp(event->comm.comm, comm) == 0) |
| 49 | found += 1; |
Jiri Olsa | 7728fa0 | 2019-10-07 14:53:17 +0200 | [diff] [blame] | 50 | perf_mmap__consume(&md->core); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 51 | } |
Jiri Olsa | 32fdc2c | 2019-10-07 14:53:19 +0200 | [diff] [blame] | 52 | perf_mmap__read_done(&md->core); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 53 | } |
| 54 | return found; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * test__keep_tracking - test using a dummy software event to keep tracking. |
| 59 | * |
| 60 | * This function implements a test that checks that tracking events continue |
| 61 | * when an event is disabled but a dummy software event is not disabled. If the |
| 62 | * test passes %0 is returned, otherwise %-1 is returned. |
| 63 | */ |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 64 | int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused) |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 65 | { |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 66 | struct record_opts opts = { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 67 | .mmap_pages = UINT_MAX, |
| 68 | .user_freq = UINT_MAX, |
| 69 | .user_interval = ULLONG_MAX, |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 70 | .target = { |
| 71 | .uses_mmap = true, |
| 72 | }, |
| 73 | }; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 74 | struct perf_thread_map *threads = NULL; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 75 | struct perf_cpu_map *cpus = NULL; |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 76 | struct evlist *evlist = NULL; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 77 | struct evsel *evsel = NULL; |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 78 | int found, err = -1; |
| 79 | const char *comm; |
| 80 | |
| 81 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
| 82 | CHECK_NOT_NULL__(threads); |
| 83 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 84 | cpus = perf_cpu_map__new(NULL); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 85 | CHECK_NOT_NULL__(cpus); |
| 86 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 87 | evlist = evlist__new(); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 88 | CHECK_NOT_NULL__(evlist); |
| 89 | |
Jiri Olsa | 453fa03 | 2019-07-21 13:24:43 +0200 | [diff] [blame] | 90 | perf_evlist__set_maps(&evlist->core, cpus, threads); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 91 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 92 | CHECK__(parse_events(evlist, "dummy:u", NULL)); |
| 93 | CHECK__(parse_events(evlist, "cycles:u", NULL)); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 94 | |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 95 | perf_evlist__config(evlist, &opts, NULL); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 96 | |
Jiri Olsa | 515dbe4 | 2019-09-03 10:39:52 +0200 | [diff] [blame] | 97 | evsel = evlist__first(evlist); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 98 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 99 | evsel->core.attr.comm = 1; |
| 100 | evsel->core.attr.disabled = 1; |
| 101 | evsel->core.attr.enable_on_exec = 0; |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 102 | |
Jiri Olsa | 474ddc4 | 2019-07-21 13:24:06 +0200 | [diff] [blame] | 103 | if (evlist__open(evlist) < 0) { |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame] | 104 | pr_debug("Unable to open dummy and cycles event\n"); |
| 105 | err = TEST_SKIP; |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 106 | goto out_err; |
| 107 | } |
| 108 | |
Jiri Olsa | 9521b5f | 2019-07-28 12:45:35 +0200 | [diff] [blame] | 109 | CHECK__(evlist__mmap(evlist, UINT_MAX)); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * First, test that a 'comm' event can be found when the event is |
| 113 | * enabled. |
| 114 | */ |
| 115 | |
Jiri Olsa | 1c87f16 | 2019-07-21 13:24:08 +0200 | [diff] [blame] | 116 | evlist__enable(evlist); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 117 | |
| 118 | comm = "Test COMM 1"; |
| 119 | CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0)); |
| 120 | |
Jiri Olsa | e74676d | 2019-07-21 13:24:09 +0200 | [diff] [blame] | 121 | evlist__disable(evlist); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 122 | |
| 123 | found = find_comm(evlist, comm); |
| 124 | if (found != 1) { |
| 125 | pr_debug("First time, failed to find tracking event.\n"); |
| 126 | goto out_err; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Secondly, test that a 'comm' event can be found when the event is |
| 131 | * disabled with the dummy event still enabled. |
| 132 | */ |
| 133 | |
Jiri Olsa | 1c87f16 | 2019-07-21 13:24:08 +0200 | [diff] [blame] | 134 | evlist__enable(evlist); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 135 | |
Jiri Olsa | 515dbe4 | 2019-09-03 10:39:52 +0200 | [diff] [blame] | 136 | evsel = evlist__last(evlist); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 137 | |
Jiri Olsa | 9a10bb2 | 2019-07-21 13:24:03 +0200 | [diff] [blame] | 138 | CHECK__(evsel__disable(evsel)); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 139 | |
| 140 | comm = "Test COMM 2"; |
| 141 | CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0)); |
| 142 | |
Jiri Olsa | e74676d | 2019-07-21 13:24:09 +0200 | [diff] [blame] | 143 | evlist__disable(evlist); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 144 | |
| 145 | found = find_comm(evlist, comm); |
| 146 | if (found != 1) { |
Arnaldo Carvalho de Melo | 87ffb6c | 2019-09-10 16:29:02 +0100 | [diff] [blame] | 147 | pr_debug("Second time, failed to find tracking event.\n"); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 148 | goto out_err; |
| 149 | } |
| 150 | |
| 151 | err = 0; |
| 152 | |
| 153 | out_err: |
| 154 | if (evlist) { |
Jiri Olsa | e74676d | 2019-07-21 13:24:09 +0200 | [diff] [blame] | 155 | evlist__disable(evlist); |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 156 | evlist__delete(evlist); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 157 | } else { |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 158 | perf_cpu_map__put(cpus); |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 159 | perf_thread_map__put(threads); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 160 | } |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 161 | |
| 162 | return err; |
| 163 | } |