1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * gethostlatency Show latency for getaddrinfo/gethostbyname[2] calls.
6 * Copyright (c) 2021 Hengqi Chen
8 * Based on gethostlatency(8) from BCC by Brendan Gregg.
9 * 24-Mar-2021 Hengqi Chen Created this.
16 #include <bpf/libbpf.h>
18 #include "gethostlatency.h"
19 #include "gethostlatency.skel.h"
20 #include "trace_helpers.h"
21 #include "uprobe_helpers.h"
23 #define PERF_BUFFER_PAGES 16
24 #define PERF_POLL_TIMEOUT_MS 100
25 #define warn(...) fprintf(stderr, __VA_ARGS__)
27 static volatile sig_atomic_t exiting = 0;
29 static pid_t target_pid = 0;
30 static const char *libc_path = NULL;
32 const char *argp_program_version = "gethostlatency 0.1";
33 const char *argp_program_bug_address =
34 "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
35 const char argp_program_doc[] =
36 "Show latency for getaddrinfo/gethostbyname[2] calls.\n"
38 "USAGE: gethostlatency [-h] [-p PID] [-l LIBC]\n"
41 " gethostlatency # time getaddrinfo/gethostbyname[2] calls\n"
42 " gethostlatency -p 1216 # only trace PID 1216\n";
44 static const struct argp_option opts[] = {
45 { "pid", 'p', "PID", 0, "Process ID to trace" },
46 { "libc", 'l', "LIBC", 0, "Specify which libc.so to use" },
47 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
51 static error_t parse_arg(int key, char *arg, struct argp_state *state)
58 pid = strtol(arg, NULL, 10);
59 if (errno || pid <= 0) {
60 warn("Invalid PID: %s\n", arg);
66 libc_path = strdup(arg);
67 if (access(libc_path, F_OK)) {
68 warn("Invalid libc: %s\n", arg);
73 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
76 return ARGP_ERR_UNKNOWN;
81 static void sig_int(int signo)
86 static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
88 const struct event *e = data;
95 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
96 printf("%-8s %-7d %-16s %-10.3f %-s\n",
97 ts, e->pid, e->comm, (double)e->time/1000000, e->host);
100 static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
102 warn("lost %llu events on CPU #%d\n", lost_cnt, cpu);
105 static int get_libc_path(char *path)
108 char buf[PATH_MAX] = {};
113 memcpy(path, libc_path, strlen(libc_path));
117 f = fopen("/proc/self/maps", "r");
121 while (fscanf(f, "%*x-%*x %*s %*s %*s %*s %[^\n]\n", buf) != EOF) {
122 if (strchr(buf, '/') != buf)
124 filename = strrchr(buf, '/') + 1;
125 if (sscanf(filename, "libc-%f.so", &version) == 1) {
126 memcpy(path, buf, strlen(buf));
136 static int attach_uprobes(struct gethostlatency_bpf *obj, struct bpf_link *links[])
139 char libc_path[PATH_MAX] = {};
142 err = get_libc_path(libc_path);
144 warn("could not find libc.so\n");
148 func_off = get_elf_func_offset(libc_path, "getaddrinfo");
150 warn("could not find getaddrinfo in %s\n", libc_path);
153 links[0] = bpf_program__attach_uprobe(obj->progs.handle_entry, false,
154 target_pid ?: -1, libc_path, func_off);
156 warn("failed to attach getaddrinfo: %d\n", -errno);
159 links[1] = bpf_program__attach_uprobe(obj->progs.handle_return, true,
160 target_pid ?: -1, libc_path, func_off);
162 warn("failed to attach getaddrinfo: %d\n", -errno);
166 func_off = get_elf_func_offset(libc_path, "gethostbyname");
168 warn("could not find gethostbyname in %s\n", libc_path);
171 links[2] = bpf_program__attach_uprobe(obj->progs.handle_entry, false,
172 target_pid ?: -1, libc_path, func_off);
174 warn("failed to attach gethostbyname: %d\n", -errno);
177 links[3] = bpf_program__attach_uprobe(obj->progs.handle_return, true,
178 target_pid ?: -1, libc_path, func_off);
180 warn("failed to attach gethostbyname: %d\n", -errno);
184 func_off = get_elf_func_offset(libc_path, "gethostbyname2");
186 warn("could not find gethostbyname2 in %s\n", libc_path);
189 links[4] = bpf_program__attach_uprobe(obj->progs.handle_entry, false,
190 target_pid ?: -1, libc_path, func_off);
192 warn("failed to attach gethostbyname2: %d\n", -errno);
195 links[5] = bpf_program__attach_uprobe(obj->progs.handle_return, true,
196 target_pid ?: -1, libc_path, func_off);
198 warn("failed to attach gethostbyname2: %d\n", -errno);
205 int main(int argc, char **argv)
207 static const struct argp argp = {
210 .doc = argp_program_doc,
212 struct perf_buffer *pb = NULL;
213 struct bpf_link *links[6] = {};
214 struct gethostlatency_bpf *obj;
217 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
221 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
223 obj = gethostlatency_bpf__open();
225 warn("failed to open BPF object\n");
229 obj->rodata->target_pid = target_pid;
231 err = gethostlatency_bpf__load(obj);
233 warn("failed to load BPF object: %d\n", err);
237 err = attach_uprobes(obj, links);
241 pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES,
242 handle_event, handle_lost_events, NULL, NULL);
245 warn("failed to open perf buffer: %d\n", err);
249 if (signal(SIGINT, sig_int) == SIG_ERR) {
250 warn("can't set signal handler: %s\n", strerror(errno));
255 printf("%-8s %-7s %-16s %-10s %-s\n",
256 "TIME", "PID", "COMM", "LATms", "HOST");
259 err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS);
260 if (err < 0 && err != -EINTR) {
261 warn("error polling perf buffer: %s\n", strerror(-err));
264 /* reset err to return 0 if exiting */
269 perf_buffer__free(pb);
270 for (i = 0; i < 6; i++)
271 bpf_link__destroy(links[i]);
272 gethostlatency_bpf__destroy(obj);