libbpf-tools: fix error handling and cleanup
authorWenbo Zhang <ethercflow@gmail.com>
Tue, 16 Mar 2021 03:54:54 +0000 (11:54 +0800)
committeryonghong-song <ys114321@gmail.com>
Wed, 17 Mar 2021 03:52:25 +0000 (20:52 -0700)
Signed-off-by: Wenbo Zhang <ethercflow@gmail.com>
libbpf-tools/llcstat.c
libbpf-tools/numamove.c
libbpf-tools/readahead.c
libbpf-tools/xfsslower.c

index 654a911a846c1c7a960e82093ed052238c1baf2f..c2c5232b180d5d4913a17fa74f5b50805d595fe6 100644 (file)
@@ -93,6 +93,9 @@ static int open_and_attach_perf_event(__u64 config, int period,
        for (i = 0; i < nr_cpus; i++) {
                fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0);
                if (fd < 0) {
+                       /* Ignore CPU that is offline */
+                       if (errno == ENODEV)
+                               continue;
                        fprintf(stderr, "failed to init perf sampling: %s\n",
                                strerror(errno));
                        return -1;
@@ -185,23 +188,22 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       obj = llcstat_bpf__open();
-       if (!obj) {
-               fprintf(stderr, "failed to open BPF object\n");
+       nr_cpus = libbpf_num_possible_cpus();
+       if (nr_cpus < 0) {
+               fprintf(stderr, "failed to get # of possible cpus: '%s'!\n",
+                       strerror(-nr_cpus));
                return 1;
        }
-
-       nr_cpus = libbpf_num_possible_cpus();
        mlinks = calloc(nr_cpus, sizeof(*mlinks));
        rlinks = calloc(nr_cpus, sizeof(*rlinks));
        if (!mlinks || !rlinks) {
                fprintf(stderr, "failed to alloc mlinks or rlinks\n");
-               goto cleanup;
+               return 1;
        }
 
-       err = llcstat_bpf__load(obj);
-       if (err) {
-               fprintf(stderr, "failed to load BPF object: %d\n", err);
+       obj = llcstat_bpf__open_and_load();
+       if (!obj) {
+               fprintf(stderr, "failed to open and/or load BPF object\n");
                goto cleanup;
        }
 
index fa4fa1355203e490e0e8a45144d20815f433b9af..34b4b819f7c9bf1a99f594e709d53efe6c97813c 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
 // Copyright (c) 2020 Wenbo Zhang
 //
-// Based on numamove(8) from from BPF-Perf-Tools-Book by Brendan Gregg.
+// Based on numamove(8) from BPF-Perf-Tools-Book by Brendan Gregg.
 // 8-Jun-2020   Wenbo Zhang   Created this.
 #include <argp.h>
 #include <signal.h>
index 648784ed0b5d9cf7c5bcba54e17e42d23954cee5..5e3893ef09444e2d4ae353629562097ca79ac45e 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
 // Copyright (c) 2020 Wenbo Zhang
 //
-// Based on readahead(8) from from BPF-Perf-Tools-Book by Brendan Gregg.
+// Based on readahead(8) from BPF-Perf-Tools-Book by Brendan Gregg.
 // 8-Jun-2020   Wenbo Zhang   Created this.
 #include <argp.h>
 #include <signal.h>
index f94216d4da1fc2e8d67888e6a6f3f9b33a611ed0..73e300867cb4e60dcb0436801129362cb53dca44 100644 (file)
@@ -4,13 +4,11 @@
 // Based on xfsslower(8) from BCC by Brendan Gregg & Dina Goldshtein.
 // 9-Mar-2020   Wenbo Zhang   Created this.
 #include <argp.h>
-#include <limits.h>
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <time.h>
+#include <unistd.h>
 #include <bpf/libbpf.h>
 #include <bpf/bpf.h>
 #include "xfsslower.h"
@@ -21,7 +19,6 @@
 #define PERF_BUFFER_TIME_MS    10
 #define PERF_POLL_TIMEOUT_MS   100
 
-
 static struct env {
        pid_t pid;
        time_t duration;
@@ -168,7 +165,7 @@ int main(int argc, char **argv)
 
        obj = xfsslower_bpf__open();
        if (!obj) {
-               fprintf(stderr, "failed to open and/or load BPF object\n");
+               fprintf(stderr, "failed to open BPF object\n");
                return 1;
        }