libbpf-tools: add gethostlatency
authorHengqi Chen <chenhengqi@outlook.com>
Sat, 3 Apr 2021 07:35:16 +0000 (15:35 +0800)
committeryonghong-song <ys114321@gmail.com>
Sun, 9 May 2021 06:08:05 +0000 (23:08 -0700)
Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
libbpf-tools/.gitignore
libbpf-tools/Makefile
libbpf-tools/gethostlatency.bpf.c [new file with mode: 0644]
libbpf-tools/gethostlatency.c [new file with mode: 0644]
libbpf-tools/gethostlatency.h [new file with mode: 0644]

index 2b4999bd43b00481bb34035d093fa70a26f47d96..33391e969928bb51e8a746c12616777ec28e187a 100644 (file)
@@ -12,6 +12,7 @@
 /ext4dist
 /filelife
 /funclatency
+/gethostlatency
 /hardirqs
 /llcstat
 /numamove
index 92dcf5a5ff164be6be3bb4a735fcb6cb84c7a940..3ee0c552bb809ff8e2dd674014cc117ae03d7e98 100644 (file)
@@ -29,6 +29,7 @@ APPS = \
        ext4dist \
        filelife \
        funclatency \
+       gethostlatency \
        hardirqs \
        llcstat \
        numamove \
@@ -112,4 +113,3 @@ install: $(APPS)
 .DELETE_ON_ERROR:
 # keep intermediate (.skel.h, .bpf.o, etc) targets
 .SECONDARY:
