add uprobe support in funcinterval (#3512)
authorzhaoyao73 <zhaoyao73@users.noreply.github.com>
Wed, 30 Jun 2021 17:15:26 +0000 (13:15 -0400)
committerGitHub <noreply@github.com>
Wed, 30 Jun 2021 17:15:26 +0000 (10:15 -0700)
add uprobe support in funcinterval

Signed-off-by: Yao Zhao <yao.zhao1@huawei.com>
man/man8/funcinterval.8
tools/funcinterval.py
tools/funcinterval_example.txt

index 89a4a7b4f3c1838490d189f1f5d87ed51d3d28c8..8a60399871e7faf856fe5c90e985fc4885f4e223 100755 (executable)
@@ -73,6 +73,14 @@ Time process 181 only:
 Time the interval of mm_vmscan_direct_reclaim_begin tracepoint:
 #
 .B funcinterval t:vmscan:mm_vmscan_direct_reclaim_begin
+.TP
+Time the interval of c:malloc used by top every 3 seconds:
+#
+.B funcinterval -p `pidof -s top` -i 3 c:malloc
+.TP
+Time /usr/local/bin/python main function:
+#
+.B funcinterval /usr/local/bin/python:main
 .SH FIELDS
 .TP
 necs
index baac73c9a57eb7d0c2f1662c3957aac00fa850d2..097649f44a6ec1745e034d9937d3e49726dccb68 100755 (executable)
@@ -33,6 +33,10 @@ examples = """examples:
     ./funcinterval -p 181 vfs_read
     # time the interval of mm_vmscan_direct_reclaim_begin tracepoint
     ./funcinterval t:vmscan:mm_vmscan_direct_reclaim_begin
+    # time the interval of c:malloc used by top every 3 seconds
+    ./funcinterval -p `pidof -s top` -i 3 c:malloc
+    # time /usr/local/bin/python main function
+    ./funcinterval /usr/local/bin/python:main
 """
 parser = argparse.ArgumentParser(
     description="Time interval and print latency as a histogram",
@@ -69,8 +73,14 @@ def bail(error):
 
 parts = args.pattern.split(':')
 if len(parts) == 1:
-    attach_type = "function"
+    attach_type = "kprobe function"
     pattern = args.pattern
+elif len(parts) == 2:
+    attach_type = "uprobe function"
+    elf = BPF.find_library(parts[0]) or BPF.find_exe(parts[0])
+    if not elf:
+        bail("Can't find elf binary %s" % elf)
+    pattern = parts[1]
 elif len(parts) == 3:
     attach_type = "tracepoint"
     pattern = ':'.join(parts[1:])
@@ -140,6 +150,11 @@ b = BPF(text=bpf_text)
 if len(parts) == 1:
     b.attach_kprobe(event=pattern, fn_name="trace_func_entry")
     matched = b.num_open_kprobes()
+elif len(parts) == 2:
+    # sym_re is regular expression for symbols
+    b.attach_uprobe(name = elf, sym_re = pattern, fn_name = "trace_func_entry",
+                    pid = args.pid or -1)
+    matched = b.num_open_uprobes()
 elif len(parts) == 3:
     b.attach_tracepoint(tp=pattern, fn_name="trace_func_entry")
     matched = b.num_open_tracepoints()
index 8f3f8cbed8e40574ffd8db3e67a38bb0e21f9986..b3fea3e9e4b8c8192a30bc659a5d05e9cf9caf56 100755 (executable)
@@ -124,6 +124,102 @@ throughput testing, by those results you know which layer has a slower interval
 time. In our case, mmc-cmdqd is slower than block layer.
 
 
+# ./funcinterval -p `pidof -s top` c:malloc -i 3
+Tracing uprobe function for "malloc"... Hit Ctrl-C to end.
+
+     nsecs               : count     distribution
+         0 -> 1          : 0        |                                        |
+         2 -> 3          : 0        |                                        |
+         4 -> 7          : 0        |                                        |
+         8 -> 15         : 0        |                                        |
+        16 -> 31         : 0        |                                        |
+        32 -> 63         : 0        |                                        |
+        64 -> 127        : 0        |                                        |
+       128 -> 255        : 0        |                                        |
+       256 -> 511        : 0        |                                        |
+       512 -> 1023       : 0        |                                        |
+      1024 -> 2047       : 0        |                                        |
+      2048 -> 4095       : 0        |                                        |
+      4096 -> 8191       : 7        |*************************               |
+      8192 -> 16383      : 11       |****************************************|
+     16384 -> 32767      : 4        |**************                          |
+     32768 -> 65535      : 1        |***                                     |
+     65536 -> 131071     : 1        |***                                     |
+    131072 -> 262143     : 1        |***                                     |
+    262144 -> 524287     : 0        |                                        |
+    524288 -> 1048575    : 0        |                                        |
+   1048576 -> 2097151    : 0        |                                        |
+   2097152 -> 4194303    : 0        |                                        |
+   4194304 -> 8388607    : 1        |***                                     |
+
+
+     nsecs               : count     distribution
+         0 -> 1          : 0        |                                        |
+         2 -> 3          : 0        |                                        |
+         4 -> 7          : 0        |                                        |
+         8 -> 15         : 0        |                                        |
+        16 -> 31         : 0        |                                        |
+        32 -> 63         : 0        |                                        |
+        64 -> 127        : 0        |                                        |
+       128 -> 255        : 0        |                                        |
+       256 -> 511        : 0        |                                        |
+       512 -> 1023       : 0        |                                        |
+      1024 -> 2047       : 0        |                                        |
+      2048 -> 4095       : 0        |                                        |
+      4096 -> 8191       : 8        |********************************        |
+      8192 -> 16383      : 10       |****************************************|
+     16384 -> 32767      : 4        |****************                        |
+     32768 -> 65535      : 1        |****                                    |
+     65536 -> 131071     : 1        |****                                    |
+    131072 -> 262143     : 1        |****                                    |
+    262144 -> 524287     : 0        |                                        |
+    524288 -> 1048575    : 0        |                                        |
+   1048576 -> 2097151    : 0        |                                        |
+   2097152 -> 4194303    : 0        |                                        |
+   4194304 -> 8388607    : 1        |****                                    |
+
+Time the interval of libc's malloc for top utility every 3 seconds.
+
+# ./funcinterval /usr/local/bin/python:main
+Tracing uprobe function for "main"... Hit Ctrl-C to end.
+^C
+     nsecs               : count     distribution
+         0 -> 1          : 0        |                                        |
+         2 -> 3          : 0        |                                        |
+         4 -> 7          : 0        |                                        |
+         8 -> 15         : 0        |                                        |
+        16 -> 31         : 0        |                                        |
+        32 -> 63         : 0        |                                        |
+        64 -> 127        : 0        |                                        |
+       128 -> 255        : 0        |                                        |
+       256 -> 511        : 0        |                                        |
+       512 -> 1023       : 0        |                                        |
+      1024 -> 2047       : 0        |                                        |
+      2048 -> 4095       : 0        |                                        |
+      4096 -> 8191       : 0        |                                        |
+      8192 -> 16383      : 0        |                                        |
+     16384 -> 32767      : 0        |                                        |
+     32768 -> 65535      : 0        |                                        |
+     65536 -> 131071     : 0        |                                        |
+    131072 -> 262143     : 0        |                                        |
+    262144 -> 524287     : 0        |                                        |
+    524288 -> 1048575    : 0        |                                        |
+   1048576 -> 2097151    : 0        |                                        |
+   2097152 -> 4194303    : 0        |                                        |
+   4194304 -> 8388607    : 0        |                                        |
+   8388608 -> 16777215   : 0        |                                        |
+  16777216 -> 33554431   : 0        |                                        |
+  33554432 -> 67108863   : 0        |                                        |
+  67108864 -> 134217727  : 0        |                                        |
+ 134217728 -> 268435455  : 0        |                                        |
+ 268435456 -> 536870911  : 1        |****************************************|
+ 536870912 -> 1073741823 : 1        |****************************************|
+1073741824 -> 2147483647 : 1        |****************************************|
+2147483648 -> 4294967295 : 1        |****************************************|
+Detaching...
+
+Time the interal of python's main function.
+
 USAGE message:
 
 # ./funcinterval -h
@@ -161,3 +257,7 @@ examples:
     ./funcinterval -p 181 vfs_read
     # time the interval of mm_vmscan_direct_reclaim_begin tracepoint
     ./funcinterval t:vmscan:mm_vmscan_direct_reclaim_begin
+    # time the interval of c:malloc used by top every 3 seconds
+    ./funcinterval -p `pidof -s top` -i 3 c:malloc
+    # time /usr/local/bin/python main function
+    ./funcinterval /usr/local/bin/python:main