xref: /openbmc/linux/tools/perf/tests/keep-tracking.c (revision 806731a9)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d944c4eeSBorislav Petkov #include <linux/types.h>
3f2a39fe8SArnaldo Carvalho de Melo #include <limits.h>
4395c3070SAdrian Hunter #include <unistd.h>
5395c3070SAdrian Hunter #include <sys/prctl.h>
69c3516d1SJiri Olsa #include <perf/cpumap.h>
7453fa030SJiri Olsa #include <perf/evlist.h>
87728fa0cSJiri Olsa #include <perf/mmap.h>
9395c3070SAdrian Hunter 
10b4209025SArnaldo Carvalho de Melo #include "debug.h"
11395c3070SAdrian Hunter #include "parse-events.h"
12395c3070SAdrian Hunter #include "evlist.h"
13395c3070SAdrian Hunter #include "evsel.h"
14aeb00b1aSArnaldo Carvalho de Melo #include "record.h"
15395c3070SAdrian Hunter #include "thread_map.h"
16395c3070SAdrian Hunter #include "tests.h"
17e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
18395c3070SAdrian Hunter 
19395c3070SAdrian Hunter #define CHECK__(x) {				\
20395c3070SAdrian Hunter 	while ((x) < 0) {			\
21395c3070SAdrian Hunter 		pr_debug(#x " failed!\n");	\
22395c3070SAdrian Hunter 		goto out_err;			\
23395c3070SAdrian Hunter 	}					\
24395c3070SAdrian Hunter }
25395c3070SAdrian Hunter 
26395c3070SAdrian Hunter #define CHECK_NOT_NULL__(x) {			\
27395c3070SAdrian Hunter 	while ((x) == NULL) {			\
28395c3070SAdrian Hunter 		pr_debug(#x " failed!\n");	\
29395c3070SAdrian Hunter 		goto out_err;			\
30395c3070SAdrian Hunter 	}					\
31395c3070SAdrian Hunter }
32395c3070SAdrian Hunter 
find_comm(struct evlist * evlist,const char * comm)3363503dbaSJiri Olsa static int find_comm(struct evlist *evlist, const char *comm)
34395c3070SAdrian Hunter {
35395c3070SAdrian Hunter 	union perf_event *event;
36a5830532SJiri Olsa 	struct mmap *md;
37395c3070SAdrian Hunter 	int i, found;
38395c3070SAdrian Hunter 
39395c3070SAdrian Hunter 	found = 0;
40c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
41693d32aeSKan Liang 		md = &evlist->mmap[i];
427c4d4182SJiri Olsa 		if (perf_mmap__read_init(&md->core) < 0)
43693d32aeSKan Liang 			continue;
44151ed5d7SJiri Olsa 		while ((event = perf_mmap__read_event(&md->core)) != NULL) {
45395c3070SAdrian Hunter 			if (event->header.type == PERF_RECORD_COMM &&
46395c3070SAdrian Hunter 			    (pid_t)event->comm.pid == getpid() &&
47395c3070SAdrian Hunter 			    (pid_t)event->comm.tid == getpid() &&
48395c3070SAdrian Hunter 			    strcmp(event->comm.comm, comm) == 0)
49395c3070SAdrian Hunter 				found += 1;
507728fa0cSJiri Olsa 			perf_mmap__consume(&md->core);
51395c3070SAdrian Hunter 		}
5232fdc2caSJiri Olsa 		perf_mmap__read_done(&md->core);
53395c3070SAdrian Hunter 	}
54395c3070SAdrian Hunter 	return found;
55395c3070SAdrian Hunter }
56395c3070SAdrian Hunter 
57395c3070SAdrian Hunter /**
58395c3070SAdrian Hunter  * test__keep_tracking - test using a dummy software event to keep tracking.
59395c3070SAdrian Hunter  *
60395c3070SAdrian Hunter  * This function implements a test that checks that tracking events continue
61395c3070SAdrian Hunter  * when an event is disabled but a dummy software event is not disabled.  If the
62395c3070SAdrian Hunter  * test passes %0 is returned, otherwise %-1 is returned.
63395c3070SAdrian Hunter  */
test__keep_tracking(struct test_suite * test __maybe_unused,int subtest __maybe_unused)6433f44bfdSIan Rogers static int test__keep_tracking(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
65395c3070SAdrian Hunter {
66b4006796SArnaldo Carvalho de Melo 	struct record_opts opts = {
67395c3070SAdrian Hunter 		.mmap_pages	     = UINT_MAX,
68395c3070SAdrian Hunter 		.user_freq	     = UINT_MAX,
69395c3070SAdrian Hunter 		.user_interval	     = ULLONG_MAX,
70395c3070SAdrian Hunter 		.target		     = {
71395c3070SAdrian Hunter 			.uses_mmap   = true,
72395c3070SAdrian Hunter 		},
73395c3070SAdrian Hunter 	};
749749b90eSJiri Olsa 	struct perf_thread_map *threads = NULL;
75f854839bSJiri Olsa 	struct perf_cpu_map *cpus = NULL;
7663503dbaSJiri Olsa 	struct evlist *evlist = NULL;
7732dcd021SJiri Olsa 	struct evsel *evsel = NULL;
78395c3070SAdrian Hunter 	int found, err = -1;
79395c3070SAdrian Hunter 	const char *comm;
80395c3070SAdrian Hunter 
81395c3070SAdrian Hunter 	threads = thread_map__new(-1, getpid(), UINT_MAX);
82395c3070SAdrian Hunter 	CHECK_NOT_NULL__(threads);
83395c3070SAdrian Hunter 
849c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
85395c3070SAdrian Hunter 	CHECK_NOT_NULL__(cpus);
86395c3070SAdrian Hunter 
870f98b11cSJiri Olsa 	evlist = evlist__new();
88395c3070SAdrian Hunter 	CHECK_NOT_NULL__(evlist);
89395c3070SAdrian Hunter 
90453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
91395c3070SAdrian Hunter 
92*806731a9SAdrian Hunter 	CHECK__(parse_event(evlist, "dummy:u"));
93*806731a9SAdrian Hunter 	CHECK__(parse_event(evlist, "cycles:u"));
94395c3070SAdrian Hunter 
9578e1bc25SArnaldo Carvalho de Melo 	evlist__config(evlist, &opts, NULL);
96395c3070SAdrian Hunter 
97515dbe48SJiri Olsa 	evsel = evlist__first(evlist);
98395c3070SAdrian Hunter 
991fc632ceSJiri Olsa 	evsel->core.attr.comm = 1;
1001fc632ceSJiri Olsa 	evsel->core.attr.disabled = 1;
1011fc632ceSJiri Olsa 	evsel->core.attr.enable_on_exec = 0;
102395c3070SAdrian Hunter 
103474ddc4cSJiri Olsa 	if (evlist__open(evlist) < 0) {
104597bdeb4SWang Nan 		pr_debug("Unable to open dummy and cycles event\n");
105597bdeb4SWang Nan 		err = TEST_SKIP;
106395c3070SAdrian Hunter 		goto out_err;
107395c3070SAdrian Hunter 	}
108395c3070SAdrian Hunter 
1099521b5f2SJiri Olsa 	CHECK__(evlist__mmap(evlist, UINT_MAX));
110395c3070SAdrian Hunter 
111395c3070SAdrian Hunter 	/*
112395c3070SAdrian Hunter 	 * First, test that a 'comm' event can be found when the event is
113395c3070SAdrian Hunter 	 * enabled.
114395c3070SAdrian Hunter 	 */
115395c3070SAdrian Hunter 
1161c87f165SJiri Olsa 	evlist__enable(evlist);
117395c3070SAdrian Hunter 
118395c3070SAdrian Hunter 	comm = "Test COMM 1";
119395c3070SAdrian Hunter 	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
120395c3070SAdrian Hunter 
121e74676deSJiri Olsa 	evlist__disable(evlist);
122395c3070SAdrian Hunter 
123395c3070SAdrian Hunter 	found = find_comm(evlist, comm);
124395c3070SAdrian Hunter 	if (found != 1) {
125395c3070SAdrian Hunter 		pr_debug("First time, failed to find tracking event.\n");
126395c3070SAdrian Hunter 		goto out_err;
127395c3070SAdrian Hunter 	}
128395c3070SAdrian Hunter 
129395c3070SAdrian Hunter 	/*
130395c3070SAdrian Hunter 	 * Secondly, test that a 'comm' event can be found when the event is
131395c3070SAdrian Hunter 	 * disabled with the dummy event still enabled.
132395c3070SAdrian Hunter 	 */
133395c3070SAdrian Hunter 
1341c87f165SJiri Olsa 	evlist__enable(evlist);
135395c3070SAdrian Hunter 
136515dbe48SJiri Olsa 	evsel = evlist__last(evlist);
137395c3070SAdrian Hunter 
1389a10bb22SJiri Olsa 	CHECK__(evsel__disable(evsel));
139395c3070SAdrian Hunter 
140395c3070SAdrian Hunter 	comm = "Test COMM 2";
141395c3070SAdrian Hunter 	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
142395c3070SAdrian Hunter 
143e74676deSJiri Olsa 	evlist__disable(evlist);
144395c3070SAdrian Hunter 
145395c3070SAdrian Hunter 	found = find_comm(evlist, comm);
146395c3070SAdrian Hunter 	if (found != 1) {
14787ffb6c6SArnaldo Carvalho de Melo 		pr_debug("Second time, failed to find tracking event.\n");
148395c3070SAdrian Hunter 		goto out_err;
149395c3070SAdrian Hunter 	}
150395c3070SAdrian Hunter 
151395c3070SAdrian Hunter 	err = 0;
152395c3070SAdrian Hunter 
153395c3070SAdrian Hunter out_err:
154395c3070SAdrian Hunter 	if (evlist) {
155e74676deSJiri Olsa 		evlist__disable(evlist);
156c12995a5SJiri Olsa 		evlist__delete(evlist);
157f2c3202bSNamhyung Kim 	}
15838f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1597836e52eSJiri Olsa 	perf_thread_map__put(threads);
160395c3070SAdrian Hunter 
161395c3070SAdrian Hunter 	return err;
162395c3070SAdrian Hunter }
163d68f0365SIan Rogers 
164d68f0365SIan Rogers DEFINE_SUITE("Use a dummy software event to keep tracking", keep_tracking);
165