hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/tools/testing/selftests/bpf/bpf_util.h
....@@ -6,44 +6,17 @@
66 #include <stdlib.h>
77 #include <string.h>
88 #include <errno.h>
9
+#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
910
1011 static inline unsigned int bpf_num_possible_cpus(void)
1112 {
12
- static const char *fcpu = "/sys/devices/system/cpu/possible";
13
- unsigned int start, end, possible_cpus = 0;
14
- char buff[128];
15
- FILE *fp;
16
- int len, n, i, j = 0;
13
+ int possible_cpus = libbpf_num_possible_cpus();
1714
18
- fp = fopen(fcpu, "r");
19
- if (!fp) {
20
- printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
15
+ if (possible_cpus < 0) {
16
+ printf("Failed to get # of possible cpus: '%s'!\n",
17
+ strerror(-possible_cpus));
2118 exit(1);
2219 }
23
-
24
- if (!fgets(buff, sizeof(buff), fp)) {
25
- printf("Failed to read %s!\n", fcpu);
26
- exit(1);
27
- }
28
-
29
- len = strlen(buff);
30
- for (i = 0; i <= len; i++) {
31
- if (buff[i] == ',' || buff[i] == '\0') {
32
- buff[i] = '\0';
33
- n = sscanf(&buff[j], "%u-%u", &start, &end);
34
- if (n <= 0) {
35
- printf("Failed to retrieve # possible CPUs!\n");
36
- exit(1);
37
- } else if (n == 1) {
38
- end = start;
39
- }
40
- possible_cpus += end - start + 1;
41
- j = i + 1;
42
- }
43
- }
44
-
45
- fclose(fp);
46
-
4720 return possible_cpus;
4821 }
4922
....@@ -58,4 +31,13 @@
5831 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
5932 #endif
6033
34
+#ifndef sizeof_field
35
+#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
36
+#endif
37
+
38
+#ifndef offsetofend
39
+#define offsetofend(TYPE, MEMBER) \
40
+ (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
41
+#endif
42
+
6143 #endif /* __BPF_UTIL__ */