Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/cputopology.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014-2015,2022 Davide Madrisan <[email protected]>
5 *
6 * A library for checking the CPU topology
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* A note on terminology
22 * ---------------------
23 * CPU The logical CPU number of a CPU as used by the Linux kernel.
24 * CORE The logical core number. A core can contain several CPUs.
25 * SOCKET The logical socket number. A socket can contain several cores.
26 * BOOK The logical book number. A book can contain several sockets.
27 * NODE The logical NUMA node number. A node may contain several books. */
28
29 #ifndef _GNU_SOURCE
30 # define _GNU_SOURCE /* activate extra prototypes for glibc */
31 #endif
32
33 #include <sys/sysinfo.h>
34
35 #include <ctype.h>
36 #include <sched.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "common.h"
42 #include "cputopology.h"
43 #include "sysfsparser.h"
44
45 #define PATH_SYS_SYSTEM PATH_SYS "/devices/system"
46 #define PATH_SYS_CPU PATH_SYS_SYSTEM "/cpu"
47
48 #if !HAVE_DECL_CPU_ALLOC
49 /* Please, use CPU_COUNT_S() macro. This is fallback */
50 int
51 __cpuset_count_s (size_t setsize, const cpu_set_t * set)
52 {
53 int s = 0;
54 const __cpu_mask *p = set->__bits;
55 const __cpu_mask *end = &set->__bits[setsize / sizeof (__cpu_mask)];
56
57 while (p < end)
58 {
59 __cpu_mask l = *p++;
60
61 if (l == 0)
62 continue;
63 #if LONG_BIT > 32
64 l = (l & 0x5555555555555555ul) + ((l >> 1) & 0x5555555555555555ul);
65 l = (l & 0x3333333333333333ul) + ((l >> 2) & 0x3333333333333333ul);
66 l = (l & 0x0f0f0f0f0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0f0f0f0f0ful);
67 l = (l & 0x00ff00ff00ff00fful) + ((l >> 8) & 0x00ff00ff00ff00fful);
68 l = (l & 0x0000ffff0000fffful) + ((l >> 16) & 0x0000ffff0000fffful);
69 l = (l & 0x00000000fffffffful) + ((l >> 32) & 0x00000000fffffffful);
70 #else
71 l = (l & 0x55555555ul) + ((l >> 1) & 0x55555555ul);
72 l = (l & 0x33333333ul) + ((l >> 2) & 0x33333333ul);
73 l = (l & 0x0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0ful);
74 l = (l & 0x00ff00fful) + ((l >> 8) & 0x00ff00fful);
75 l = (l & 0x0000fffful) + ((l >> 16) & 0x0000fffful);
76 #endif
77 s += l;
78 }
79 return s;
80 }
81 #endif
82
83 /* Get the number of total and active cpus */
84
85 /* Note! On Linux systems with glibc,
86 * sysconf (_SC_NPROCESSORS_CONF)
87 * sysconf (_SC_NPROCESSORS_ONLN)
88 * come from the /sys and /proc file systems (see
89 * glibc/sysdeps/unix/sysv/linux/getsysstats.c).
90 * In some situations these file systems are not mounted, and the sysconf
91 * call returns 1, which does not reflect the reality. */
92
93 int
94 get_processor_number_total (void)
95 {
96 /* The number of CPUs configured in the system. */
97 return
98 #if defined (HAVE_GET_NPROCS_CONF)
99 get_nprocs_conf ();
100 #elif defined (HAVE_SYSCONF__SC_NPROCESSORS_CONF)
101 sysconf (_SC_NPROCESSORS_CONF);
102 #else
103 -1;
104 #endif
105 }
106
107 int
108 get_processor_number_online (void)
109 {
110 /* The number of CPUs available to the scheduler. */
111 return
112 #if defined (HAVE_GET_NPROCS)
113 get_nprocs ();
114 #elif defined (HAVE_SYSCONF__SC_NPROCESSORS_ONLN)
115 sysconf (_SC_NPROCESSORS_ONLN);
116 #else
117 -1;
118 #endif
119 }
120
121 /* Get the maximum cpu index allowed by the kernel configuration. */
122
123 int
124 get_processor_number_kernel_max ()
125 {
126 unsigned long long value;
127
128 sysfsparser_getvalue (&value, PATH_SYS_CPU "/kernel_max");
129 return value + 1;
130 }
131
132 static inline int
133 char_to_val (int c)
134 {
135 int cl;
136
137 cl = tolower (c);
138 if (c >= '0' && c <= '9')
139 return c - '0';
140 else if (cl >= 'a' && cl <= 'f')
141 return cl + (10 - 'a');
142 else
143 return -1;
144 }
145
146 /* Parses string with CPUs mask and return the number of CPUs present. */
147
148 static int
149 cpumask_parse (const char *str, cpu_set_t *set, size_t setsize)
150 {
151 const char *ptr;
152 int len, cpu = 0;
153
154 if (!str)
155 return -1;
156
157 len = strlen (str);
158 ptr = str + len - 1;
159
160 /* skip 0x, it's all hex anyway */
161 if (len > 1 && !memcmp (str, "0x", 2L))
162 str += 2;
163
164 CPU_ZERO_S (setsize, set);
165
166 while (ptr >= str)
167 {
168 char val;
169
170 /* cpu masks in /sys uses comma as a separator */
171 if (*ptr == ',')
172 ptr--;
173
174 val = char_to_val (*ptr);
175 if (val == (char) -1)
176 return -1;
177 if (val & 1)
178 CPU_SET_S (cpu, setsize, set);
179 if (val & 2)
180 CPU_SET_S (cpu + 1, setsize, set);
181 if (val & 4)
182 CPU_SET_S (cpu + 2, setsize, set);
183 if (val & 8)
184 CPU_SET_S (cpu + 3, setsize, set);
185 ptr--;
186 cpu += 4;
187 }
188
189 return CPU_COUNT_S (setsize, set);
190 }
191
192 /* Get the number of threads within one core */
193
194 void
195 get_cputopology_read (unsigned int *nsockets, unsigned int *ncores,
196 unsigned int *nthreads)
197 {
198 cpu_set_t *set;
199 size_t cpu, /* most 32 bit architectures use "unsigned int" size_t,
200 * and all 64 bit architectures use "unsigned long" size_t */
201 maxcpus, setsize = 0;
202
203 *nsockets = *ncores = *nthreads = 1;
204 maxcpus = get_processor_number_kernel_max ();
205
206 if (!(set = CPU_ALLOC (maxcpus)))
207 return;
208 setsize = CPU_ALLOC_SIZE (maxcpus);
209
210 for (cpu = 0; cpu < maxcpus; cpu++)
211 {
212 /* thread_siblings: internal kernel map of cpu#'s hardware threads
213 * within the same core as cpu# */
214 char *thread_siblings =
215 sysfsparser_getline (PATH_SYS_CPU "/cpu%lu/topology/thread_siblings",
216 (unsigned long)cpu);
217 if (!thread_siblings)
218 continue;
219
220 /* threads within one core */
221 *nthreads = cpumask_parse (thread_siblings, set, setsize);
222 if (*nthreads == 0)
223 *nthreads = 1;
224
225 /* core_siblings: internal kernel map of cpu#'s hardware threads
226 * within the same physical_package_id. */
227 char *core_siblings =
228 sysfsparser_getline (PATH_SYS_CPU "/cpu%lu/topology/core_siblings",
229 (unsigned long)cpu);
230 /* cores within one socket */
231 *ncores = cpumask_parse (core_siblings, set, setsize) / *nthreads;
232 if (*ncores == 0)
233 *ncores = 1;
234
235 int ncpus_online = get_processor_number_online ();
236 unsigned int ncpus = ncpus_online > 0 ? ncpus_online : 0;
237
238 *nsockets = ncpus / *nthreads / *ncores;
239 if (!*nsockets)
240 *nsockets = 1;
241
242 free (core_siblings);
243 free (thread_siblings);
244 }
245
246 CPU_FREE (set);
247 }