From a037f2d9d92ea4a5e659297c5c9839557040f1ce Mon Sep 17 00:00:00 2001
From: zhengjiebing <zhengjiebing_yewu@cmss.chinamobile.com>
Date: Fri, 21 Jun 2024 16:48:20 +0800
Subject: [PATCH] perftool: add latency tool

---
 tools/perf/latency.bt | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100755 tools/perf/latency.bt

diff --git a/tools/perf/latency.bt b/tools/perf/latency.bt
new file mode 100755
index 0000000..bfe18ae
--- /dev/null
+++ b/tools/perf/latency.bt
@@ -0,0 +1,38 @@
+#!/usr/bin/env bpftrace
+/* 
+reference: https://github.com/bpftrace/bpftrace/blob/master/man/adoc/bpftrace.adoc
+prepare: yum install bpftrace
+example: ./latency.bt xxx.so func1 func2
+supported functions: bpftrace -l 'uprobe:xxx.so'
+*/
+
+BEGIN
+{
+}
+
+uprobe:$1:$2
+{
+    @t_start[tid] = nsecs;
+}
+
+uretprobe:$1:$3
+/@t_start[tid]/
+{
+    $t_dur = nsecs - @t_start[tid];
+    @t_avg = avg($t_dur);
+
+    //@t_hist = lhist($t_dur, 0, 4000, 100);
+    @t_hist = hist($t_dur);
+
+    delete(@t_start[tid]);
+}
+
+END
+{
+    printf("\n\n%s -> %s \n", str($2), str($3));
+    printf("average time(nsec):");
+    print(@t_avg);
+
+    clear(@t_avg);
+    clear(@t_start);
+}
-- 
2.33.0

