Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # gethostlatency Show latency for getaddrinfo/gethostbyname[2] calls. |
| 4 | # For Linux, uses BCC, eBPF. Embedded C. |
| 5 | # |
| 6 | # This can be useful for identifying DNS latency, by identifying which |
| 7 | # remote host name lookups were slow, and by how much. |
| 8 | # |
| 9 | # This uses dynamic tracing of user-level functions and registers, and may |
| 10 | # need modifications to match your software and processor architecture. |
| 11 | # |
| 12 | # Copyright 2016 Netflix, Inc. |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | # |
| 15 | # 28-Jan-2016 Brendan Gregg Created this. |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 16 | # 30-Mar-2016 Allan McAleavy updated for BPF_PERF_OUTPUT |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 17 | |
| 18 | from __future__ import print_function |
| 19 | from bcc import BPF |
| 20 | from time import strftime |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 21 | import argparse |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 22 | import ctypes as ct |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 23 | |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 24 | examples = """examples: |
| 25 | ./gethostlatency # trace all TCP accept()s |
| 26 | ./gethostlatency -p 181 # only trace PID 181 |
| 27 | """ |
| 28 | parser = argparse.ArgumentParser( |
| 29 | description="Show latency for getaddrinfo/gethostbyname[2] calls", |
| 30 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 31 | epilog=examples) |
| 32 | parser.add_argument("-p", "--pid", help="trace this PID only", type=int, |
| 33 | default=-1) |
| 34 | args = parser.parse_args() |
| 35 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 36 | # load BPF program |
| 37 | bpf_text = """ |
| 38 | #include <uapi/linux/ptrace.h> |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 39 | #include <linux/sched.h> |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 40 | |
| 41 | struct val_t { |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 42 | u32 pid; |
| 43 | char comm[TASK_COMM_LEN]; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 44 | char host[80]; |
| 45 | u64 ts; |
| 46 | }; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 47 | |
| 48 | struct data_t { |
| 49 | u32 pid; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 50 | u64 delta; |
| 51 | char comm[TASK_COMM_LEN]; |
| 52 | char host[80]; |
| 53 | }; |
| 54 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 55 | BPF_HASH(start, u32, struct val_t); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 56 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 57 | |
| 58 | int do_entry(struct pt_regs *ctx) { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 59 | if (!PT_REGS_PARM1(ctx)) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 60 | return 0; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 61 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 62 | struct val_t val = {}; |
| 63 | u32 pid = bpf_get_current_pid_tgid(); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 64 | |
| 65 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 66 | bpf_probe_read(&val.host, sizeof(val.host), |
| 67 | (void *)PT_REGS_PARM1(ctx)); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 68 | val.pid = bpf_get_current_pid_tgid(); |
| 69 | val.ts = bpf_ktime_get_ns(); |
| 70 | start.update(&pid, &val); |
| 71 | } |
| 72 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int do_return(struct pt_regs *ctx) { |
| 77 | struct val_t *valp; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 78 | struct data_t data = {}; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 79 | u64 delta; |
| 80 | u32 pid = bpf_get_current_pid_tgid(); |
| 81 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 82 | u64 tsp = bpf_ktime_get_ns(); |
| 83 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 84 | valp = start.lookup(&pid); |
| 85 | if (valp == 0) |
| 86 | return 0; // missed start |
| 87 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 88 | bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm); |
| 89 | bpf_probe_read(&data.host, sizeof(data.host), (void *)valp->host); |
| 90 | data.pid = valp->pid; |
| 91 | data.delta = tsp - valp->ts; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 92 | events.perf_submit(ctx, &data, sizeof(data)); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 93 | start.delete(&pid); |
| 94 | return 0; |
| 95 | } |
| 96 | """ |
| 97 | b = BPF(text=bpf_text) |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 98 | b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid) |
| 99 | b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry", |
| 100 | pid=args.pid) |
| 101 | b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry", |
| 102 | pid=args.pid) |
| 103 | b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return", |
| 104 | pid=args.pid) |
| 105 | b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return", |
| 106 | pid=args.pid) |
| 107 | b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return", |
| 108 | pid=args.pid) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 109 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 110 | TASK_COMM_LEN = 16 # linux/sched.h |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 111 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 112 | class Data(ct.Structure): |
| 113 | _fields_ = [ |
| 114 | ("pid", ct.c_ulonglong), |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 115 | ("delta", ct.c_ulonglong), |
| 116 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 117 | ("host", ct.c_char * 80) |
| 118 | ] |
| 119 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 120 | # header |
| 121 | print("%-9s %-6s %-16s %10s %s" % ("TIME", "PID", "COMM", "LATms", "HOST")) |
| 122 | |
| 123 | def print_event(cpu, data, size): |
| 124 | event = ct.cast(data, ct.POINTER(Data)).contents |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 125 | print("%-9s %-6d %-16s %10.2f %s" % (strftime("%H:%M:%S"), event.pid, |
Brendan Gregg | 9aa1d75 | 2017-06-22 11:58:21 -0700 | [diff] [blame] | 126 | event.comm.decode(), (float(event.delta) / 1000000), |
| 127 | event.host.decode())) |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 128 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 129 | # loop with callback to print_event |
| 130 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 131 | while 1: |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 132 | b.kprobe_poll() |