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