gethostlatency: Filter on PID
authorPaul Chaignon <paul.chaignon@gmail.com>
Mon, 23 Jan 2017 21:43:10 +0000 (22:43 +0100)
committerPaul Chaignon <paul.chaignon@gmail.com>
Mon, 13 Feb 2017 21:34:56 +0000 (22:34 +0100)
man/man8/gethostlatency.8
tools/gethostlatency.py
tools/gethostlatency_example.txt

index e16afe7..c5a5330 100644 (file)
@@ -21,6 +21,10 @@ and may need modifications to match your software and processor architecture.
 Since this uses BPF, only the root user can use this tool.
 .SH REQUIREMENTS
 CONFIG_BPF and bcc.
+.SH OPTIONS
+.TP
+\-p PID
+Trace this process ID only.
 .SH EXAMPLES
 .TP
 Trace host lookups (getaddrinfo/gethostbyname[2]) system wide:
index 572eb51..f3f975e 100755 (executable)
 from __future__ import print_function
 from bcc import BPF
 from time import strftime
+import argparse
 import ctypes as ct
 
+examples = """examples:
+    ./gethostlatency           # trace all TCP accept()s
+    ./gethostlatency -p 181    # only trace PID 181
+"""
+parser = argparse.ArgumentParser(
+    description="Show latency for getaddrinfo/gethostbyname[2] calls",
+    formatter_class=argparse.RawDescriptionHelpFormatter,
+    epilog=examples)
+parser.add_argument("-p", "--pid", help="trace this PID only", type=int,
+    default=-1)
+args = parser.parse_args()
+
 # load BPF program
 bpf_text = """
 #include <uapi/linux/ptrace.h>
@@ -82,12 +95,17 @@ int do_return(struct pt_regs *ctx) {
 }
 """
 b = BPF(text=bpf_text)
-b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry")
-b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry")
-b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry")
-b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return")
-b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return")
-b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return")
+b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid)
+b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry",
+                pid=args.pid)
+b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry",
+                pid=args.pid)
+b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return",
+                   pid=args.pid)
+b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return",
+                   pid=args.pid)
+b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return",
+                   pid=args.pid)
 
 TASK_COMM_LEN = 16    # linux/sched.h
 
index 429574f..debb2df 100644 (file)
@@ -19,3 +19,19 @@ TIME      PID    COMM          LATms HOST
 In this example, the first call to lookup "www.iovisor.org" took 90 ms, and
 the second took 0 ms (cached). The slowest call in this example was to "foo",
 which was an unsuccessful lookup.
+
+
+USAGE message:
+
+# ./gethostlatency -h
+usage: gethostlatency [-h] [-p PID]
+
+Show latency for getaddrinfo/gethostbyname[2] calls
+
+optional arguments:
+  -h, --help         show this help message and exit
+  -p PID, --pid PID  trace this PID only
+
+examples:
+    ./gethostlatency           # trace all TCP accept()s
+    ./gethostlatency -p 181    # only trace PID 181