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