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