Commit | Line | Data |
---|---|---|
a6352fd4 MD |
1 | #ifndef _LIBRINGBUFFER_SMP_H |
2 | #define _LIBRINGBUFFER_SMP_H | |
3 | ||
4 | /* | |
5 | * libringbuffer/smp.h | |
6 | * | |
e92f3e28 | 7 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
a6352fd4 | 8 | * |
e92f3e28 MD |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; only | |
12 | * version 2.1 of the License. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
a6352fd4 MD |
22 | */ |
23 | ||
2b2d6ff7 | 24 | #include "getcpu.h" |
a6352fd4 MD |
25 | |
26 | /* | |
27 | * 4kB of per-cpu data available. Enough to hold the control structures, | |
28 | * but not ring buffers. | |
29 | */ | |
30 | #define PER_CPU_MEM_SIZE 4096 | |
31 | ||
32 | extern int __num_possible_cpus; | |
23aee768 MJ |
33 | |
34 | /* | |
35 | * Get the CPU possible mask string from sysfs. | |
36 | * | |
37 | * buf: the buffer where the mask will be read. | |
38 | * max_bytes: the maximum number of bytes to write in the buffer. | |
39 | * | |
40 | * Returns the number of bytes read or -1 on error. | |
41 | */ | |
42 | int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) | |
43 | __attribute__((visibility("hidden"))); | |
44 | ||
45 | /* | |
46 | * Get the number of possible CPUs in the system from either | |
47 | * sysconf(_SC_NPROCESSORS_CONF) or some other mechanism depending on the libc. | |
48 | * | |
49 | * Returns the number of possible CPUs in the system or 0 on error. | |
50 | */ | |
51 | int get_num_possible_cpus_fallback(void) | |
52 | __attribute__((visibility("hidden"))); | |
53 | ||
54 | /* | |
55 | * Get the number of CPUs from the possible cpu mask. | |
56 | * | |
57 | * pmask: the mask to parse. | |
58 | * len: the len of the mask excluding '\0'. | |
59 | * | |
60 | * Returns the number of possible CPUs from the mask or 0 on error. | |
61 | */ | |
62 | int get_num_possible_cpus_from_mask(const char *pmask, size_t len) | |
63 | __attribute__((visibility("hidden"))); | |
64 | ||
a6352fd4 MD |
65 | extern void _get_num_possible_cpus(void); |
66 | ||
23aee768 MJ |
67 | /* |
68 | * Returns the total number of CPUs in the system. If the cache is not yet | |
69 | * initialized, get the value from "/sys/devices/system/cpu/possible" or | |
70 | * fallback to sysconf and cache it. | |
71 | * | |
72 | * If all methods fail, don't populate the cache and return 0. | |
73 | */ | |
a6352fd4 MD |
74 | static inline |
75 | int num_possible_cpus(void) | |
76 | { | |
23aee768 | 77 | if (caa_unlikely(!__num_possible_cpus)) |
a6352fd4 | 78 | _get_num_possible_cpus(); |
23aee768 | 79 | |
a6352fd4 MD |
80 | return __num_possible_cpus; |
81 | } | |
82 | ||
a6352fd4 MD |
83 | #define for_each_possible_cpu(cpu) \ |
84 | for ((cpu) = 0; (cpu) < num_possible_cpus(); (cpu)++) | |
85 | ||
86 | #endif /* _LIBRINGBUFFER_SMP_H */ |