1 // SPDX-License-Identifier: GPL-2.0 2 #include <sys/time.h> 3 #include <sys/prctl.h> 4 #include <errno.h> 5 #include <limits.h> 6 #include <time.h> 7 #include <stdlib.h> 8 #include <linux/zalloc.h> 9 #include <linux/err.h> 10 #include <perf/cpumap.h> 11 #include <perf/evlist.h> 12 #include <perf/mmap.h> 13 14 #include "debug.h" 15 #include "parse-events.h" 16 #include "evlist.h" 17 #include "evsel.h" 18 #include "thread_map.h" 19 #include "record.h" 20 #include "tests.h" 21 #include "util/mmap.h" 22 #include "util/sample.h" 23 #include "pmu.h" 24 25 static int spin_sleep(void) 26 { 27 struct timeval start, now, diff, maxtime; 28 struct timespec ts; 29 int err, i; 30 31 maxtime.tv_sec = 0; 32 maxtime.tv_usec = 50000; 33 34 err = gettimeofday(&start, NULL); 35 if (err) 36 return err; 37 38 /* Spin for 50ms */ 39 while (1) { 40 for (i = 0; i < 1000; i++) 41 barrier(); 42 43 err = gettimeofday(&now, NULL); 44 if (err) 45 return err; 46 47 timersub(&now, &start, &diff); 48 if (timercmp(&diff, &maxtime, > /* For checkpatch */)) 49 break; 50 } 51 52 ts.tv_nsec = 50 * 1000 * 1000; 53 ts.tv_sec = 0; 54 55 /* Sleep for 50ms */ 56 err = nanosleep(&ts, NULL); 57 if (err == EINTR) 58 err = 0; 59 60 return err; 61 } 62 63 struct switch_tracking { 64 struct evsel *switch_evsel; 65 struct evsel *cycles_evsel; 66 pid_t *tids; 67 int nr_tids; 68 int comm_seen[4]; 69 int cycles_before_comm_1; 70 int cycles_between_comm_2_and_comm_3; 71 int cycles_after_comm_4; 72 }; 73 74 static int check_comm(struct switch_tracking *switch_tracking, 75 union perf_event *event, const char *comm, int nr) 76 { 77 if (event->header.type == PERF_RECORD_COMM && 78 (pid_t)event->comm.pid == getpid() && 79 (pid_t)event->comm.tid == getpid() && 80 strcmp(event->comm.comm, comm) == 0) { 81 if (switch_tracking->comm_seen[nr]) { 82 pr_debug("Duplicate comm event\n"); 83 return -1; 84 } 85 switch_tracking->comm_seen[nr] = 1; 86 pr_debug3("comm event: %s nr: %d\n", event->comm.comm, nr); 87 return 1; 88 } 89 return 0; 90 } 91 92 static int check_cpu(struct switch_tracking *switch_tracking, int cpu) 93 { 94 int i, nr = cpu + 1; 95 96 if (cpu < 0) 97 return -1; 98 99 if (!switch_tracking->tids) { 100 switch_tracking->tids = calloc(nr, sizeof(pid_t)); 101 if (!switch_tracking->tids) 102 return -1; 103 for (i = 0; i < nr; i++) 104 switch_tracking->tids[i] = -1; 105 switch_tracking->nr_tids = nr; 106 return 0; 107 } 108 109 if (cpu >= switch_tracking->nr_tids) { 110 void *addr; 111 112 addr = realloc(switch_tracking->tids, nr * sizeof(pid_t)); 113 if (!addr) 114 return -1; 115 switch_tracking->tids = addr; 116 for (i = switch_tracking->nr_tids; i < nr; i++) 117 switch_tracking->tids[i] = -1; 118 switch_tracking->nr_tids = nr; 119 return 0; 120 } 121 122 return 0; 123 } 124 125 static int process_sample_event(struct evlist *evlist, 126 union perf_event *event, 127 struct switch_tracking *switch_tracking) 128 { 129 struct perf_sample sample; 130 struct evsel *evsel; 131 pid_t next_tid, prev_tid; 132 int cpu, err; 133 134 if (evlist__parse_sample(evlist, event, &sample)) { 135 pr_debug("evlist__parse_sample failed\n"); 136 return -1; 137 } 138 139 evsel = evlist__id2evsel(evlist, sample.id); 140 if (evsel == switch_tracking->switch_evsel) { 141 next_tid = evsel__intval(evsel, &sample, "next_pid"); 142 prev_tid = evsel__intval(evsel, &sample, "prev_pid"); 143 cpu = sample.cpu; 144 pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n", 145 cpu, prev_tid, next_tid); 146 err = check_cpu(switch_tracking, cpu); 147 if (err) 148 return err; 149 /* 150 * Check for no missing sched_switch events i.e. that the 151 * evsel->core.system_wide flag has worked. 152 */ 153 if (switch_tracking->tids[cpu] != -1 && 154 switch_tracking->tids[cpu] != prev_tid) { 155 pr_debug("Missing sched_switch events\n"); 156 return -1; 157 } 158 switch_tracking->tids[cpu] = next_tid; 159 } 160 161 if (evsel == switch_tracking->cycles_evsel) { 162 pr_debug3("cycles event\n"); 163 if (!switch_tracking->comm_seen[0]) 164 switch_tracking->cycles_before_comm_1 = 1; 165 if (switch_tracking->comm_seen[1] && 166 !switch_tracking->comm_seen[2]) 167 switch_tracking->cycles_between_comm_2_and_comm_3 = 1; 168 if (switch_tracking->comm_seen[3]) 169 switch_tracking->cycles_after_comm_4 = 1; 170 } 171 172 return 0; 173 } 174 175 static int process_event(struct evlist *evlist, union perf_event *event, 176 struct switch_tracking *switch_tracking) 177 { 178 if (event->header.type == PERF_RECORD_SAMPLE) 179 return process_sample_event(evlist, event, switch_tracking); 180 181 if (event->header.type == PERF_RECORD_COMM) { 182 int err, done = 0; 183 184 err = check_comm(switch_tracking, event, "Test COMM 1", 0); 185 if (err < 0) 186 return -1; 187 done += err; 188 err = check_comm(switch_tracking, event, "Test COMM 2", 1); 189 if (err < 0) 190 return -1; 191 done += err; 192 err = check_comm(switch_tracking, event, "Test COMM 3", 2); 193 if (err < 0) 194 return -1; 195 done += err; 196 err = check_comm(switch_tracking, event, "Test COMM 4", 3); 197 if (err < 0) 198 return -1; 199 done += err; 200 if (done != 1) { 201 pr_debug("Unexpected comm event\n"); 202 return -1; 203 } 204 } 205 206 return 0; 207 } 208 209 struct event_node { 210 struct list_head list; 211 union perf_event *event; 212 u64 event_time; 213 }; 214 215 static int add_event(struct evlist *evlist, struct list_head *events, 216 union perf_event *event) 217 { 218 struct perf_sample sample; 219 struct event_node *node; 220 221 node = malloc(sizeof(struct event_node)); 222 if (!node) { 223 pr_debug("malloc failed\n"); 224 return -1; 225 } 226 node->event = event; 227 list_add(&node->list, events); 228 229 if (evlist__parse_sample(evlist, event, &sample)) { 230 pr_debug("evlist__parse_sample failed\n"); 231 return -1; 232 } 233 234 if (!sample.time) { 235 pr_debug("event with no time\n"); 236 return -1; 237 } 238 239 node->event_time = sample.time; 240 241 return 0; 242 } 243 244 static void free_event_nodes(struct list_head *events) 245 { 246 struct event_node *node; 247 248 while (!list_empty(events)) { 249 node = list_entry(events->next, struct event_node, list); 250 list_del_init(&node->list); 251 free(node); 252 } 253 } 254 255 static int compar(const void *a, const void *b) 256 { 257 const struct event_node *nodea = a; 258 const struct event_node *nodeb = b; 259 s64 cmp = nodea->event_time - nodeb->event_time; 260 261 return cmp; 262 } 263 264 static int process_events(struct evlist *evlist, 265 struct switch_tracking *switch_tracking) 266 { 267 union perf_event *event; 268 unsigned pos, cnt = 0; 269 LIST_HEAD(events); 270 struct event_node *events_array, *node; 271 struct mmap *md; 272 int i, ret; 273 274 for (i = 0; i < evlist->core.nr_mmaps; i++) { 275 md = &evlist->mmap[i]; 276 if (perf_mmap__read_init(&md->core) < 0) 277 continue; 278 279 while ((event = perf_mmap__read_event(&md->core)) != NULL) { 280 cnt += 1; 281 ret = add_event(evlist, &events, event); 282 perf_mmap__consume(&md->core); 283 if (ret < 0) 284 goto out_free_nodes; 285 } 286 perf_mmap__read_done(&md->core); 287 } 288 289 events_array = calloc(cnt, sizeof(struct event_node)); 290 if (!events_array) { 291 pr_debug("calloc failed\n"); 292 ret = -1; 293 goto out_free_nodes; 294 } 295 296 pos = 0; 297 list_for_each_entry(node, &events, list) 298 events_array[pos++] = *node; 299 300 qsort(events_array, cnt, sizeof(struct event_node), compar); 301 302 for (pos = 0; pos < cnt; pos++) { 303 ret = process_event(evlist, events_array[pos].event, 304 switch_tracking); 305 if (ret < 0) 306 goto out_free; 307 } 308 309 ret = 0; 310 out_free: 311 pr_debug("%u events recorded\n", cnt); 312 free(events_array); 313 out_free_nodes: 314 free_event_nodes(&events); 315 return ret; 316 } 317 318 /** 319 * test__switch_tracking - test using sched_switch and tracking events. 320 * 321 * This function implements a test that checks that sched_switch events and 322 * tracking events can be recorded for a workload (current process) using the 323 * evsel->core.system_wide and evsel->tracking flags (respectively) with other events 324 * sometimes enabled or disabled. 325 */ 326 static int test__switch_tracking(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 327 { 328 const char *sched_switch = "sched:sched_switch"; 329 const char *cycles = "cycles:u"; 330 struct switch_tracking switch_tracking = { .tids = NULL, }; 331 struct record_opts opts = { 332 .mmap_pages = UINT_MAX, 333 .user_freq = UINT_MAX, 334 .user_interval = ULLONG_MAX, 335 .freq = 4000, 336 .target = { 337 .uses_mmap = true, 338 }, 339 }; 340 struct perf_thread_map *threads = NULL; 341 struct perf_cpu_map *cpus = NULL; 342 struct evlist *evlist = NULL; 343 struct evsel *evsel, *cpu_clocks_evsel, *cycles_evsel; 344 struct evsel *switch_evsel, *tracking_evsel; 345 const char *comm; 346 int err = -1; 347 348 threads = thread_map__new(-1, getpid(), UINT_MAX); 349 if (!threads) { 350 pr_debug("thread_map__new failed!\n"); 351 goto out_err; 352 } 353 354 cpus = perf_cpu_map__new(NULL); 355 if (!cpus) { 356 pr_debug("perf_cpu_map__new failed!\n"); 357 goto out_err; 358 } 359 360 evlist = evlist__new(); 361 if (!evlist) { 362 pr_debug("evlist__new failed!\n"); 363 goto out_err; 364 } 365 366 perf_evlist__set_maps(&evlist->core, cpus, threads); 367 368 /* First event */ 369 err = parse_event(evlist, "cpu-clock:u"); 370 if (err) { 371 pr_debug("Failed to parse event dummy:u\n"); 372 goto out_err; 373 } 374 375 cpu_clocks_evsel = evlist__last(evlist); 376 377 /* Second event */ 378 if (perf_pmu__has_hybrid()) { 379 cycles = "cpu_core/cycles/u"; 380 err = parse_event(evlist, cycles); 381 if (err) { 382 cycles = "cpu_atom/cycles/u"; 383 pr_debug("Trying %s\n", cycles); 384 err = parse_event(evlist, cycles); 385 } 386 } else { 387 err = parse_event(evlist, cycles); 388 } 389 if (err) { 390 pr_debug("Failed to parse event %s\n", cycles); 391 goto out_err; 392 } 393 394 cycles_evsel = evlist__last(evlist); 395 396 /* Third event */ 397 if (!evlist__can_select_event(evlist, sched_switch)) { 398 pr_debug("No sched_switch\n"); 399 err = 0; 400 goto out; 401 } 402 403 switch_evsel = evlist__add_sched_switch(evlist, true); 404 if (IS_ERR(switch_evsel)) { 405 err = PTR_ERR(switch_evsel); 406 pr_debug("Failed to create event %s\n", sched_switch); 407 goto out_err; 408 } 409 410 switch_evsel->immediate = true; 411 412 /* Test moving an event to the front */ 413 if (cycles_evsel == evlist__first(evlist)) { 414 pr_debug("cycles event already at front"); 415 goto out_err; 416 } 417 evlist__to_front(evlist, cycles_evsel); 418 if (cycles_evsel != evlist__first(evlist)) { 419 pr_debug("Failed to move cycles event to front"); 420 goto out_err; 421 } 422 423 evsel__set_sample_bit(cycles_evsel, CPU); 424 evsel__set_sample_bit(cycles_evsel, TIME); 425 426 /* Fourth event */ 427 err = parse_event(evlist, "dummy:u"); 428 if (err) { 429 pr_debug("Failed to parse event dummy:u\n"); 430 goto out_err; 431 } 432 433 tracking_evsel = evlist__last(evlist); 434 435 evlist__set_tracking_event(evlist, tracking_evsel); 436 437 tracking_evsel->core.attr.freq = 0; 438 tracking_evsel->core.attr.sample_period = 1; 439 440 evsel__set_sample_bit(tracking_evsel, TIME); 441 442 /* Config events */ 443 evlist__config(evlist, &opts, NULL); 444 445 /* Check moved event is still at the front */ 446 if (cycles_evsel != evlist__first(evlist)) { 447 pr_debug("Front event no longer at front"); 448 goto out_err; 449 } 450 451 /* Check tracking event is tracking */ 452 if (!tracking_evsel->core.attr.mmap || !tracking_evsel->core.attr.comm) { 453 pr_debug("Tracking event not tracking\n"); 454 goto out_err; 455 } 456 457 /* Check non-tracking events are not tracking */ 458 evlist__for_each_entry(evlist, evsel) { 459 if (evsel != tracking_evsel) { 460 if (evsel->core.attr.mmap || evsel->core.attr.comm) { 461 pr_debug("Non-tracking event is tracking\n"); 462 goto out_err; 463 } 464 } 465 } 466 467 if (evlist__open(evlist) < 0) { 468 pr_debug("Not supported\n"); 469 err = 0; 470 goto out; 471 } 472 473 err = evlist__mmap(evlist, UINT_MAX); 474 if (err) { 475 pr_debug("evlist__mmap failed!\n"); 476 goto out_err; 477 } 478 479 evlist__enable(evlist); 480 481 err = evsel__disable(cpu_clocks_evsel); 482 if (err) { 483 pr_debug("perf_evlist__disable_event failed!\n"); 484 goto out_err; 485 } 486 487 err = spin_sleep(); 488 if (err) { 489 pr_debug("spin_sleep failed!\n"); 490 goto out_err; 491 } 492 493 comm = "Test COMM 1"; 494 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0); 495 if (err) { 496 pr_debug("PR_SET_NAME failed!\n"); 497 goto out_err; 498 } 499 500 err = evsel__disable(cycles_evsel); 501 if (err) { 502 pr_debug("perf_evlist__disable_event failed!\n"); 503 goto out_err; 504 } 505 506 comm = "Test COMM 2"; 507 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0); 508 if (err) { 509 pr_debug("PR_SET_NAME failed!\n"); 510 goto out_err; 511 } 512 513 err = spin_sleep(); 514 if (err) { 515 pr_debug("spin_sleep failed!\n"); 516 goto out_err; 517 } 518 519 comm = "Test COMM 3"; 520 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0); 521 if (err) { 522 pr_debug("PR_SET_NAME failed!\n"); 523 goto out_err; 524 } 525 526 err = evsel__enable(cycles_evsel); 527 if (err) { 528 pr_debug("perf_evlist__disable_event failed!\n"); 529 goto out_err; 530 } 531 532 comm = "Test COMM 4"; 533 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0); 534 if (err) { 535 pr_debug("PR_SET_NAME failed!\n"); 536 goto out_err; 537 } 538 539 err = spin_sleep(); 540 if (err) { 541 pr_debug("spin_sleep failed!\n"); 542 goto out_err; 543 } 544 545 evlist__disable(evlist); 546 547 switch_tracking.switch_evsel = switch_evsel; 548 switch_tracking.cycles_evsel = cycles_evsel; 549 550 err = process_events(evlist, &switch_tracking); 551 552 zfree(&switch_tracking.tids); 553 554 if (err) 555 goto out_err; 556 557 /* Check all 4 comm events were seen i.e. that evsel->tracking works */ 558 if (!switch_tracking.comm_seen[0] || !switch_tracking.comm_seen[1] || 559 !switch_tracking.comm_seen[2] || !switch_tracking.comm_seen[3]) { 560 pr_debug("Missing comm events\n"); 561 goto out_err; 562 } 563 564 /* Check cycles event got enabled */ 565 if (!switch_tracking.cycles_before_comm_1) { 566 pr_debug("Missing cycles events\n"); 567 goto out_err; 568 } 569 570 /* Check cycles event got disabled */ 571 if (switch_tracking.cycles_between_comm_2_and_comm_3) { 572 pr_debug("cycles events even though event was disabled\n"); 573 goto out_err; 574 } 575 576 /* Check cycles event got enabled again */ 577 if (!switch_tracking.cycles_after_comm_4) { 578 pr_debug("Missing cycles events\n"); 579 goto out_err; 580 } 581 out: 582 if (evlist) { 583 evlist__disable(evlist); 584 evlist__delete(evlist); 585 } 586 perf_cpu_map__put(cpus); 587 perf_thread_map__put(threads); 588 589 return err; 590 591 out_err: 592 err = -1; 593 goto out; 594 } 595 596 DEFINE_SUITE("Track with sched_switch", switch_tracking); 597