Skip to content

Commit f604646

Browse files
ethercflowyonghong-song
authored andcommitted
libbpf-tools: fix error handling and cleanup
Signed-off-by: Wenbo Zhang <[email protected]>
1 parent 456cb95 commit f604646

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

libbpf-tools/llcstat.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ static int open_and_attach_perf_event(__u64 config, int period,
9393
for (i = 0; i < nr_cpus; i++) {
9494
fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0);
9595
if (fd < 0) {
96+
/* Ignore CPU that is offline */
97+
if (errno == ENODEV)
98+
continue;
9699
fprintf(stderr, "failed to init perf sampling: %s\n",
97100
strerror(errno));
98101
return -1;
@@ -185,23 +188,22 @@ int main(int argc, char **argv)
185188
return 1;
186189
}
187190

188-
obj = llcstat_bpf__open();
189-
if (!obj) {
190-
fprintf(stderr, "failed to open BPF object\n");
191+
nr_cpus = libbpf_num_possible_cpus();
192+
if (nr_cpus < 0) {
193+
fprintf(stderr, "failed to get # of possible cpus: '%s'!\n",
194+
strerror(-nr_cpus));
191195
return 1;
192196
}
193-
194-
nr_cpus = libbpf_num_possible_cpus();
195197
mlinks = calloc(nr_cpus, sizeof(*mlinks));
196198
rlinks = calloc(nr_cpus, sizeof(*rlinks));
197199
if (!mlinks || !rlinks) {
198200
fprintf(stderr, "failed to alloc mlinks or rlinks\n");
199-
goto cleanup;
201+
return 1;
200202
}
201203

202-
err = llcstat_bpf__load(obj);
203-
if (err) {
204-
fprintf(stderr, "failed to load BPF object: %d\n", err);
204+
obj = llcstat_bpf__open_and_load();
205+
if (!obj) {
206+
fprintf(stderr, "failed to open and/or load BPF object\n");
205207
goto cleanup;
206208
}
207209

libbpf-tools/numamove.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22
// Copyright (c) 2020 Wenbo Zhang
33
//
4-
// Based on numamove(8) from from BPF-Perf-Tools-Book by Brendan Gregg.
4+
// Based on numamove(8) from BPF-Perf-Tools-Book by Brendan Gregg.
55
// 8-Jun-2020 Wenbo Zhang Created this.
66
#include <argp.h>
77
#include <signal.h>

libbpf-tools/readahead.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22
// Copyright (c) 2020 Wenbo Zhang
33
//
4-
// Based on readahead(8) from from BPF-Perf-Tools-Book by Brendan Gregg.
4+
// Based on readahead(8) from BPF-Perf-Tools-Book by Brendan Gregg.
55
// 8-Jun-2020 Wenbo Zhang Created this.
66
#include <argp.h>
77
#include <signal.h>

libbpf-tools/xfsslower.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
// Based on xfsslower(8) from BCC by Brendan Gregg & Dina Goldshtein.
55
// 9-Mar-2020 Wenbo Zhang Created this.
66
#include <argp.h>
7-
#include <limits.h>
8-
#include <stdint.h>
97
#include <stdio.h>
108
#include <stdlib.h>
119
#include <string.h>
12-
#include <unistd.h>
1310
#include <time.h>
11+
#include <unistd.h>
1412
#include <bpf/libbpf.h>
1513
#include <bpf/bpf.h>
1614
#include "xfsslower.h"
@@ -21,7 +19,6 @@
2119
#define PERF_BUFFER_TIME_MS 10
2220
#define PERF_POLL_TIMEOUT_MS 100
2321

24-
2522
static struct env {
2623
pid_t pid;
2724
time_t duration;
@@ -168,7 +165,7 @@ int main(int argc, char **argv)
168165

169166
obj = xfsslower_bpf__open();
170167
if (!obj) {
171-
fprintf(stderr, "failed to open and/or load BPF object\n");
168+
fprintf(stderr, "failed to open BPF object\n");
172169
return 1;
173170
}
174171

0 commit comments

Comments
 (0)