Linux Perf
task-exit.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 #include "evlist.h"
3 #include "evsel.h"
4 #include "thread_map.h"
5 #include "cpumap.h"
6 #include "tests.h"
7 
8 #include <errno.h>
9 #include <signal.h>
10 
11 static int exited;
12 static int nr_exit;
13 
14 static void sig_handler(int sig __maybe_unused)
15 {
16  exited = 1;
17 }
18 
19 /*
20  * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
21  * we asked by setting its exec_error to this handler.
22  */
23 static void workload_exec_failed_signal(int signo __maybe_unused,
24  siginfo_t *info __maybe_unused,
25  void *ucontext __maybe_unused)
26 {
27  exited = 1;
28  nr_exit = -1;
29 }
30 
31 /*
32  * This test will start a workload that does nothing then it checks
33  * if the number of exit event reported by the kernel is 1 or not
34  * in order to check the kernel returns correct number of event.
35  */
36 int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
37 {
38  int err = -1;
39  union perf_event *event;
40  struct perf_evsel *evsel;
41  struct perf_evlist *evlist;
42  struct target target = {
43  .uid = UINT_MAX,
44  .uses_mmap = true,
45  };
46  const char *argv[] = { "true", NULL };
47  char sbuf[STRERR_BUFSIZE];
48  struct cpu_map *cpus;
49  struct thread_map *threads;
50  struct perf_mmap *md;
51 
52  signal(SIGCHLD, sig_handler);
53 
54  evlist = perf_evlist__new_default();
55  if (evlist == NULL) {
56  pr_debug("perf_evlist__new_default\n");
57  return -1;
58  }
59 
60  /*
61  * Create maps of threads and cpus to monitor. In this case
62  * we start with all threads and cpus (-1, -1) but then in
63  * perf_evlist__prepare_workload we'll fill in the only thread
64  * we're monitoring, the one forked there.
65  */
66  cpus = cpu_map__dummy_new();
67  threads = thread_map__new_by_tid(-1);
68  if (!cpus || !threads) {
69  err = -ENOMEM;
70  pr_debug("Not enough memory to create thread/cpu maps\n");
71  goto out_free_maps;
72  }
73 
74  perf_evlist__set_maps(evlist, cpus, threads);
75 
76  cpus = NULL;
77  threads = NULL;
78 
79  err = perf_evlist__prepare_workload(evlist, &target, argv, false,
81  if (err < 0) {
82  pr_debug("Couldn't run the workload!\n");
83  goto out_delete_evlist;
84  }
85 
86  evsel = perf_evlist__first(evlist);
87  evsel->attr.task = 1;
88 #ifdef __s390x__
89  evsel->attr.sample_freq = 1000000;
90 #else
91  evsel->attr.sample_freq = 1;
92 #endif
93  evsel->attr.inherit = 0;
94  evsel->attr.watermark = 0;
95  evsel->attr.wakeup_events = 1;
96  evsel->attr.exclude_kernel = 1;
97 
98  err = perf_evlist__open(evlist);
99  if (err < 0) {
100  pr_debug("Couldn't open the evlist: %s\n",
101  str_error_r(-err, sbuf, sizeof(sbuf)));
102  goto out_delete_evlist;
103  }
104 
105  if (perf_evlist__mmap(evlist, 128) < 0) {
106  pr_debug("failed to mmap events: %d (%s)\n", errno,
107  str_error_r(errno, sbuf, sizeof(sbuf)));
108  goto out_delete_evlist;
109  }
110 
112 
113 retry:
114  md = &evlist->mmap[0];
115  if (perf_mmap__read_init(md) < 0)
116  goto out_init;
117 
118  while ((event = perf_mmap__read_event(md)) != NULL) {
119  if (event->header.type == PERF_RECORD_EXIT)
120  nr_exit++;
121 
122  perf_mmap__consume(md);
123  }
125 
126 out_init:
127  if (!exited || !nr_exit) {
128  perf_evlist__poll(evlist, -1);
129  goto retry;
130  }
131 
132  if (nr_exit != 1) {
133  pr_debug("received %d EXIT records\n", nr_exit);
134  err = -1;
135  }
136 
137 out_free_maps:
138  cpu_map__put(cpus);
139  thread_map__put(threads);
140 out_delete_evlist:
141  perf_evlist__delete(evlist);
142  return err;
143 }
void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, struct thread_map *threads)
Definition: evlist.c:1115
int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, const char *argv[], bool pipe_output, void(*exec_error)(int signo, siginfo_t *info, void *ucontext))
Definition: evlist.c:1428
void perf_mmap__consume(struct perf_mmap *map)
Definition: mmap.c:118
struct perf_mmap * mmap
Definition: evlist.h:45
struct cpu_map * cpu_map__dummy_new(void)
Definition: cpumap.c:252
struct perf_evlist * perf_evlist__new_default(void)
Definition: evlist.c:64
int int err
Definition: 5sec.c:44
void perf_evlist__delete(struct perf_evlist *evlist)
Definition: evlist.c:133
Definition: cpumap.h:12
int perf_evlist__start_workload(struct perf_evlist *evlist)
Definition: evlist.c:1542
Definition: mmap.h:17
struct thread_map * thread_map__new_by_tid(pid_t tid)
Definition: thread_map.c:83
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
int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
Definition: evlist.c:491
#define pr_debug(fmt,...)
Definition: json.h:27
int perf_evlist__open(struct perf_evlist *evlist)
Definition: evlist.c:1398
static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info __maybe_unused, void *ucontext __maybe_unused)
Definition: task-exit.c:23
list cpus
Definition: stat-cpi.py:7
int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
Definition: task-exit.c:36
static int nr_exit
Definition: task-exit.c:12
#define event
static void sig_handler(int sig __maybe_unused)
Definition: task-exit.c:14
int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages)
Definition: evlist.c:1061
static int exited
Definition: task-exit.c:11
static struct thread_data threads[THREADS]
void perf_mmap__read_done(struct perf_mmap *map)
Definition: mmap.c:329
struct perf_event_header header
Definition: event.h:624
uid_t uid
Definition: target.h:13
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
Definition: tests.h:30
#define STRERR_BUFSIZE
Definition: debug.h:43
void thread_map__put(struct thread_map *map)
Definition: thread_map.c:361
struct perf_event_attr attr
Definition: evsel.h:93
Definition: target.h:8