| Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 2 | #include "evlist.h" | 
|  | 3 | #include "evsel.h" | 
|  | 4 | #include "thread_map.h" | 
|  | 5 | #include "cpumap.h" | 
|  | 6 | #include "tests.h" | 
|  | 7 |  | 
| Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 8 | #include <errno.h> | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 9 | #include <signal.h> | 
|  | 10 |  | 
|  | 11 | static int exited; | 
|  | 12 | static int nr_exit; | 
|  | 13 |  | 
| Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 14 | static void sig_handler(int sig __maybe_unused) | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 15 | { | 
|  | 16 | exited = 1; | 
| Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 17 | } | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 18 |  | 
| Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 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; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 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 | */ | 
| Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 36 | int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused) | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 37 | { | 
|  | 38 | int err = -1; | 
|  | 39 | union perf_event *event; | 
|  | 40 | struct perf_evsel *evsel; | 
|  | 41 | struct perf_evlist *evlist; | 
| Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 42 | struct target target = { | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 43 | .uid		= UINT_MAX, | 
|  | 44 | .uses_mmap	= true, | 
|  | 45 | }; | 
|  | 46 | const char *argv[] = { "true", NULL }; | 
| Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 47 | char sbuf[STRERR_BUFSIZE]; | 
| Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 48 | struct cpu_map *cpus; | 
|  | 49 | struct thread_map *threads; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 50 |  | 
|  | 51 | signal(SIGCHLD, sig_handler); | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 52 |  | 
| Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 53 | evlist = perf_evlist__new_default(); | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 54 | if (evlist == NULL) { | 
| Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 55 | pr_debug("perf_evlist__new_default\n"); | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 56 | return -1; | 
|  | 57 | } | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 58 |  | 
|  | 59 | /* | 
|  | 60 | * Create maps of threads and cpus to monitor. In this case | 
|  | 61 | * we start with all threads and cpus (-1, -1) but then in | 
|  | 62 | * perf_evlist__prepare_workload we'll fill in the only thread | 
|  | 63 | * we're monitoring, the one forked there. | 
|  | 64 | */ | 
| Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 65 | cpus = cpu_map__dummy_new(); | 
|  | 66 | threads = thread_map__new_by_tid(-1); | 
|  | 67 | if (!cpus || !threads) { | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 68 | err = -ENOMEM; | 
|  | 69 | pr_debug("Not enough memory to create thread/cpu maps\n"); | 
| Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 70 | goto out_free_maps; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 73 | perf_evlist__set_maps(evlist, cpus, threads); | 
|  | 74 |  | 
|  | 75 | cpus	= NULL; | 
|  | 76 | threads = NULL; | 
|  | 77 |  | 
| Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 78 | err = perf_evlist__prepare_workload(evlist, &target, argv, false, | 
|  | 79 | workload_exec_failed_signal); | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 80 | if (err < 0) { | 
|  | 81 | pr_debug("Couldn't run the workload!\n"); | 
| Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 82 | goto out_delete_evlist; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | evsel = perf_evlist__first(evlist); | 
|  | 86 | evsel->attr.task = 1; | 
| Thomas Richter | 9965484 | 2017-11-23 12:46:11 +0100 | [diff] [blame^] | 87 | #ifdef __s390x__ | 
|  | 88 | evsel->attr.sample_freq = 1000000; | 
|  | 89 | #else | 
| Arnaldo Carvalho de Melo | 7a1ac11 | 2017-06-09 16:54:28 -0300 | [diff] [blame] | 90 | evsel->attr.sample_freq = 1; | 
| Thomas Richter | 9965484 | 2017-11-23 12:46:11 +0100 | [diff] [blame^] | 91 | #endif | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 92 | evsel->attr.inherit = 0; | 
|  | 93 | evsel->attr.watermark = 0; | 
|  | 94 | evsel->attr.wakeup_events = 1; | 
|  | 95 | evsel->attr.exclude_kernel = 1; | 
|  | 96 |  | 
|  | 97 | err = perf_evlist__open(evlist); | 
|  | 98 | if (err < 0) { | 
| Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 99 | pr_debug("Couldn't open the evlist: %s\n", | 
| Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 100 | str_error_r(-err, sbuf, sizeof(sbuf))); | 
| Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 101 | goto out_delete_evlist; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | if (perf_evlist__mmap(evlist, 128, true) < 0) { | 
|  | 105 | pr_debug("failed to mmap events: %d (%s)\n", errno, | 
| Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 106 | str_error_r(errno, sbuf, sizeof(sbuf))); | 
| Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 107 | goto out_delete_evlist; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | perf_evlist__start_workload(evlist); | 
|  | 111 |  | 
|  | 112 | retry: | 
|  | 113 | while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) { | 
| Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 114 | if (event->header.type == PERF_RECORD_EXIT) | 
|  | 115 | nr_exit++; | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 116 |  | 
| Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 117 | perf_evlist__mmap_consume(evlist, 0); | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
|  | 120 | if (!exited || !nr_exit) { | 
| Arnaldo Carvalho de Melo | f66a889 | 2014-08-18 17:25:59 -0300 | [diff] [blame] | 121 | perf_evlist__poll(evlist, -1); | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 122 | goto retry; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | if (nr_exit != 1) { | 
|  | 126 | pr_debug("received %d EXIT records\n", nr_exit); | 
|  | 127 | err = -1; | 
|  | 128 | } | 
|  | 129 |  | 
| Adrian Hunter | 2998272 | 2015-09-08 10:59:01 +0300 | [diff] [blame] | 130 | out_free_maps: | 
|  | 131 | cpu_map__put(cpus); | 
|  | 132 | thread_map__put(threads); | 
| Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 133 | out_delete_evlist: | 
| Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 134 | perf_evlist__delete(evlist); | 
|  | 135 | return err; | 
|  | 136 | } |