tracex6_user.c 5.5 KB

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