libbpf-tools: gethostlatency code cleanup
authorHengqi Chen <chenhengqi@outlook.com>
Sun, 27 Jun 2021 15:44:14 +0000 (23:44 +0800)
committeryonghong-song <ys114321@gmail.com>
Wed, 14 Jul 2021 17:19:45 +0000 (10:19 -0700)
This commit updates the code to conform the kernel
code style guide.

Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
libbpf-tools/gethostlatency.bpf.c
libbpf-tools/gethostlatency.c
libbpf-tools/gethostlatency.h

index ef26c77ee1081824d26342cfaee3056304e428ea..aa3fbd329f4a7cbf8e25b210616ec30187e5af16 100644 (file)
@@ -1,21 +1,21 @@
-// SPDX-License-Identifier: GPL-2.0
-// Copyright (c) 2021 Hengqi Chen
+/* 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
+#define MAX_ENTRIES    10240
 
-const volatile pid_t targ_tgid = 0;
+const volatile pid_t target_pid = 0;
 
 struct {
        __uint(type, BPF_MAP_TYPE_HASH);
        __uint(max_entries, MAX_ENTRIES);
        __type(key, __u32);
-       __type(value, struct val_t);
-} start SEC(".maps");
+       __type(value, struct event);
+} starts SEC(".maps");
 
 struct {
        __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
@@ -23,47 +23,42 @@ struct {
        __uint(value_size, sizeof(__u32));
 } events SEC(".maps");
 
-static __always_inline
-int probe_entry(struct pt_regs *ctx) {
+static int probe_entry(struct pt_regs *ctx)
+{
        if (!PT_REGS_PARM1(ctx))
                return 0;
 
-       struct val_t val = {};
        __u64 pid_tgid = bpf_get_current_pid_tgid();
        __u32 pid = pid_tgid >> 32;
        __u32 tid = (__u32)pid_tgid;
+       struct event event = {};
 
-       if (targ_tgid && targ_tgid != pid)
+       if (target_pid && target_pid != 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, &tid, &val, BPF_ANY);
-       }
-
+       event.time = bpf_ktime_get_ns();
+       event.pid = pid;
+       bpf_get_current_comm(&event.comm, sizeof(event.comm));
+       bpf_probe_read_user(&event.host, sizeof(event.host), (void *)PT_REGS_PARM1(ctx));
+       bpf_map_update_elem(&starts, &tid, &event, BPF_ANY);
        return 0;
 }
 
-static __always_inline
-int probe_return(struct pt_regs *ctx) {
-       struct val_t *valp;
+static int probe_return(struct pt_regs *ctx)
+{
        __u64 pid_tgid = bpf_get_current_pid_tgid();
        __u32 pid = pid_tgid >> 32;
        __u32 tid = (__u32)pid_tgid;
-       __u64 now = bpf_ktime_get_ns();
+       struct event *eventp;
 
-       valp = bpf_map_lookup_elem(&start, &tid);
-       if (!valp)
+       eventp = bpf_map_lookup_elem(&starts, &tid);
+       if (!eventp)
                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, &tid);
+       /* update time from timestamp to delta */
+       eventp->time = bpf_ktime_get_ns() - eventp->time;
+       bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, eventp, sizeof(*eventp));
+       bpf_map_delete_elem(&starts, &tid);
        return 0;
 }
 
