1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include <unistd.h> 5 #include <stdlib.h> 6 #include <signal.h> 7 #include <sys/mman.h> 8 #include <linux/compiler.h> 9 #include <linux/string.h> 10 11 #include "tests.h" 12 #include "util/debug.h" 13 #include "util/evsel.h" 14 #include "util/evlist.h" 15 #include "util/cpumap.h" 16 #include "util/mmap.h" 17 #include "util/sample.h" 18 #include "util/thread_map.h" 19 #include <perf/evlist.h> 20 #include <perf/mmap.h> 21 22 #define NR_LOOPS 10000000 23 24 /* 25 * This test will open software clock events (cpu-clock, task-clock) 26 * then check their frequency -> period conversion has no artifact of 27 * setting period to 1 forcefully. 28 */ 29 static int __test__sw_clock_freq(enum perf_sw_ids clock_id) 30 { 31 int i, err = -1; 32 volatile int tmp __maybe_unused = 0; 33 u64 total_periods = 0; 34 int nr_samples = 0; 35 char sbuf[STRERR_BUFSIZE]; 36 union perf_event *event; 37 struct evsel *evsel; 38 struct evlist *evlist; 39 struct perf_event_attr attr = { 40 .type = PERF_TYPE_SOFTWARE, 41 .config = clock_id, 42 .sample_type = PERF_SAMPLE_PERIOD, 43 .exclude_kernel = 1, 44 .disabled = 1, 45 .freq = 1, 46 }; 47 struct perf_cpu_map *cpus = NULL; 48 struct perf_thread_map *threads = NULL; 49 struct mmap *md; 50 51 attr.sample_freq = 500; 52 53 evlist = evlist__new(); 54 if (evlist == NULL) { 55 pr_debug("evlist__new\n"); 56 return -1; 57 } 58 59 evsel = evsel__new(&attr); 60 if (evsel == NULL) { 61 pr_debug("evsel__new\n"); 62 goto out_delete_evlist; 63 } 64 evlist__add(evlist, evsel); 65 66 cpus = perf_cpu_map__new_any_cpu(); 67 threads = thread_map__new_by_tid(getpid()); 68 if (!cpus || !threads) { 69 err = -ENOMEM; 70 pr_debug("Not enough memory to create thread/cpu maps\n"); 71 goto out_delete_evlist; 72 } 73 74 perf_evlist__set_maps(&evlist->core, cpus, threads); 75 76 if (evlist__open(evlist)) { 77 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate"; 78 79 err = -errno; 80 pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n", 81 str_error_r(errno, sbuf, sizeof(sbuf)), 82 knob, (u64)attr.sample_freq); 83 goto out_delete_evlist; 84 } 85 86 err = evlist__mmap(evlist, 128); 87 if (err < 0) { 88 pr_debug("failed to mmap event: %d (%s)\n", errno, 89 str_error_r(errno, sbuf, sizeof(sbuf))); 90 goto out_delete_evlist; 91 } 92 93 evlist__enable(evlist); 94 95 /* collect samples */ 96 for (i = 0; i < NR_LOOPS; i++) 97 tmp++; 98 99 evlist__disable(evlist); 100 101 md = &evlist->mmap[0]; 102 if (perf_mmap__read_init(&md->core) < 0) 103 goto out_init; 104 105 while ((event = perf_mmap__read_event(&md->core)) != NULL) { 106 struct perf_sample sample; 107 108 perf_sample__init(&sample, /*all=*/false); 109 if (event->header.type != PERF_RECORD_SAMPLE) 110 goto next_event; 111 112 err = evlist__parse_sample(evlist, event, &sample); 113 if (err < 0) { 114 pr_debug("Error during parse sample\n"); 115 perf_sample__exit(&sample); 116 goto out_delete_evlist; 117 } 118 119 total_periods += sample.period; 120 nr_samples++; 121 next_event: 122 perf_mmap__consume(&md->core); 123 perf_sample__exit(&sample); 124 } 125 perf_mmap__read_done(&md->core); 126 127 out_init: 128 if ((u64) nr_samples == total_periods) { 129 pr_debug("All (%d) samples have period value of 1!\n", 130 nr_samples); 131 err = -1; 132 } 133 134 out_delete_evlist: 135 perf_cpu_map__put(cpus); 136 perf_thread_map__put(threads); 137 evlist__delete(evlist); 138 return err; 139 } 140 141 static int test__sw_clock_freq(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 142 { 143 int ret; 144 145 ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK); 146 if (!ret) 147 ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK); 148 149 return ret; 150 } 151 152 DEFINE_SUITE("Software clock events period values", sw_clock_freq); 153