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