index 0624acca4e02469643a1e5a844d0eaa35304488b..a5529bcda183c431889ebab163c6c3bcd5bf09db 100644 (file)
@@ -1,8 +1,13 @@
-// 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.
+/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
+
+/*
+ * gethostlatency  Show latency for getaddrinfo/gethostbyname[2] calls.
+ *
+ * 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 "trace_helpers.h"
 #include "uprobe_helpers.h"
 
-#define PERF_BUFFER_PAGES 16
-#define PERF_POLL_TIMEOUT_MS 100
+#define PERF_BUFFER_PAGES      16
+#define PERF_POLL_TIMEOUT_MS   100
 #define warn(...) fprintf(stderr, __VA_ARGS__)
 
 static volatile sig_atomic_t exiting = 0;
-static pid_t traced_pid = 0;
+static pid_t target_pid = 0;
 
 const char *argp_program_version = "gethostlatency 0.1";
 const char *argp_program_bug_address =
@@ -52,7 +57,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
                        warn("Invalid PID: %s\n", arg);
                        argp_usage(state);
                }
-               traced_pid = pid;
+               target_pid = pid;
                break;
        case 'h':
                argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
@@ -68,33 +73,18 @@ static void sig_int(int signo)
        exiting = 1;
 }
 
-static const char *strftime_now(char *s, size_t max, const char *format)
+static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
 {
+       const struct event *e = data;
        struct tm *tm;
+       char ts[16];
        time_t t;
 
-       t = time(NULL);
+       time(&t);
        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;
-}
-
-static 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);
+       strftime(ts, sizeof(ts), "%H:%M:%S", tm);
+       printf("%-8s %-7d %-16s %-10.3f %-s\n",
+              ts, e->pid, e->comm, (double)e->time/1000000, e->host);
 }
 
 static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
@@ -105,19 +95,17 @@ static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
 static int get_libc_path(char *path)
 {
        FILE *f;
-       char buf[256] = {};
+       char buf[PATH_MAX] = {};
        char *filename;
        float version;
 
        f = fopen("/proc/self/maps", "r");
-       if (!f) {
+       if (!f)
                return -errno;
-       }
 
        while (fscanf(f, "%*x-%*x %*s %*s %*s %*s %[^\n]\n", buf) != EOF) {
-               if (strchr(buf, '/') != buf) {
+               if (strchr(buf, '/') != buf)
                        continue;
-               }
                filename = strrchr(buf, '/') + 1;
                if (sscanf(filename, "libc-%f.so", &version) == 1) {
                        memcpy(path, buf, strlen(buf));
@@ -149,7 +137,7 @@ static int attach_uprobes(struct gethostlatency_bpf *obj)
        }
        obj->links.handle_entry =
                bpf_program__attach_uprobe(obj->progs.handle_entry, false,
-                                          traced_pid ?: -1, libc_path, func_off);
+                                          target_pid ?: -1, libc_path, func_off);
        err = libbpf_get_error(obj->links.handle_entry);
        if (err) {
                warn("failed to attach getaddrinfo: %d\n", err);
@@ -157,7 +145,7 @@ static int attach_uprobes(struct gethostlatency_bpf *obj)
        }
        obj->links.handle_return =
                bpf_program__attach_uprobe(obj->progs.handle_return, true,
-                                          traced_pid ?: -1, libc_path, func_off);
+                                          target_pid ?: -1, libc_path, func_off);
        err = libbpf_get_error(obj->links.handle_return);
        if (err) {
                warn("failed to attach getaddrinfo: %d\n", err);
@@ -166,12 +154,12 @@ static int attach_uprobes(struct gethostlatency_bpf *obj)
 
        func_off = get_elf_func_offset(libc_path, "gethostbyname");
        if (func_off < 0) {
-               warn("Could not find gethostbyname in %s\n", libc_path);
+               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);
+                                          target_pid ?: -1, libc_path, func_off);
        err = libbpf_get_error(obj->links.handle_entry);
        if (err) {
                warn("failed to attach gethostbyname: %d\n", err);
@@ -179,7 +167,7 @@ static int attach_uprobes(struct gethostlatency_bpf *obj)
        }
        obj->links.handle_return =
                bpf_program__attach_uprobe(obj->progs.handle_return, true,
-                                          traced_pid ?: -1, libc_path, func_off);
+                                          target_pid ?: -1, libc_path, func_off);
        err = libbpf_get_error(obj->links.handle_return);
        if (err) {
                warn("failed to attach gethostbyname: %d\n", err);
@@ -188,12 +176,12 @@ static int attach_uprobes(struct gethostlatency_bpf *obj)
 
        func_off = get_elf_func_offset(libc_path, "gethostbyname2");
        if (func_off < 0) {
-               warn("Could not find gethostbyname2 in %s\n", libc_path);
+               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);
+                                          target_pid ?: -1, libc_path, func_off);
        err = libbpf_get_error(obj->links.handle_entry);
        if (err) {
                warn("failed to attach gethostbyname2: %d\n", err);
@@ -201,7 +189,7 @@ static int attach_uprobes(struct gethostlatency_bpf *obj)
        }
        obj->links.handle_return =
                bpf_program__attach_uprobe(obj->progs.handle_return, true,
-                                          traced_pid ?: -1, libc_path, func_off);
+                                          target_pid ?: -1, libc_path, func_off);
        err = libbpf_get_error(obj->links.handle_return);
        if (err) {
                warn("failed to attach gethostbyname2: %d\n", err);
@@ -239,7 +227,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       obj->rodata->targ_tgid = traced_pid;
+       obj->rodata->target_pid = target_pid;
 
        err = gethostlatency_bpf__load(obj);
        if (err) {
@@ -248,9 +236,8 @@ int main(int argc, char **argv)
        }
 
        err = attach_uprobes(obj);
-       if (err) {
+       if (err)
                goto cleanup;
-       }
 
        pb_opts.sample_cb = handle_event;
        pb_opts.lost_cb = handle_lost_events;
@@ -267,8 +254,8 @@ int main(int argc, char **argv)
                goto cleanup;
        }
 
-       printf("%-11s %-10s %-20s %-10s %-16s\n",
-               "TIME", "PID", "COMM", "LATms", "HOST");
+       printf("%-8s %-7s %-16s %-10s %-s\n",
+              "TIME", "PID", "COMM", "LATms", "HOST");
 
        while (1) {
                if ((err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS)) < 0)
@@ -279,6 +266,7 @@ int main(int argc, char **argv)
        warn("error polling perf buffer: %d\n", err);
 
 cleanup:
+       perf_buffer__free(pb);
        gethostlatency_bpf__destroy(obj);
 
        return err != 0;
index 853b1a0fc9a5968af4dbd6f1cf38b463affad3a2..9158fa65b927d980bb5e070d64575e2c9a7a4828 100644 (file)
@@ -1,15 +1,15 @@
-// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+/* 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
+#define TASK_COMM_LEN  16
+#define HOST_LEN       80
 
-struct val_t {
+struct event {
+       __u64 time;
        __u32 pid;
        char comm[TASK_COMM_LEN];
        char host[HOST_LEN];
-       __u64 time;
 };
 
 #endif /* __GETHOSTLATENCY_H */