1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/compiler.h> 3 #include <linux/string.h> 4 #include <errno.h> 5 #include <inttypes.h> 6 #include <string.h> 7 #include <sys/wait.h> 8 #include <perf/cpumap.h> 9 #include "tests.h" 10 #include "evlist.h" 11 #include "evsel.h" 12 #include "util.h" 13 #include "debug.h" 14 #include "parse-events.h" 15 #include "thread_map.h" 16 #include "target.h" 17 18 static int attach__enable_on_exec(struct evlist *evlist) 19 { 20 struct evsel *evsel = perf_evlist__last(evlist); 21 struct target target = { 22 .uid = UINT_MAX, 23 }; 24 const char *argv[] = { "true", NULL, }; 25 char sbuf[STRERR_BUFSIZE]; 26 int err; 27 28 pr_debug("attaching to spawned child, enable on exec\n"); 29 30 err = perf_evlist__create_maps(evlist, &target); 31 if (err < 0) { 32 pr_debug("Not enough memory to create thread/cpu maps\n"); 33 return err; 34 } 35 36 err = perf_evlist__prepare_workload(evlist, &target, argv, false, NULL); 37 if (err < 0) { 38 pr_debug("Couldn't run the workload!\n"); 39 return err; 40 } 41 42 evsel->core.attr.enable_on_exec = 1; 43 44 err = evlist__open(evlist); 45 if (err < 0) { 46 pr_debug("perf_evlist__open: %s\n", 47 str_error_r(errno, sbuf, sizeof(sbuf))); 48 return err; 49 } 50 51 return perf_evlist__start_workload(evlist) == 1 ? TEST_OK : TEST_FAIL; 52 } 53 54 static int detach__enable_on_exec(struct evlist *evlist) 55 { 56 waitpid(evlist->workload.pid, NULL, 0); 57 return 0; 58 } 59 60 static int attach__current_disabled(struct evlist *evlist) 61 { 62 struct evsel *evsel = perf_evlist__last(evlist); 63 struct perf_thread_map *threads; 64 int err; 65 66 pr_debug("attaching to current thread as disabled\n"); 67 68 threads = thread_map__new(-1, getpid(), UINT_MAX); 69 if (threads == NULL) { 70 pr_debug("thread_map__new\n"); 71 return -1; 72 } 73 74 evsel->core.attr.disabled = 1; 75 76 err = perf_evsel__open_per_thread(evsel, threads); 77 if (err) { 78 pr_debug("Failed to open event cpu-clock:u\n"); 79 return err; 80 } 81 82 perf_thread_map__put(threads); 83 return evsel__enable(evsel) == 0 ? TEST_OK : TEST_FAIL; 84 } 85 86 static int attach__current_enabled(struct evlist *evlist) 87 { 88 struct evsel *evsel = perf_evlist__last(evlist); 89 struct perf_thread_map *threads; 90 int err; 91 92 pr_debug("attaching to current thread as enabled\n"); 93 94 threads = thread_map__new(-1, getpid(), UINT_MAX); 95 if (threads == NULL) { 96 pr_debug("failed to call thread_map__new\n"); 97 return -1; 98 } 99 100 err = perf_evsel__open_per_thread(evsel, threads); 101 102 perf_thread_map__put(threads); 103 return err == 0 ? TEST_OK : TEST_FAIL; 104 } 105 106 static int detach__disable(struct evlist *evlist) 107 { 108 struct evsel *evsel = perf_evlist__last(evlist); 109 110 return evsel__enable(evsel); 111 } 112 113 static int attach__cpu_disabled(struct evlist *evlist) 114 { 115 struct evsel *evsel = perf_evlist__last(evlist); 116 struct perf_cpu_map *cpus; 117 int err; 118 119 pr_debug("attaching to CPU 0 as enabled\n"); 120 121 cpus = perf_cpu_map__new("0"); 122 if (cpus == NULL) { 123 pr_debug("failed to call perf_cpu_map__new\n"); 124 return -1; 125 } 126 127 evsel->core.attr.disabled = 1; 128 129 err = perf_evsel__open_per_cpu(evsel, cpus); 130 if (err) { 131 if (err == -EACCES) 132 return TEST_SKIP; 133 134 pr_debug("Failed to open event cpu-clock:u\n"); 135 return err; 136 } 137 138 perf_cpu_map__put(cpus); 139 return evsel__enable(evsel); 140 } 141 142 static int attach__cpu_enabled(struct evlist *evlist) 143 { 144 struct evsel *evsel = perf_evlist__last(evlist); 145 struct perf_cpu_map *cpus; 146 int err; 147 148 pr_debug("attaching to CPU 0 as enabled\n"); 149 150 cpus = perf_cpu_map__new("0"); 151 if (cpus == NULL) { 152 pr_debug("failed to call perf_cpu_map__new\n"); 153 return -1; 154 } 155 156 err = perf_evsel__open_per_cpu(evsel, cpus); 157 if (err == -EACCES) 158 return TEST_SKIP; 159 160 perf_cpu_map__put(cpus); 161 return err ? TEST_FAIL : TEST_OK; 162 } 163 164 static int test_times(int (attach)(struct evlist *), 165 int (detach)(struct evlist *)) 166 { 167 struct perf_counts_values count; 168 struct evlist *evlist = NULL; 169 struct evsel *evsel; 170 int err = -1, i; 171 172 evlist = evlist__new(); 173 if (!evlist) { 174 pr_debug("failed to create event list\n"); 175 goto out_err; 176 } 177 178 err = parse_events(evlist, "cpu-clock:u", NULL); 179 if (err) { 180 pr_debug("failed to parse event cpu-clock:u\n"); 181 goto out_err; 182 } 183 184 evsel = perf_evlist__last(evlist); 185 evsel->core.attr.read_format |= 186 PERF_FORMAT_TOTAL_TIME_ENABLED | 187 PERF_FORMAT_TOTAL_TIME_RUNNING; 188 189 err = attach(evlist); 190 if (err == TEST_SKIP) { 191 pr_debug(" SKIP : not enough rights\n"); 192 return err; 193 } 194 195 TEST_ASSERT_VAL("failed to attach", !err); 196 197 for (i = 0; i < 100000000; i++) { } 198 199 TEST_ASSERT_VAL("failed to detach", !detach(evlist)); 200 201 perf_evsel__read(&evsel->core, 0, 0, &count); 202 203 err = !(count.ena == count.run); 204 205 pr_debug(" %s: ena %" PRIu64", run %" PRIu64"\n", 206 !err ? "OK " : "FAILED", 207 count.ena, count.run); 208 209 out_err: 210 evlist__delete(evlist); 211 return !err ? TEST_OK : TEST_FAIL; 212 } 213 214 /* 215 * This test creates software event 'cpu-clock' 216 * attaches it in several ways (explained below) 217 * and checks that enabled and running times 218 * match. 219 */ 220 int test__event_times(struct test *test __maybe_unused, int subtest __maybe_unused) 221 { 222 int err, ret = 0; 223 224 #define _T(attach, detach) \ 225 err = test_times(attach, detach); \ 226 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \ 227 ret = err; 228 229 /* attach on newly spawned process after exec */ 230 _T(attach__enable_on_exec, detach__enable_on_exec) 231 /* attach on current process as enabled */ 232 _T(attach__current_enabled, detach__disable) 233 /* attach on current process as disabled */ 234 _T(attach__current_disabled, detach__disable) 235 /* attach on cpu as disabled */ 236 _T(attach__cpu_disabled, detach__disable) 237 /* attach on cpu as enabled */ 238 _T(attach__cpu_enabled, detach__disable) 239 240 #undef _T 241 return ret; 242 } 243