Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
e00c7b21 DB |
2 | #ifndef __BPF_UTIL__ |
3 | #define __BPF_UTIL__ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <string.h> | |
8 | #include <errno.h> | |
9 | ||
10 | static inline unsigned int bpf_num_possible_cpus(void) | |
11 | { | |
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; | |
1bb54c40 | 16 | int len, n, i, j = 0; |
e00c7b21 DB |
17 | |
18 | fp = fopen(fcpu, "r"); | |
19 | if (!fp) { | |
20 | printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno)); | |
21 | exit(1); | |
22 | } | |
23 | ||
1bb54c40 MP |
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; | |
e00c7b21 DB |
42 | } |
43 | } | |
1bb54c40 | 44 | |
e00c7b21 | 45 | fclose(fp); |
e00c7b21 DB |
46 | |
47 | return possible_cpus; | |
48 | } | |
49 | ||
f3515b5d DB |
50 | #define __bpf_percpu_val_align __attribute__((__aligned__(8))) |
51 | ||
52 | #define BPF_DECLARE_PERCPU(type, name) \ | |
53 | struct { type v; /* padding */ } __bpf_percpu_val_align \ | |
54 | name[bpf_num_possible_cpus()] | |
55 | #define bpf_percpu(name, cpu) name[(cpu)].v | |
56 | ||
aa5f0c96 MKL |
57 | #ifndef ARRAY_SIZE |
58 | # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
59 | #endif | |
60 | ||
e00c7b21 | 61 | #endif /* __BPF_UTIL__ */ |