offwaketime_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <signal.h>
  8. #include <linux/perf_event.h>
  9. #include <errno.h>
  10. #include <stdbool.h>
  11. #include <bpf/libbpf.h>
  12. #include <bpf/bpf.h>
  13. #include "trace_helpers.h"
  14. #define PRINT_RAW_ADDR 0
  15. /* counts, stackmap */
  16. static int map_fd[2];
  17. static void print_ksym(__u64 addr)
  18. {
  19. struct ksym *sym;
  20. if (!addr)
  21. return;
  22. sym = ksym_search(addr);
  23. if (!sym) {
  24. printf("ksym not found. Is kallsyms loaded?\n");
  25. return;
  26. }
  27. if (PRINT_RAW_ADDR)
  28. printf("%s/%llx;", sym->name, addr);
  29. else
  30. printf("%s;", sym->name);
  31. }
  32. #define TASK_COMM_LEN 16
  33. struct key_t {
  34. char waker[TASK_COMM_LEN];
  35. char target[TASK_COMM_LEN];
  36. __u32 wret;
  37. __u32 tret;
  38. };
  39. static void print_stack(struct key_t *key, __u64 count)
  40. {
  41. __u64 ip[PERF_MAX_STACK_DEPTH] = {};
  42. static bool warned;
  43. int i;
  44. printf("%s;", key->target);
  45. if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) {
  46. printf("---;");
  47. } else {
  48. for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
  49. print_ksym(ip[i]);
  50. }
  51. printf("-;");
  52. if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) {
  53. printf("---;");
  54. } else {
  55. for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
  56. print_ksym(ip[i]);
  57. }
  58. printf(";%s %lld\n", key->waker, count);
  59. if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) {
  60. printf("stackmap collisions seen. Consider increasing size\n");
  61. warned = true;
  62. } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) {
  63. printf("err stackid %d %d\n", key->tret, key->wret);
  64. }
  65. }
  66. static void print_stacks(int fd)
  67. {
  68. struct key_t key = {}, next_key;
  69. __u64 value;
  70. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  71. bpf_map_lookup_elem(fd, &next_key, &value);
  72. print_stack(&next_key, value);
  73. key = next_key;
  74. }
  75. }
  76. static void int_exit(int sig)
  77. {
  78. print_stacks(map_fd[0]);
  79. exit(0);
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. struct bpf_object *obj = NULL;
  84. struct bpf_link *links[2];
  85. struct bpf_program *prog;
  86. int delay = 1, i = 0;
  87. char filename[256];
  88. if (load_kallsyms()) {
  89. printf("failed to process /proc/kallsyms\n");
  90. return 2;
  91. }
  92. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  93. obj = bpf_object__open_file(filename, NULL);
  94. if (libbpf_get_error(obj)) {
  95. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  96. obj = NULL;
  97. goto cleanup;
  98. }
  99. /* load BPF program */
  100. if (bpf_object__load(obj)) {
  101. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  102. goto cleanup;
  103. }
  104. map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
  105. map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
  106. if (map_fd[0] < 0 || map_fd[1] < 0) {
  107. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  108. goto cleanup;
  109. }
  110. signal(SIGINT, int_exit);
  111. signal(SIGTERM, int_exit);
  112. bpf_object__for_each_program(prog, obj) {
  113. links[i] = bpf_program__attach(prog);
  114. if (libbpf_get_error(links[i])) {
  115. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  116. links[i] = NULL;
  117. goto cleanup;
  118. }
  119. i++;
  120. }
  121. if (argc > 1)
  122. delay = atoi(argv[1]);
  123. sleep(delay);
  124. print_stacks(map_fd[0]);
  125. cleanup:
  126. for (i--; i >= 0; i--)
  127. bpf_link__destroy(links[i]);
  128. bpf_object__close(obj);
  129. return 0;
  130. }