Linux Perf
keep-tracking.c
Go to the documentation of this file.
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  struct perf_mmap *md;
31  int i, found;
32 
33  found = 0;
34  for (i = 0; i < evlist->nr_mmaps; i++) {
35  md = &evlist->mmap[i];
36  if (perf_mmap__read_init(md) < 0)
37  continue;
38  while ((event = perf_mmap__read_event(md)) != NULL) {
39  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;
45  }
47  }
48  return found;
49 }
50 
58 int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
59 {
60  struct record_opts opts = {
61  .mmap_pages = UINT_MAX,
62  .user_freq = UINT_MAX,
63  .user_interval = ULLONG_MAX,
64  .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 
86  CHECK__(parse_events(evlist, "dummy:u", NULL));
87  CHECK__(parse_events(evlist, "cycles:u", NULL));
88 
89  perf_evlist__config(evlist, &opts, NULL);
90 
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) {
98  pr_debug("Unable to open dummy and cycles event\n");
99  err = TEST_SKIP;
100  goto out_err;
101  }
102 
103  CHECK__(perf_evlist__mmap(evlist, UINT_MAX));
104 
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 
133 
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 
147 out_err:
148  if (evlist) {
149  perf_evlist__disable(evlist);
150  perf_evlist__delete(evlist);
151  } else {
152  cpu_map__put(cpus);
153  thread_map__put(threads);
154  }
155 
156  return err;
157 }
void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, struct thread_map *threads)
Definition: evlist.c:1115
const char * comm
Definition: hists_common.c:16
void perf_mmap__consume(struct perf_mmap *map)
Definition: mmap.c:118
struct perf_mmap * mmap
Definition: evlist.h:45
u32 tid
Definition: event.h:39
void perf_evlist__enable(struct perf_evlist *evlist)
Definition: evlist.c:369
int int err
Definition: 5sec.c:44
void perf_evlist__delete(struct perf_evlist *evlist)
Definition: evlist.c:133
Definition: cpumap.h:12
Definition: mmap.h:17
int perf_evsel__disable(struct perf_evsel *evsel)
Definition: evsel.c:1182
#define PR_SET_NAME
Definition: builtin-sched.c:41
static int find_comm(struct perf_evlist *evlist, const char *comm)
Definition: keep-tracking.c:27
Definition: comm.h:11
int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *err)
void cpu_map__put(struct cpu_map *map)
Definition: cpumap.c:298
int perf_mmap__read_init(struct perf_mmap *map)
Definition: mmap.c:271
#define pr_debug(fmt,...)
Definition: json.h:27
int perf_evlist__open(struct perf_evlist *evlist)
Definition: evlist.c:1398
int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
Definition: keep-tracking.c:58
#define CHECK_NOT_NULL__(x)
Definition: keep-tracking.c:20
#define CHECK__(x)
Definition: keep-tracking.c:13
list cpus
Definition: stat-cpi.py:7
unsigned int mmap_pages
Definition: perf.h:67
#define event
void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts, struct callchain_param *callchain)
Definition: record.c:135
int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages)
Definition: evlist.c:1061
int nr_mmaps
Definition: evlist.h:32
void perf_mmap__read_done(struct perf_mmap *map)
Definition: mmap.c:329
struct perf_event_header header
Definition: event.h:624
static struct perf_evsel * perf_evlist__first(struct perf_evlist *evlist)
Definition: evlist.h:215
union perf_event * perf_mmap__read_event(struct perf_mmap *map)
Definition: mmap.c:78
void perf_evlist__disable(struct perf_evlist *evlist)
Definition: evlist.c:356
Definition: tests.h:30
struct comm_event comm
Definition: event.h:627
char comm[16]
Definition: event.h:40
void thread_map__put(struct thread_map *map)
Definition: thread_map.c:361
static struct perf_evsel * perf_evlist__last(struct perf_evlist *evlist)
Definition: evlist.h:220
struct thread_map * thread_map__new(pid_t pid, pid_t tid, uid_t uid)
Definition: thread_map.c:192
struct cpu_map * cpu_map__new(const char *cpu_list)
Definition: cpumap.c:125
u32 pid
Definition: event.h:39
struct perf_evlist * perf_evlist__new(void)
Definition: evlist.c:54
struct perf_event_attr attr
Definition: evsel.h:93