Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
41e9a804 TQ |
2 | #define _GNU_SOURCE |
3 | ||
4 | #include <assert.h> | |
47efb302 | 5 | #include <fcntl.h> |
47efb302 | 6 | #include <linux/perf_event.h> |
41e9a804 TQ |
7 | #include <sched.h> |
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <sys/ioctl.h> | |
41e9a804 TQ |
11 | #include <sys/time.h> |
12 | #include <sys/types.h> | |
13 | #include <sys/wait.h> | |
14 | #include <unistd.h> | |
15 | ||
2bf3e2ef | 16 | #include <bpf/bpf.h> |
63841bc0 | 17 | #include <bpf/libbpf.h> |
205c8ada | 18 | #include "perf-sys.h" |
47efb302 KX |
19 | |
20 | #define SAMPLE_PERIOD 0x7fffffffffffffffULL | |
21 | ||
63841bc0 DL |
22 | /* counters, values, values2 */ |
23 | static int map_fd[3]; | |
24 | ||
41e9a804 | 25 | static void check_on_cpu(int cpu, struct perf_event_attr *attr) |
47efb302 | 26 | { |
020a32d9 | 27 | struct bpf_perf_event_value value2; |
41e9a804 TQ |
28 | int pmu_fd, error = 0; |
29 | cpu_set_t set; | |
30 | __u64 value; | |
47efb302 | 31 | |
41e9a804 TQ |
32 | /* Move to target CPU */ |
33 | CPU_ZERO(&set); | |
34 | CPU_SET(cpu, &set); | |
35 | assert(sched_setaffinity(0, sizeof(set), &set) == 0); | |
36 | /* Open perf event and attach to the perf_event_array */ | |
37 | pmu_fd = sys_perf_event_open(attr, -1/*pid*/, cpu/*cpu*/, -1/*group_fd*/, 0); | |
38 | if (pmu_fd < 0) { | |
39 | fprintf(stderr, "sys_perf_event_open failed on CPU %d\n", cpu); | |
40 | error = 1; | |
41 | goto on_exit; | |
42 | } | |
43 | assert(bpf_map_update_elem(map_fd[0], &cpu, &pmu_fd, BPF_ANY) == 0); | |
44 | assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0) == 0); | |
45 | /* Trigger the kprobe */ | |
46 | bpf_map_get_next_key(map_fd[1], &cpu, NULL); | |
47 | /* Check the value */ | |
48 | if (bpf_map_lookup_elem(map_fd[1], &cpu, &value)) { | |
49 | fprintf(stderr, "Value missing for CPU %d\n", cpu); | |
50 | error = 1; | |
51 | goto on_exit; | |
020a32d9 YS |
52 | } else { |
53 | fprintf(stderr, "CPU %d: %llu\n", cpu, value); | |
54 | } | |
55 | /* The above bpf_map_lookup_elem should trigger the second kprobe */ | |
56 | if (bpf_map_lookup_elem(map_fd[2], &cpu, &value2)) { | |
57 | fprintf(stderr, "Value2 missing for CPU %d\n", cpu); | |
58 | error = 1; | |
59 | goto on_exit; | |
60 | } else { | |
61 | fprintf(stderr, "CPU %d: counter: %llu, enabled: %llu, running: %llu\n", cpu, | |
62 | value2.counter, value2.enabled, value2.running); | |
41e9a804 | 63 | } |
41e9a804 TQ |
64 | |
65 | on_exit: | |
66 | assert(bpf_map_delete_elem(map_fd[0], &cpu) == 0 || error); | |
67 | assert(ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE, 0) == 0 || error); | |
68 | assert(close(pmu_fd) == 0 || error); | |
69 | assert(bpf_map_delete_elem(map_fd[1], &cpu) == 0 || error); | |
70 | exit(error); | |
71 | } | |
72 | ||
73 | static void test_perf_event_array(struct perf_event_attr *attr, | |
74 | const char *name) | |
75 | { | |
76 | int i, status, nr_cpus = sysconf(_SC_NPROCESSORS_CONF); | |
77 | pid_t pid[nr_cpus]; | |
78 | int err = 0; | |
79 | ||
80 | printf("Test reading %s counters\n", name); | |
81 | ||
82 | for (i = 0; i < nr_cpus; i++) { | |
83 | pid[i] = fork(); | |
84 | assert(pid[i] >= 0); | |
85 | if (pid[i] == 0) { | |
86 | check_on_cpu(i, attr); | |
87 | exit(1); | |
88 | } | |
89 | } | |
90 | ||
91 | for (i = 0; i < nr_cpus; i++) { | |
92 | assert(waitpid(pid[i], &status, 0) == pid[i]); | |
93 | err |= status; | |
94 | } | |
95 | ||
96 | if (err) | |
97 | printf("Test: %s FAILED\n", name); | |
98 | } | |
99 | ||
100 | static void test_bpf_perf_event(void) | |
101 | { | |
102 | struct perf_event_attr attr_cycles = { | |
47efb302 KX |
103 | .freq = 0, |
104 | .sample_period = SAMPLE_PERIOD, | |
105 | .inherit = 0, | |
106 | .type = PERF_TYPE_HARDWARE, | |
107 | .read_format = 0, | |
108 | .sample_type = 0, | |
41e9a804 TQ |
109 | .config = PERF_COUNT_HW_CPU_CYCLES, |
110 | }; | |
111 | struct perf_event_attr attr_clock = { | |
112 | .freq = 0, | |
113 | .sample_period = SAMPLE_PERIOD, | |
114 | .inherit = 0, | |
115 | .type = PERF_TYPE_SOFTWARE, | |
116 | .read_format = 0, | |
117 | .sample_type = 0, | |
118 | .config = PERF_COUNT_SW_CPU_CLOCK, | |
119 | }; | |
120 | struct perf_event_attr attr_raw = { | |
121 | .freq = 0, | |
122 | .sample_period = SAMPLE_PERIOD, | |
123 | .inherit = 0, | |
124 | .type = PERF_TYPE_RAW, | |
125 | .read_format = 0, | |
126 | .sample_type = 0, | |
127 | /* Intel Instruction Retired */ | |
128 | .config = 0xc0, | |
129 | }; | |
130 | struct perf_event_attr attr_l1d_load = { | |
131 | .freq = 0, | |
132 | .sample_period = SAMPLE_PERIOD, | |
133 | .inherit = 0, | |
134 | .type = PERF_TYPE_HW_CACHE, | |
135 | .read_format = 0, | |
136 | .sample_type = 0, | |
137 | .config = | |
138 | PERF_COUNT_HW_CACHE_L1D | | |
139 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
140 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16), | |
141 | }; | |
142 | struct perf_event_attr attr_llc_miss = { | |
143 | .freq = 0, | |
144 | .sample_period = SAMPLE_PERIOD, | |
145 | .inherit = 0, | |
146 | .type = PERF_TYPE_HW_CACHE, | |
147 | .read_format = 0, | |
148 | .sample_type = 0, | |
149 | .config = | |
150 | PERF_COUNT_HW_CACHE_LL | | |
151 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
152 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), | |
153 | }; | |
154 | struct perf_event_attr attr_msr_tsc = { | |
155 | .freq = 0, | |
156 | .sample_period = 0, | |
157 | .inherit = 0, | |
158 | /* From /sys/bus/event_source/devices/msr/ */ | |
159 | .type = 7, | |
160 | .read_format = 0, | |
161 | .sample_type = 0, | |
162 | .config = 0, | |
47efb302 KX |
163 | }; |
164 | ||
41e9a804 TQ |
165 | test_perf_event_array(&attr_cycles, "HARDWARE-cycles"); |
166 | test_perf_event_array(&attr_clock, "SOFTWARE-clock"); | |
167 | test_perf_event_array(&attr_raw, "RAW-instruction-retired"); | |
168 | test_perf_event_array(&attr_l1d_load, "HW_CACHE-L1D-load"); | |
47efb302 | 169 | |
41e9a804 TQ |
170 | /* below tests may fail in qemu */ |
171 | test_perf_event_array(&attr_llc_miss, "HW_CACHE-LLC-miss"); | |
172 | test_perf_event_array(&attr_msr_tsc, "Dynamic-msr-tsc"); | |
47efb302 KX |
173 | } |
174 | ||
175 | int main(int argc, char **argv) | |
176 | { | |
63841bc0 DL |
177 | struct bpf_link *links[2]; |
178 | struct bpf_program *prog; | |
179 | struct bpf_object *obj; | |
47efb302 | 180 | char filename[256]; |
63841bc0 DL |
181 | int i = 0; |
182 | ||
47efb302 | 183 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
63841bc0 DL |
184 | obj = bpf_object__open_file(filename, NULL); |
185 | if (libbpf_get_error(obj)) { | |
186 | fprintf(stderr, "ERROR: opening BPF object file failed\n"); | |
187 | return 0; | |
188 | } | |
47efb302 | 189 | |
63841bc0 DL |
190 | /* load BPF program */ |
191 | if (bpf_object__load(obj)) { | |
192 | fprintf(stderr, "ERROR: loading BPF object file failed\n"); | |
193 | goto cleanup; | |
194 | } | |
195 | ||
196 | map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counters"); | |
197 | map_fd[1] = bpf_object__find_map_fd_by_name(obj, "values"); | |
198 | map_fd[2] = bpf_object__find_map_fd_by_name(obj, "values2"); | |
199 | if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) { | |
200 | fprintf(stderr, "ERROR: finding a map in obj file failed\n"); | |
201 | goto cleanup; | |
202 | } | |
203 | ||
204 | bpf_object__for_each_program(prog, obj) { | |
205 | links[i] = bpf_program__attach(prog); | |
206 | if (libbpf_get_error(links[i])) { | |
207 | fprintf(stderr, "ERROR: bpf_program__attach failed\n"); | |
208 | links[i] = NULL; | |
209 | goto cleanup; | |
210 | } | |
211 | i++; | |
47efb302 KX |
212 | } |
213 | ||
214 | test_bpf_perf_event(); | |
63841bc0 DL |
215 | |
216 | cleanup: | |
217 | for (i--; i >= 0; i--) | |
218 | bpf_link__destroy(links[i]); | |
219 | ||
220 | bpf_object__close(obj); | |
47efb302 KX |
221 | return 0; |
222 | } |