blob: 3a967ae283c4ae9454adec6b00e61927025e57b4 [file] [log] [blame]
Brendan Gregg5a06c2c2016-01-28 23:00:00 -08001#!/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.
mcaleavyaa9b886c2016-04-02 18:22:37 +010016# 30-Mar-2016 Allan McAleavy updated for BPF_PERF_OUTPUT
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080017
18from __future__ import print_function
19from bcc import BPF
20from time import strftime
Paul Chaignonedca17a2017-01-23 22:43:10 +010021import argparse
mcaleavyaa9b886c2016-04-02 18:22:37 +010022import ctypes as ct
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080023
Paul Chaignonedca17a2017-01-23 22:43:10 +010024examples = """examples:
25 ./gethostlatency # trace all TCP accept()s
26 ./gethostlatency -p 181 # only trace PID 181
27"""
28parser = argparse.ArgumentParser(
29 description="Show latency for getaddrinfo/gethostbyname[2] calls",
30 formatter_class=argparse.RawDescriptionHelpFormatter,
31 epilog=examples)
32parser.add_argument("-p", "--pid", help="trace this PID only", type=int,
33 default=-1)
Nathan Scottcf0792f2018-02-02 16:56:50 +110034parser.add_argument("--ebpf", action="store_true",
35 help=argparse.SUPPRESS)
Paul Chaignonedca17a2017-01-23 22:43:10 +010036args = parser.parse_args()
37
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080038# load BPF program
39bpf_text = """
40#include <uapi/linux/ptrace.h>
mcaleavyaa9b886c2016-04-02 18:22:37 +010041#include <linux/sched.h>
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080042
43struct val_t {
mcaleavyaa9b886c2016-04-02 18:22:37 +010044 u32 pid;
45 char comm[TASK_COMM_LEN];
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080046 char host[80];
47 u64 ts;
48};
mcaleavyaa9b886c2016-04-02 18:22:37 +010049
50struct data_t {
51 u32 pid;
mcaleavyaa9b886c2016-04-02 18:22:37 +010052 u64 delta;
53 char comm[TASK_COMM_LEN];
54 char host[80];
55};
56
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080057BPF_HASH(start, u32, struct val_t);
mcaleavyaa9b886c2016-04-02 18:22:37 +010058BPF_PERF_OUTPUT(events);
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080059
60int do_entry(struct pt_regs *ctx) {
Naveen N. Rao4afa96a2016-05-03 14:54:21 +053061 if (!PT_REGS_PARM1(ctx))
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080062 return 0;
mcaleavyaa9b886c2016-04-02 18:22:37 +010063
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080064 struct val_t val = {};
65 u32 pid = bpf_get_current_pid_tgid();
mcaleavyaa9b886c2016-04-02 18:22:37 +010066
67 if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030068 bpf_probe_read(&val.host, sizeof(val.host),
69 (void *)PT_REGS_PARM1(ctx));
mcaleavyaa9b886c2016-04-02 18:22:37 +010070 val.pid = bpf_get_current_pid_tgid();
71 val.ts = bpf_ktime_get_ns();
72 start.update(&pid, &val);
73 }
74
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080075 return 0;
76}
77
78int do_return(struct pt_regs *ctx) {
79 struct val_t *valp;
mcaleavyaa9b886c2016-04-02 18:22:37 +010080 struct data_t data = {};
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080081 u64 delta;
82 u32 pid = bpf_get_current_pid_tgid();
83
mcaleavyaa9b886c2016-04-02 18:22:37 +010084 u64 tsp = bpf_ktime_get_ns();
85
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080086 valp = start.lookup(&pid);
87 if (valp == 0)
88 return 0; // missed start
89
mcaleavyaa9b886c2016-04-02 18:22:37 +010090 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;
mcaleavyaa9b886c2016-04-02 18:22:37 +010094 events.perf_submit(ctx, &data, sizeof(data));
Brendan Gregg5a06c2c2016-01-28 23:00:00 -080095 start.delete(&pid);
96 return 0;
97}
98"""
Nathan Scottcf0792f2018-02-02 16:56:50 +110099if args.ebpf:
100 print(bpf_text)
101 exit()
102
Brendan Gregg5a06c2c2016-01-28 23:00:00 -0800103b = BPF(text=bpf_text)
Paul Chaignonedca17a2017-01-23 22:43:10 +0100104b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid)
105b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry",
106 pid=args.pid)
107b.attach_uprobe(name="c", sym="gethostbyname2", fn_name="do_entry",
108 pid=args.pid)
109b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return",
110 pid=args.pid)
111b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return",
112 pid=args.pid)
113b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return",
114 pid=args.pid)
Brendan Gregg5a06c2c2016-01-28 23:00:00 -0800115
mcaleavyaa9b886c2016-04-02 18:22:37 +0100116TASK_COMM_LEN = 16 # linux/sched.h
Brendan Gregg5a06c2c2016-01-28 23:00:00 -0800117
mcaleavyaa9b886c2016-04-02 18:22:37 +0100118class Data(ct.Structure):
119 _fields_ = [
120 ("pid", ct.c_ulonglong),
mcaleavyaa9b886c2016-04-02 18:22:37 +0100121 ("delta", ct.c_ulonglong),
122 ("comm", ct.c_char * TASK_COMM_LEN),
123 ("host", ct.c_char * 80)
124 ]
125
mcaleavyaa9b886c2016-04-02 18:22:37 +0100126# header
127print("%-9s %-6s %-16s %10s %s" % ("TIME", "PID", "COMM", "LATms", "HOST"))
128
129def print_event(cpu, data, size):
130 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavyaa9b886c2016-04-02 18:22:37 +0100131 print("%-9s %-6d %-16s %10.2f %s" % (strftime("%H:%M:%S"), event.pid,
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200132 event.comm.decode('utf-8', 'replace'), (float(event.delta) / 1000000),
133 event.host.decode('utf-8', 'replace')))
mcaleavyaa9b886c2016-04-02 18:22:37 +0100134
mcaleavyaa9b886c2016-04-02 18:22:37 +0100135# loop with callback to print_event
136b["events"].open_perf_buffer(print_event)
Brendan Gregg5a06c2c2016-01-28 23:00:00 -0800137while 1:
Teng Qindbf00292018-02-28 21:47:50 -0800138 b.perf_buffer_poll()