Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 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 |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 22 | |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 23 | examples = """examples: |
synical | 6b989db | 2019-10-13 02:17:44 -0400 | [diff] [blame] | 24 | ./gethostlatency # time getaddrinfo/gethostbyname[2] calls |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 25 | ./gethostlatency -p 181 # only trace PID 181 |
| 26 | """ |
| 27 | parser = argparse.ArgumentParser( |
| 28 | description="Show latency for getaddrinfo/gethostbyname[2] calls", |
| 29 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 30 | epilog=examples) |
| 31 | parser.add_argument("-p", "--pid", help="trace this PID only", type=int, |
| 32 | default=-1) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 33 | parser.add_argument("--ebpf", action="store_true", |
| 34 | help=argparse.SUPPRESS) |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 35 | args = parser.parse_args() |
| 36 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 37 | # load BPF program |
| 38 | bpf_text = """ |
| 39 | #include <uapi/linux/ptrace.h> |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 40 | #include <linux/sched.h> |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 41 | |
| 42 | struct val_t { |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 43 | u32 pid; |
| 44 | char comm[TASK_COMM_LEN]; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 45 | char host[80]; |
| 46 | u64 ts; |
| 47 | }; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 48 | |
| 49 | struct data_t { |
| 50 | u32 pid; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 51 | u64 delta; |
| 52 | char comm[TASK_COMM_LEN]; |
| 53 | char host[80]; |
| 54 | }; |
| 55 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 56 | BPF_HASH(start, u32, struct val_t); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 57 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 58 | |
| 59 | int do_entry(struct pt_regs *ctx) { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 60 | if (!PT_REGS_PARM1(ctx)) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 61 | return 0; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 62 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 63 | struct val_t val = {}; |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 64 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 65 | u32 pid = pid_tgid >> 32; |
| 66 | u32 tid = (u32)pid_tgid; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 67 | |
| 68 | if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) { |
Sumanth Korikkar | 023154c | 2020-04-20 05:54:57 -0500 | [diff] [blame] | 69 | bpf_probe_read_user(&val.host, sizeof(val.host), |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 70 | (void *)PT_REGS_PARM1(ctx)); |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 71 | val.pid = pid; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 72 | val.ts = bpf_ktime_get_ns(); |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 73 | start.update(&tid, &val); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int do_return(struct pt_regs *ctx) { |
| 80 | struct val_t *valp; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 81 | struct data_t data = {}; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 82 | u64 delta; |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 83 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 84 | u32 tid = (u32)pid_tgid; |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 85 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 86 | u64 tsp = bpf_ktime_get_ns(); |
| 87 | |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 88 | valp = start.lookup(&tid); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 89 | if (valp == 0) |
| 90 | return 0; // missed start |
| 91 | |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 92 | bpf_probe_read_kernel(&data.comm, sizeof(data.comm), valp->comm); |
| 93 | bpf_probe_read_kernel(&data.host, sizeof(data.host), (void *)valp->host); |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 94 | data.pid = valp->pid; |
| 95 | data.delta = tsp - valp->ts; |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 96 | events.perf_submit(ctx, &data, sizeof(data)); |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 97 | start.delete(&tid); |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | """ |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 101 | if args.ebpf: |
| 102 | print(bpf_text) |
| 103 | exit() |
| 104 | |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 105 | b = BPF(text=bpf_text) |
Paul Chaignon | edca17a | 2017-01-23 22:43:10 +0100 | [diff] [blame] | 106 | b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid) |
| 107 | b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry", |
| 108 | pid=args.pid) |
| 109 | b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry", |
| 110 | pid=args.pid) |
| 111 | b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return", |
| 112 | pid=args.pid) |
| 113 | b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return", |
| 114 | pid=args.pid) |
| 115 | b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return", |
| 116 | pid=args.pid) |
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 | # header |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 119 | print("%-9s %-7s %-16s %10s %s" % ("TIME", "PID", "COMM", "LATms", "HOST")) |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 120 | |
| 121 | def print_event(cpu, data, size): |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 122 | event = b["events"].event(data) |
Hengqi Chen | 151fe19 | 2021-05-16 17:18:27 +0800 | [diff] [blame] | 123 | print("%-9s %-7d %-16s %10.2f %s" % (strftime("%H:%M:%S"), event.pid, |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 124 | event.comm.decode('utf-8', 'replace'), (float(event.delta) / 1000000), |
| 125 | event.host.decode('utf-8', 'replace'))) |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 126 | |
mcaleavya | a9b886c | 2016-04-02 18:22:37 +0100 | [diff] [blame] | 127 | # loop with callback to print_event |
| 128 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | 5a06c2c | 2016-01-28 23:00:00 -0800 | [diff] [blame] | 129 | while 1: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 130 | try: |
| 131 | b.perf_buffer_poll() |
| 132 | except KeyboardInterrupt: |
| 133 | exit() |