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