perf evlist: Use the right prefix for 'struct evlist' 'workload' methods
[linux-2.6-block.git] / tools / perf / tests / task-exit.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
b4209025 2#include "debug.h"
d723a550
NK
3#include "evlist.h"
4#include "evsel.h"
aeb00b1a 5#include "target.h"
d723a550 6#include "thread_map.h"
d723a550 7#include "tests.h"
e0fcfb08 8#include "util/mmap.h"
d723a550 9
a43783ae 10#include <errno.h>
d723a550 11#include <signal.h>
8520a98d 12#include <linux/string.h>
87ffb6c6 13#include <perf/cpumap.h>
453fa030 14#include <perf/evlist.h>
7728fa0c 15#include <perf/mmap.h>
d723a550
NK
16
17static int exited;
18static int nr_exit;
19
735f7e0b 20static void sig_handler(int sig __maybe_unused)
d723a550
NK
21{
22 exited = 1;
735f7e0b 23}
d723a550 24
735f7e0b 25/*
7b392ef0 26 * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
735f7e0b
ACM
27 * we asked by setting its exec_error to this handler.
28 */
29static void workload_exec_failed_signal(int signo __maybe_unused,
30 siginfo_t *info __maybe_unused,
31 void *ucontext __maybe_unused)
32{
33 exited = 1;
34 nr_exit = -1;
d723a550
NK
35}
36
37/*
38 * This test will start a workload that does nothing then it checks
39 * if the number of exit event reported by the kernel is 1 or not
40 * in order to check the kernel returns correct number of event.
41 */
81f17c90 42int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
d723a550
NK
43{
44 int err = -1;
45 union perf_event *event;
32dcd021 46 struct evsel *evsel;
63503dba 47 struct evlist *evlist;
602ad878 48 struct target target = {
d723a550
NK
49 .uid = UINT_MAX,
50 .uses_mmap = true,
51 };
52 const char *argv[] = { "true", NULL };
ba3dfff8 53 char sbuf[STRERR_BUFSIZE];
f854839b 54 struct perf_cpu_map *cpus;
9749b90e 55 struct perf_thread_map *threads;
a5830532 56 struct mmap *md;
791ce9c4 57 int retry_count = 0;
d723a550
NK
58
59 signal(SIGCHLD, sig_handler);
d723a550 60
b22d54b0 61 evlist = perf_evlist__new_default();
d723a550 62 if (evlist == NULL) {
b22d54b0 63 pr_debug("perf_evlist__new_default\n");
d723a550
NK
64 return -1;
65 }
d723a550
NK
66
67 /*
68 * Create maps of threads and cpus to monitor. In this case
69 * we start with all threads and cpus (-1, -1) but then in
7b392ef0 70 * evlist__prepare_workload we'll fill in the only thread
d723a550
NK
71 * we're monitoring, the one forked there.
72 */
397721e0 73 cpus = perf_cpu_map__dummy_new();
29982722
AH
74 threads = thread_map__new_by_tid(-1);
75 if (!cpus || !threads) {
d723a550
NK
76 err = -ENOMEM;
77 pr_debug("Not enough memory to create thread/cpu maps\n");
29982722 78 goto out_free_maps;
d723a550
NK
79 }
80
453fa030 81 perf_evlist__set_maps(&evlist->core, cpus, threads);
29982722
AH
82
83 cpus = NULL;
84 threads = NULL;
85
7b392ef0 86 err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
d723a550
NK
87 if (err < 0) {
88 pr_debug("Couldn't run the workload!\n");
03ad9747 89 goto out_delete_evlist;
d723a550
NK
90 }
91
515dbe48 92 evsel = evlist__first(evlist);
1fc632ce 93 evsel->core.attr.task = 1;
99654849 94#ifdef __s390x__
1fc632ce 95 evsel->core.attr.sample_freq = 1000000;
99654849 96#else
1fc632ce 97 evsel->core.attr.sample_freq = 1;
99654849 98#endif
1fc632ce
JO
99 evsel->core.attr.inherit = 0;
100 evsel->core.attr.watermark = 0;
101 evsel->core.attr.wakeup_events = 1;
102 evsel->core.attr.exclude_kernel = 1;
d723a550 103
474ddc4c 104 err = evlist__open(evlist);
d723a550 105 if (err < 0) {
ba3dfff8 106 pr_debug("Couldn't open the evlist: %s\n",
c8b5f2c9 107 str_error_r(-err, sbuf, sizeof(sbuf)));
03ad9747 108 goto out_delete_evlist;
d723a550
NK
109 }
110
9521b5f2 111 if (evlist__mmap(evlist, 128) < 0) {
d723a550 112 pr_debug("failed to mmap events: %d (%s)\n", errno,
c8b5f2c9 113 str_error_r(errno, sbuf, sizeof(sbuf)));
6add129c 114 err = -1;
f26e1c7c 115 goto out_delete_evlist;
d723a550
NK
116 }
117
7b392ef0 118 evlist__start_workload(evlist);
d723a550
NK
119
120retry:
75948730 121 md = &evlist->mmap[0];
7c4d4182 122 if (perf_mmap__read_init(&md->core) < 0)
75948730
KL
123 goto out_init;
124
151ed5d7 125 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
8e50d384
ZZ
126 if (event->header.type == PERF_RECORD_EXIT)
127 nr_exit++;
d723a550 128
7728fa0c 129 perf_mmap__consume(&md->core);
d723a550 130 }
32fdc2ca 131 perf_mmap__read_done(&md->core);
d723a550 132
75948730 133out_init:
d723a550 134 if (!exited || !nr_exit) {
80ab2987 135 evlist__poll(evlist, -1);
791ce9c4
LY
136
137 if (retry_count++ > 1000) {
138 pr_debug("Failed after retrying 1000 times\n");
139 err = -1;
140 goto out_free_maps;
141 }
142
d723a550
NK
143 goto retry;
144 }
145
146 if (nr_exit != 1) {
147 pr_debug("received %d EXIT records\n", nr_exit);
148 err = -1;
149 }
150
29982722 151out_free_maps:
38f01d8d 152 perf_cpu_map__put(cpus);
7836e52e 153 perf_thread_map__put(threads);
03ad9747 154out_delete_evlist:
c12995a5 155 evlist__delete(evlist);
d723a550
NK
156 return err;
157}