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