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