Merge branches 'acpi-bus' and 'acpi-video'
[linux-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 */
33f44bfd 42static int test__task_exit(struct test_suite *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
2480232c 61 evlist = evlist__new_dummy();
d723a550 62 if (evlist == NULL) {
2480232c 63 pr_debug("evlist__new_dummy\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");
83d25ccd 78 goto out_delete_evlist;
d723a550
NK
79 }
80
453fa030 81 perf_evlist__set_maps(&evlist->core, cpus, threads);
29982722 82
7b392ef0 83 err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
d723a550
NK
84 if (err < 0) {
85 pr_debug("Couldn't run the workload!\n");
03ad9747 86 goto out_delete_evlist;
d723a550
NK
87 }
88
515dbe48 89 evsel = evlist__first(evlist);
1fc632ce 90 evsel->core.attr.task = 1;
99654849 91#ifdef __s390x__
1fc632ce 92 evsel->core.attr.sample_freq = 1000000;
99654849 93#else
1fc632ce 94 evsel->core.attr.sample_freq = 1;
99654849 95#endif
1fc632ce
JO
96 evsel->core.attr.inherit = 0;
97 evsel->core.attr.watermark = 0;
98 evsel->core.attr.wakeup_events = 1;
99 evsel->core.attr.exclude_kernel = 1;
d723a550 100
474ddc4c 101 err = evlist__open(evlist);
d723a550 102 if (err < 0) {
ba3dfff8 103 pr_debug("Couldn't open the evlist: %s\n",
c8b5f2c9 104 str_error_r(-err, sbuf, sizeof(sbuf)));
03ad9747 105 goto out_delete_evlist;
d723a550
NK
106 }
107
9521b5f2 108 if (evlist__mmap(evlist, 128) < 0) {
d723a550 109 pr_debug("failed to mmap events: %d (%s)\n", errno,
c8b5f2c9 110 str_error_r(errno, sbuf, sizeof(sbuf)));
6add129c 111 err = -1;
f26e1c7c 112 goto out_delete_evlist;
d723a550
NK
113 }
114
7b392ef0 115 evlist__start_workload(evlist);
d723a550
NK
116
117retry:
75948730 118 md = &evlist->mmap[0];
7c4d4182 119 if (perf_mmap__read_init(&md->core) < 0)
75948730
KL
120 goto out_init;
121
151ed5d7 122 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
8e50d384
ZZ
123 if (event->header.type == PERF_RECORD_EXIT)
124 nr_exit++;
d723a550 125
7728fa0c 126 perf_mmap__consume(&md->core);
d723a550 127 }
32fdc2ca 128 perf_mmap__read_done(&md->core);
d723a550 129
75948730 130out_init:
d723a550 131 if (!exited || !nr_exit) {
80ab2987 132 evlist__poll(evlist, -1);
791ce9c4
LY
133
134 if (retry_count++ > 1000) {
135 pr_debug("Failed after retrying 1000 times\n");
136 err = -1;
83d25ccd 137 goto out_delete_evlist;
791ce9c4
LY
138 }
139
d723a550
NK
140 goto retry;
141 }
142
143 if (nr_exit != 1) {
144 pr_debug("received %d EXIT records\n", nr_exit);
145 err = -1;
146 }
147
83d25ccd 148out_delete_evlist:
38f01d8d 149 perf_cpu_map__put(cpus);
7836e52e 150 perf_thread_map__put(threads);
c12995a5 151 evlist__delete(evlist);
d723a550
NK
152 return err;
153}
d68f0365
IR
154
155DEFINE_SUITE("Number of exit events of a simple workload", task_exit);