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