Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.h> |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 4 | #include <stdio.h> |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 5 | #include <unistd.h> |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 6 | #include <linux/types.h> |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 7 | #include <sys/prctl.h> |
| 8 | |
| 9 | #include "parse-events.h" |
| 10 | #include "evlist.h" |
| 11 | #include "evsel.h" |
| 12 | #include "thread_map.h" |
| 13 | #include "cpumap.h" |
Adrian Hunter | 0b43786 | 2014-07-14 13:03:03 +0300 | [diff] [blame] | 14 | #include "tsc.h" |
Matt Fleming | d8b167f | 2015-10-05 15:40:20 +0100 | [diff] [blame] | 15 | #include "tests/tests.h" |
| 16 | |
| 17 | #include "arch-tests.h" |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 18 | |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 19 | #define CHECK__(x) { \ |
| 20 | while ((x) < 0) { \ |
| 21 | pr_debug(#x " failed!\n"); \ |
| 22 | goto out_err; \ |
| 23 | } \ |
| 24 | } |
| 25 | |
| 26 | #define CHECK_NOT_NULL__(x) { \ |
| 27 | while ((x) == NULL) { \ |
| 28 | pr_debug(#x " failed!\n"); \ |
| 29 | goto out_err; \ |
| 30 | } \ |
| 31 | } |
| 32 | |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 33 | /** |
| 34 | * test__perf_time_to_tsc - test converting perf time to TSC. |
| 35 | * |
| 36 | * This function implements a test that checks that the conversion of perf time |
| 37 | * to and from TSC is consistent with the order of events. If the test passes |
| 38 | * %0 is returned, otherwise %-1 is returned. If TSC conversion is not |
| 39 | * supported then then the test passes but " (not supported)" is printed. |
| 40 | */ |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 41 | int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe_unused) |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 42 | { |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 43 | struct record_opts opts = { |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 44 | .mmap_pages = UINT_MAX, |
| 45 | .user_freq = UINT_MAX, |
| 46 | .user_interval = ULLONG_MAX, |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 47 | .target = { |
| 48 | .uses_mmap = true, |
| 49 | }, |
| 50 | .sample_time = true, |
| 51 | }; |
| 52 | struct thread_map *threads = NULL; |
| 53 | struct cpu_map *cpus = NULL; |
| 54 | struct perf_evlist *evlist = NULL; |
| 55 | struct perf_evsel *evsel = NULL; |
| 56 | int err = -1, ret, i; |
| 57 | const char *comm1, *comm2; |
| 58 | struct perf_tsc_conversion tc; |
| 59 | struct perf_event_mmap_page *pc; |
| 60 | union perf_event *event; |
| 61 | u64 test_tsc, comm1_tsc, comm2_tsc; |
| 62 | u64 test_time, comm1_time = 0, comm2_time = 0; |
Kan Liang | 9dfb85d | 2018-03-01 18:09:07 -0500 | [diff] [blame] | 63 | struct perf_mmap *md; |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 64 | |
| 65 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
| 66 | CHECK_NOT_NULL__(threads); |
| 67 | |
| 68 | cpus = cpu_map__new(NULL); |
| 69 | CHECK_NOT_NULL__(cpus); |
| 70 | |
| 71 | evlist = perf_evlist__new(); |
| 72 | CHECK_NOT_NULL__(evlist); |
| 73 | |
| 74 | perf_evlist__set_maps(evlist, cpus, threads); |
| 75 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 76 | CHECK__(parse_events(evlist, "cycles:u", NULL)); |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 77 | |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 78 | perf_evlist__config(evlist, &opts, NULL); |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 79 | |
| 80 | evsel = perf_evlist__first(evlist); |
| 81 | |
| 82 | evsel->attr.comm = 1; |
| 83 | evsel->attr.disabled = 1; |
| 84 | evsel->attr.enable_on_exec = 0; |
| 85 | |
| 86 | CHECK__(perf_evlist__open(evlist)); |
| 87 | |
Wang Nan | f74b9d3a | 2017-12-03 02:00:37 +0000 | [diff] [blame] | 88 | CHECK__(perf_evlist__mmap(evlist, UINT_MAX)); |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 89 | |
| 90 | pc = evlist->mmap[0].base; |
| 91 | ret = perf_read_tsc_conversion(pc, &tc); |
| 92 | if (ret) { |
| 93 | if (ret == -EOPNOTSUPP) { |
| 94 | fprintf(stderr, " (not supported)"); |
| 95 | return 0; |
| 96 | } |
| 97 | goto out_err; |
| 98 | } |
| 99 | |
| 100 | perf_evlist__enable(evlist); |
| 101 | |
| 102 | comm1 = "Test COMM 1"; |
| 103 | CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0)); |
| 104 | |
| 105 | test_tsc = rdtsc(); |
| 106 | |
| 107 | comm2 = "Test COMM 2"; |
| 108 | CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0)); |
| 109 | |
| 110 | perf_evlist__disable(evlist); |
| 111 | |
| 112 | for (i = 0; i < evlist->nr_mmaps; i++) { |
Kan Liang | 9dfb85d | 2018-03-01 18:09:07 -0500 | [diff] [blame] | 113 | md = &evlist->mmap[i]; |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame] | 114 | if (perf_mmap__read_init(md) < 0) |
Kan Liang | 9dfb85d | 2018-03-01 18:09:07 -0500 | [diff] [blame] | 115 | continue; |
| 116 | |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 117 | while ((event = perf_mmap__read_event(md)) != NULL) { |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 118 | struct perf_sample sample; |
| 119 | |
| 120 | if (event->header.type != PERF_RECORD_COMM || |
| 121 | (pid_t)event->comm.pid != getpid() || |
| 122 | (pid_t)event->comm.tid != getpid()) |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 123 | goto next_event; |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 124 | |
| 125 | if (strcmp(event->comm.comm, comm1) == 0) { |
| 126 | CHECK__(perf_evsel__parse_sample(evsel, event, |
| 127 | &sample)); |
| 128 | comm1_time = sample.time; |
| 129 | } |
| 130 | if (strcmp(event->comm.comm, comm2) == 0) { |
| 131 | CHECK__(perf_evsel__parse_sample(evsel, event, |
| 132 | &sample)); |
| 133 | comm2_time = sample.time; |
| 134 | } |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 135 | next_event: |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 136 | perf_mmap__consume(md); |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 137 | } |
Kan Liang | 9dfb85d | 2018-03-01 18:09:07 -0500 | [diff] [blame] | 138 | perf_mmap__read_done(md); |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (!comm1_time || !comm2_time) |
| 142 | goto out_err; |
| 143 | |
| 144 | test_time = tsc_to_perf_time(test_tsc, &tc); |
| 145 | comm1_tsc = perf_time_to_tsc(comm1_time, &tc); |
| 146 | comm2_tsc = perf_time_to_tsc(comm2_time, &tc); |
| 147 | |
| 148 | pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n", |
| 149 | comm1_time, comm1_tsc); |
| 150 | pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n", |
| 151 | test_time, test_tsc); |
| 152 | pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n", |
| 153 | comm2_time, comm2_tsc); |
| 154 | |
| 155 | if (test_time <= comm1_time || |
| 156 | test_time >= comm2_time) |
| 157 | goto out_err; |
| 158 | |
| 159 | if (test_tsc <= comm1_tsc || |
| 160 | test_tsc >= comm2_tsc) |
| 161 | goto out_err; |
| 162 | |
| 163 | err = 0; |
| 164 | |
| 165 | out_err: |
Arnaldo Carvalho de Melo | 61b3f66 | 2016-06-22 10:10:52 -0300 | [diff] [blame] | 166 | perf_evlist__delete(evlist); |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 167 | return err; |
| 168 | } |