offwaketime_user.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (c) 2016 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11. #include <linux/bpf.h>
  12. #include <string.h>
  13. #include <linux/perf_event.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include <stdbool.h>
  17. #include <sys/resource.h>
  18. #include "libbpf.h"
  19. #include "bpf_load.h"
  20. #include "trace_helpers.h"
  21. #define PRINT_RAW_ADDR 0
  22. static void print_ksym(__u64 addr)
  23. {
  24. struct ksym *sym;
  25. if (!addr)
  26. return;
  27. sym = ksym_search(addr);
  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[3], &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[3], &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. char filename[256];
  86. int delay = 1;
  87. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  88. setrlimit(RLIMIT_MEMLOCK, &r);
  89. signal(SIGINT, int_exit);
  90. signal(SIGTERM, int_exit);
  91. if (load_kallsyms()) {
  92. printf("failed to process /proc/kallsyms\n");
  93. return 2;
  94. }
  95. if (load_bpf_file(filename)) {
  96. printf("%s", bpf_log_buf);
  97. return 1;
  98. }
  99. if (argc > 1)
  100. delay = atoi(argv[1]);
  101. sleep(delay);
  102. print_stacks(map_fd[0]);
  103. return 0;
  104. }