Skip to content

Commit

Permalink
libbpf-tools: fix error handling and cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Wenbo Zhang <[email protected]>
  • Loading branch information
ethercflow authored and yonghong-song committed Mar 17, 2021
1 parent 456cb95 commit f604646
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
20 changes: 11 additions & 9 deletions libbpf-tools/llcstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion libbpf-tools/numamove.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
2 changes: 1 addition & 1 deletion libbpf-tools/readahead.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
7 changes: 2 additions & 5 deletions libbpf-tools/xfsslower.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit f604646

Please sign in to comment.