1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/perf_event.h>
11 #include <sys/ioctl.h>
12 #include <sys/resource.h>
14 #include <sys/types.h>
22 #define SAMPLE_PERIOD 0x7fffffffffffffffULL
24 static void check_on_cpu(int cpu, struct perf_event_attr *attr)
26 int pmu_fd, error = 0;
30 /* Move to target CPU */
33 assert(sched_setaffinity(0, sizeof(set), &set) == 0);
34 /* Open perf event and attach to the perf_event_array */
35 pmu_fd = sys_perf_event_open(attr, -1/*pid*/, cpu/*cpu*/, -1/*group_fd*/, 0);
37 fprintf(stderr, "sys_perf_event_open failed on CPU %d\n", cpu);
41 assert(bpf_map_update_elem(map_fd[0], &cpu, &pmu_fd, BPF_ANY) == 0);
42 assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0) == 0);
43 /* Trigger the kprobe */
44 bpf_map_get_next_key(map_fd[1], &cpu, NULL);
46 if (bpf_map_lookup_elem(map_fd[1], &cpu, &value)) {
47 fprintf(stderr, "Value missing for CPU %d\n", cpu);
51 fprintf(stderr, "CPU %d: %llu\n", cpu, value);
54 assert(bpf_map_delete_elem(map_fd[0], &cpu) == 0 || error);
55 assert(ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE, 0) == 0 || error);
56 assert(close(pmu_fd) == 0 || error);
57 assert(bpf_map_delete_elem(map_fd[1], &cpu) == 0 || error);
61 static void test_perf_event_array(struct perf_event_attr *attr,
64 int i, status, nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
68 printf("Test reading %s counters\n", name);
70 for (i = 0; i < nr_cpus; i++) {
74 check_on_cpu(i, attr);
79 for (i = 0; i < nr_cpus; i++) {
80 assert(waitpid(pid[i], &status, 0) == pid[i]);
85 printf("Test: %s FAILED\n", name);
88 static void test_bpf_perf_event(void)
90 struct perf_event_attr attr_cycles = {
92 .sample_period = SAMPLE_PERIOD,
94 .type = PERF_TYPE_HARDWARE,
97 .config = PERF_COUNT_HW_CPU_CYCLES,
99 struct perf_event_attr attr_clock = {
101 .sample_period = SAMPLE_PERIOD,
103 .type = PERF_TYPE_SOFTWARE,
106 .config = PERF_COUNT_SW_CPU_CLOCK,
108 struct perf_event_attr attr_raw = {
110 .sample_period = SAMPLE_PERIOD,
112 .type = PERF_TYPE_RAW,
115 /* Intel Instruction Retired */
118 struct perf_event_attr attr_l1d_load = {
120 .sample_period = SAMPLE_PERIOD,
122 .type = PERF_TYPE_HW_CACHE,
126 PERF_COUNT_HW_CACHE_L1D |
127 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
128 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
130 struct perf_event_attr attr_llc_miss = {
132 .sample_period = SAMPLE_PERIOD,
134 .type = PERF_TYPE_HW_CACHE,
138 PERF_COUNT_HW_CACHE_LL |
139 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
140 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
142 struct perf_event_attr attr_msr_tsc = {
146 /* From /sys/bus/event_source/devices/msr/ */
153 test_perf_event_array(&attr_cycles, "HARDWARE-cycles");
154 test_perf_event_array(&attr_clock, "SOFTWARE-clock");
155 test_perf_event_array(&attr_raw, "RAW-instruction-retired");
156 test_perf_event_array(&attr_l1d_load, "HW_CACHE-L1D-load");
158 /* below tests may fail in qemu */
159 test_perf_event_array(&attr_llc_miss, "HW_CACHE-LLC-miss");
160 test_perf_event_array(&attr_msr_tsc, "Dynamic-msr-tsc");
163 int main(int argc, char **argv)
165 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
168 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
170 setrlimit(RLIMIT_MEMLOCK, &r);
171 if (load_bpf_file(filename)) {
172 printf("%s", bpf_log_buf);
176 test_bpf_perf_event();