tracex6_user.c 5.6 KB

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