blob: a8e37f3148c264b9a726bf022bbdba79c62afa8e [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03002#include <stdio.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03003#include <unistd.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02004#include <linux/types.h>
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +03005#include <sys/prctl.h>
6
7#include "parse-events.h"
8#include "evlist.h"
9#include "evsel.h"
10#include "thread_map.h"
11#include "cpumap.h"
Adrian Hunter0b437862014-07-14 13:03:03 +030012#include "tsc.h"
Matt Flemingd8b167f2015-10-05 15:40:20 +010013#include "tests/tests.h"
14
15#include "arch-tests.h"
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030016
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030017#define CHECK__(x) { \
18 while ((x) < 0) { \
19 pr_debug(#x " failed!\n"); \
20 goto out_err; \
21 } \
22}
23
24#define CHECK_NOT_NULL__(x) { \
25 while ((x) == NULL) { \
26 pr_debug(#x " failed!\n"); \
27 goto out_err; \
28 } \
29}
30
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030031/**
32 * test__perf_time_to_tsc - test converting perf time to TSC.
33 *
34 * This function implements a test that checks that the conversion of perf time
35 * to and from TSC is consistent with the order of events. If the test passes
36 * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
37 * supported then then the test passes but " (not supported)" is printed.
38 */
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -030039int test__perf_time_to_tsc(int subtest __maybe_unused)
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030040{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -030041 struct record_opts opts = {
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030042 .mmap_pages = UINT_MAX,
43 .user_freq = UINT_MAX,
44 .user_interval = ULLONG_MAX,
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030045 .target = {
46 .uses_mmap = true,
47 },
48 .sample_time = true,
49 };
50 struct thread_map *threads = NULL;
51 struct cpu_map *cpus = NULL;
52 struct perf_evlist *evlist = NULL;
53 struct perf_evsel *evsel = NULL;
54 int err = -1, ret, i;
55 const char *comm1, *comm2;
56 struct perf_tsc_conversion tc;
57 struct perf_event_mmap_page *pc;
58 union perf_event *event;
59 u64 test_tsc, comm1_tsc, comm2_tsc;
60 u64 test_time, comm1_time = 0, comm2_time = 0;
61
62 threads = thread_map__new(-1, getpid(), UINT_MAX);
63 CHECK_NOT_NULL__(threads);
64
65 cpus = cpu_map__new(NULL);
66 CHECK_NOT_NULL__(cpus);
67
68 evlist = perf_evlist__new();
69 CHECK_NOT_NULL__(evlist);
70
71 perf_evlist__set_maps(evlist, cpus, threads);
72
Jiri Olsab39b8392015-04-22 21:10:16 +020073 CHECK__(parse_events(evlist, "cycles:u", NULL));
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030074
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -030075 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +030076
77 evsel = perf_evlist__first(evlist);
78
79 evsel->attr.comm = 1;
80 evsel->attr.disabled = 1;
81 evsel->attr.enable_on_exec = 0;
82
83 CHECK__(perf_evlist__open(evlist));
84
85 CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false));
86
87 pc = evlist->mmap[0].base;
88 ret = perf_read_tsc_conversion(pc, &tc);
89 if (ret) {
90 if (ret == -EOPNOTSUPP) {
91 fprintf(stderr, " (not supported)");
92 return 0;
93 }
94 goto out_err;
95 }
96
97 perf_evlist__enable(evlist);
98
99 comm1 = "Test COMM 1";
100 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
101
102 test_tsc = rdtsc();
103
104 comm2 = "Test COMM 2";
105 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
106
107 perf_evlist__disable(evlist);
108
109 for (i = 0; i < evlist->nr_mmaps; i++) {
110 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
111 struct perf_sample sample;
112
113 if (event->header.type != PERF_RECORD_COMM ||
114 (pid_t)event->comm.pid != getpid() ||
115 (pid_t)event->comm.tid != getpid())
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800116 goto next_event;
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300117
118 if (strcmp(event->comm.comm, comm1) == 0) {
119 CHECK__(perf_evsel__parse_sample(evsel, event,
120 &sample));
121 comm1_time = sample.time;
122 }
123 if (strcmp(event->comm.comm, comm2) == 0) {
124 CHECK__(perf_evsel__parse_sample(evsel, event,
125 &sample));
126 comm2_time = sample.time;
127 }
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800128next_event:
129 perf_evlist__mmap_consume(evlist, i);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300130 }
131 }
132
133 if (!comm1_time || !comm2_time)
134 goto out_err;
135
136 test_time = tsc_to_perf_time(test_tsc, &tc);
137 comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
138 comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
139
140 pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
141 comm1_time, comm1_tsc);
142 pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
143 test_time, test_tsc);
144 pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
145 comm2_time, comm2_tsc);
146
147 if (test_time <= comm1_time ||
148 test_time >= comm2_time)
149 goto out_err;
150
151 if (test_tsc <= comm1_tsc ||
152 test_tsc >= comm2_tsc)
153 goto out_err;
154
155 err = 0;
156
157out_err:
Arnaldo Carvalho de Melo61b3f662016-06-22 10:10:52 -0300158 perf_evlist__delete(evlist);
Adrian Hunter3bd5a5f2013-06-28 16:22:19 +0300159 return err;
160}