task_storage.php 996 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. $prog = <<<EOT
  3. BPF_TASK_STORAGE(task_storage_map, __u64);
  4. KFUNC_PROBE(inet_listen)
  5. {
  6. __u64 ts = bpf_ktime_get_ns();
  7. /* save timestamp to local storage on function entry */
  8. task_storage_map.task_storage_get(bpf_get_current_task_btf(), &ts, BPF_LOCAL_STORAGE_GET_F_CREATE);
  9. bpf_trace_printk("inet_listen entry: store timestamp %lld", ts);
  10. return 0;
  11. }
  12. KRETFUNC_PROBE(inet_listen)
  13. {
  14. __u64 *ts;
  15. /* retrieve timestamp stored at local storage on function exit */
  16. ts = task_storage_map.task_storage_get(bpf_get_current_task_btf(), 0, 0);
  17. if (!ts)
  18. return 0;
  19. /* delete timestamp from local storage */
  20. task_storage_map.task_storage_delete(bpf_get_current_task_btf());
  21. /* calculate latency */
  22. bpf_trace_printk("inet_listen exit: cost %lldus", (bpf_ktime_get_ns() - *ts) / 1000);
  23. return 0;
  24. }
  25. EOT;
  26. # load BPF program
  27. $ebpf = new Bpf(["text" => $prog]);
  28. # format output
  29. while (true) {
  30. try {
  31. $ebpf->trace_print();
  32. } catch (Exception $e) {
  33. continue;
  34. }
  35. }