~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/arch/x86/tests/perf-time-to-tsc.c

Version: ~ [ linux-5.11-rc3 ] ~ [ linux-5.10.7 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.89 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.167 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.215 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.251 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.251 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp