1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
6 #include <perf/cpumap.h>
7 #include <perf/evlist.h>
10 #include "parse-events.h"
14 #include "thread_map.h"
16 #include "util/mmap.h"
18 #define CHECK__(x) { \
20 pr_debug(#x " failed!\n"); \
25 #define CHECK_NOT_NULL__(x) { \
26 while ((x) == NULL) { \
27 pr_debug(#x " failed!\n"); \
32 static int find_comm(struct evlist *evlist, const char *comm)
34 union perf_event *event;
39 for (i = 0; i < evlist->core.nr_mmaps; i++) {
40 md = &evlist->mmap[i];
41 if (perf_mmap__read_init(md) < 0)
43 while ((event = perf_mmap__read_event(md)) != NULL) {
44 if (event->header.type == PERF_RECORD_COMM &&
45 (pid_t)event->comm.pid == getpid() &&
46 (pid_t)event->comm.tid == getpid() &&
47 strcmp(event->comm.comm, comm) == 0)
49 perf_mmap__consume(md);
51 perf_mmap__read_done(md);
57 * test__keep_tracking - test using a dummy software event to keep tracking.
59 * This function implements a test that checks that tracking events continue
60 * when an event is disabled but a dummy software event is not disabled. If the
61 * test passes %0 is returned, otherwise %-1 is returned.
63 int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
65 struct record_opts opts = {
66 .mmap_pages = UINT_MAX,
67 .user_freq = UINT_MAX,
68 .user_interval = ULLONG_MAX,
73 struct perf_thread_map *threads = NULL;
74 struct perf_cpu_map *cpus = NULL;
75 struct evlist *evlist = NULL;
76 struct evsel *evsel = NULL;
80 threads = thread_map__new(-1, getpid(), UINT_MAX);
81 CHECK_NOT_NULL__(threads);
83 cpus = perf_cpu_map__new(NULL);
84 CHECK_NOT_NULL__(cpus);
86 evlist = evlist__new();
87 CHECK_NOT_NULL__(evlist);
89 perf_evlist__set_maps(&evlist->core, cpus, threads);
91 CHECK__(parse_events(evlist, "dummy:u", NULL));
92 CHECK__(parse_events(evlist, "cycles:u", NULL));
94 perf_evlist__config(evlist, &opts, NULL);
96 evsel = perf_evlist__first(evlist);
98 evsel->core.attr.comm = 1;
99 evsel->core.attr.disabled = 1;
100 evsel->core.attr.enable_on_exec = 0;
102 if (evlist__open(evlist) < 0) {
103 pr_debug("Unable to open dummy and cycles event\n");
108 CHECK__(evlist__mmap(evlist, UINT_MAX));
111 * First, test that a 'comm' event can be found when the event is
115 evlist__enable(evlist);
117 comm = "Test COMM 1";
118 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
120 evlist__disable(evlist);
122 found = find_comm(evlist, comm);
124 pr_debug("First time, failed to find tracking event.\n");
129 * Secondly, test that a 'comm' event can be found when the event is
130 * disabled with the dummy event still enabled.
133 evlist__enable(evlist);
135 evsel = perf_evlist__last(evlist);
137 CHECK__(evsel__disable(evsel));
139 comm = "Test COMM 2";
140 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
142 evlist__disable(evlist);
144 found = find_comm(evlist, comm);
146 pr_debug("Second time, failed to find tracking event.\n");
154 evlist__disable(evlist);
155 evlist__delete(evlist);
157 perf_cpu_map__put(cpus);
158 perf_thread_map__put(threads);