offwaketime.bpf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "vmlinux.h"
  8. #include <linux/version.h>
  9. #include <bpf/bpf_helpers.h>
  10. #include <bpf/bpf_tracing.h>
  11. #include <bpf/bpf_core_read.h>
  12. #ifndef PERF_MAX_STACK_DEPTH
  13. #define PERF_MAX_STACK_DEPTH 127
  14. #endif
  15. #define MINBLOCK_US 1
  16. #define MAX_ENTRIES 10000
  17. struct key_t {
  18. char waker[TASK_COMM_LEN];
  19. char target[TASK_COMM_LEN];
  20. u32 wret;
  21. u32 tret;
  22. };
  23. struct {
  24. __uint(type, BPF_MAP_TYPE_HASH);
  25. __type(key, struct key_t);
  26. __type(value, u64);
  27. __uint(max_entries, MAX_ENTRIES);
  28. } counts SEC(".maps");
  29. struct {
  30. __uint(type, BPF_MAP_TYPE_HASH);
  31. __type(key, u32);
  32. __type(value, u64);
  33. __uint(max_entries, MAX_ENTRIES);
  34. } start SEC(".maps");
  35. struct wokeby_t {
  36. char name[TASK_COMM_LEN];
  37. u32 ret;
  38. };
  39. struct {
  40. __uint(type, BPF_MAP_TYPE_HASH);
  41. __type(key, u32);
  42. __type(value, struct wokeby_t);
  43. __uint(max_entries, MAX_ENTRIES);
  44. } wokeby SEC(".maps");
  45. struct {
  46. __uint(type, BPF_MAP_TYPE_STACK_TRACE);
  47. __uint(key_size, sizeof(u32));
  48. __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
  49. __uint(max_entries, MAX_ENTRIES);
  50. } stackmap SEC(".maps");
  51. #define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
  52. SEC("kprobe/try_to_wake_up")
  53. int waker(struct pt_regs *ctx)
  54. {
  55. struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
  56. u32 pid = BPF_CORE_READ(p, pid);
  57. struct wokeby_t woke;
  58. bpf_get_current_comm(&woke.name, sizeof(woke.name));
  59. woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
  60. bpf_map_update_elem(&wokeby, &pid, &woke, BPF_ANY);
  61. return 0;
  62. }
  63. static inline int update_counts(void *ctx, u32 pid, u64 delta)
  64. {
  65. struct wokeby_t *woke;
  66. u64 zero = 0, *val;
  67. struct key_t key;
  68. __builtin_memset(&key.waker, 0, sizeof(key.waker));
  69. bpf_get_current_comm(&key.target, sizeof(key.target));
  70. key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
  71. key.wret = 0;
  72. woke = bpf_map_lookup_elem(&wokeby, &pid);
  73. if (woke) {
  74. key.wret = woke->ret;
  75. __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker));
  76. bpf_map_delete_elem(&wokeby, &pid);
  77. }
  78. val = bpf_map_lookup_elem(&counts, &key);
  79. if (!val) {
  80. bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
  81. val = bpf_map_lookup_elem(&counts, &key);
  82. if (!val)
  83. return 0;
  84. }
  85. (*val) += delta;
  86. return 0;
  87. }
  88. #if 1
  89. /* taken from /sys/kernel/tracing/events/sched/sched_switch/format */
  90. SEC("tracepoint/sched/sched_switch")
  91. int oncpu(struct trace_event_raw_sched_switch *ctx)
  92. {
  93. /* record previous thread sleep time */
  94. u32 pid = ctx->prev_pid;
  95. #else
  96. SEC("kprobe.multi/finish_task_switch*")
  97. int oncpu(struct pt_regs *ctx)
  98. {
  99. struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
  100. /* record previous thread sleep time */
  101. u32 pid = BPF_CORE_READ(p, pid);
  102. #endif
  103. u64 delta, ts, *tsp;
  104. ts = bpf_ktime_get_ns();
  105. bpf_map_update_elem(&start, &pid, &ts, BPF_ANY);
  106. /* calculate current thread's delta time */
  107. pid = bpf_get_current_pid_tgid();
  108. tsp = bpf_map_lookup_elem(&start, &pid);
  109. if (!tsp)
  110. /* missed start or filtered */
  111. return 0;
  112. delta = bpf_ktime_get_ns() - *tsp;
  113. bpf_map_delete_elem(&start, &pid);
  114. delta = delta / 1000;
  115. if (delta < MINBLOCK_US)
  116. return 0;
  117. return update_counts(ctx, pid, delta);
  118. }
  119. char _license[] SEC("license") = "GPL";
  120. u32 _version SEC("version") = LINUX_VERSION_CODE;