linux/tools/perf/arch/x86/tests/perf-time-to-tsc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <errno.h>
   3#include <inttypes.h>
   4#include <stdio.h>
   5#include <unistd.h>
   6#include <linux/types.h>
   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"
  14#include "tsc.h"
  15#include "tests/tests.h"
  16
  17#include "arch-tests.h"
  18
  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
  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 */
  41int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe_unused)
  42{
  43        struct record_opts opts = {
  44                .mmap_pages          = UINT_MAX,
  45                .user_freq           = UINT_MAX,
  46                .user_interval       = ULLONG_MAX,
  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;
  63
  64        threads = thread_map__new(-1, getpid(), UINT_MAX);
  65        CHECK_NOT_NULL__(threads);
  66
  67        cpus = cpu_map__new(NULL);
  68        CHECK_NOT_NULL__(cpus);
  69
  70        evlist = perf_evlist__new();
  71        CHECK_NOT_NULL__(evlist);
  72
  73        perf_evlist__set_maps(evlist, cpus, threads);
  74
  75        CHECK__(parse_events(evlist, "cycles:u", NULL));
  76
  77        perf_evlist__config(evlist, &opts, NULL);
  78
  79        evsel = perf_evlist__first(evlist);
  80
  81        evsel->attr.comm = 1;
  82        evsel->attr.disabled = 1;
  83        evsel->attr.enable_on_exec = 0;
  84
  85        CHECK__(perf_evlist__open(evlist));
  86
  87        CHECK__(perf_evlist__mmap(evlist, UINT_MAX));
  88
  89        pc = evlist->mmap[0].base;
  90        ret = perf_read_tsc_conversion(pc, &tc);
  91        if (ret) {
  92                if (ret == -EOPNOTSUPP) {
  93                        fprintf(stderr, " (not supported)");
  94                        return 0;
  95                }
  96                goto out_err;
  97        }
  98
  99        perf_evlist__enable(evlist);
 100
 101        comm1 = "Test COMM 1";
 102        CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
 103
 104        test_tsc = rdtsc();
 105
 106        comm2 = "Test COMM 2";
 107        CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
 108
 109        perf_evlist__disable(evlist);
 110
 111        for (i = 0; i < evlist->nr_mmaps; i++) {
 112                while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
 113                        struct perf_sample sample;
 114
 115                        if (event->header.type != PERF_RECORD_COMM ||
 116                            (pid_t)event->comm.pid != getpid() ||
 117                            (pid_t)event->comm.tid != getpid())
 118                                goto next_event;
 119
 120                        if (strcmp(event->comm.comm, comm1) == 0) {
 121                                CHECK__(perf_evsel__parse_sample(evsel, event,
 122                                                                 &sample));
 123                                comm1_time = sample.time;
 124                        }
 125                        if (strcmp(event->comm.comm, comm2) == 0) {
 126                                CHECK__(perf_evsel__parse_sample(evsel, event,
 127                                                                 &sample));
 128                                comm2_time = sample.time;
 129                        }
 130next_event:
 131                        perf_evlist__mmap_consume(evlist, i);
 132                }
 133        }
 134
 135        if (!comm1_time || !comm2_time)
 136                goto out_err;
 137
 138        test_time = tsc_to_perf_time(test_tsc, &tc);
 139        comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
 140        comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
 141
 142        pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
 143                 comm1_time, comm1_tsc);
 144        pr_debug("rdtsc          time %"PRIu64" tsc %"PRIu64"\n",
 145                 test_time, test_tsc);
 146        pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
 147                 comm2_time, comm2_tsc);
 148
 149        if (test_time <= comm1_time ||
 150            test_time >= comm2_time)
 151                goto out_err;
 152
 153        if (test_tsc <= comm1_tsc ||
 154            test_tsc >= comm2_tsc)
 155                goto out_err;
 156
 157        err = 0;
 158
 159out_err:
 160        perf_evlist__delete(evlist);
 161        return err;
 162}
 163