blob: 50a0c9fcde7da3c79e3a80706b5e08fa451f8b08 [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>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03003#include <limits.h>
Adrian Hunter395c3072013-08-31 21:50:53 +03004#include <unistd.h>
5#include <sys/prctl.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02006#include <perf/cpumap.h>
Jiri Olsa453fa032019-07-21 13:24:43 +02007#include <perf/evlist.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +02008#include <perf/mmap.h>
Adrian Hunter395c3072013-08-31 21:50:53 +03009
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -030010#include "debug.h"
Adrian Hunter395c3072013-08-31 21:50:53 +030011#include "parse-events.h"
12#include "evlist.h"
13#include "evsel.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030014#include "record.h"
Adrian Hunter395c3072013-08-31 21:50:53 +030015#include "thread_map.h"
Adrian Hunter395c3072013-08-31 21:50:53 +030016#include "tests.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030017#include "util/mmap.h"
Adrian Hunter395c3072013-08-31 21:50:53 +030018
19#define CHECK__(x) { \
20 while ((x) < 0) { \
21 pr_debug(#x " failed!\n"); \
22 goto out_err; \
23 } \
24}
25
26#define CHECK_NOT_NULL__(x) { \
27 while ((x) == NULL) { \
28 pr_debug(#x " failed!\n"); \
29 goto out_err; \
30 } \
31}
32
Jiri Olsa63503db2019-07-21 13:23:52 +020033static int find_comm(struct evlist *evlist, const char *comm)
Adrian Hunter395c3072013-08-31 21:50:53 +030034{
35 union perf_event *event;
Jiri Olsaa5830532019-07-27 20:30:53 +020036 struct mmap *md;
Adrian Hunter395c3072013-08-31 21:50:53 +030037 int i, found;
38
39 found = 0;
Jiri Olsac976ee12019-07-30 13:04:59 +020040 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Kan Liang693d32a2018-03-01 18:09:03 -050041 md = &evlist->mmap[i];
Jiri Olsa7c4d4182019-10-07 14:53:18 +020042 if (perf_mmap__read_init(&md->core) < 0)
Kan Liang693d32a2018-03-01 18:09:03 -050043 continue;
Jiri Olsa151ed5d2019-10-07 14:53:20 +020044 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Adrian Hunter395c3072013-08-31 21:50:53 +030045 if (event->header.type == PERF_RECORD_COMM &&
46 (pid_t)event->comm.pid == getpid() &&
47 (pid_t)event->comm.tid == getpid() &&
48 strcmp(event->comm.comm, comm) == 0)
49 found += 1;
Jiri Olsa7728fa02019-10-07 14:53:17 +020050 perf_mmap__consume(&md->core);
Adrian Hunter395c3072013-08-31 21:50:53 +030051 }
Jiri Olsa32fdc2c2019-10-07 14:53:19 +020052 perf_mmap__read_done(&md->core);
Adrian Hunter395c3072013-08-31 21:50:53 +030053 }
54 return found;
55}
56
57/**
58 * test__keep_tracking - test using a dummy software event to keep tracking.
59 *
60 * This function implements a test that checks that tracking events continue
61 * when an event is disabled but a dummy software event is not disabled. If the
62 * test passes %0 is returned, otherwise %-1 is returned.
63 */
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030064int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
Adrian Hunter395c3072013-08-31 21:50:53 +030065{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030066 struct record_opts opts = {
Adrian Hunter395c3072013-08-31 21:50:53 +030067 .mmap_pages = UINT_MAX,
68 .user_freq = UINT_MAX,
69 .user_interval = ULLONG_MAX,
Adrian Hunter395c3072013-08-31 21:50:53 +030070 .target = {
71 .uses_mmap = true,
72 },
73 };
Jiri Olsa9749b902019-07-21 13:23:50 +020074 struct perf_thread_map *threads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +020075 struct perf_cpu_map *cpus = NULL;
Jiri Olsa63503db2019-07-21 13:23:52 +020076 struct evlist *evlist = NULL;
Jiri Olsa32dcd022019-07-21 13:23:51 +020077 struct evsel *evsel = NULL;
Adrian Hunter395c3072013-08-31 21:50:53 +030078 int found, err = -1;
79 const char *comm;
80
81 threads = thread_map__new(-1, getpid(), UINT_MAX);
82 CHECK_NOT_NULL__(threads);
83
Jiri Olsa9c3516d2019-07-21 13:24:30 +020084 cpus = perf_cpu_map__new(NULL);
Adrian Hunter395c3072013-08-31 21:50:53 +030085 CHECK_NOT_NULL__(cpus);
86
Jiri Olsa0f98b112019-07-21 13:23:55 +020087 evlist = evlist__new();
Adrian Hunter395c3072013-08-31 21:50:53 +030088 CHECK_NOT_NULL__(evlist);
89
Jiri Olsa453fa032019-07-21 13:24:43 +020090 perf_evlist__set_maps(&evlist->core, cpus, threads);
Adrian Hunter395c3072013-08-31 21:50:53 +030091
Jiri Olsab39b8392015-04-22 21:10:16 +020092 CHECK__(parse_events(evlist, "dummy:u", NULL));
93 CHECK__(parse_events(evlist, "cycles:u", NULL));
Adrian Hunter395c3072013-08-31 21:50:53 +030094
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030095 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunter395c3072013-08-31 21:50:53 +030096
Jiri Olsa515dbe42019-09-03 10:39:52 +020097 evsel = evlist__first(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +030098
Jiri Olsa1fc632c2019-07-21 13:24:29 +020099 evsel->core.attr.comm = 1;
100 evsel->core.attr.disabled = 1;
101 evsel->core.attr.enable_on_exec = 0;
Adrian Hunter395c3072013-08-31 21:50:53 +0300102
Jiri Olsa474ddc42019-07-21 13:24:06 +0200103 if (evlist__open(evlist) < 0) {
Wang Nan597bdeb2015-11-03 10:44:42 +0000104 pr_debug("Unable to open dummy and cycles event\n");
105 err = TEST_SKIP;
Adrian Hunter395c3072013-08-31 21:50:53 +0300106 goto out_err;
107 }
108
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200109 CHECK__(evlist__mmap(evlist, UINT_MAX));
Adrian Hunter395c3072013-08-31 21:50:53 +0300110
111 /*
112 * First, test that a 'comm' event can be found when the event is
113 * enabled.
114 */
115
Jiri Olsa1c87f162019-07-21 13:24:08 +0200116 evlist__enable(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +0300117
118 comm = "Test COMM 1";
119 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
120
Jiri Olsae74676d2019-07-21 13:24:09 +0200121 evlist__disable(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +0300122
123 found = find_comm(evlist, comm);
124 if (found != 1) {
125 pr_debug("First time, failed to find tracking event.\n");
126 goto out_err;
127 }
128
129 /*
130 * Secondly, test that a 'comm' event can be found when the event is
131 * disabled with the dummy event still enabled.
132 */
133
Jiri Olsa1c87f162019-07-21 13:24:08 +0200134 evlist__enable(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +0300135
Jiri Olsa515dbe42019-09-03 10:39:52 +0200136 evsel = evlist__last(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +0300137
Jiri Olsa9a10bb22019-07-21 13:24:03 +0200138 CHECK__(evsel__disable(evsel));
Adrian Hunter395c3072013-08-31 21:50:53 +0300139
140 comm = "Test COMM 2";
141 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
142
Jiri Olsae74676d2019-07-21 13:24:09 +0200143 evlist__disable(evlist);
Adrian Hunter395c3072013-08-31 21:50:53 +0300144
145 found = find_comm(evlist, comm);
146 if (found != 1) {
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +0100147 pr_debug("Second time, failed to find tracking event.\n");
Adrian Hunter395c3072013-08-31 21:50:53 +0300148 goto out_err;
149 }
150
151 err = 0;
152
153out_err:
154 if (evlist) {
Jiri Olsae74676d2019-07-21 13:24:09 +0200155 evlist__disable(evlist);
Jiri Olsac12995a2019-07-21 13:23:56 +0200156 evlist__delete(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300157 } else {
Jiri Olsa38f01d82019-07-21 13:24:17 +0200158 perf_cpu_map__put(cpus);
Jiri Olsa7836e522019-07-21 13:24:20 +0200159 perf_thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300160 }
Adrian Hunter395c3072013-08-31 21:50:53 +0300161
162 return err;
163}