Commit 06a1c85f authored by Brendan Gregg's avatar Brendan Gregg Committed by GitHub

Merge pull request #920 from pchaigno/pid-filter-gethostlatency

gethostlatency: Filter by process ID
parents 5472dbcd edca17a2
...@@ -21,6 +21,10 @@ and may need modifications to match your software and processor architecture. ...@@ -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. Since this uses BPF, only the root user can use this tool.
.SH REQUIREMENTS .SH REQUIREMENTS
CONFIG_BPF and bcc. CONFIG_BPF and bcc.
.SH OPTIONS
.TP
\-p PID
Trace this process ID only.
.SH EXAMPLES .SH EXAMPLES
.TP .TP
Trace host lookups (getaddrinfo/gethostbyname[2]) system wide: Trace host lookups (getaddrinfo/gethostbyname[2]) system wide:
......
...@@ -18,8 +18,21 @@ ...@@ -18,8 +18,21 @@
from __future__ import print_function from __future__ import print_function
from bcc import BPF from bcc import BPF
from time import strftime from time import strftime
import argparse
import ctypes as ct 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 # load BPF program
bpf_text = """ bpf_text = """
#include <uapi/linux/ptrace.h> #include <uapi/linux/ptrace.h>
...@@ -82,12 +95,17 @@ int do_return(struct pt_regs *ctx) { ...@@ -82,12 +95,17 @@ int do_return(struct pt_regs *ctx) {
} }
""" """
b = BPF(text=bpf_text) b = BPF(text=bpf_text)
b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry") 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") b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry",
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") b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry",
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") 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 TASK_COMM_LEN = 16 # linux/sched.h
......
...@@ -19,3 +19,19 @@ TIME PID COMM LATms HOST ...@@ -19,3 +19,19 @@ TIME PID COMM LATms HOST
In this example, the first call to lookup "www.iovisor.org" took 90 ms, and 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", the second took 0 ms (cached). The slowest call in this example was to "foo",
which was an unsuccessful lookup. 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment