blob: 17c46f3e6f1ef45731966829ce606325c3e5c281 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Borislav Petkovd944c4e2014-04-25 21:31:02 +02002#include <linux/types.h>
Adrian Hunter395c3072013-08-31 21:50:53 +03003#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
27static int find_comm(struct perf_evlist *evlist, const char *comm)
28{
29 union perf_event *event;
Kan Liang693d32a2018-03-01 18:09:03 -050030 struct perf_mmap *md;
Adrian Hunter395c3072013-08-31 21:50:53 +030031 int i, found;
32
33 found = 0;
34 for (i = 0; i < evlist->nr_mmaps; i++) {
Kan Liang693d32a2018-03-01 18:09:03 -050035 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -050036 if (perf_mmap__read_init(md) < 0)
Kan Liang693d32a2018-03-01 18:09:03 -050037 continue;
Kan Liang0019dc872018-03-06 10:36:06 -050038 while ((event = perf_mmap__read_event(md)) != NULL) {
Adrian Hunter395c3072013-08-31 21:50:53 +030039 if (event->header.type == PERF_RECORD_COMM &&
40 (pid_t)event->comm.pid == getpid() &&
41 (pid_t)event->comm.tid == getpid() &&
42 strcmp(event->comm.comm, comm) == 0)
43 found += 1;
Kan Liangd6ace3d2018-03-06 10:36:05 -050044 perf_mmap__consume(md);
Adrian Hunter395c3072013-08-31 21:50:53 +030045 }
Kan Liang693d32a2018-03-01 18:09:03 -050046 perf_mmap__read_done(md);
Adrian Hunter395c3072013-08-31 21:50:53 +030047 }
48 return found;
49}
50
51/**
52 * test__keep_tracking - test using a dummy software event to keep tracking.
53 *
54 * This function implements a test that checks that tracking events continue
55 * when an event is disabled but a dummy software event is not disabled. If the
56 * test passes %0 is returned, otherwise %-1 is returned.
57 */
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030058int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
Adrian Hunter395c3072013-08-31 21:50:53 +030059{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030060 struct record_opts opts = {
Adrian Hunter395c3072013-08-31 21:50:53 +030061 .mmap_pages = UINT_MAX,
62 .user_freq = UINT_MAX,
63 .user_interval = ULLONG_MAX,
Adrian Hunter395c3072013-08-31 21:50:53 +030064 .target = {
65 .uses_mmap = true,
66 },
67 };
68 struct thread_map *threads = NULL;
69 struct cpu_map *cpus = NULL;
70 struct perf_evlist *evlist = NULL;
71 struct perf_evsel *evsel = NULL;
72 int found, err = -1;
73 const char *comm;
74
75 threads = thread_map__new(-1, getpid(), UINT_MAX);
76 CHECK_NOT_NULL__(threads);
77
78 cpus = cpu_map__new(NULL);
79 CHECK_NOT_NULL__(cpus);
80
81 evlist = perf_evlist__new();
82 CHECK_NOT_NULL__(evlist);
83
84 perf_evlist__set_maps(evlist, cpus, threads);
85
Jiri Olsab39b8392015-04-22 21:10:16 +020086 CHECK__(parse_events(evlist, "dummy:u", NULL));
87 CHECK__(parse_events(evlist, "cycles:u", NULL));
Adrian Hunter395c3072013-08-31 21:50:53 +030088
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030089 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunter395c3072013-08-31 21:50:53 +030090
91 evsel = perf_evlist__first(evlist);
92
93 evsel->attr.comm = 1;
94 evsel->attr.disabled = 1;
95 evsel->attr.enable_on_exec = 0;
96
97 if (perf_evlist__open(evlist) < 0) {
Wang Nan597bdeb2015-11-03 10:44:42 +000098 pr_debug("Unable to open dummy and cycles event\n");
99 err = TEST_SKIP;
Adrian Hunter395c3072013-08-31 21:50:53 +0300100 goto out_err;
101 }
102
Wang Nanf74b9d3a2017-12-03 02:00:37 +0000103 CHECK__(perf_evlist__mmap(evlist, UINT_MAX));
Adrian Hunter395c3072013-08-31 21:50:53 +0300104
105 /*
106 * First, test that a 'comm' event can be found when the event is
107 * enabled.
108 */
109
110 perf_evlist__enable(evlist);
111
112 comm = "Test COMM 1";
113 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
114
115 perf_evlist__disable(evlist);
116
117 found = find_comm(evlist, comm);
118 if (found != 1) {
119 pr_debug("First time, failed to find tracking event.\n");
120 goto out_err;
121 }
122
123 /*
124 * Secondly, test that a 'comm' event can be found when the event is
125 * disabled with the dummy event still enabled.
126 */
127
128 perf_evlist__enable(evlist);
129
130 evsel = perf_evlist__last(evlist);
131
Jiri Olsad2190a82016-01-07 10:13:58 +0100132 CHECK__(perf_evsel__disable(evsel));
Adrian Hunter395c3072013-08-31 21:50:53 +0300133
134 comm = "Test COMM 2";
135 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
136
137 perf_evlist__disable(evlist);
138
139 found = find_comm(evlist, comm);
140 if (found != 1) {
141 pr_debug("Seconf time, failed to find tracking event.\n");
142 goto out_err;
143 }
144
145 err = 0;
146
147out_err:
148 if (evlist) {
149 perf_evlist__disable(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +0300150 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300151 } else {
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200152 cpu_map__put(cpus);
Jiri Olsa186fbb72015-06-23 00:36:05 +0200153 thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300154 }
Adrian Hunter395c3072013-08-31 21:50:53 +0300155
156 return err;
157}