test.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. $bpf_text = <<<EOT
  3. #include <uapi/linux/ptrace.h>
  4. #include <linux/blk-mq.h>
  5. BPF_HISTOGRAM(dist);
  6. BPF_HISTOGRAM(dist_linear);
  7. int trace_req_done(struct pt_regs *ctx, struct request *req)
  8. {
  9. dist.increment(bpf_log2l(req->__data_len / 1024));
  10. dist_linear.increment(req->__data_len / 1024);
  11. return 0;
  12. }
  13. EOT;
  14. $b = new Bpf(["text" => $bpf_text]);
  15. // Attach kprobe based on kernel version
  16. if ($b->get_kprobe_functions('__blk_account_io_done')) {
  17. // __blk_account_io_done is available before kernel v6.4
  18. $b->attach_kprobe('__blk_account_io_done', 'trace_req_done');
  19. } elseif ($b->get_kprobe_functions('blk_account_io_done')) {
  20. // blk_account_io_done is traceable (not inline) before v5.16
  21. $b->attach_kprobe('blk_account_io_done', 'trace_req_done');
  22. } else {
  23. $b->attach_kprobe('blk_mq_end_request', 'trace_req_done');
  24. }
  25. echo "Tracing... Hit Ctrl-C to end.\n";
  26. // Set up signal handler for Ctrl-C
  27. pcntl_signal(SIGINT, "signalHandler");
  28. pcntl_async_signals(true);
  29. // Keep running until interrupted
  30. while (true) {
  31. sleep(99999999);
  32. }
  33. function signalHandler($signo)
  34. {
  35. global $b;
  36. switch ($signo) {
  37. case SIGINT:
  38. echo "\n";
  39. echo "log2 histogram\n";
  40. echo "~~~~~~~~~~~~~~\n";
  41. $b->dist->print_log2_hist("kbytes");
  42. echo "\nlinear histogram\n";
  43. echo "~~~~~~~~~~~~~~~~\n";
  44. $b->dist_linear->print_linear_hist("kbytes");
  45. exit(0);
  46. }
  47. }