-
diff --git a/libbpf-tools/gethostlatency.bpf.c b/libbpf-tools/gethostlatency.bpf.c
new file mode 100644 (file)
index 0000000..2873ad9
--- /dev/null
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2021 Hengqi Chen
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_tracing.h>
+#include "gethostlatency.h"
+
+#define MAX_ENTRIES 10240
+
+const volatile pid_t targ_tgid = 0;
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, MAX_ENTRIES);
+       __type(key, __u32);
+       __type(value, struct val_t);
+} start SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+       __uint(key_size, sizeof(__u32));
+       __uint(value_size, sizeof(__u32));
+} events SEC(".maps");
+
+static __always_inline
+int probe_entry(struct pt_regs *ctx) {
+       if (!PT_REGS_PARM1(ctx))
+               return 0;
+
+       struct val_t val = {};
+       __u32 pid = bpf_get_current_pid_tgid() >> 32;
+
+       if (targ_tgid && targ_tgid != pid)
+               return 0;
+
+       if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
+               bpf_probe_read_user(&val.host, sizeof(val.host),
+                                          (void *)PT_REGS_PARM1(ctx));
+               val.pid = pid;
+               val.time = bpf_ktime_get_ns();
+               bpf_map_update_elem(&start, &pid, &val, BPF_ANY);
+       }
+
+       return 0;
+}
+
+static __always_inline
+int probe_return(struct pt_regs *ctx) {
+       struct val_t *valp;
+       __u32 pid = bpf_get_current_pid_tgid() >> 32;
+       __u64 now = bpf_ktime_get_ns();
+
+       valp = bpf_map_lookup_elem(&start, &pid);
+       if (!valp)
+               return 0;
+
+       // update time from timestamp to delta
+       valp->time = now - valp->time;
+       bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, valp,
+                       sizeof(*valp));
+       bpf_map_delete_elem(&start, &pid);
+       return 0;
+}
+
+SEC("kprobe/handle_entry")
+int handle_entry(struct pt_regs *ctx)
+{
+       return probe_entry(ctx);
+}
+
+SEC("kretprobe/handle_return")
+int handle_return(struct pt_regs *ctx)
+{
+       return probe_return(ctx);
+}
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/libbpf-tools/gethostlatency.c b/libbpf-tools/gethostlatency.c
new file mode 100644 (file)
index 0000000..eb049a1
--- /dev/null
@@ -0,0 +1,285 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+// Copyright (c) 2021 Hengqi Chen
+//
+// Based on gethostlatency(8) from BCC by Brendan Gregg.
+// 24-Mar-2021   Hengqi Chen   Created this.
+#include <argp.h>
+#include <errno.h>
+#include <signal.h>
+#include <time.h>
+
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+#include "gethostlatency.h"
+#include "gethostlatency.skel.h"
+#include "trace_helpers.h"
+#include "uprobe_helpers.h"
+
+#define PERF_BUFFER_PAGES 16
+#define PERF_POLL_TIMEOUT_MS 100
+#define warn(...) fprintf(stderr, __VA_ARGS__)
+
+volatile sig_atomic_t canceled = 0;
+pid_t traced_pid = 0;
+
+const char *argp_program_version = "gethostlatency 0.1";
+const char *argp_program_bug_address =
+       "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
+const char argp_program_doc[] =
+"Show latency for getaddrinfo/gethostbyname[2] calls.\n"
+"\n"
+"USAGE: gethostlatency [-h] [-p PID]\n"
+"\n"
+"EXAMPLES:\n"
+"    gethostlatency             # time getaddrinfo/gethostbyname[2] calls\n"
+"    gethostlatency -p 1216     # only trace PID 1216\n";
+
+static const struct argp_option opts[] = {
+       {"pid", 'p', "PID", 0, "Process ID to trace"},
+       {NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"},
+       {},
+};
+
+static error_t parse_arg(int key, char *arg, struct argp_state *state)
+{
+       long pid;
+
+       switch (key) {
+       case 'p':
+               errno = 0;
+               pid = strtol(arg, NULL, 10);
+               if (errno || pid <= 0) {
+                       warn("Invalid PID: %s\n", arg);
+                       argp_usage(state);
+               }
+               traced_pid = pid;
+               break;
+       case 'h':
+               argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
+               break;
+       default:
+               return ARGP_ERR_UNKNOWN;
+       }
+       return 0;
+}
+
+static void sig_int(int signo)
+{
+       canceled = 1;
+}
+
+static const char *strftime_now(char *s, size_t max, const char *format)
+{
+       struct tm *tm;
+       time_t t;
+
+       t = time(NULL);
+       tm = localtime(&t);
+       if (tm == NULL) {
+               warn("localtime: %s\n", strerror(errno));
+               return "<failed>";
+       }
+       if (strftime(s, max, format, tm) == 0) {
+               warn("strftime error\n");
+               return "<failed>";
+       }
+       return s;
+}
+
+void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
+{
+       const struct val_t *e = data;
+       char s[16] = {};
+       const char *now;
+
+       now = strftime_now(s, sizeof(s), "%H:%M:%S");
+       printf("%-11s %-10d %-20s %-10.2f %-16s\n",
+               now, e->pid, e->comm, (double)e->time/1000000, e->host);
+}
+
+void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
+{
+       warn("lost %llu events on CPU #%d\n", lost_cnt, cpu);
+}
+
+static int get_libc_path(char *path)
+{
+       FILE *f;
+       char buf[256] = {};
+       char *filename;
+       float version;
+
+       f = fopen("/proc/self/maps", "r");
+       if (!f) {
+               return -errno;
+       }
+
+       while (fscanf(f, "%*x-%*x %*s %*s %*s %*s %[^\n]\n", buf) != EOF) {
+               if (strchr(buf, '/') != buf) {
+                       continue;
+               }
+               filename = strrchr(buf, '/') + 1;
+               if (sscanf(filename, "libc-%f.so", &version) == 1) {
+                       memcpy(path, buf, strlen(buf));
+                       fclose(f);
+                       return 0;
+               }
+       }
+
+       fclose(f);
+       return -1;
+}
+
+static int attach_uprobes(struct gethostlatency_bpf *obj)
+{
+       int err;
+       char libc_path[PATH_MAX] = {};
+       off_t func_off;
+
+       err = get_libc_path(libc_path);
+       if (err) {
+               warn("could not find libc.so\n");
+               return -1;
+       }
+
+       func_off = get_elf_func_offset(libc_path, "getaddrinfo");
+       if (func_off < 0) {
+               warn("could not find getaddrinfo in %s\n", libc_path);
+               return -1;
+       }
+       obj->links.handle_entry =
+               bpf_program__attach_uprobe(obj->progs.handle_entry, false,
+                                          traced_pid ?: -1, libc_path, func_off);
+       err = libbpf_get_error(obj->links.handle_entry);
+       if (err) {
+               warn("failed to attach getaddrinfo: %d\n", err);
+               return -1;
+       }
+       obj->links.handle_return =
+               bpf_program__attach_uprobe(obj->progs.handle_return, true,
+                                          traced_pid ?: -1, libc_path, func_off);
+       err = libbpf_get_error(obj->links.handle_return);
+       if (err) {
+               warn("failed to attach getaddrinfo: %d\n", err);
+               return -1;
+       }
+
+       func_off = get_elf_func_offset(libc_path, "gethostbyname");
+       if (func_off < 0) {
+               warn("Could not find gethostbyname in %s\n", libc_path);
+               return -1;
+       }
+       obj->links.handle_entry =
+               bpf_program__attach_uprobe(obj->progs.handle_entry, false,
+                                          traced_pid ?: -1, libc_path, func_off);
+       err = libbpf_get_error(obj->links.handle_entry);
+       if (err) {
+               warn("failed to attach gethostbyname: %d\n", err);
+               return -1;
+       }
+       obj->links.handle_return =
+               bpf_program__attach_uprobe(obj->progs.handle_return, true,
+                                          traced_pid ?: -1, libc_path, func_off);
+       err = libbpf_get_error(obj->links.handle_return);
+       if (err) {
+               warn("failed to attach gethostbyname: %d\n", err);
+               return -1;
+       }
+
+       func_off = get_elf_func_offset(libc_path, "gethostbyname2");
+       if (func_off < 0) {
+               warn("Could not find gethostbyname2 in %s\n", libc_path);
+               return -1;
+       }
+       obj->links.handle_entry =
+               bpf_program__attach_uprobe(obj->progs.handle_entry, false,
+                                          traced_pid ?: -1, libc_path, func_off);
+       err = libbpf_get_error(obj->links.handle_entry);
+       if (err) {
+               warn("failed to attach gethostbyname2: %d\n", err);
+               return -1;
+       }
+       obj->links.handle_return =
+               bpf_program__attach_uprobe(obj->progs.handle_return, true,
+                                          traced_pid ?: -1, libc_path, func_off);
+       err = libbpf_get_error(obj->links.handle_return);
+       if (err) {
+               warn("failed to attach gethostbyname2: %d\n", err);
+               return -1;
+       }
+
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       static const struct argp argp = {
+               .options = opts,
+               .parser = parse_arg,
+               .doc = argp_program_doc,
+       };
+       struct perf_buffer_opts pb_opts;
+       struct perf_buffer *pb = NULL;
+       struct gethostlatency_bpf *obj;
+       int err;
+
+       err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
+       if (err)
+               return err;
+
+       err = bump_memlock_rlimit();
+       if (err) {
+               warn("failed to increase rlimit: %d\n", err);
+               return 1;
+       }
+
+       obj = gethostlatency_bpf__open();
+       if (!obj) {
+               warn("failed to open BPF object\n");
+               return 1;
+       }
+
+       obj->rodata->targ_tgid = traced_pid;
+
+       err = gethostlatency_bpf__load(obj);
+       if (err) {
+               warn("failed to load BPF object: %d\n", err);
+               goto cleanup;
+       }
+
+       err = attach_uprobes(obj);
+       if (err) {
+               goto cleanup;
+       }
+
+       pb_opts.sample_cb = handle_event;
+       pb_opts.lost_cb = handle_lost_events;
+       pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES,
+                       &pb_opts);
+       err = libbpf_get_error(pb);
+       if (err) {
+               warn("failed to open perf buffer: %d\n", err);
+               goto cleanup;
+       }
+
+       if (signal(SIGINT, sig_int) == SIG_ERR) {
+               warn("can't set signal handler: %s\n", strerror(-errno));
+               goto cleanup;
+       }
+
+       printf("%-11s %-10s %-20s %-10s %-16s\n",
+               "TIME", "PID", "COMM", "LATms", "HOST");
+
+       while (1) {
+               if ((err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS)) < 0)
+                       break;
+               if (canceled)
+                       goto cleanup;
+       }
+       warn("error polling perf buffer: %d\n", err);
+
+cleanup:
+       gethostlatency_bpf__destroy(obj);
+
+       return err != 0;
+}
diff --git a/libbpf-tools/gethostlatency.h b/libbpf-tools/gethostlatency.h
new file mode 100644 (file)
index 0000000..853b1a0
--- /dev/null
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+#ifndef __GETHOSTLATENCY_H
+#define __GETHOSTLATENCY_H
+
+#define TASK_COMM_LEN 16
+#define HOST_LEN 80
+
+struct val_t {
+       __u32 pid;
+       char comm[TASK_COMM_LEN];
+       char host[HOST_LEN];
+       __u64 time;
+};
+
+#endif /* __GETHOSTLATENCY_H */