Skip to content

Commit e64d525

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: bpftool: move get_possible_cpus() to common code
Move the get_possible_cpus() function to shared code. No functional changes. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent c642ea2 commit e64d525

File tree

3 files changed

+59
-58
lines changed

3 files changed

+59
-58
lines changed

tools/bpf/bpftool/common.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Netronome Systems, Inc.
2+
* Copyright (C) 2017-2018 Netronome Systems, Inc.
33
*
44
* This software is dual licensed under the GNU General License Version 2,
55
* June 1991 as shown in the file COPYING in the top-level directory of this
@@ -33,6 +33,7 @@
3333

3434
/* Author: Jakub Kicinski <kubakici@wp.pl> */
3535

36+
#include <ctype.h>
3637
#include <errno.h>
3738
#include <fcntl.h>
3839
#include <fts.h>
@@ -420,6 +421,61 @@ void delete_pinned_obj_table(struct pinned_obj_table *tab)
420421
}
421422
}
422423

424+
unsigned int get_possible_cpus(void)
425+
{
426+
static unsigned int result;
427+
char buf[128];
428+
long int n;
429+
char *ptr;
430+
int fd;
431+
432+
if (result)
433+
return result;
434+
435+
fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
436+
if (fd < 0) {
437+
p_err("can't open sysfs possible cpus");
438+
exit(-1);
439+
}
440+
441+
n = read(fd, buf, sizeof(buf));
442+
if (n < 2) {
443+
p_err("can't read sysfs possible cpus");
444+
exit(-1);
445+
}
446+
close(fd);
447+
448+
if (n == sizeof(buf)) {
449+
p_err("read sysfs possible cpus overflow");
450+
exit(-1);
451+
}
452+
453+
ptr = buf;
454+
n = 0;
455+
while (*ptr && *ptr != '\n') {
456+
unsigned int a, b;
457+
458+
if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
459+
n += b - a + 1;
460+
461+
ptr = strchr(ptr, '-') + 1;
462+
} else if (sscanf(ptr, "%u", &a) == 1) {
463+
n++;
464+
} else {
465+
assert(0);
466+
}
467+
468+
while (isdigit(*ptr))
469+
ptr++;
470+
if (*ptr == ',')
471+
ptr++;
472+
}
473+
474+
result = n;
475+
476+
return result;
477+
}
478+
423479
static char *
424480
ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
425481
{

tools/bpf/bpftool/main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Netronome Systems, Inc.
2+
* Copyright (C) 2017-2018 Netronome Systems, Inc.
33
*
44
* This software is dual licensed under the GNU General License Version 2,
55
* June 1991 as shown in the file COPYING in the top-level directory of this
@@ -125,6 +125,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
125125
const char *arch);
126126
void print_hex_data_json(uint8_t *data, size_t len);
127127

128+
unsigned int get_possible_cpus(void);
128129
const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino);
129130

130131
#endif

tools/bpf/bpftool/map.c

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
/* Author: Jakub Kicinski <kubakici@wp.pl> */
3535

3636
#include <assert.h>
37-
#include <ctype.h>
3837
#include <errno.h>
3938
#include <fcntl.h>
4039
#include <stdbool.h>
@@ -69,61 +68,6 @@ static const char * const map_type_name[] = {
6968
[BPF_MAP_TYPE_CPUMAP] = "cpumap",
7069
};
7170

72-
static unsigned int get_possible_cpus(void)
73-
{
74-
static unsigned int result;
75-
char buf[128];
76-
long int n;
77-
char *ptr;
78-
int fd;
79-
80-
if (result)
81-
return result;
82-
83-
fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
84-
if (fd < 0) {
85-
p_err("can't open sysfs possible cpus");
86-
exit(-1);
87-
}
88-
89-
n = read(fd, buf, sizeof(buf));
90-
if (n < 2) {
91-
p_err("can't read sysfs possible cpus");
92-
exit(-1);
93-
}
94-
close(fd);
95-
96-
if (n == sizeof(buf)) {
97-
p_err("read sysfs possible cpus overflow");
98-
exit(-1);
99-
}
100-
101-
ptr = buf;
102-
n = 0;
103-
while (*ptr && *ptr != '\n') {
104-
unsigned int a, b;
105-
106-
if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
107-
n += b - a + 1;
108-
109-
ptr = strchr(ptr, '-') + 1;
110-
} else if (sscanf(ptr, "%u", &a) == 1) {
111-
n++;
112-
} else {
113-
assert(0);
114-
}
115-
116-
while (isdigit(*ptr))
117-
ptr++;
118-
if (*ptr == ',')
119-
ptr++;
120-
}
121-
122-
result = n;
123-
124-
return result;
125-
}
126-
12771
static bool map_is_per_cpu(__u32 type)
12872
{
12973
return type == BPF_MAP_TYPE_PERCPU_HASH ||

0 commit comments

Comments
 (0)