Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 1 | #include "evlist.h" |
| 2 | #include "evsel.h" |
| 3 | #include "thread_map.h" |
| 4 | #include "cpumap.h" |
| 5 | #include "tests.h" |
| 6 | |
| 7 | #include <signal.h> |
| 8 | |
| 9 | static int exited; |
| 10 | static int nr_exit; |
| 11 | |
| 12 | static void sig_handler(int sig) |
| 13 | { |
| 14 | exited = 1; |
| 15 | |
| 16 | if (sig == SIGUSR1) |
| 17 | nr_exit = -1; |
| 18 | } |
| 19 | |
| 20 | /* |
| 21 | * This test will start a workload that does nothing then it checks |
| 22 | * if the number of exit event reported by the kernel is 1 or not |
| 23 | * in order to check the kernel returns correct number of event. |
| 24 | */ |
| 25 | int test__task_exit(void) |
| 26 | { |
| 27 | int err = -1; |
| 28 | union perf_event *event; |
| 29 | struct perf_evsel *evsel; |
| 30 | struct perf_evlist *evlist; |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame^] | 31 | struct target target = { |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 32 | .uid = UINT_MAX, |
| 33 | .uses_mmap = true, |
| 34 | }; |
| 35 | const char *argv[] = { "true", NULL }; |
| 36 | |
| 37 | signal(SIGCHLD, sig_handler); |
| 38 | signal(SIGUSR1, sig_handler); |
| 39 | |
Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 40 | evlist = perf_evlist__new_default(); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 41 | if (evlist == NULL) { |
Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 42 | pr_debug("perf_evlist__new_default\n"); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 43 | return -1; |
| 44 | } |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Create maps of threads and cpus to monitor. In this case |
| 48 | * we start with all threads and cpus (-1, -1) but then in |
| 49 | * perf_evlist__prepare_workload we'll fill in the only thread |
| 50 | * we're monitoring, the one forked there. |
| 51 | */ |
| 52 | evlist->cpus = cpu_map__dummy_new(); |
| 53 | evlist->threads = thread_map__new_by_tid(-1); |
| 54 | if (!evlist->cpus || !evlist->threads) { |
| 55 | err = -ENOMEM; |
| 56 | pr_debug("Not enough memory to create thread/cpu maps\n"); |
| 57 | goto out_delete_maps; |
| 58 | } |
| 59 | |
| 60 | err = perf_evlist__prepare_workload(evlist, &target, argv, false, true); |
| 61 | if (err < 0) { |
| 62 | pr_debug("Couldn't run the workload!\n"); |
| 63 | goto out_delete_maps; |
| 64 | } |
| 65 | |
| 66 | evsel = perf_evlist__first(evlist); |
| 67 | evsel->attr.task = 1; |
| 68 | evsel->attr.sample_freq = 0; |
| 69 | evsel->attr.inherit = 0; |
| 70 | evsel->attr.watermark = 0; |
| 71 | evsel->attr.wakeup_events = 1; |
| 72 | evsel->attr.exclude_kernel = 1; |
| 73 | |
| 74 | err = perf_evlist__open(evlist); |
| 75 | if (err < 0) { |
| 76 | pr_debug("Couldn't open the evlist: %s\n", strerror(-err)); |
| 77 | goto out_delete_maps; |
| 78 | } |
| 79 | |
| 80 | if (perf_evlist__mmap(evlist, 128, true) < 0) { |
| 81 | pr_debug("failed to mmap events: %d (%s)\n", errno, |
| 82 | strerror(errno)); |
| 83 | goto out_close_evlist; |
| 84 | } |
| 85 | |
| 86 | perf_evlist__start_workload(evlist); |
| 87 | |
| 88 | retry: |
| 89 | while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) { |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 90 | if (event->header.type == PERF_RECORD_EXIT) |
| 91 | nr_exit++; |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 92 | |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 93 | perf_evlist__mmap_consume(evlist, 0); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (!exited || !nr_exit) { |
| 97 | poll(evlist->pollfd, evlist->nr_fds, -1); |
| 98 | goto retry; |
| 99 | } |
| 100 | |
| 101 | if (nr_exit != 1) { |
| 102 | pr_debug("received %d EXIT records\n", nr_exit); |
| 103 | err = -1; |
| 104 | } |
| 105 | |
| 106 | perf_evlist__munmap(evlist); |
| 107 | out_close_evlist: |
| 108 | perf_evlist__close(evlist); |
| 109 | out_delete_maps: |
| 110 | perf_evlist__delete_maps(evlist); |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 111 | perf_evlist__delete(evlist); |
| 112 | return err; |
| 113 | } |