Linux Audio

Check our new training course

Loading...
v6.8
 1/* SPDX-License-Identifier: GPL-2.0 */
 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#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
10
11static inline unsigned int bpf_num_possible_cpus(void)
12{
13	int possible_cpus = libbpf_num_possible_cpus();
14
15	if (possible_cpus < 0) {
16		printf("Failed to get # of possible cpus: '%s'!\n",
17		       strerror(-possible_cpus));
 
 
 
18		exit(1);
19	}
20	return possible_cpus;
21}
22
23/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
24 * is zero-terminated string no matter what (unless sz == 0, in which case
25 * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
26 * in what is returned. Given this is internal helper, it's trivial to extend
27 * this, when necessary. Use this instead of strncpy inside libbpf source code.
28 */
29static inline void bpf_strlcpy(char *dst, const char *src, size_t sz)
30{
31	size_t i;
32
33	if (sz == 0)
34		return;
 
 
 
35
36	sz--;
37	for (i = 0; i < sz && src[i]; i++)
38		dst[i] = src[i];
39	dst[i] = '\0';
40}
41
42#define __bpf_percpu_val_align	__attribute__((__aligned__(8)))
43
44#define BPF_DECLARE_PERCPU(type, name)				\
45	struct { type v; /* padding */ } __bpf_percpu_val_align	\
46		name[bpf_num_possible_cpus()]
47#define bpf_percpu(name, cpu) name[(cpu)].v
48
49#ifndef ARRAY_SIZE
50# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
51#endif
52
53#ifndef sizeof_field
54#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
55#endif
56
57#ifndef offsetofend
58#define offsetofend(TYPE, MEMBER) \
59	(offsetof(TYPE, MEMBER)	+ sizeof_field(TYPE, MEMBER))
60#endif
61
62#endif /* __BPF_UTIL__ */
v4.10.11
 
 1#ifndef __BPF_UTIL__
 2#define __BPF_UTIL__
 3
 4#include <stdio.h>
 5#include <stdlib.h>
 6#include <string.h>
 7#include <errno.h>
 
 8
 9static inline unsigned int bpf_num_possible_cpus(void)
10{
11	static const char *fcpu = "/sys/devices/system/cpu/possible";
12	unsigned int start, end, possible_cpus = 0;
13	char buff[128];
14	FILE *fp;
15
16	fp = fopen(fcpu, "r");
17	if (!fp) {
18		printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
19		exit(1);
20	}
 
 
21
22	while (fgets(buff, sizeof(buff), fp)) {
23		if (sscanf(buff, "%u-%u", &start, &end) == 2) {
24			possible_cpus = start == 0 ? end + 1 : 0;
25			break;
26		}
27	}
 
 
 
28
29	fclose(fp);
30	if (!possible_cpus) {
31		printf("Failed to retrieve # possible CPUs!\n");
32		exit(1);
33	}
34
35	return possible_cpus;
 
 
 
36}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
38#endif /* __BPF_UTIL__ */