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) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 34 | parser.add_argument("--ebpf", action="store_true", |
| 35 | help=argparse.SUPPRESS) |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 36 | args = parser.parse_args() |
| 37 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 38 | # load BPF program |
| 39 | bpf_text = """ |
| 40 | #include <uapi/linux/ptrace.h> |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 41 | #include <linux/sched.h> |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 42 | |
| 43 | struct val_t { |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 44 | u32 pid; |
| 45 | char comm[TASK_COMM_LEN]; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 46 | char host[80]; |
| 47 | u64 ts; |
| 48 | }; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 49 | |
| 50 | struct data_t { |
| 51 | u32 pid; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 52 | u64 delta; |
| 53 | char comm[TASK_COMM_LEN]; |
| 54 | char host[80]; |
| 55 | }; |
| 56 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 57 | BPF_HASH(start, u32, struct val_t); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 58 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 59 | |
| 60 | int do_entry(struct pt_regs *ctx) { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 61 | if (!PT_REGS_PARM1(ctx)) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 62 | return 0; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 63 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 64 | struct val_t val = {}; |
| 65 | u32 pid = bpf_get_current_pid_tgid(); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 66 | |
| 67 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 68 | bpf_probe_read(&val.host, sizeof(val.host), |
| 69 | (void *)PT_REGS_PARM1(ctx)); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 70 | val.pid = bpf_get_current_pid_tgid(); |
| 71 | val.ts = bpf_ktime_get_ns(); |
| 72 | start.update(&pid, &val); |
| 73 | } |
| 74 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int do_return(struct pt_regs *ctx) { |
| 79 | struct val_t *valp; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 80 | struct data_t data = {}; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 81 | u64 delta; |
| 82 | u32 pid = bpf_get_current_pid_tgid(); |
| 83 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 84 | u64 tsp = bpf_ktime_get_ns(); |
| 85 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 86 | valp = start.lookup(&pid); |
| 87 | if (valp == 0) |
| 88 | return 0; // missed start |
| 89 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 90 | bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm); |
| 91 | bpf_probe_read(&data.host, sizeof(data.host), (void *)valp->host); |
| 92 | data.pid = valp->pid; |
| 93 | data.delta = tsp - valp->ts; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 94 | events.perf_submit(ctx, &data, sizeof(data)); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 95 | start.delete(&pid); |
| 96 | return 0; |
| 97 | } |
| 98 | """ |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 99 | if args.ebpf: |
| 100 | print(bpf_text) |
| 101 | exit() |
| 102 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 103 | b = BPF(text=bpf_text) |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 104 | b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid) |
| 105 | b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry", |
| 106 | pid=args.pid) |
| 107 | b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry", |
| 108 | pid=args.pid) |
| 109 | b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return", |
| 110 | pid=args.pid) |
| 111 | b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return", |
| 112 | pid=args.pid) |
| 113 | b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return", |
| 114 | pid=args.pid) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 115 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 116 | TASK_COMM_LEN = 16 # linux/sched.h |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 117 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 118 | class Data(ct.Structure): |
| 119 | _fields_ = [ |
| 120 | ("pid", ct.c_ulonglong), |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 121 | ("delta", ct.c_ulonglong), |
| 122 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 123 | ("host", ct.c_char * 80) |
| 124 | ] |
| 125 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 126 | # header |
| 127 | print("%-9s %-6s %-16s %10s %s" % ("TIME", "PID", "COMM", "LATms", "HOST")) |
| 128 | |
| 129 | def print_event(cpu, data, size): |
| 130 | event = ct.cast(data, ct.POINTER(Data)).contents |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 131 | 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] | 132 | event.comm.decode(), (float(event.delta) / 1000000), |
| 133 | event.host.decode())) |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 134 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 135 | # loop with callback to print_event |
| 136 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 137 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 138 | b.perf_buffer_poll() |