blob: 197574230493c319073dbb99a144686fd99cde0d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -03002#include "debug.h"
Namhyung Kimd723a552013-03-15 14:58:11 +09003#include "evlist.h"
4#include "evsel.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -03005#include "target.h"
Namhyung Kimd723a552013-03-15 14:58:11 +09006#include "thread_map.h"
Namhyung Kimd723a552013-03-15 14:58:11 +09007#include "tests.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -03008#include "util/mmap.h"
Namhyung Kimd723a552013-03-15 14:58:11 +09009
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030010#include <errno.h>
Namhyung Kimd723a552013-03-15 14:58:11 +090011#include <signal.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030012#include <linux/string.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +010013#include <perf/cpumap.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020014#include <perf/evlist.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +020015#include <perf/mmap.h>
Namhyung Kimd723a552013-03-15 14:58:11 +090016
17static int exited;
18static int nr_exit;
19
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -030020static void sig_handler(int sig __maybe_unused)
Namhyung Kimd723a552013-03-15 14:58:11 +090021{
22 exited = 1;
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -030023}
Namhyung Kimd723a552013-03-15 14:58:11 +090024
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -030025/*
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -030026 * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -030027 * 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;
Namhyung Kimd723a552013-03-15 14:58:11 +090035}
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 */
Ian Rogersd68f0362021-11-03 23:41:50 -070042static int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
Namhyung Kimd723a552013-03-15 14:58:11 +090043{
44 int err = -1;
45 union perf_event *event;
Jiri Olsa32dcd022019-07-21 13:23:51 +020046 struct evsel *evsel;
Jiri Olsa63503db2019-07-21 13:23:52 +020047 struct evlist *evlist;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -030048 struct target target = {
Namhyung Kimd723a552013-03-15 14:58:11 +090049 .uid = UINT_MAX,
50 .uses_mmap = true,
51 };
52 const char *argv[] = { "true", NULL };
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000053 char sbuf[STRERR_BUFSIZE];
Jiri Olsaf8548392019-07-21 13:23:49 +020054 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +020055 struct perf_thread_map *threads;
Jiri Olsaa5830532019-07-27 20:30:53 +020056 struct mmap *md;
Leo Yan791ce9c2019-10-11 17:19:42 +080057 int retry_count = 0;
Namhyung Kimd723a552013-03-15 14:58:11 +090058
59 signal(SIGCHLD, sig_handler);
Namhyung Kimd723a552013-03-15 14:58:11 +090060
Arnaldo Carvalho de Melo606e2c22020-11-30 15:04:05 -030061 evlist = evlist__new_default();
Namhyung Kimd723a552013-03-15 14:58:11 +090062 if (evlist == NULL) {
Arnaldo Carvalho de Melo606e2c22020-11-30 15:04:05 -030063 pr_debug("evlist__new_default\n");
Namhyung Kimd723a552013-03-15 14:58:11 +090064 return -1;
65 }
Namhyung Kimd723a552013-03-15 14:58:11 +090066
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
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -030070 * evlist__prepare_workload we'll fill in the only thread
Namhyung Kimd723a552013-03-15 14:58:11 +090071 * we're monitoring, the one forked there.
72 */
Jiri Olsa397721e2019-07-21 13:24:16 +020073 cpus = perf_cpu_map__dummy_new();
Adrian Hunter29982722015-09-08 10:59:01 +030074 threads = thread_map__new_by_tid(-1);
75 if (!cpus || !threads) {
Namhyung Kimd723a552013-03-15 14:58:11 +090076 err = -ENOMEM;
77 pr_debug("Not enough memory to create thread/cpu maps\n");
Namhyung Kim83d25cc2021-03-01 23:04:01 +090078 goto out_delete_evlist;
Namhyung Kimd723a552013-03-15 14:58:11 +090079 }
80
Jiri Olsa453fa032019-07-21 13:24:43 +020081 perf_evlist__set_maps(&evlist->core, cpus, threads);
Adrian Hunter29982722015-09-08 10:59:01 +030082
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -030083 err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
Namhyung Kimd723a552013-03-15 14:58:11 +090084 if (err < 0) {
85 pr_debug("Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -030086 goto out_delete_evlist;
Namhyung Kimd723a552013-03-15 14:58:11 +090087 }
88
Jiri Olsa515dbe42019-09-03 10:39:52 +020089 evsel = evlist__first(evlist);
Jiri Olsa1fc632c2019-07-21 13:24:29 +020090 evsel->core.attr.task = 1;
Thomas Richter99654842017-11-23 12:46:11 +010091#ifdef __s390x__
Jiri Olsa1fc632c2019-07-21 13:24:29 +020092 evsel->core.attr.sample_freq = 1000000;
Thomas Richter99654842017-11-23 12:46:11 +010093#else
Jiri Olsa1fc632c2019-07-21 13:24:29 +020094 evsel->core.attr.sample_freq = 1;
Thomas Richter99654842017-11-23 12:46:11 +010095#endif
Jiri Olsa1fc632c2019-07-21 13:24:29 +020096 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;
Namhyung Kimd723a552013-03-15 14:58:11 +0900100
Jiri Olsa474ddc42019-07-21 13:24:06 +0200101 err = evlist__open(evlist);
Namhyung Kimd723a552013-03-15 14:58:11 +0900102 if (err < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000103 pr_debug("Couldn't open the evlist: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300104 str_error_r(-err, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300105 goto out_delete_evlist;
Namhyung Kimd723a552013-03-15 14:58:11 +0900106 }
107
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200108 if (evlist__mmap(evlist, 128) < 0) {
Namhyung Kimd723a552013-03-15 14:58:11 +0900109 pr_debug("failed to mmap events: %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300110 str_error_r(errno, sbuf, sizeof(sbuf)));
Leo Yan6add1292019-10-11 17:19:41 +0800111 err = -1;
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300112 goto out_delete_evlist;
Namhyung Kimd723a552013-03-15 14:58:11 +0900113 }
114
Arnaldo Carvalho de Melo7b392ef2020-11-30 09:26:54 -0300115 evlist__start_workload(evlist);
Namhyung Kimd723a552013-03-15 14:58:11 +0900116
117retry:
Kan Liang75948732018-03-01 18:09:10 -0500118 md = &evlist->mmap[0];
Jiri Olsa7c4d4182019-10-07 14:53:18 +0200119 if (perf_mmap__read_init(&md->core) < 0)
Kan Liang75948732018-03-01 18:09:10 -0500120 goto out_init;
121
Jiri Olsa151ed5d2019-10-07 14:53:20 +0200122 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800123 if (event->header.type == PERF_RECORD_EXIT)
124 nr_exit++;
Namhyung Kimd723a552013-03-15 14:58:11 +0900125
Jiri Olsa7728fa02019-10-07 14:53:17 +0200126 perf_mmap__consume(&md->core);
Namhyung Kimd723a552013-03-15 14:58:11 +0900127 }
Jiri Olsa32fdc2c2019-10-07 14:53:19 +0200128 perf_mmap__read_done(&md->core);
Namhyung Kimd723a552013-03-15 14:58:11 +0900129
Kan Liang75948732018-03-01 18:09:10 -0500130out_init:
Namhyung Kimd723a552013-03-15 14:58:11 +0900131 if (!exited || !nr_exit) {
Jiri Olsa80ab2982019-08-31 22:48:33 +0200132 evlist__poll(evlist, -1);
Leo Yan791ce9c2019-10-11 17:19:42 +0800133
134 if (retry_count++ > 1000) {
135 pr_debug("Failed after retrying 1000 times\n");
136 err = -1;
Namhyung Kim83d25cc2021-03-01 23:04:01 +0900137 goto out_delete_evlist;
Leo Yan791ce9c2019-10-11 17:19:42 +0800138 }
139
Namhyung Kimd723a552013-03-15 14:58:11 +0900140 goto retry;
141 }
142
143 if (nr_exit != 1) {
144 pr_debug("received %d EXIT records\n", nr_exit);
145 err = -1;
146 }
147
Namhyung Kim83d25cc2021-03-01 23:04:01 +0900148out_delete_evlist:
Jiri Olsa38f01d82019-07-21 13:24:17 +0200149 perf_cpu_map__put(cpus);
Jiri Olsa7836e522019-07-21 13:24:20 +0200150 perf_thread_map__put(threads);
Jiri Olsac12995a2019-07-21 13:23:56 +0200151 evlist__delete(evlist);
Namhyung Kimd723a552013-03-15 14:58:11 +0900152 return err;
153}
Ian Rogersd68f0362021-11-03 23:41:50 -0700154
155DEFINE_SUITE("Number of exit events of a simple workload", task_exit);