bitehist.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. #
  3. # bitehist.php Block I/O size histogram.
  4. # For Linux, uses BCC, eBPF. Embedded C.
  5. #
  6. # Written as a basic example of using histograms to show a distribution.
  7. #
  8. # A Ctrl-C will print the gathered histogram then exit.
  9. #
  10. # Copyright (c) 2015 Brendan Gregg.
  11. # Licensed under the Apache License, Version 2.0 (the "License")
  12. #
  13. # 15-Aug-2015 Brendan Gregg Created this.
  14. # 03-Feb-2019 Xiaozhou Liu added linear histogram.
  15. # 02-Mar-2025 Wei Use blk_mq_end_request for newer kernel.
  16. $bpf_text = <<<EOT
  17. #include <uapi/linux/ptrace.h>
  18. #include <linux/blk-mq.h>
  19. BPF_HISTOGRAM(dist);
  20. BPF_HISTOGRAM(dist_linear);
  21. int trace_req_done(struct pt_regs *ctx, struct request *req)
  22. {
  23. dist.increment(bpf_log2l(req->__data_len / 1024));
  24. dist_linear.increment(req->__data_len / 1024);
  25. return 0;
  26. }
  27. EOT;
  28. # load BPF program
  29. $b = new Bpf(["text" => $bpf_text]);
  30. if ($b->get_kprobe_functions("__blk_account_io_done")) {
  31. # __blk_account_io_done is available before kernel v6.4.
  32. $b->attach_kprobe("__blk_account_io_done", "trace_req_done");
  33. } elseif ($b->get_kprobe_functions("blk_account_io_done")) {
  34. # blk_account_io_done is traceable (not inline) before v5.16.
  35. $b->attach_kprobe("blk_account_io_done", "trace_req_done");
  36. } else {
  37. $b->attach_kprobe("blk_mq_end_request", "trace_req_done");
  38. }
  39. # header
  40. echo "Tracing... Hit Ctrl-C to end.\n";
  41. # Set up signal handler for Ctrl-C
  42. pcntl_signal(SIGINT, "signalHandler");
  43. pcntl_async_signals(true);
  44. # sleep until Ctrl-C
  45. while (true) {
  46. sleep(99999999);
  47. }
  48. function signalHandler($signo) {
  49. global $b;
  50. switch ($signo) {
  51. case SIGINT:
  52. echo "\nlog2 histogram\n";
  53. echo "~~~~~~~~~~~~~~\n";
  54. $b->dist->print_log2_hist("kbytes");
  55. echo "\nlinear histogram\n";
  56. echo "~~~~~~~~~~~~~~~~\n";
  57. $b->dist_linear->print_linear_hist("kbytes");
  58. exit(0);
  59. }
  60. }