sync_timing.php 962 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. $prog = <<<EOT
  3. #include <uapi/linux/ptrace.h>
  4. BPF_HASH(last);
  5. int do_trace(struct pt_regs *ctx) {
  6. u64 ts, *tsp, delta, key = 0;
  7. // attempt to read stored timestamp
  8. tsp = last.lookup(&key);
  9. if (tsp != NULL) {
  10. delta = bpf_ktime_get_ns() - *tsp;
  11. if (delta < 1000000000) {
  12. // output if time is less than 1 second
  13. bpf_trace_printk("%d\\n", delta / 1000000);
  14. }
  15. last.delete(&key);
  16. }
  17. // update stored timestamp
  18. ts = bpf_ktime_get_ns();
  19. last.update(&key, &ts);
  20. return 0;
  21. }
  22. EOT;
  23. $b = new Bpf(["text" => $prog]);
  24. $b->attach_kprobe($b->get_syscall_fnname("sync"), "do_trace");
  25. echo "Tracing for quick sync's... Ctrl-C to end\n";
  26. $start = 0;
  27. while (true) {
  28. list($task, $pid, $cpu, $flags, $ts, $ms) = $b->trace_fields();
  29. if ($start === 0) $start = $ts;
  30. $ts -= $start;
  31. printf("At time %.2f s: multiple syncs detected, last %s ms ago\n", $ts, $ms);
  32. }