0001
0002 #include <errno.h>
0003 #include <inttypes.h>
0004 #include <limits.h>
0005 #include <stdbool.h>
0006 #include <stdio.h>
0007 #include <unistd.h>
0008 #include <linux/types.h>
0009 #include <sys/prctl.h>
0010 #include <perf/cpumap.h>
0011 #include <perf/evlist.h>
0012 #include <perf/mmap.h>
0013
0014 #include "debug.h"
0015 #include "parse-events.h"
0016 #include "evlist.h"
0017 #include "evsel.h"
0018 #include "thread_map.h"
0019 #include "record.h"
0020 #include "tsc.h"
0021 #include "mmap.h"
0022 #include "tests.h"
0023
0024
0025
0026
0027
0028 #if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__)
0029 #define TSC_IS_SUPPORTED 1
0030 #else
0031 #define TSC_IS_SUPPORTED 0
0032 #endif
0033
0034 #define CHECK__(x) { \
0035 while ((x) < 0) { \
0036 pr_debug(#x " failed!\n"); \
0037 goto out_err; \
0038 } \
0039 }
0040
0041 #define CHECK_NOT_NULL__(x) { \
0042 while ((x) == NULL) { \
0043 pr_debug(#x " failed!\n"); \
0044 goto out_err; \
0045 } \
0046 }
0047
0048 static int test__tsc_is_supported(struct test_suite *test __maybe_unused,
0049 int subtest __maybe_unused)
0050 {
0051 if (!TSC_IS_SUPPORTED) {
0052 pr_debug("Test not supported on this architecture\n");
0053 return TEST_SKIP;
0054 }
0055
0056 return TEST_OK;
0057 }
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0068 {
0069 struct record_opts opts = {
0070 .mmap_pages = UINT_MAX,
0071 .user_freq = UINT_MAX,
0072 .user_interval = ULLONG_MAX,
0073 .target = {
0074 .uses_mmap = true,
0075 },
0076 .sample_time = true,
0077 };
0078 struct perf_thread_map *threads = NULL;
0079 struct perf_cpu_map *cpus = NULL;
0080 struct evlist *evlist = NULL;
0081 struct evsel *evsel = NULL;
0082 int err = TEST_FAIL, ret, i;
0083 const char *comm1, *comm2;
0084 struct perf_tsc_conversion tc;
0085 struct perf_event_mmap_page *pc;
0086 union perf_event *event;
0087 u64 test_tsc, comm1_tsc, comm2_tsc;
0088 u64 test_time, comm1_time = 0, comm2_time = 0;
0089 struct mmap *md;
0090
0091
0092 threads = thread_map__new(-1, getpid(), UINT_MAX);
0093 CHECK_NOT_NULL__(threads);
0094
0095 cpus = perf_cpu_map__new(NULL);
0096 CHECK_NOT_NULL__(cpus);
0097
0098 evlist = evlist__new();
0099 CHECK_NOT_NULL__(evlist);
0100
0101 perf_evlist__set_maps(&evlist->core, cpus, threads);
0102
0103 CHECK__(parse_event(evlist, "cycles:u"));
0104
0105 evlist__config(evlist, &opts, NULL);
0106
0107
0108 evlist__for_each_entry(evlist, evsel) {
0109 evsel->core.attr.comm = 1;
0110 evsel->core.attr.disabled = 1;
0111 evsel->core.attr.enable_on_exec = 0;
0112 }
0113
0114 ret = evlist__open(evlist);
0115 if (ret < 0) {
0116 if (ret == -ENOENT)
0117 err = TEST_SKIP;
0118 else
0119 pr_debug("evlist__open() failed\n");
0120 goto out_err;
0121 }
0122
0123 CHECK__(evlist__mmap(evlist, UINT_MAX));
0124
0125 pc = evlist->mmap[0].core.base;
0126 ret = perf_read_tsc_conversion(pc, &tc);
0127 if (ret) {
0128 if (ret == -EOPNOTSUPP) {
0129 pr_debug("perf_read_tsc_conversion is not supported in current kernel\n");
0130 err = TEST_SKIP;
0131 }
0132 goto out_err;
0133 }
0134
0135 evlist__enable(evlist);
0136
0137 comm1 = "Test COMM 1";
0138 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
0139
0140 test_tsc = rdtsc();
0141
0142 comm2 = "Test COMM 2";
0143 CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
0144
0145 evlist__disable(evlist);
0146
0147 for (i = 0; i < evlist->core.nr_mmaps; i++) {
0148 md = &evlist->mmap[i];
0149 if (perf_mmap__read_init(&md->core) < 0)
0150 continue;
0151
0152 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
0153 struct perf_sample sample;
0154
0155 if (event->header.type != PERF_RECORD_COMM ||
0156 (pid_t)event->comm.pid != getpid() ||
0157 (pid_t)event->comm.tid != getpid())
0158 goto next_event;
0159
0160 if (strcmp(event->comm.comm, comm1) == 0) {
0161 CHECK_NOT_NULL__(evsel = evlist__event2evsel(evlist, event));
0162 CHECK__(evsel__parse_sample(evsel, event, &sample));
0163 comm1_time = sample.time;
0164 }
0165 if (strcmp(event->comm.comm, comm2) == 0) {
0166 CHECK_NOT_NULL__(evsel = evlist__event2evsel(evlist, event));
0167 CHECK__(evsel__parse_sample(evsel, event, &sample));
0168 comm2_time = sample.time;
0169 }
0170 next_event:
0171 perf_mmap__consume(&md->core);
0172 }
0173 perf_mmap__read_done(&md->core);
0174 }
0175
0176 if (!comm1_time || !comm2_time)
0177 goto out_err;
0178
0179 test_time = tsc_to_perf_time(test_tsc, &tc);
0180 comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
0181 comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
0182
0183 pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
0184 comm1_time, comm1_tsc);
0185 pr_debug("rdtsc time %"PRIu64" tsc %"PRIu64"\n",
0186 test_time, test_tsc);
0187 pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
0188 comm2_time, comm2_tsc);
0189
0190 if (test_time <= comm1_time ||
0191 test_time >= comm2_time)
0192 goto out_err;
0193
0194 if (test_tsc <= comm1_tsc ||
0195 test_tsc >= comm2_tsc)
0196 goto out_err;
0197
0198 err = TEST_OK;
0199
0200 out_err:
0201 evlist__delete(evlist);
0202 perf_cpu_map__put(cpus);
0203 perf_thread_map__put(threads);
0204 return err;
0205 }
0206
0207 static struct test_case time_to_tsc_tests[] = {
0208 TEST_CASE_REASON("TSC support", tsc_is_supported,
0209 "This architecture does not support"),
0210 TEST_CASE_REASON("Perf time to TSC", perf_time_to_tsc,
0211 "perf_read_tsc_conversion is not supported"),
0212 { .name = NULL, }
0213 };
0214
0215 struct test_suite suite__perf_time_to_tsc = {
0216 .desc = "Convert perf time to TSC",
0217 .test_cases = time_to_tsc_tests,
0218 };