Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 1 | #include "perf.h" |
| 2 | #include "evlist.h" |
| 3 | #include "evsel.h" |
| 4 | #include "thread_map.h" |
| 5 | #include "tests.h" |
| 6 | |
| 7 | int test__syscall_open_tp_fields(void) |
| 8 | { |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 9 | struct record_opts opts = { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 10 | .target = { |
| 11 | .uid = UINT_MAX, |
| 12 | .uses_mmap = true, |
| 13 | }, |
| 14 | .no_delay = true, |
| 15 | .freq = 1, |
| 16 | .mmap_pages = 256, |
| 17 | .raw_samples = true, |
| 18 | }; |
| 19 | const char *filename = "/etc/passwd"; |
| 20 | int flags = O_RDONLY | O_DIRECTORY; |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 21 | struct perf_evlist *evlist = perf_evlist__new(); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 22 | struct perf_evsel *evsel; |
| 23 | int err = -1, i, nr_events = 0, nr_polls = 0; |
| 24 | |
| 25 | if (evlist == NULL) { |
| 26 | pr_debug("%s: perf_evlist__new\n", __func__); |
| 27 | goto out; |
| 28 | } |
| 29 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 30 | evsel = perf_evsel__newtp("syscalls", "sys_enter_open"); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 31 | if (evsel == NULL) { |
| 32 | pr_debug("%s: perf_evsel__newtp\n", __func__); |
| 33 | goto out_delete_evlist; |
| 34 | } |
| 35 | |
| 36 | perf_evlist__add(evlist, evsel); |
| 37 | |
| 38 | err = perf_evlist__create_maps(evlist, &opts.target); |
| 39 | if (err < 0) { |
| 40 | pr_debug("%s: perf_evlist__create_maps\n", __func__); |
| 41 | goto out_delete_evlist; |
| 42 | } |
| 43 | |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 44 | perf_evsel__config(evsel, &opts); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 45 | |
| 46 | evlist->threads->map[0] = getpid(); |
| 47 | |
| 48 | err = perf_evlist__open(evlist); |
| 49 | if (err < 0) { |
| 50 | pr_debug("perf_evlist__open: %s\n", strerror(errno)); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame^] | 51 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | err = perf_evlist__mmap(evlist, UINT_MAX, false); |
| 55 | if (err < 0) { |
| 56 | pr_debug("perf_evlist__mmap: %s\n", strerror(errno)); |
Namhyung Kim | 9b5b7cd | 2013-03-15 14:48:52 +0900 | [diff] [blame] | 57 | goto out_close_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | perf_evlist__enable(evlist); |
| 61 | |
| 62 | /* |
| 63 | * Generate the event: |
| 64 | */ |
| 65 | open(filename, flags); |
| 66 | |
| 67 | while (1) { |
| 68 | int before = nr_events; |
| 69 | |
| 70 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 71 | union perf_event *event; |
| 72 | |
| 73 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 74 | const u32 type = event->header.type; |
| 75 | int tp_flags; |
| 76 | struct perf_sample sample; |
| 77 | |
| 78 | ++nr_events; |
| 79 | |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 80 | if (type != PERF_RECORD_SAMPLE) { |
| 81 | perf_evlist__mmap_consume(evlist, i); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 82 | continue; |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 83 | } |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 84 | |
| 85 | err = perf_evsel__parse_sample(evsel, event, &sample); |
| 86 | if (err) { |
| 87 | pr_err("Can't parse sample, err = %d\n", err); |
| 88 | goto out_munmap; |
| 89 | } |
| 90 | |
| 91 | tp_flags = perf_evsel__intval(evsel, &sample, "flags"); |
| 92 | |
| 93 | if (flags != tp_flags) { |
| 94 | pr_debug("%s: Expected flags=%#x, got %#x\n", |
| 95 | __func__, flags, tp_flags); |
| 96 | goto out_munmap; |
| 97 | } |
| 98 | |
| 99 | goto out_ok; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (nr_events == before) |
| 104 | poll(evlist->pollfd, evlist->nr_fds, 10); |
| 105 | |
| 106 | if (++nr_polls > 5) { |
| 107 | pr_debug("%s: no events!\n", __func__); |
| 108 | goto out_munmap; |
| 109 | } |
| 110 | } |
| 111 | out_ok: |
| 112 | err = 0; |
| 113 | out_munmap: |
| 114 | perf_evlist__munmap(evlist); |
Namhyung Kim | 9b5b7cd | 2013-03-15 14:48:52 +0900 | [diff] [blame] | 115 | out_close_evlist: |
| 116 | perf_evlist__close(evlist); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 117 | out_delete_evlist: |
| 118 | perf_evlist__delete(evlist); |
| 119 | out: |
| 120 | return err; |
| 121 | } |