xref: /openbmc/linux/tools/perf/tests/switch-tracking.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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>
9182bb594SNamhyung Kim #include <linux/err.h>
109c3516d1SJiri Olsa #include <perf/cpumap.h>
11453fa030SJiri Olsa #include <perf/evlist.h>
127728fa0cSJiri Olsa #include <perf/mmap.h>
13d44bc558SAdrian Hunter 
14b4209025SArnaldo Carvalho de Melo #include "debug.h"
15d44bc558SAdrian Hunter #include "parse-events.h"
16d44bc558SAdrian Hunter #include "evlist.h"
17d44bc558SAdrian Hunter #include "evsel.h"
18d44bc558SAdrian Hunter #include "thread_map.h"
19aeb00b1aSArnaldo Carvalho de Melo #include "record.h"
20d44bc558SAdrian Hunter #include "tests.h"
21e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
229823147dSArnaldo Carvalho de Melo #include "util/sample.h"
23*1eaf496eSIan Rogers #include "pmus.h"
24d44bc558SAdrian Hunter 
spin_sleep(void)25d44bc558SAdrian Hunter static int spin_sleep(void)
26d44bc558SAdrian Hunter {
27d44bc558SAdrian Hunter 	struct timeval start, now, diff, maxtime;
28d44bc558SAdrian Hunter 	struct timespec ts;
29d44bc558SAdrian Hunter 	int err, i;
30d44bc558SAdrian Hunter 
31d44bc558SAdrian Hunter 	maxtime.tv_sec = 0;
32d44bc558SAdrian Hunter 	maxtime.tv_usec = 50000;
33d44bc558SAdrian Hunter 
34d44bc558SAdrian Hunter 	err = gettimeofday(&start, NULL);
35d44bc558SAdrian Hunter 	if (err)
36d44bc558SAdrian Hunter 		return err;
37d44bc558SAdrian Hunter 
38d44bc558SAdrian Hunter 	/* Spin for 50ms */
39d44bc558SAdrian Hunter 	while (1) {
40d44bc558SAdrian Hunter 		for (i = 0; i < 1000; i++)
41d44bc558SAdrian Hunter 			barrier();
42d44bc558SAdrian Hunter 
43d44bc558SAdrian Hunter 		err = gettimeofday(&now, NULL);
44d44bc558SAdrian Hunter 		if (err)
45d44bc558SAdrian Hunter 			return err;
46d44bc558SAdrian Hunter 
47d44bc558SAdrian Hunter 		timersub(&now, &start, &diff);
48d44bc558SAdrian Hunter 		if (timercmp(&diff, &maxtime, > /* For checkpatch */))
49d44bc558SAdrian Hunter 			break;
50d44bc558SAdrian Hunter 	}
51d44bc558SAdrian Hunter 
52d44bc558SAdrian Hunter 	ts.tv_nsec = 50 * 1000 * 1000;
53d44bc558SAdrian Hunter 	ts.tv_sec = 0;
54d44bc558SAdrian Hunter 
55d44bc558SAdrian Hunter 	/* Sleep for 50ms */
56d44bc558SAdrian Hunter 	err = nanosleep(&ts, NULL);
57d44bc558SAdrian Hunter 	if (err == EINTR)
58d44bc558SAdrian Hunter 		err = 0;
59d44bc558SAdrian Hunter 
60d44bc558SAdrian Hunter 	return err;
61d44bc558SAdrian Hunter }
62d44bc558SAdrian Hunter 
63d44bc558SAdrian Hunter struct switch_tracking {
6432dcd021SJiri Olsa 	struct evsel *switch_evsel;
6532dcd021SJiri Olsa 	struct evsel *cycles_evsel;
66d44bc558SAdrian Hunter 	pid_t *tids;
67d44bc558SAdrian Hunter 	int nr_tids;
68d44bc558SAdrian Hunter 	int comm_seen[4];
69d44bc558SAdrian Hunter 	int cycles_before_comm_1;
70d44bc558SAdrian Hunter 	int cycles_between_comm_2_and_comm_3;
71d44bc558SAdrian Hunter 	int cycles_after_comm_4;
72d44bc558SAdrian Hunter };
73d44bc558SAdrian Hunter 
check_comm(struct switch_tracking * switch_tracking,union perf_event * event,const char * comm,int nr)74d44bc558SAdrian Hunter static int check_comm(struct switch_tracking *switch_tracking,
75d44bc558SAdrian Hunter 		      union perf_event *event, const char *comm, int nr)
76d44bc558SAdrian Hunter {
77d44bc558SAdrian Hunter 	if (event->header.type == PERF_RECORD_COMM &&
78d44bc558SAdrian Hunter 	    (pid_t)event->comm.pid == getpid() &&
79d44bc558SAdrian Hunter 	    (pid_t)event->comm.tid == getpid() &&
80d44bc558SAdrian Hunter 	    strcmp(event->comm.comm, comm) == 0) {
81d44bc558SAdrian Hunter 		if (switch_tracking->comm_seen[nr]) {
82d44bc558SAdrian Hunter 			pr_debug("Duplicate comm event\n");
83d44bc558SAdrian Hunter 			return -1;
84d44bc558SAdrian Hunter 		}
85d44bc558SAdrian Hunter 		switch_tracking->comm_seen[nr] = 1;
86d44bc558SAdrian Hunter 		pr_debug3("comm event: %s nr: %d\n", event->comm.comm, nr);
87d44bc558SAdrian Hunter 		return 1;
88d44bc558SAdrian Hunter 	}
89d44bc558SAdrian Hunter 	return 0;
90d44bc558SAdrian Hunter }
91d44bc558SAdrian Hunter 
check_cpu(struct switch_tracking * switch_tracking,int cpu)92d44bc558SAdrian Hunter static int check_cpu(struct switch_tracking *switch_tracking, int cpu)
93d44bc558SAdrian Hunter {
94d44bc558SAdrian Hunter 	int i, nr = cpu + 1;
95d44bc558SAdrian Hunter 
96d44bc558SAdrian Hunter 	if (cpu < 0)
97d44bc558SAdrian Hunter 		return -1;
98d44bc558SAdrian Hunter 
99d44bc558SAdrian Hunter 	if (!switch_tracking->tids) {
100d44bc558SAdrian Hunter 		switch_tracking->tids = calloc(nr, sizeof(pid_t));
101d44bc558SAdrian Hunter 		if (!switch_tracking->tids)
102d44bc558SAdrian Hunter 			return -1;
103d44bc558SAdrian Hunter 		for (i = 0; i < nr; i++)
104d44bc558SAdrian Hunter 			switch_tracking->tids[i] = -1;
105d44bc558SAdrian Hunter 		switch_tracking->nr_tids = nr;
106d44bc558SAdrian Hunter 		return 0;
107d44bc558SAdrian Hunter 	}
108d44bc558SAdrian Hunter 
109d44bc558SAdrian Hunter 	if (cpu >= switch_tracking->nr_tids) {
110d44bc558SAdrian Hunter 		void *addr;
111d44bc558SAdrian Hunter 
112d44bc558SAdrian Hunter 		addr = realloc(switch_tracking->tids, nr * sizeof(pid_t));
113d44bc558SAdrian Hunter 		if (!addr)
114d44bc558SAdrian Hunter 			return -1;
115d44bc558SAdrian Hunter 		switch_tracking->tids = addr;
116d44bc558SAdrian Hunter 		for (i = switch_tracking->nr_tids; i < nr; i++)
117d44bc558SAdrian Hunter 			switch_tracking->tids[i] = -1;
118d44bc558SAdrian Hunter 		switch_tracking->nr_tids = nr;
119d44bc558SAdrian Hunter 		return 0;
120d44bc558SAdrian Hunter 	}
121d44bc558SAdrian Hunter 
122d44bc558SAdrian Hunter 	return 0;
123d44bc558SAdrian Hunter }
124d44bc558SAdrian Hunter 
process_sample_event(struct evlist * evlist,union perf_event * event,struct switch_tracking * switch_tracking)12563503dbaSJiri Olsa static int process_sample_event(struct evlist *evlist,
126d44bc558SAdrian Hunter 				union perf_event *event,
127d44bc558SAdrian Hunter 				struct switch_tracking *switch_tracking)
128d44bc558SAdrian Hunter {
129d44bc558SAdrian Hunter 	struct perf_sample sample;
13032dcd021SJiri Olsa 	struct evsel *evsel;
131d44bc558SAdrian Hunter 	pid_t next_tid, prev_tid;
132d44bc558SAdrian Hunter 	int cpu, err;
133d44bc558SAdrian Hunter 
1342a6599cdSArnaldo Carvalho de Melo 	if (evlist__parse_sample(evlist, event, &sample)) {
1352a6599cdSArnaldo Carvalho de Melo 		pr_debug("evlist__parse_sample failed\n");
136d44bc558SAdrian Hunter 		return -1;
137d44bc558SAdrian Hunter 	}
138d44bc558SAdrian Hunter 
1393ccf8a7bSArnaldo Carvalho de Melo 	evsel = evlist__id2evsel(evlist, sample.id);
140d44bc558SAdrian Hunter 	if (evsel == switch_tracking->switch_evsel) {
141efc0cdc9SArnaldo Carvalho de Melo 		next_tid = evsel__intval(evsel, &sample, "next_pid");
142efc0cdc9SArnaldo Carvalho de Melo 		prev_tid = evsel__intval(evsel, &sample, "prev_pid");
143d44bc558SAdrian Hunter 		cpu = sample.cpu;
144d44bc558SAdrian Hunter 		pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
145d44bc558SAdrian Hunter 			  cpu, prev_tid, next_tid);
146d44bc558SAdrian Hunter 		err = check_cpu(switch_tracking, cpu);
147d44bc558SAdrian Hunter 		if (err)
148d44bc558SAdrian Hunter 			return err;
149d44bc558SAdrian Hunter 		/*
150d44bc558SAdrian Hunter 		 * Check for no missing sched_switch events i.e. that the
151648b5af3SJiri Olsa 		 * evsel->core.system_wide flag has worked.
152d44bc558SAdrian Hunter 		 */
153d44bc558SAdrian Hunter 		if (switch_tracking->tids[cpu] != -1 &&
154d44bc558SAdrian Hunter 		    switch_tracking->tids[cpu] != prev_tid) {
155d44bc558SAdrian Hunter 			pr_debug("Missing sched_switch events\n");
156d44bc558SAdrian Hunter 			return -1;
157d44bc558SAdrian Hunter 		}
158d44bc558SAdrian Hunter 		switch_tracking->tids[cpu] = next_tid;
159d44bc558SAdrian Hunter 	}
160d44bc558SAdrian Hunter 
161d44bc558SAdrian Hunter 	if (evsel == switch_tracking->cycles_evsel) {
162d44bc558SAdrian Hunter 		pr_debug3("cycles event\n");
163d44bc558SAdrian Hunter 		if (!switch_tracking->comm_seen[0])
164d44bc558SAdrian Hunter 			switch_tracking->cycles_before_comm_1 = 1;
165d44bc558SAdrian Hunter 		if (switch_tracking->comm_seen[1] &&
166d44bc558SAdrian Hunter 		    !switch_tracking->comm_seen[2])
167d44bc558SAdrian Hunter 			switch_tracking->cycles_between_comm_2_and_comm_3 = 1;
168d44bc558SAdrian Hunter 		if (switch_tracking->comm_seen[3])
169d44bc558SAdrian Hunter 			switch_tracking->cycles_after_comm_4 = 1;
170d44bc558SAdrian Hunter 	}
171d44bc558SAdrian Hunter 
172d44bc558SAdrian Hunter 	return 0;
173d44bc558SAdrian Hunter }
174d44bc558SAdrian Hunter 
process_event(struct evlist * evlist,union perf_event * event,struct switch_tracking * switch_tracking)17563503dbaSJiri Olsa static int process_event(struct evlist *evlist, union perf_event *event,
176d44bc558SAdrian Hunter 			 struct switch_tracking *switch_tracking)
177d44bc558SAdrian Hunter {
178d44bc558SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE)
179d44bc558SAdrian Hunter 		return process_sample_event(evlist, event, switch_tracking);
180d44bc558SAdrian Hunter 
181d44bc558SAdrian Hunter 	if (event->header.type == PERF_RECORD_COMM) {
182d44bc558SAdrian Hunter 		int err, done = 0;
183d44bc558SAdrian Hunter 
184d44bc558SAdrian Hunter 		err = check_comm(switch_tracking, event, "Test COMM 1", 0);
185d44bc558SAdrian Hunter 		if (err < 0)
186d44bc558SAdrian Hunter 			return -1;
187d44bc558SAdrian Hunter 		done += err;
188d44bc558SAdrian Hunter 		err = check_comm(switch_tracking, event, "Test COMM 2", 1);
189d44bc558SAdrian Hunter 		if (err < 0)
190d44bc558SAdrian Hunter 			return -1;
191d44bc558SAdrian Hunter 		done += err;
192d44bc558SAdrian Hunter 		err = check_comm(switch_tracking, event, "Test COMM 3", 2);
193d44bc558SAdrian Hunter 		if (err < 0)
194d44bc558SAdrian Hunter 			return -1;
195d44bc558SAdrian Hunter 		done += err;
196d44bc558SAdrian Hunter 		err = check_comm(switch_tracking, event, "Test COMM 4", 3);
197d44bc558SAdrian Hunter 		if (err < 0)
198d44bc558SAdrian Hunter 			return -1;
199d44bc558SAdrian Hunter 		done += err;
200d44bc558SAdrian Hunter 		if (done != 1) {
201d44bc558SAdrian Hunter 			pr_debug("Unexpected comm event\n");
202d44bc558SAdrian Hunter 			return -1;
203d44bc558SAdrian Hunter 		}
204d44bc558SAdrian Hunter 	}
205d44bc558SAdrian Hunter 
206d44bc558SAdrian Hunter 	return 0;
207d44bc558SAdrian Hunter }
208d44bc558SAdrian Hunter 
209d44bc558SAdrian Hunter struct event_node {
210d44bc558SAdrian Hunter 	struct list_head list;
211d44bc558SAdrian Hunter 	union perf_event *event;
212d44bc558SAdrian Hunter 	u64 event_time;
213d44bc558SAdrian Hunter };
214d44bc558SAdrian Hunter 
add_event(struct evlist * evlist,struct list_head * events,union perf_event * event)21563503dbaSJiri Olsa static int add_event(struct evlist *evlist, struct list_head *events,
216d44bc558SAdrian Hunter 		     union perf_event *event)
217d44bc558SAdrian Hunter {
218d44bc558SAdrian Hunter 	struct perf_sample sample;
219d44bc558SAdrian Hunter 	struct event_node *node;
220d44bc558SAdrian Hunter 
221d44bc558SAdrian Hunter 	node = malloc(sizeof(struct event_node));
222d44bc558SAdrian Hunter 	if (!node) {
223d44bc558SAdrian Hunter 		pr_debug("malloc failed\n");
224d44bc558SAdrian Hunter 		return -1;
225d44bc558SAdrian Hunter 	}
226d44bc558SAdrian Hunter 	node->event = event;
227d44bc558SAdrian Hunter 	list_add(&node->list, events);
228d44bc558SAdrian Hunter 
2292a6599cdSArnaldo Carvalho de Melo 	if (evlist__parse_sample(evlist, event, &sample)) {
2302a6599cdSArnaldo Carvalho de Melo 		pr_debug("evlist__parse_sample failed\n");
231d44bc558SAdrian Hunter 		return -1;
232d44bc558SAdrian Hunter 	}
233d44bc558SAdrian Hunter 
234d44bc558SAdrian Hunter 	if (!sample.time) {
235d44bc558SAdrian Hunter 		pr_debug("event with no time\n");
236d44bc558SAdrian Hunter 		return -1;
237d44bc558SAdrian Hunter 	}
238d44bc558SAdrian Hunter 
239d44bc558SAdrian Hunter 	node->event_time = sample.time;
240d44bc558SAdrian Hunter 
241d44bc558SAdrian Hunter 	return 0;
242d44bc558SAdrian Hunter }
243d44bc558SAdrian Hunter 
free_event_nodes(struct list_head * events)244d44bc558SAdrian Hunter static void free_event_nodes(struct list_head *events)
245d44bc558SAdrian Hunter {
246d44bc558SAdrian Hunter 	struct event_node *node;
247d44bc558SAdrian Hunter 
248d44bc558SAdrian Hunter 	while (!list_empty(events)) {
249d44bc558SAdrian Hunter 		node = list_entry(events->next, struct event_node, list);
250e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&node->list);
251d44bc558SAdrian Hunter 		free(node);
252d44bc558SAdrian Hunter 	}
253d44bc558SAdrian Hunter }
254d44bc558SAdrian Hunter 
compar(const void * a,const void * b)255d44bc558SAdrian Hunter static int compar(const void *a, const void *b)
256d44bc558SAdrian Hunter {
257d44bc558SAdrian Hunter 	const struct event_node *nodea = a;
258d44bc558SAdrian Hunter 	const struct event_node *nodeb = b;
259d44bc558SAdrian Hunter 	s64 cmp = nodea->event_time - nodeb->event_time;
260d44bc558SAdrian Hunter 
261d44bc558SAdrian Hunter 	return cmp;
262d44bc558SAdrian Hunter }
263d44bc558SAdrian Hunter 
process_events(struct evlist * evlist,struct switch_tracking * switch_tracking)26463503dbaSJiri Olsa static int process_events(struct evlist *evlist,
265d44bc558SAdrian Hunter 			  struct switch_tracking *switch_tracking)
266d44bc558SAdrian Hunter {
267d44bc558SAdrian Hunter 	union perf_event *event;
268d44bc558SAdrian Hunter 	unsigned pos, cnt = 0;
269d44bc558SAdrian Hunter 	LIST_HEAD(events);
270d44bc558SAdrian Hunter 	struct event_node *events_array, *node;
271a5830532SJiri Olsa 	struct mmap *md;
272d44bc558SAdrian Hunter 	int i, ret;
273d44bc558SAdrian Hunter 
274c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
275ee4024ffSKan Liang 		md = &evlist->mmap[i];
2767c4d4182SJiri Olsa 		if (perf_mmap__read_init(&md->core) < 0)
277ee4024ffSKan Liang 			continue;
278ee4024ffSKan Liang 
279151ed5d7SJiri Olsa 		while ((event = perf_mmap__read_event(&md->core)) != NULL) {
280d44bc558SAdrian Hunter 			cnt += 1;
281d44bc558SAdrian Hunter 			ret = add_event(evlist, &events, event);
2827728fa0cSJiri Olsa 			 perf_mmap__consume(&md->core);
283d44bc558SAdrian Hunter 			if (ret < 0)
284d44bc558SAdrian Hunter 				goto out_free_nodes;
285d44bc558SAdrian Hunter 		}
28632fdc2caSJiri Olsa 		perf_mmap__read_done(&md->core);
287d44bc558SAdrian Hunter 	}
288d44bc558SAdrian Hunter 
289d44bc558SAdrian Hunter 	events_array = calloc(cnt, sizeof(struct event_node));
290d44bc558SAdrian Hunter 	if (!events_array) {
291d44bc558SAdrian Hunter 		pr_debug("calloc failed\n");
292d44bc558SAdrian Hunter 		ret = -1;
293d44bc558SAdrian Hunter 		goto out_free_nodes;
294d44bc558SAdrian Hunter 	}
295d44bc558SAdrian Hunter 
296d44bc558SAdrian Hunter 	pos = 0;
297d44bc558SAdrian Hunter 	list_for_each_entry(node, &events, list)
298d44bc558SAdrian Hunter 		events_array[pos++] = *node;
299d44bc558SAdrian Hunter 
300d44bc558SAdrian Hunter 	qsort(events_array, cnt, sizeof(struct event_node), compar);
301d44bc558SAdrian Hunter 
302d44bc558SAdrian Hunter 	for (pos = 0; pos < cnt; pos++) {
303d44bc558SAdrian Hunter 		ret = process_event(evlist, events_array[pos].event,
304d44bc558SAdrian Hunter 				    switch_tracking);
305d44bc558SAdrian Hunter 		if (ret < 0)
306d44bc558SAdrian Hunter 			goto out_free;
307d44bc558SAdrian Hunter 	}
308d44bc558SAdrian Hunter 
309d44bc558SAdrian Hunter 	ret = 0;
310d44bc558SAdrian Hunter out_free:
311d44bc558SAdrian Hunter 	pr_debug("%u events recorded\n", cnt);
312d44bc558SAdrian Hunter 	free(events_array);
313d44bc558SAdrian Hunter out_free_nodes:
314d44bc558SAdrian Hunter 	free_event_nodes(&events);
315d44bc558SAdrian Hunter 	return ret;
316d44bc558SAdrian Hunter }
317d44bc558SAdrian Hunter 
318d44bc558SAdrian Hunter /**
319d44bc558SAdrian Hunter  * test__switch_tracking - test using sched_switch and tracking events.
320d44bc558SAdrian Hunter  *
321d44bc558SAdrian Hunter  * This function implements a test that checks that sched_switch events and
322d44bc558SAdrian Hunter  * tracking events can be recorded for a workload (current process) using the
323648b5af3SJiri Olsa  * evsel->core.system_wide and evsel->tracking flags (respectively) with other events
324d44bc558SAdrian Hunter  * sometimes enabled or disabled.
325d44bc558SAdrian Hunter  */
test__switch_tracking(struct test_suite * test __maybe_unused,int subtest __maybe_unused)32633f44bfdSIan Rogers static int test__switch_tracking(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
327d44bc558SAdrian Hunter {
328d44bc558SAdrian Hunter 	const char *sched_switch = "sched:sched_switch";
3291da1d607SAdrian Hunter 	const char *cycles = "cycles:u";
330d44bc558SAdrian Hunter 	struct switch_tracking switch_tracking = { .tids = NULL, };
331d44bc558SAdrian Hunter 	struct record_opts opts = {
332d44bc558SAdrian Hunter 		.mmap_pages	     = UINT_MAX,
333d44bc558SAdrian Hunter 		.user_freq	     = UINT_MAX,
334d44bc558SAdrian Hunter 		.user_interval	     = ULLONG_MAX,
335d44bc558SAdrian Hunter 		.freq		     = 4000,
336d44bc558SAdrian Hunter 		.target		     = {
337d44bc558SAdrian Hunter 			.uses_mmap   = true,
338d44bc558SAdrian Hunter 		},
339d44bc558SAdrian Hunter 	};
3409749b90eSJiri Olsa 	struct perf_thread_map *threads = NULL;
341f854839bSJiri Olsa 	struct perf_cpu_map *cpus = NULL;
34263503dbaSJiri Olsa 	struct evlist *evlist = NULL;
34332dcd021SJiri Olsa 	struct evsel *evsel, *cpu_clocks_evsel, *cycles_evsel;
34432dcd021SJiri Olsa 	struct evsel *switch_evsel, *tracking_evsel;
345d44bc558SAdrian Hunter 	const char *comm;
346d44bc558SAdrian Hunter 	int err = -1;
347d44bc558SAdrian Hunter 
348d44bc558SAdrian Hunter 	threads = thread_map__new(-1, getpid(), UINT_MAX);
349d44bc558SAdrian Hunter 	if (!threads) {
350d44bc558SAdrian Hunter 		pr_debug("thread_map__new failed!\n");
351d44bc558SAdrian Hunter 		goto out_err;
352d44bc558SAdrian Hunter 	}
353d44bc558SAdrian Hunter 
3549c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
355d44bc558SAdrian Hunter 	if (!cpus) {
3569c3516d1SJiri Olsa 		pr_debug("perf_cpu_map__new failed!\n");
357d44bc558SAdrian Hunter 		goto out_err;
358d44bc558SAdrian Hunter 	}
359d44bc558SAdrian Hunter 
3600f98b11cSJiri Olsa 	evlist = evlist__new();
361d44bc558SAdrian Hunter 	if (!evlist) {
3620f98b11cSJiri Olsa 		pr_debug("evlist__new failed!\n");
363d44bc558SAdrian Hunter 		goto out_err;
364d44bc558SAdrian Hunter 	}
365d44bc558SAdrian Hunter 
366453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
367d44bc558SAdrian Hunter 
368d44bc558SAdrian Hunter 	/* First event */
369806731a9SAdrian Hunter 	err = parse_event(evlist, "cpu-clock:u");
370d44bc558SAdrian Hunter 	if (err) {
371d44bc558SAdrian Hunter 		pr_debug("Failed to parse event dummy:u\n");
372d44bc558SAdrian Hunter 		goto out_err;
373d44bc558SAdrian Hunter 	}
374d44bc558SAdrian Hunter 
375515dbe48SJiri Olsa 	cpu_clocks_evsel = evlist__last(evlist);
376d44bc558SAdrian Hunter 
377d44bc558SAdrian Hunter 	/* Second event */
378806731a9SAdrian Hunter 	err = parse_event(evlist, cycles);
379d44bc558SAdrian Hunter 	if (err) {
3801da1d607SAdrian Hunter 		pr_debug("Failed to parse event %s\n", cycles);
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 
393182bb594SNamhyung Kim 	switch_evsel = evlist__add_sched_switch(evlist, true);
394182bb594SNamhyung Kim 	if (IS_ERR(switch_evsel)) {
395182bb594SNamhyung Kim 		err = PTR_ERR(switch_evsel);
396182bb594SNamhyung Kim 		pr_debug("Failed to create event %s\n", sched_switch);
397d44bc558SAdrian Hunter 		goto out_err;
398d44bc558SAdrian Hunter 	}
399d44bc558SAdrian Hunter 
400d44bc558SAdrian Hunter 	switch_evsel->immediate = true;
401d44bc558SAdrian Hunter 
402d44bc558SAdrian Hunter 	/* Test moving an event to the front */
403515dbe48SJiri Olsa 	if (cycles_evsel == evlist__first(evlist)) {
404d44bc558SAdrian Hunter 		pr_debug("cycles event already at front");
405d44bc558SAdrian Hunter 		goto out_err;
406d44bc558SAdrian Hunter 	}
407e414fd1aSArnaldo Carvalho de Melo 	evlist__to_front(evlist, cycles_evsel);
408515dbe48SJiri Olsa 	if (cycles_evsel != evlist__first(evlist)) {
409d44bc558SAdrian Hunter 		pr_debug("Failed to move cycles event to front");
410d44bc558SAdrian Hunter 		goto out_err;
411d44bc558SAdrian Hunter 	}
412d44bc558SAdrian Hunter 
413862b2f8fSArnaldo Carvalho de Melo 	evsel__set_sample_bit(cycles_evsel, CPU);
414862b2f8fSArnaldo Carvalho de Melo 	evsel__set_sample_bit(cycles_evsel, TIME);
415d44bc558SAdrian Hunter 
416d44bc558SAdrian Hunter 	/* Fourth event */
417806731a9SAdrian Hunter 	err = parse_event(evlist, "dummy:u");
418d44bc558SAdrian Hunter 	if (err) {
419d44bc558SAdrian Hunter 		pr_debug("Failed to parse event dummy:u\n");
420d44bc558SAdrian Hunter 		goto out_err;
421d44bc558SAdrian Hunter 	}
422d44bc558SAdrian Hunter 
423515dbe48SJiri Olsa 	tracking_evsel = evlist__last(evlist);
424d44bc558SAdrian Hunter 
425e80db255SArnaldo Carvalho de Melo 	evlist__set_tracking_event(evlist, tracking_evsel);
426d44bc558SAdrian Hunter 
4271fc632ceSJiri Olsa 	tracking_evsel->core.attr.freq = 0;
4281fc632ceSJiri Olsa 	tracking_evsel->core.attr.sample_period = 1;
429d44bc558SAdrian Hunter 
430862b2f8fSArnaldo Carvalho de Melo 	evsel__set_sample_bit(tracking_evsel, TIME);
431d44bc558SAdrian Hunter 
432d44bc558SAdrian Hunter 	/* Config events */
43378e1bc25SArnaldo Carvalho de Melo 	evlist__config(evlist, &opts, NULL);
434d44bc558SAdrian Hunter 
435d44bc558SAdrian Hunter 	/* Check moved event is still at the front */
436515dbe48SJiri Olsa 	if (cycles_evsel != evlist__first(evlist)) {
437d44bc558SAdrian Hunter 		pr_debug("Front event no longer at front");
438d44bc558SAdrian Hunter 		goto out_err;
439d44bc558SAdrian Hunter 	}
440d44bc558SAdrian Hunter 
441d44bc558SAdrian Hunter 	/* Check tracking event is tracking */
4421fc632ceSJiri Olsa 	if (!tracking_evsel->core.attr.mmap || !tracking_evsel->core.attr.comm) {
443d44bc558SAdrian Hunter 		pr_debug("Tracking event not tracking\n");
444d44bc558SAdrian Hunter 		goto out_err;
445d44bc558SAdrian Hunter 	}
446d44bc558SAdrian Hunter 
447d44bc558SAdrian Hunter 	/* Check non-tracking events are not tracking */
448e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
449d44bc558SAdrian Hunter 		if (evsel != tracking_evsel) {
4501fc632ceSJiri Olsa 			if (evsel->core.attr.mmap || evsel->core.attr.comm) {
451d44bc558SAdrian Hunter 				pr_debug("Non-tracking event is tracking\n");
452d44bc558SAdrian Hunter 				goto out_err;
453d44bc558SAdrian Hunter 			}
454d44bc558SAdrian Hunter 		}
455d44bc558SAdrian Hunter 	}
456d44bc558SAdrian Hunter 
457474ddc4cSJiri Olsa 	if (evlist__open(evlist) < 0) {
458597bdeb4SWang Nan 		pr_debug("Not supported\n");
459d44bc558SAdrian Hunter 		err = 0;
460d44bc558SAdrian Hunter 		goto out;
461d44bc558SAdrian Hunter 	}
462d44bc558SAdrian Hunter 
4639521b5f2SJiri Olsa 	err = evlist__mmap(evlist, UINT_MAX);
464d44bc558SAdrian Hunter 	if (err) {
4659521b5f2SJiri Olsa 		pr_debug("evlist__mmap failed!\n");
466d44bc558SAdrian Hunter 		goto out_err;
467d44bc558SAdrian Hunter 	}
468d44bc558SAdrian Hunter 
4691c87f165SJiri Olsa 	evlist__enable(evlist);
470d44bc558SAdrian Hunter 
4719a10bb22SJiri Olsa 	err = evsel__disable(cpu_clocks_evsel);
472d44bc558SAdrian Hunter 	if (err) {
473d44bc558SAdrian Hunter 		pr_debug("perf_evlist__disable_event failed!\n");
474d44bc558SAdrian Hunter 		goto out_err;
475d44bc558SAdrian Hunter 	}
476d44bc558SAdrian Hunter 
477d44bc558SAdrian Hunter 	err = spin_sleep();
478d44bc558SAdrian Hunter 	if (err) {
479d44bc558SAdrian Hunter 		pr_debug("spin_sleep failed!\n");
480d44bc558SAdrian Hunter 		goto out_err;
481d44bc558SAdrian Hunter 	}
482d44bc558SAdrian Hunter 
483d44bc558SAdrian Hunter 	comm = "Test COMM 1";
484d44bc558SAdrian Hunter 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
485d44bc558SAdrian Hunter 	if (err) {
486d44bc558SAdrian Hunter 		pr_debug("PR_SET_NAME failed!\n");
487d44bc558SAdrian Hunter 		goto out_err;
488d44bc558SAdrian Hunter 	}
489d44bc558SAdrian Hunter 
4909a10bb22SJiri Olsa 	err = evsel__disable(cycles_evsel);
491d44bc558SAdrian Hunter 	if (err) {
492d44bc558SAdrian Hunter 		pr_debug("perf_evlist__disable_event failed!\n");
493d44bc558SAdrian Hunter 		goto out_err;
494d44bc558SAdrian Hunter 	}
495d44bc558SAdrian Hunter 
496d44bc558SAdrian Hunter 	comm = "Test COMM 2";
497d44bc558SAdrian Hunter 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
498d44bc558SAdrian Hunter 	if (err) {
499d44bc558SAdrian Hunter 		pr_debug("PR_SET_NAME failed!\n");
500d44bc558SAdrian Hunter 		goto out_err;
501d44bc558SAdrian Hunter 	}
502d44bc558SAdrian Hunter 
503d44bc558SAdrian Hunter 	err = spin_sleep();
504d44bc558SAdrian Hunter 	if (err) {
505d44bc558SAdrian Hunter 		pr_debug("spin_sleep failed!\n");
506d44bc558SAdrian Hunter 		goto out_err;
507d44bc558SAdrian Hunter 	}
508d44bc558SAdrian Hunter 
509d44bc558SAdrian Hunter 	comm = "Test COMM 3";
510d44bc558SAdrian Hunter 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
511d44bc558SAdrian Hunter 	if (err) {
512d44bc558SAdrian Hunter 		pr_debug("PR_SET_NAME failed!\n");
513d44bc558SAdrian Hunter 		goto out_err;
514d44bc558SAdrian Hunter 	}
515d44bc558SAdrian Hunter 
516ec7f24efSJiri Olsa 	err = evsel__enable(cycles_evsel);
517d44bc558SAdrian Hunter 	if (err) {
518d44bc558SAdrian Hunter 		pr_debug("perf_evlist__disable_event failed!\n");
519d44bc558SAdrian Hunter 		goto out_err;
520d44bc558SAdrian Hunter 	}
521d44bc558SAdrian Hunter 
522d44bc558SAdrian Hunter 	comm = "Test COMM 4";
523d44bc558SAdrian Hunter 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
524d44bc558SAdrian Hunter 	if (err) {
525d44bc558SAdrian Hunter 		pr_debug("PR_SET_NAME failed!\n");
526d44bc558SAdrian Hunter 		goto out_err;
527d44bc558SAdrian Hunter 	}
528d44bc558SAdrian Hunter 
529d44bc558SAdrian Hunter 	err = spin_sleep();
530d44bc558SAdrian Hunter 	if (err) {
531d44bc558SAdrian Hunter 		pr_debug("spin_sleep failed!\n");
532d44bc558SAdrian Hunter 		goto out_err;
533d44bc558SAdrian Hunter 	}
534d44bc558SAdrian Hunter 
535e74676deSJiri Olsa 	evlist__disable(evlist);
536d44bc558SAdrian Hunter 
537d44bc558SAdrian Hunter 	switch_tracking.switch_evsel = switch_evsel;
538d44bc558SAdrian Hunter 	switch_tracking.cycles_evsel = cycles_evsel;
539d44bc558SAdrian Hunter 
540d44bc558SAdrian Hunter 	err = process_events(evlist, &switch_tracking);
541d44bc558SAdrian Hunter 
542d44bc558SAdrian Hunter 	zfree(&switch_tracking.tids);
543d44bc558SAdrian Hunter 
544d44bc558SAdrian Hunter 	if (err)
545d44bc558SAdrian Hunter 		goto out_err;
546d44bc558SAdrian Hunter 
547d44bc558SAdrian Hunter 	/* Check all 4 comm events were seen i.e. that evsel->tracking works */
548d44bc558SAdrian Hunter 	if (!switch_tracking.comm_seen[0] || !switch_tracking.comm_seen[1] ||
549d44bc558SAdrian Hunter 	    !switch_tracking.comm_seen[2] || !switch_tracking.comm_seen[3]) {
550d44bc558SAdrian Hunter 		pr_debug("Missing comm events\n");
551d44bc558SAdrian Hunter 		goto out_err;
552d44bc558SAdrian Hunter 	}
553d44bc558SAdrian Hunter 
554d44bc558SAdrian Hunter 	/* Check cycles event got enabled */
555d44bc558SAdrian Hunter 	if (!switch_tracking.cycles_before_comm_1) {
556d44bc558SAdrian Hunter 		pr_debug("Missing cycles events\n");
557d44bc558SAdrian Hunter 		goto out_err;
558d44bc558SAdrian Hunter 	}
559d44bc558SAdrian Hunter 
560d44bc558SAdrian Hunter 	/* Check cycles event got disabled */
561d44bc558SAdrian Hunter 	if (switch_tracking.cycles_between_comm_2_and_comm_3) {
562d44bc558SAdrian Hunter 		pr_debug("cycles events even though event was disabled\n");
563d44bc558SAdrian Hunter 		goto out_err;
564d44bc558SAdrian Hunter 	}
565d44bc558SAdrian Hunter 
566d44bc558SAdrian Hunter 	/* Check cycles event got enabled again */
567d44bc558SAdrian Hunter 	if (!switch_tracking.cycles_after_comm_4) {
568d44bc558SAdrian Hunter 		pr_debug("Missing cycles events\n");
569d44bc558SAdrian Hunter 		goto out_err;
570d44bc558SAdrian Hunter 	}
571d44bc558SAdrian Hunter out:
572d44bc558SAdrian Hunter 	if (evlist) {
573e74676deSJiri Olsa 		evlist__disable(evlist);
574c12995a5SJiri Olsa 		evlist__delete(evlist);
575953e7b59SNamhyung Kim 	}
57638f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
5777836e52eSJiri Olsa 	perf_thread_map__put(threads);
578d44bc558SAdrian Hunter 
579d44bc558SAdrian Hunter 	return err;
580d44bc558SAdrian Hunter 
581d44bc558SAdrian Hunter out_err:
582d44bc558SAdrian Hunter 	err = -1;
583d44bc558SAdrian Hunter 	goto out;
584d44bc558SAdrian Hunter }
585d68f0365SIan Rogers 
586d68f0365SIan Rogers DEFINE_SUITE("Track with sched_switch", switch_tracking);
587