offwaketime_user.c 3.3 KB

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