xref: /linux/tools/perf/tests/keep-tracking.c (revision 5643b1a59e581ac3f66d36caba8124313cc446c0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <unistd.h>
4 #include <sys/prctl.h>
5 #include <perf/cpumap.h>
6 #include <perf/evlist.h>
7 
8 #include "parse-events.h"
9 #include "evlist.h"
10 #include "evsel.h"
11 #include "thread_map.h"
12 #include "cpumap.h"
13 #include "tests.h"
14 
15 #define CHECK__(x) {				\
16 	while ((x) < 0) {			\
17 		pr_debug(#x " failed!\n");	\
18 		goto out_err;			\
19 	}					\
20 }
21 
22 #define CHECK_NOT_NULL__(x) {			\
23 	while ((x) == NULL) {			\
24 		pr_debug(#x " failed!\n");	\
25 		goto out_err;			\
26 	}					\
27 }
28 
29 static int find_comm(struct evlist *evlist, const char *comm)
30 {
31 	union perf_event *event;
32 	struct perf_mmap *md;
33 	int i, found;
34 
35 	found = 0;
36 	for (i = 0; i < evlist->nr_mmaps; i++) {
37 		md = &evlist->mmap[i];
38 		if (perf_mmap__read_init(md) < 0)
39 			continue;
40 		while ((event = perf_mmap__read_event(md)) != NULL) {
41 			if (event->header.type == PERF_RECORD_COMM &&
42 			    (pid_t)event->comm.pid == getpid() &&
43 			    (pid_t)event->comm.tid == getpid() &&
44 			    strcmp(event->comm.comm, comm) == 0)
45 				found += 1;
46 			perf_mmap__consume(md);
47 		}
48 		perf_mmap__read_done(md);
49 	}
50 	return found;
51 }
52 
53 /**
54  * test__keep_tracking - test using a dummy software event to keep tracking.
55  *
56  * This function implements a test that checks that tracking events continue
57  * when an event is disabled but a dummy software event is not disabled.  If the
58  * test passes %0 is returned, otherwise %-1 is returned.
59  */
60 int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
61 {
62 	struct record_opts opts = {
63 		.mmap_pages	     = UINT_MAX,
64 		.user_freq	     = UINT_MAX,
65 		.user_interval	     = ULLONG_MAX,
66 		.target		     = {
67 			.uses_mmap   = true,
68 		},
69 	};
70 	struct perf_thread_map *threads = NULL;
71 	struct perf_cpu_map *cpus = NULL;
72 	struct evlist *evlist = NULL;
73 	struct evsel *evsel = NULL;
74 	int found, err = -1;
75 	const char *comm;
76 
77 	threads = thread_map__new(-1, getpid(), UINT_MAX);
78 	CHECK_NOT_NULL__(threads);
79 
80 	cpus = perf_cpu_map__new(NULL);
81 	CHECK_NOT_NULL__(cpus);
82 
83 	evlist = evlist__new();
84 	CHECK_NOT_NULL__(evlist);
85 
86 	perf_evlist__set_maps(&evlist->core, cpus, threads);
87 
88 	CHECK__(parse_events(evlist, "dummy:u", NULL));
89 	CHECK__(parse_events(evlist, "cycles:u", NULL));
90 
91 	perf_evlist__config(evlist, &opts, NULL);
92 
93 	evsel = perf_evlist__first(evlist);
94 
95 	evsel->core.attr.comm = 1;
96 	evsel->core.attr.disabled = 1;
97 	evsel->core.attr.enable_on_exec = 0;
98 
99 	if (evlist__open(evlist) < 0) {
100 		pr_debug("Unable to open dummy and cycles event\n");
101 		err = TEST_SKIP;
102 		goto out_err;
103 	}
104 
105 	CHECK__(perf_evlist__mmap(evlist, UINT_MAX));
106 
107 	/*
108 	 * First, test that a 'comm' event can be found when the event is
109 	 * enabled.
110 	 */
111 
112 	evlist__enable(evlist);
113 
114 	comm = "Test COMM 1";
115 	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
116 
117 	evlist__disable(evlist);
118 
119 	found = find_comm(evlist, comm);
120 	if (found != 1) {
121 		pr_debug("First time, failed to find tracking event.\n");
122 		goto out_err;
123 	}
124 
125 	/*
126 	 * Secondly, test that a 'comm' event can be found when the event is
127 	 * disabled with the dummy event still enabled.
128 	 */
129 
130 	evlist__enable(evlist);
131 
132 	evsel = perf_evlist__last(evlist);
133 
134 	CHECK__(evsel__disable(evsel));
135 
136 	comm = "Test COMM 2";
137 	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
138 
139 	evlist__disable(evlist);
140 
141 	found = find_comm(evlist, comm);
142 	if (found != 1) {
143 		pr_debug("Seconf time, failed to find tracking event.\n");
144 		goto out_err;
145 	}
146 
147 	err = 0;
148 
149 out_err:
150 	if (evlist) {
151 		evlist__disable(evlist);
152 		evlist__delete(evlist);
153 	} else {
154 		perf_cpu_map__put(cpus);
155 		perf_thread_map__put(threads);
156 	}
157 
158 	return err;
159 }
160