blob: 84c79882608d3e6bdf1a37bdf351e8c0a8b4e407 [file] [log] [blame]
#!/usr/bin/env python
# gethostlatency Show latency for getaddrinfo/gethostbyname[2] calls.
# For Linux, uses BCC, eBPF. Embedded C.
# This can be useful for identifying DNS latency, by identifying which
# remote host name lookups were slow, and by how much.
# This uses dynamic tracing of user-level functions and registers, and may
# need modifications to match your software and processor architecture.
# Copyright 2016 Netflix, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
# 28-Jan-2016 Brendan Gregg Created this.
# 30-Mar-2016 Allan McAleavy updated for BPF_PERF_OUTPUT
from __future__ import print_function
from bcc import BPF
from time import strftime
import argparse
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",
parser.add_argument("-p", "--pid", help="trace this PID only", type=int,
parser.add_argument("--ebpf", action="store_true",
args = parser.parse_args()
# load BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
struct val_t {
u32 pid;
char comm[TASK_COMM_LEN];
char host[80];
u64 ts;
struct data_t {
u32 pid;
u64 delta;
char comm[TASK_COMM_LEN];
char host[80];
BPF_HASH(start, u32, struct val_t);
int do_entry(struct pt_regs *ctx) {
if (!PT_REGS_PARM1(ctx))
return 0;
struct val_t val = {};
u32 pid = bpf_get_current_pid_tgid();
if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
bpf_probe_read(&, sizeof(,
(void *)PT_REGS_PARM1(ctx)); = bpf_get_current_pid_tgid();
val.ts = bpf_ktime_get_ns();
start.update(&pid, &val);
return 0;
int do_return(struct pt_regs *ctx) {
struct val_t *valp;
struct data_t data = {};
u64 delta;
u32 pid = bpf_get_current_pid_tgid();
u64 tsp = bpf_ktime_get_ns();
valp = start.lookup(&pid);
if (valp == 0)
return 0; // missed start
bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
bpf_probe_read(&, sizeof(, (void *)valp->host); = valp->pid; = tsp - valp->ts;
events.perf_submit(ctx, &data, sizeof(data));
return 0;
if args.ebpf:
b = BPF(text=bpf_text)
b.attach_uprobe(name="c", sym="getaddrinfo", 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",
b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return",
b.attach_uretprobe(name="c", sym="gethostbyname", fn_name="do_return",
b.attach_uretprobe(name="c", sym="gethostbyname2", fn_name="do_return",
TASK_COMM_LEN = 16 # linux/sched.h
class Data(ct.Structure):
_fields_ = [
("pid", ct.c_ulonglong),
("delta", ct.c_ulonglong),
("comm", ct.c_char * TASK_COMM_LEN),
("host", ct.c_char * 80)
# header
print("%-9s %-6s %-16s %10s %s" % ("TIME", "PID", "COMM", "LATms", "HOST"))
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
print("%-9s %-6d %-16s %10.2f %s" % (strftime("%H:%M:%S"),,
event.comm.decode('utf-8', 'replace'), (float( / 1000000),'utf-8', 'replace')))
# loop with callback to print_event
while 1:
except KeyboardInterrupt: