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