]>
Commit | Line | Data |
---|---|---|
5dd7507c CQ |
1 | /* |
2 | * lscpu - CPU architecture information helper | |
3 | * | |
4 | * Copyright (C) 2008 Cai Qian <qcai@redhat.com> | |
5 | * Copyright (C) 2008 Karel Zak <kzak@redhat.com> | |
6 | * | |
80dde62f | 7 | * This program is free software; you can redistribute it and/or modify |
5dd7507c | 8 | * it under the terms of the GNU General Public License as published by |
80dde62f | 9 | * the Free Software Foundation; either version 2 of the License, or |
5dd7507c CQ |
10 | * (at your option) any later version. |
11 | * | |
80dde62f | 12 | * This program is distributed in the hope that it would be useful, |
5dd7507c CQ |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
7cebf0bb SK |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
5dd7507c CQ |
20 | */ |
21 | ||
5bd31c6d | 22 | #include <assert.h> |
5dd7507c CQ |
23 | #include <ctype.h> |
24 | #include <dirent.h> | |
5dd7507c CQ |
25 | #include <errno.h> |
26 | #include <fcntl.h> | |
27 | #include <getopt.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/utsname.h> | |
32 | #include <unistd.h> | |
33 | #include <stdarg.h> | |
39561c70 KZ |
34 | #include <sys/types.h> |
35 | #include <sys/stat.h> | |
32865bd5 | 36 | #include <sys/personality.h> |
5dd7507c | 37 | |
6d9b1c19 KZ |
38 | #if (defined(__x86_64__) || defined(__i386__)) |
39 | # if !defined( __SANITIZE_ADDRESS__) | |
40 | # define INCLUDE_VMWARE_BDOOR | |
41 | # else | |
42 | # warning VMWARE detection disabled by __SANITIZE_ADDRESS__ | |
43 | # endif | |
b7744730 RM |
44 | #endif |
45 | ||
46 | #ifdef INCLUDE_VMWARE_BDOOR | |
47 | # include <stdint.h> | |
48 | # include <signal.h> | |
49 | # include <strings.h> | |
50 | # include <setjmp.h> | |
65322517 | 51 | # ifdef HAVE_SYS_IO_H |
b7744730 RM |
52 | # include <sys/io.h> |
53 | # endif | |
54 | #endif | |
55 | ||
bd9b94d1 SB |
56 | #if defined(HAVE_LIBRTAS) |
57 | #include <librtas.h> | |
58 | #endif | |
59 | ||
83db4eb2 OO |
60 | #include <libsmartcols.h> |
61 | ||
efb8854f | 62 | #include "closestream.h" |
41a8940d | 63 | #include "optutils.h" |
71061694 | 64 | |
fb2627ce | 65 | #include "lscpu.h" |
477251f8 | 66 | |
5dd7507c CQ |
67 | #define CACHE_MAX 100 |
68 | ||
69 | /* /sys paths */ | |
d50363cd | 70 | #define _PATH_SYS_SYSTEM "/sys/devices/system" |
6e509042 | 71 | #define _PATH_SYS_HYP_FEATURES "/sys/hypervisor/properties/features" |
7e03f383 | 72 | #define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu" |
e9074a16 | 73 | #define _PATH_SYS_NODE _PATH_SYS_SYSTEM "/node" |
5dd7507c | 74 | |
0ebbe9f1 DZ |
75 | /* Xen Domain feature flag used for /sys/hypervisor/properties/features */ |
76 | #define XENFEAT_supervisor_mode_kernel 3 | |
77 | #define XENFEAT_mmu_pt_update_preserve_ad 5 | |
78 | #define XENFEAT_hvm_callback_vector 8 | |
79 | ||
80 | #define XEN_FEATURES_PV_MASK (1U << XENFEAT_mmu_pt_update_preserve_ad) | |
81 | #define XEN_FEATURES_PVH_MASK ( (1U << XENFEAT_supervisor_mode_kernel) \ | |
82 | | (1U << XENFEAT_hvm_callback_vector) ) | |
83 | ||
2ba641e5 | 84 | static const char *virt_types[] = { |
c8b64f6d KZ |
85 | [VIRT_NONE] = N_("none"), |
86 | [VIRT_PARA] = N_("para"), | |
857d9cdf KZ |
87 | [VIRT_FULL] = N_("full"), |
88 | [VIRT_CONT] = N_("container"), | |
c8b64f6d KZ |
89 | }; |
90 | ||
2ba641e5 | 91 | static const char *hv_vendors[] = { |
c8b64f6d KZ |
92 | [HYPER_NONE] = NULL, |
93 | [HYPER_XEN] = "Xen", | |
94 | [HYPER_KVM] = "KVM", | |
99fbc877 | 95 | [HYPER_MSHV] = "Microsoft", |
b8ec7bdf | 96 | [HYPER_VMWARE] = "VMware", |
fb2627ce OO |
97 | [HYPER_IBM] = "IBM", |
98 | [HYPER_VSERVER] = "Linux-VServer", | |
99 | [HYPER_UML] = "User-mode Linux", | |
100 | [HYPER_INNOTEK] = "Innotek GmbH", | |
101 | [HYPER_HITACHI] = "Hitachi", | |
96ce475f RM |
102 | [HYPER_PARALLELS] = "Parallels", |
103 | [HYPER_VBOX] = "Oracle", | |
5bd31c6d RM |
104 | [HYPER_OS400] = "OS/400", |
105 | [HYPER_PHYP] = "pHyp", | |
7572fb2b SB |
106 | [HYPER_SPAR] = "Unisys s-Par", |
107 | [HYPER_WSL] = "Windows Subsystem for Linux" | |
96ce475f RM |
108 | }; |
109 | ||
2ba641e5 | 110 | static const int hv_vendor_pci[] = { |
96ce475f RM |
111 | [HYPER_NONE] = 0x0000, |
112 | [HYPER_XEN] = 0x5853, | |
113 | [HYPER_KVM] = 0x0000, | |
114 | [HYPER_MSHV] = 0x1414, | |
115 | [HYPER_VMWARE] = 0x15ad, | |
116 | [HYPER_VBOX] = 0x80ee, | |
117 | }; | |
118 | ||
2ba641e5 | 119 | static const int hv_graphics_pci[] = { |
96ce475f RM |
120 | [HYPER_NONE] = 0x0000, |
121 | [HYPER_XEN] = 0x0001, | |
122 | [HYPER_KVM] = 0x0000, | |
123 | [HYPER_MSHV] = 0x5353, | |
124 | [HYPER_VMWARE] = 0x0710, | |
125 | [HYPER_VBOX] = 0xbeef, | |
c8b64f6d KZ |
126 | }; |
127 | ||
e8aa16ee | 128 | |
a0fff77e | 129 | /* dispatching modes */ |
2ba641e5 | 130 | static const char *disp_modes[] = { |
a0fff77e HC |
131 | [DISP_HORIZONTAL] = N_("horizontal"), |
132 | [DISP_VERTICAL] = N_("vertical") | |
133 | }; | |
134 | ||
2ba641e5 | 135 | static struct polarization_modes polar_modes[] = { |
8005924a KZ |
136 | [POLAR_UNKNOWN] = {"U", "-"}, |
137 | [POLAR_VLOW] = {"VL", "vert-low"}, | |
138 | [POLAR_VMEDIUM] = {"VM", "vert-medium"}, | |
139 | [POLAR_VHIGH] = {"VH", "vert-high"}, | |
140 | [POLAR_HORIZONTAL] = {"H", "horizontal"}, | |
2b8fcb81 HC |
141 | }; |
142 | ||
7e03f383 | 143 | static int maxcpus; /* size in bits of kernel cpu mask */ |
5dd7507c | 144 | |
aac1e59e | 145 | #define is_cpu_online(_d, _cpu) \ |
a5cfffff TK |
146 | ((_d) && (_d)->online ? \ |
147 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0) | |
148 | #define is_cpu_present(_d, _cpu) \ | |
149 | ((_d) && (_d)->present ? \ | |
150 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->present) : 0) | |
aac1e59e | 151 | |
4f642863 AT |
152 | #define real_cpu_num(_d, _i) ((_d)->idx2cpunum[(_i)]) |
153 | ||
477251f8 | 154 | /* |
3d27b76a | 155 | * IDs |
477251f8 KZ |
156 | */ |
157 | enum { | |
158 | COL_CPU, | |
159 | COL_CORE, | |
160 | COL_SOCKET, | |
161 | COL_NODE, | |
162 | COL_BOOK, | |
b3adf6ef | 163 | COL_DRAWER, |
2b8fcb81 | 164 | COL_CACHE, |
596b8845 | 165 | COL_POLARIZATION, |
d231eea1 HC |
166 | COL_ADDRESS, |
167 | COL_CONFIGURED, | |
a7e5300c | 168 | COL_ONLINE, |
e065a597 DB |
169 | COL_MAXMHZ, |
170 | COL_MINMHZ, | |
477251f8 KZ |
171 | }; |
172 | ||
3d27b76a KZ |
173 | /* column description |
174 | */ | |
175 | struct lscpu_coldesc { | |
176 | const char *name; | |
177 | const char *help; | |
178 | ||
179 | unsigned int is_abbr:1; /* name is abbreviation */ | |
477251f8 KZ |
180 | }; |
181 | ||
3d27b76a KZ |
182 | static struct lscpu_coldesc coldescs[] = |
183 | { | |
184 | [COL_CPU] = { "CPU", N_("logical CPU number"), 1 }, | |
185 | [COL_CORE] = { "CORE", N_("logical core number") }, | |
186 | [COL_SOCKET] = { "SOCKET", N_("logical socket number") }, | |
187 | [COL_NODE] = { "NODE", N_("logical NUMA node number") }, | |
188 | [COL_BOOK] = { "BOOK", N_("logical book number") }, | |
b3adf6ef | 189 | [COL_DRAWER] = { "DRAWER", N_("logical drawer number") }, |
3d27b76a KZ |
190 | [COL_CACHE] = { "CACHE", N_("shows how caches are shared between CPUs") }, |
191 | [COL_POLARIZATION] = { "POLARIZATION", N_("CPU dispatching mode on virtual hardware") }, | |
192 | [COL_ADDRESS] = { "ADDRESS", N_("physical address of a CPU") }, | |
193 | [COL_CONFIGURED] = { "CONFIGURED", N_("shows if the hypervisor has allocated the CPU") }, | |
44320710 | 194 | [COL_ONLINE] = { "ONLINE", N_("shows if Linux currently makes use of the CPU") }, |
4df28845 BS |
195 | [COL_MAXMHZ] = { "MAXMHZ", N_("shows the maximum MHz of the CPU") }, |
196 | [COL_MINMHZ] = { "MINMHZ", N_("shows the minimum MHz of the CPU") } | |
3d27b76a | 197 | }; |
477251f8 | 198 | |
583f14cc KZ |
199 | static int |
200 | column_name_to_id(const char *name, size_t namesz) | |
477251f8 | 201 | { |
329fd1c3 | 202 | size_t i; |
477251f8 | 203 | |
3d27b76a KZ |
204 | for (i = 0; i < ARRAY_SIZE(coldescs); i++) { |
205 | const char *cn = coldescs[i].name; | |
477251f8 KZ |
206 | |
207 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
208 | return i; | |
209 | } | |
210 | warnx(_("unknown column: %s"), name); | |
211 | return -1; | |
212 | } | |
213 | ||
5dd7507c CQ |
214 | /* Lookup a pattern and get the value from cpuinfo. |
215 | * Format is: | |
216 | * | |
217 | * "<pattern> : <key>" | |
218 | */ | |
583f14cc KZ |
219 | static int |
220 | lookup(char *line, char *pattern, char **value) | |
5dd7507c CQ |
221 | { |
222 | char *p, *v; | |
223 | int len = strlen(pattern); | |
224 | ||
e5fc6d6f RM |
225 | /* don't re-fill already found tags, first one wins */ |
226 | if (!*line || *value) | |
5dd7507c CQ |
227 | return 0; |
228 | ||
229 | /* pattern */ | |
230 | if (strncmp(line, pattern, len)) | |
231 | return 0; | |
232 | ||
233 | /* white spaces */ | |
234 | for (p = line + len; isspace(*p); p++); | |
235 | ||
236 | /* separator */ | |
237 | if (*p != ':') | |
238 | return 0; | |
239 | ||
240 | /* white spaces */ | |
241 | for (++p; isspace(*p); p++); | |
242 | ||
243 | /* value */ | |
244 | if (!*p) | |
245 | return 0; | |
246 | v = p; | |
247 | ||
248 | /* end of value */ | |
249 | len = strlen(line) - 1; | |
250 | for (p = line + len; isspace(*(p-1)); p--); | |
251 | *p = '\0'; | |
252 | ||
253 | *value = xstrdup(v); | |
254 | return 1; | |
255 | } | |
256 | ||
28b1658f HC |
257 | /* Parse extra cache lines contained within /proc/cpuinfo but which are not |
258 | * part of the cache topology information within the sysfs filesystem. | |
259 | * This is true for all shared caches on e.g. s390. When there are layers of | |
260 | * hypervisors in between it is not knows which CPUs share which caches. | |
261 | * Therefore information about shared caches is only available in | |
262 | * /proc/cpuinfo. | |
263 | * Format is: | |
264 | * "cache<nr> : level=<lvl> type=<type> scope=<scope> size=<size> line_size=<lsz> associativity=<as>" | |
265 | */ | |
266 | static int | |
267 | lookup_cache(char *line, struct lscpu_desc *desc) | |
268 | { | |
269 | struct cpu_cache *cache; | |
270 | long long size; | |
271 | char *p, type; | |
272 | int level; | |
273 | ||
274 | /* Make sure line starts with "cache<nr> :" */ | |
275 | if (strncmp(line, "cache", 5)) | |
276 | return 0; | |
277 | for (p = line + 5; isdigit(*p); p++); | |
278 | for (; isspace(*p); p++); | |
279 | if (*p != ':') | |
280 | return 0; | |
281 | ||
282 | p = strstr(line, "scope=") + 6; | |
283 | /* Skip private caches, also present in sysfs */ | |
284 | if (!p || strncmp(p, "Private", 7) == 0) | |
285 | return 0; | |
286 | p = strstr(line, "level="); | |
81a307bd KZ |
287 | if (!p || sscanf(p, "level=%d", &level) != 1) |
288 | return 0; | |
28b1658f | 289 | p = strstr(line, "type=") + 5; |
81a307bd KZ |
290 | if (!p || !*p) |
291 | return 0; | |
28b1658f HC |
292 | type = 0; |
293 | if (strncmp(p, "Data", 4) == 0) | |
294 | type = 'd'; | |
295 | if (strncmp(p, "Instruction", 11) == 0) | |
296 | type = 'i'; | |
297 | p = strstr(line, "size="); | |
81a307bd KZ |
298 | if (!p || sscanf(p, "size=%lld", &size) != 1) |
299 | return 0; | |
300 | ||
28b1658f HC |
301 | desc->necaches++; |
302 | desc->ecaches = xrealloc(desc->ecaches, | |
303 | desc->necaches * sizeof(struct cpu_cache)); | |
304 | cache = &desc->ecaches[desc->necaches - 1]; | |
305 | memset(cache, 0 , sizeof(*cache)); | |
306 | if (type) | |
307 | xasprintf(&cache->name, "L%d%c", level, type); | |
308 | else | |
309 | xasprintf(&cache->name, "L%d", level); | |
310 | xasprintf(&cache->size, "%lldK", size); | |
311 | return 1; | |
312 | } | |
313 | ||
f633ad4e KZ |
314 | /* Don't init the mode for platforms where we are not able to |
315 | * detect that CPU supports 64-bit mode. | |
316 | */ | |
317 | static int | |
8148217b | 318 | init_mode(struct lscpu_modifier *mod) |
f633ad4e KZ |
319 | { |
320 | int m = 0; | |
321 | ||
8148217b | 322 | if (mod->system == SYSTEM_SNAPSHOT) |
4e740fd8 KZ |
323 | /* reading info from any /{sys,proc} dump, don't mix it with |
324 | * information about our real CPU */ | |
325 | return 0; | |
326 | ||
f633ad4e KZ |
327 | #if defined(__alpha__) || defined(__ia64__) |
328 | m |= MODE_64BIT; /* 64bit platforms only */ | |
329 | #endif | |
330 | /* platforms with 64bit flag in /proc/cpuinfo, define | |
331 | * 32bit default here */ | |
332 | #if defined(__i386__) || defined(__x86_64__) || \ | |
c487c90c | 333 | defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) |
f633ad4e KZ |
334 | m |= MODE_32BIT; |
335 | #endif | |
32865bd5 JL |
336 | |
337 | #if defined(__aarch64__) | |
338 | { | |
339 | /* personality() is the most reliable way (since 4.7) | |
340 | * to determine aarch32 support */ | |
341 | int pers = personality(PER_LINUX32); | |
342 | if (pers != -1) { | |
343 | personality(pers); | |
344 | m |= MODE_32BIT; | |
345 | } | |
346 | m |= MODE_64BIT; | |
347 | } | |
348 | #endif | |
f633ad4e KZ |
349 | return m; |
350 | } | |
351 | ||
bd9b94d1 SB |
352 | #if defined(HAVE_LIBRTAS) |
353 | #define PROCESSOR_MODULE_INFO 43 | |
354 | static int strbe16toh(const char *buf, int offset) | |
355 | { | |
356 | return (buf[offset] << 8) + buf[offset+1]; | |
357 | } | |
358 | ||
359 | static void read_physical_info_powerpc(struct lscpu_desc *desc) | |
360 | { | |
361 | char buf[BUFSIZ]; | |
362 | int rc, len, ntypes; | |
363 | ||
364 | desc->physsockets = desc->physchips = desc->physcoresperchip = 0; | |
365 | ||
366 | rc = rtas_get_sysparm(PROCESSOR_MODULE_INFO, sizeof(buf), buf); | |
367 | if (rc < 0) | |
368 | return; | |
369 | ||
370 | len = strbe16toh(buf, 0); | |
371 | if (len < 8) | |
372 | return; | |
373 | ||
374 | ntypes = strbe16toh(buf, 2); | |
375 | ||
376 | assert(ntypes <= 1); | |
377 | if (!ntypes) | |
378 | return; | |
379 | ||
380 | desc->physsockets = strbe16toh(buf, 4); | |
381 | desc->physchips = strbe16toh(buf, 6); | |
382 | desc->physcoresperchip = strbe16toh(buf, 8); | |
383 | } | |
384 | #else | |
385 | static void read_physical_info_powerpc( | |
386 | struct lscpu_desc *desc __attribute__((__unused__))) | |
387 | { | |
388 | } | |
389 | #endif | |
390 | ||
744d62ee | 391 | |
5dd7507c | 392 | static void |
8148217b | 393 | read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
5dd7507c | 394 | { |
6e509042 | 395 | FILE *fp; |
5dd7507c CQ |
396 | char buf[BUFSIZ]; |
397 | struct utsname utsbuf; | |
a5cfffff | 398 | size_t setsize; |
6e509042 | 399 | cpu_set_t *cpuset = NULL; |
5dd7507c CQ |
400 | |
401 | /* architecture */ | |
402 | if (uname(&utsbuf) == -1) | |
403 | err(EXIT_FAILURE, _("error: uname failed")); | |
6e509042 KZ |
404 | |
405 | fp = ul_path_fopen(desc->procfs, "r", "cpuinfo"); | |
406 | if (!fp) | |
407 | err(EXIT_FAILURE, _("cannot open %s"), "/proc/cpuinfo"); | |
e8aa16ee | 408 | desc->arch = xstrdup(utsbuf.machine); |
5dd7507c | 409 | |
5dd7507c CQ |
410 | /* details */ |
411 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
e8aa16ee KZ |
412 | if (lookup(buf, "vendor", &desc->vendor)) ; |
413 | else if (lookup(buf, "vendor_id", &desc->vendor)) ; | |
744d62ee | 414 | else if (lookup(buf, "CPU implementer", &desc->vendor)) ; /* ARM and aarch64 */ |
e8aa16ee KZ |
415 | else if (lookup(buf, "family", &desc->family)) ; |
416 | else if (lookup(buf, "cpu family", &desc->family)) ; | |
86c4817e | 417 | else if (lookup(buf, "model", &desc->model)) ; |
744d62ee | 418 | else if (lookup(buf, "CPU part", &desc->model)) ; /* ARM and aarch64 */ |
86c4817e | 419 | else if (lookup(buf, "model name", &desc->modelname)) ; |
e8aa16ee | 420 | else if (lookup(buf, "stepping", &desc->stepping)) ; |
744d62ee | 421 | else if (lookup(buf, "CPU variant", &desc->stepping)) ; /* aarch64 */ |
e8aa16ee | 422 | else if (lookup(buf, "cpu MHz", &desc->mhz)) ; |
4632b288 HC |
423 | else if (lookup(buf, "cpu MHz dynamic", &desc->dynamic_mhz)) ; /* s390 */ |
424 | else if (lookup(buf, "cpu MHz static", &desc->static_mhz)) ; /* s390 */ | |
f633ad4e KZ |
425 | else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */ |
426 | else if (lookup(buf, "features", &desc->flags)) ; /* s390 */ | |
a3c455ac | 427 | else if (lookup(buf, "Features", &desc->flags)) ; /* aarch64 */ |
c487c90c | 428 | else if (lookup(buf, "type", &desc->flags)) ; /* sparc64 */ |
9b8d4d5f | 429 | else if (lookup(buf, "bogomips", &desc->bogomips)) ; |
a3c455ac | 430 | else if (lookup(buf, "BogoMIPS", &desc->bogomips)) ; /* aarch64 */ |
abcd6368 | 431 | else if (lookup(buf, "bogomips per cpu", &desc->bogomips)) ; /* s390 */ |
c95e3889 RM |
432 | else if (lookup(buf, "cpu", &desc->cpu)) ; |
433 | else if (lookup(buf, "revision", &desc->revision)) ; | |
a3c455ac | 434 | else if (lookup(buf, "CPU revision", &desc->revision)) ; /* aarch64 */ |
2c497d32 | 435 | else if (lookup(buf, "max thread id", &desc->mtid)) ; /* s390 */ |
3e48ef72 | 436 | else if (lookup(buf, "address sizes", &desc->addrsz)) ; /* x86 */ |
28b1658f | 437 | else if (lookup_cache(buf, desc)) ; |
5dd7507c CQ |
438 | else |
439 | continue; | |
440 | } | |
c8b64f6d | 441 | |
8148217b | 442 | desc->mode = init_mode(mod); |
f633ad4e | 443 | |
e8aa16ee KZ |
444 | if (desc->flags) { |
445 | snprintf(buf, sizeof(buf), " %s ", desc->flags); | |
c8b64f6d | 446 | if (strstr(buf, " svm ")) |
8290a249 | 447 | desc->virtflag = xstrdup("svm"); |
c8b64f6d | 448 | else if (strstr(buf, " vmx ")) |
8290a249 | 449 | desc->virtflag = xstrdup("vmx"); |
79e8b41a | 450 | if (strstr(buf, " lm ")) |
f633ad4e KZ |
451 | desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */ |
452 | if (strstr(buf, " zarch ")) | |
453 | desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */ | |
c487c90c KZ |
454 | if (strstr(buf, " sun4v ") || strstr(buf, " sun4u ")) |
455 | desc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */ | |
c8b64f6d KZ |
456 | } |
457 | ||
4581b716 KZ |
458 | if (desc->arch && mod->system != SYSTEM_SNAPSHOT) { |
459 | if (strcmp(desc->arch, "ppc64") == 0) | |
460 | desc->mode |= MODE_32BIT | MODE_64BIT; | |
461 | else if (strcmp(desc->arch, "ppc") == 0) | |
462 | desc->mode |= MODE_32BIT; | |
463 | } | |
464 | ||
5dd7507c | 465 | fclose(fp); |
7e03f383 | 466 | |
6e509042 | 467 | if (ul_path_read_s32(desc->syscpu, &maxcpus, "kernel_max") == 0) |
3d6e5c35 | 468 | /* note that kernel_max is maximum index [NR_CPUS-1] */ |
6e509042 | 469 | maxcpus += 1; |
7e03f383 | 470 | |
8148217b | 471 | else if (mod->system == SYSTEM_LIVE) |
7e03f383 KZ |
472 | /* the root is '/' so we are working with data from the current kernel */ |
473 | maxcpus = get_max_number_of_cpus(); | |
701e2b8e KZ |
474 | |
475 | if (maxcpus <= 0) | |
476 | /* error or we are reading some /sys snapshot instead of the | |
477 | * real /sys, let's use any crazy number... */ | |
a5cfffff TK |
478 | maxcpus = 2048; |
479 | ||
480 | setsize = CPU_ALLOC_SIZE(maxcpus); | |
481 | ||
6e509042 | 482 | if (ul_path_readf_cpulist(desc->syscpu, &cpuset, maxcpus, "possible") == 0) { |
4f642863 AT |
483 | int num, idx; |
484 | ||
6e509042 | 485 | desc->ncpuspos = CPU_COUNT_S(setsize, cpuset); |
4f642863 AT |
486 | desc->idx2cpunum = xcalloc(desc->ncpuspos, sizeof(int)); |
487 | ||
488 | for (num = 0, idx = 0; num < maxcpus; num++) { | |
6e509042 | 489 | if (CPU_ISSET_S(num, setsize, cpuset)) |
4f642863 AT |
490 | desc->idx2cpunum[idx++] = num; |
491 | } | |
6e509042 KZ |
492 | cpuset_free(cpuset); |
493 | cpuset = NULL; | |
a5cfffff TK |
494 | } else |
495 | err(EXIT_FAILURE, _("failed to determine number of CPUs: %s"), | |
1d56b55e | 496 | _PATH_SYS_CPU "/possible"); |
a5cfffff TK |
497 | |
498 | ||
499 | /* get mask for present CPUs */ | |
6e509042 | 500 | if (ul_path_readf_cpulist(desc->syscpu, &desc->present, maxcpus, "present") == 0) |
a5cfffff | 501 | desc->ncpus = CPU_COUNT_S(setsize, desc->present); |
aac1e59e KZ |
502 | |
503 | /* get mask for online CPUs */ | |
6e509042 | 504 | if (ul_path_readf_cpulist(desc->syscpu, &desc->online, maxcpus, "online") == 0) |
e282eec2 | 505 | desc->nthreads = CPU_COUNT_S(setsize, desc->online); |
a0fff77e HC |
506 | |
507 | /* get dispatching mode */ | |
6e509042 | 508 | if (ul_path_read_s32(desc->syscpu, &desc->dispatching, "dispatching") != 0) |
a0fff77e | 509 | desc->dispatching = -1; |
bd9b94d1 | 510 | |
16ca0551 KZ |
511 | /* get cpufreq boost mode */ |
512 | if (ul_path_read_s32(desc->syscpu, &desc->freqboost, "cpufreq/boost") != 0) | |
513 | desc->freqboost = -1; | |
514 | ||
bd9b94d1 SB |
515 | if (mod->system == SYSTEM_LIVE) |
516 | read_physical_info_powerpc(desc); | |
0c28f0c8 | 517 | |
6e509042 | 518 | if ((fp = ul_path_fopen(desc->procfs, "r", "sysinfo"))) { |
904ffe1f | 519 | while (fgets(buf, sizeof(buf), fp) != NULL && !desc->machinetype) |
0c28f0c8 | 520 | lookup(buf, "Type", &desc->machinetype); |
904ffe1f | 521 | fclose(fp); |
0c28f0c8 | 522 | } |
5dd7507c CQ |
523 | } |
524 | ||
c8b64f6d | 525 | static int |
6e509042 | 526 | has_pci_device(struct lscpu_desc *desc, unsigned int vendor, unsigned int device) |
c8b64f6d KZ |
527 | { |
528 | FILE *f; | |
13cf6fc8 | 529 | unsigned int num, fn, ven, dev; |
c8b64f6d KZ |
530 | int res = 1; |
531 | ||
6e509042 | 532 | f = ul_path_fopen(desc->procfs, "r", "bus/pci/devices"); |
c8b64f6d KZ |
533 | if (!f) |
534 | return 0; | |
535 | ||
536 | /* for more details about bus/pci/devices format see | |
537 | * drivers/pci/proc.c in linux kernel | |
538 | */ | |
539 | while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]", | |
540 | &num, &fn, &ven, &dev) == 4) { | |
541 | ||
542 | if (ven == vendor && dev == device) | |
543 | goto found; | |
544 | } | |
545 | ||
546 | res = 0; | |
547 | found: | |
548 | fclose(f); | |
549 | return res; | |
550 | } | |
551 | ||
552 | #if defined(__x86_64__) || defined(__i386__) | |
553 | ||
554 | /* | |
555 | * This CPUID leaf returns the information about the hypervisor. | |
556 | * EAX : maximum input value for CPUID supported by the hypervisor. | |
557 | * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware. | |
558 | */ | |
559 | #define HYPERVISOR_INFO_LEAF 0x40000000 | |
560 | ||
561 | static inline void | |
562 | cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, | |
563 | unsigned int *ecx, unsigned int *edx) | |
564 | { | |
c9239f23 MF |
565 | __asm__( |
566 | #if defined(__PIC__) && defined(__i386__) | |
567 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
7845b91d | 568 | "xchg %%ebx, %%esi;" |
c9239f23 | 569 | "cpuid;" |
7845b91d | 570 | "xchg %%esi, %%ebx;" |
c9239f23 MF |
571 | : "=S" (*ebx), |
572 | #else | |
573 | "cpuid;" | |
574 | : "=b" (*ebx), | |
575 | #endif | |
576 | "=a" (*eax), | |
c8b64f6d KZ |
577 | "=c" (*ecx), |
578 | "=d" (*edx) | |
bc54770d | 579 | : "1" (op), "c"(0)); |
c8b64f6d KZ |
580 | } |
581 | ||
582 | static void | |
e8aa16ee | 583 | read_hypervisor_cpuid(struct lscpu_desc *desc) |
c8b64f6d KZ |
584 | { |
585 | unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; | |
586 | char hyper_vendor_id[13]; | |
587 | ||
588 | memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id)); | |
589 | ||
590 | cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx); | |
591 | memcpy(hyper_vendor_id + 0, &ebx, 4); | |
592 | memcpy(hyper_vendor_id + 4, &ecx, 4); | |
593 | memcpy(hyper_vendor_id + 8, &edx, 4); | |
594 | hyper_vendor_id[12] = '\0'; | |
595 | ||
596 | if (!hyper_vendor_id[0]) | |
597 | return; | |
598 | ||
599 | if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12)) | |
e8aa16ee | 600 | desc->hyper = HYPER_XEN; |
c8b64f6d | 601 | else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9)) |
e8aa16ee | 602 | desc->hyper = HYPER_KVM; |
c8b64f6d | 603 | else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12)) |
e8aa16ee | 604 | desc->hyper = HYPER_MSHV; |
99fbc877 SH |
605 | else if (!strncmp("VMwareVMware", hyper_vendor_id, 12)) |
606 | desc->hyper = HYPER_VMWARE; | |
4597b692 BR |
607 | else if (!strncmp("UnisysSpar64", hyper_vendor_id, 12)) |
608 | desc->hyper = HYPER_SPAR; | |
c8b64f6d KZ |
609 | } |
610 | ||
b7744730 | 611 | #else /* ! (__x86_64__ || __i386__) */ |
c8b64f6d | 612 | static void |
6d791b4c | 613 | read_hypervisor_cpuid(struct lscpu_desc *desc __attribute__((__unused__))) |
c8b64f6d KZ |
614 | { |
615 | } | |
616 | #endif | |
617 | ||
6e509042 | 618 | static int is_devtree_compatible(struct lscpu_desc *desc, const char *str) |
3565bd7a | 619 | { |
6e509042 | 620 | FILE *fd = ul_path_fopen(desc->procfs, "r", "device-tree/compatible"); |
3565bd7a | 621 | |
3565bd7a KZ |
622 | if (fd) { |
623 | char buf[256]; | |
624 | size_t i, len; | |
625 | ||
626 | memset(buf, 0, sizeof(buf)); | |
627 | len = fread(buf, 1, sizeof(buf) - 1, fd); | |
628 | fclose(fd); | |
629 | ||
630 | for (i = 0; i < len;) { | |
631 | if (!strcmp(&buf[i], str)) | |
632 | return 1; | |
633 | i += strlen(&buf[i]); | |
634 | i++; | |
635 | } | |
636 | } | |
637 | ||
638 | return 0; | |
639 | } | |
640 | ||
5bd31c6d RM |
641 | static int |
642 | read_hypervisor_powerpc(struct lscpu_desc *desc) | |
643 | { | |
644 | assert(!desc->hyper); | |
645 | ||
79738105 | 646 | /* IBM iSeries: legacy, para-virtualized on top of OS/400 */ |
6e509042 | 647 | if (ul_path_access(desc->procfs, F_OK, "iSeries") == 0) { |
5bd31c6d RM |
648 | desc->hyper = HYPER_OS400; |
649 | desc->virtype = VIRT_PARA; | |
79738105 KZ |
650 | |
651 | /* PowerNV (POWER Non-Virtualized, bare-metal) */ | |
6e509042 | 652 | } else if (is_devtree_compatible(desc, "ibm,powernv")) { |
e03b613e KZ |
653 | desc->hyper = HYPER_NONE; |
654 | desc->virtype = VIRT_NONE; | |
79738105 | 655 | |
d45984be | 656 | /* PowerVM (IBM's proprietary hypervisor, aka pHyp) */ |
6e509042 KZ |
657 | } else if (ul_path_access(desc->procfs, F_OK, "device-tree/ibm,partition-name") == 0 |
658 | && ul_path_access(desc->procfs, F_OK, "device-tree/hmc-managed?") == 0 | |
659 | && ul_path_access(desc->procfs, F_OK, "device-tree/chosen/qemu,graphic-width") != 0) { | |
660 | ||
5bd31c6d RM |
661 | FILE *fd; |
662 | desc->hyper = HYPER_PHYP; | |
663 | desc->virtype = VIRT_PARA; | |
6e509042 KZ |
664 | |
665 | fd = ul_path_fopen(desc->procfs, "r", "device-tree/ibm,partition-name"); | |
5bd31c6d RM |
666 | if (fd) { |
667 | char buf[256]; | |
6cbf75e5 | 668 | if (fscanf(fd, "%255s", buf) == 1 && !strcmp(buf, "full")) |
5bd31c6d RM |
669 | desc->virtype = VIRT_NONE; |
670 | fclose(fd); | |
671 | } | |
79738105 KZ |
672 | |
673 | /* Qemu */ | |
6e509042 | 674 | } else if (is_devtree_compatible(desc, "qemu,pseries")) { |
e03b613e KZ |
675 | desc->hyper = HYPER_KVM; |
676 | desc->virtype = VIRT_PARA; | |
5bd31c6d | 677 | } |
5bd31c6d RM |
678 | return desc->hyper; |
679 | } | |
680 | ||
b7744730 RM |
681 | #ifdef INCLUDE_VMWARE_BDOOR |
682 | ||
683 | #define VMWARE_BDOOR_MAGIC 0x564D5868 | |
684 | #define VMWARE_BDOOR_PORT 0x5658 | |
685 | #define VMWARE_BDOOR_CMD_GETVERSION 10 | |
686 | ||
77242032 | 687 | static UL_ASAN_BLACKLIST |
7845b91d MF |
688 | void vmware_bdoor(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) |
689 | { | |
690 | __asm__( | |
691 | #if defined(__PIC__) && defined(__i386__) | |
692 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
693 | "xchg %%ebx, %%esi;" | |
694 | "inl (%%dx), %%eax;" | |
695 | "xchg %%esi, %%ebx;" | |
696 | : "=S" (*ebx), | |
697 | #else | |
698 | "inl (%%dx), %%eax;" | |
699 | : "=b" (*ebx), | |
700 | #endif | |
701 | "=a" (*eax), | |
702 | "=c" (*ecx), | |
703 | "=d" (*edx) | |
704 | : "0" (VMWARE_BDOOR_MAGIC), | |
705 | "1" (VMWARE_BDOOR_CMD_GETVERSION), | |
706 | "2" (VMWARE_BDOOR_PORT), | |
707 | "3" (0) | |
708 | : "memory"); | |
709 | } | |
b7744730 RM |
710 | |
711 | static jmp_buf segv_handler_env; | |
712 | ||
713 | static void | |
714 | segv_handler(__attribute__((__unused__)) int sig, | |
715 | __attribute__((__unused__)) siginfo_t *info, | |
716 | __attribute__((__unused__)) void *ignored) | |
717 | { | |
718 | siglongjmp(segv_handler_env, 1); | |
719 | } | |
720 | ||
721 | static int | |
722 | is_vmware_platform(void) | |
723 | { | |
724 | uint32_t eax, ebx, ecx, edx; | |
725 | struct sigaction act, oact; | |
726 | ||
b5d61108 KZ |
727 | /* |
728 | * FIXME: Not reliable for non-root users. Note it works as expected if | |
729 | * vmware_bdoor() is not optimized for PIE, but then it fails to build | |
730 | * on 32bit x86 systems. See lscpu git log for more details (commit | |
731 | * 7845b91dbc7690064a2be6df690e4aaba728fb04). kzak [3-Nov-2016] | |
732 | */ | |
733 | if (getuid() != 0) | |
734 | return 0; | |
735 | ||
b7744730 RM |
736 | /* |
737 | * The assembly routine for vmware detection works | |
738 | * fine under vmware, even if ran as regular user. But | |
739 | * on real HW or under other hypervisors, it segfaults (which is | |
740 | * expected). So we temporarily install SIGSEGV handler to catch | |
741 | * the signal. All this magic is needed because lscpu | |
742 | * isn't supposed to require root privileges. | |
743 | */ | |
744 | if (sigsetjmp(segv_handler_env, 1)) | |
745 | return 0; | |
746 | ||
9dd2d497 | 747 | memset(&act, 0, sizeof(act)); |
b7744730 RM |
748 | act.sa_sigaction = segv_handler; |
749 | act.sa_flags = SA_SIGINFO; | |
750 | ||
751 | if (sigaction(SIGSEGV, &act, &oact)) | |
223939d9 | 752 | err(EXIT_FAILURE, _("cannot set signal handler")); |
b7744730 | 753 | |
7845b91d | 754 | vmware_bdoor(&eax, &ebx, &ecx, &edx); |
b7744730 RM |
755 | |
756 | if (sigaction(SIGSEGV, &oact, NULL)) | |
223939d9 | 757 | err(EXIT_FAILURE, _("cannot restore signal handler")); |
b7744730 RM |
758 | |
759 | return eax != (uint32_t)-1 && ebx == VMWARE_BDOOR_MAGIC; | |
760 | } | |
761 | ||
762 | #else /* ! INCLUDE_VMWARE_BDOOR */ | |
763 | ||
764 | static int | |
765 | is_vmware_platform(void) | |
766 | { | |
767 | return 0; | |
768 | } | |
769 | ||
770 | #endif /* INCLUDE_VMWARE_BDOOR */ | |
771 | ||
c8b64f6d | 772 | static void |
eff79ceb | 773 | read_hypervisor(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
c8b64f6d | 774 | { |
fb2627ce OO |
775 | FILE *fd; |
776 | ||
7572fb2b SB |
777 | /* We have to detect WSL first. is_vmware_platform() crashes on Windows 10. */ |
778 | ||
6e509042 | 779 | if ((fd = ul_path_fopen(desc->procfs, "r", "sys/kernel/osrelease"))) { |
7572fb2b SB |
780 | char buf[256]; |
781 | ||
782 | if (fgets(buf, sizeof(buf), fd) != NULL) { | |
783 | if (strstr(buf, "Microsoft")) { | |
784 | desc->hyper = HYPER_WSL; | |
785 | desc->virtype = VIRT_CONT; | |
786 | } | |
787 | } | |
788 | fclose(fd); | |
789 | if (desc->virtype) | |
790 | return; | |
791 | } | |
792 | ||
fb2627ce | 793 | if (mod->system != SYSTEM_SNAPSHOT) { |
eff79ceb | 794 | read_hypervisor_cpuid(desc); |
fb2627ce OO |
795 | if (!desc->hyper) |
796 | desc->hyper = read_hypervisor_dmi(); | |
b7744730 RM |
797 | if (!desc->hyper && is_vmware_platform()) |
798 | desc->hyper = HYPER_VMWARE; | |
fb2627ce | 799 | } |
c8b64f6d | 800 | |
0ebbe9f1 | 801 | if (desc->hyper) { |
e8aa16ee | 802 | desc->virtype = VIRT_FULL; |
c8b64f6d | 803 | |
b4d1fbda | 804 | if (desc->hyper == HYPER_XEN) { |
0ebbe9f1 DZ |
805 | uint32_t features; |
806 | ||
6e509042 KZ |
807 | fd = ul_prefix_fopen(desc->prefix, "r", _PATH_SYS_HYP_FEATURES); |
808 | ||
0ebbe9f1 DZ |
809 | if (fd && fscanf(fd, "%x", &features) == 1) { |
810 | /* Xen PV domain */ | |
811 | if (features & XEN_FEATURES_PV_MASK) | |
812 | desc->virtype = VIRT_PARA; | |
813 | /* Xen PVH domain */ | |
814 | else if ((features & XEN_FEATURES_PVH_MASK) | |
815 | == XEN_FEATURES_PVH_MASK) | |
816 | desc->virtype = VIRT_PARA; | |
0ebbe9f1 | 817 | } |
d29b7ced KZ |
818 | if (fd) |
819 | fclose(fd); | |
0ebbe9f1 DZ |
820 | } |
821 | } else if (read_hypervisor_powerpc(desc) > 0) {} | |
5bd31c6d | 822 | |
fb2627ce | 823 | /* Xen para-virt or dom0 */ |
6e509042 | 824 | else if (ul_path_access(desc->procfs, F_OK, "xen") == 0) { |
c8b64f6d KZ |
825 | int dom0 = 0; |
826 | ||
6e509042 | 827 | fd = ul_path_fopen(desc->procfs, "r", "xen/capabilities"); |
c8b64f6d KZ |
828 | if (fd) { |
829 | char buf[256]; | |
830 | ||
6cbf75e5 | 831 | if (fscanf(fd, "%255s", buf) == 1 && |
c8b64f6d KZ |
832 | !strcmp(buf, "control_d")) |
833 | dom0 = 1; | |
834 | fclose(fd); | |
835 | } | |
e8aa16ee KZ |
836 | desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA; |
837 | desc->hyper = HYPER_XEN; | |
c8b64f6d | 838 | |
fb2627ce | 839 | /* Xen full-virt on non-x86_64 */ |
6e509042 | 840 | } else if (has_pci_device(desc, hv_vendor_pci[HYPER_XEN], hv_graphics_pci[HYPER_XEN])) { |
e8aa16ee KZ |
841 | desc->hyper = HYPER_XEN; |
842 | desc->virtype = VIRT_FULL; | |
6e509042 | 843 | } else if (has_pci_device(desc, hv_vendor_pci[HYPER_VMWARE], hv_graphics_pci[HYPER_VMWARE])) { |
96ce475f RM |
844 | desc->hyper = HYPER_VMWARE; |
845 | desc->virtype = VIRT_FULL; | |
6e509042 | 846 | } else if (has_pci_device(desc, hv_vendor_pci[HYPER_VBOX], hv_graphics_pci[HYPER_VBOX])) { |
96ce475f RM |
847 | desc->hyper = HYPER_VBOX; |
848 | desc->virtype = VIRT_FULL; | |
fb2627ce OO |
849 | |
850 | /* IBM PR/SM */ | |
6e509042 | 851 | } else if ((fd = ul_path_fopen(desc->procfs, "r", "sysinfo"))) { |
b8ec7bdf HC |
852 | char buf[BUFSIZ]; |
853 | ||
854 | desc->hyper = HYPER_IBM; | |
10829cd7 | 855 | desc->hypervisor = "PR/SM"; |
b8ec7bdf | 856 | desc->virtype = VIRT_FULL; |
904ffe1f | 857 | while (fgets(buf, sizeof(buf), fd) != NULL) { |
d0b75b03 | 858 | char *str, *p; |
10829cd7 | 859 | |
b8ec7bdf HC |
860 | if (!strstr(buf, "Control Program:")) |
861 | continue; | |
862 | if (!strstr(buf, "KVM")) | |
863 | desc->hyper = HYPER_IBM; | |
864 | else | |
865 | desc->hyper = HYPER_KVM; | |
d0b75b03 KZ |
866 | p = strchr(buf, ':'); |
867 | if (!p) | |
10829cd7 | 868 | continue; |
d0b75b03 | 869 | xasprintf(&str, "%s", p + 1); |
9d8537ff | 870 | |
10829cd7 HC |
871 | /* remove leading, trailing and repeating whitespace */ |
872 | while (*str == ' ') | |
873 | str++; | |
874 | desc->hypervisor = str; | |
875 | str += strlen(str) - 1; | |
876 | while ((*str == '\n') || (*str == ' ')) | |
877 | *(str--) = '\0'; | |
878 | while ((str = strstr(desc->hypervisor, " "))) | |
879 | memmove(str, str + 1, strlen(str)); | |
350f5c8d | 880 | break; |
b8ec7bdf | 881 | } |
904ffe1f | 882 | fclose(fd); |
c8b64f6d | 883 | } |
fb2627ce OO |
884 | |
885 | /* OpenVZ/Virtuozzo - /proc/vz dir should exist | |
886 | * /proc/bc should not */ | |
6e509042 KZ |
887 | else if (ul_path_access(desc->procfs, F_OK, "vz") == 0 && |
888 | ul_path_access(desc->procfs, F_OK, "bc") != 0) { | |
fb2627ce | 889 | desc->hyper = HYPER_PARALLELS; |
857d9cdf | 890 | desc->virtype = VIRT_CONT; |
fb2627ce OO |
891 | |
892 | /* IBM */ | |
857d9cdf | 893 | } else if (desc->vendor && |
fb2627ce | 894 | (strcmp(desc->vendor, "PowerVM Lx86") == 0 || |
857d9cdf | 895 | strcmp(desc->vendor, "IBM/S390") == 0)) { |
fb2627ce | 896 | desc->hyper = HYPER_IBM; |
857d9cdf | 897 | desc->virtype = VIRT_FULL; |
fb2627ce OO |
898 | |
899 | /* User-mode-linux */ | |
857d9cdf | 900 | } else if (desc->modelname && strstr(desc->modelname, "UML")) { |
fb2627ce | 901 | desc->hyper = HYPER_UML; |
857d9cdf | 902 | desc->virtype = VIRT_PARA; |
fb2627ce OO |
903 | |
904 | /* Linux-VServer */ | |
6e509042 | 905 | } else if ((fd = ul_path_fopen(desc->procfs, "r", "self/status"))) { |
fb2627ce OO |
906 | char buf[BUFSIZ]; |
907 | char *val = NULL; | |
908 | ||
fb2627ce OO |
909 | while (fgets(buf, sizeof(buf), fd) != NULL) { |
910 | if (lookup(buf, "VxID", &val)) | |
911 | break; | |
912 | } | |
913 | fclose(fd); | |
914 | ||
915 | if (val) { | |
c6f6afc1 KZ |
916 | char *org = val; |
917 | ||
fb2627ce OO |
918 | while (isdigit(*val)) |
919 | ++val; | |
857d9cdf | 920 | if (!*val) { |
fb2627ce | 921 | desc->hyper = HYPER_VSERVER; |
857d9cdf KZ |
922 | desc->virtype = VIRT_CONT; |
923 | } | |
c6f6afc1 | 924 | free(org); |
fb2627ce OO |
925 | } |
926 | } | |
c8b64f6d KZ |
927 | } |
928 | ||
455fe9a0 | 929 | /* add @set to the @ary, unnecessary set is deallocated. */ |
7e03f383 KZ |
930 | static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set) |
931 | { | |
932 | int i; | |
933 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
934 | ||
935 | if (!ary) | |
936 | return -1; | |
937 | ||
938 | for (i = 0; i < *items; i++) { | |
939 | if (CPU_EQUAL_S(setsize, set, ary[i])) | |
940 | break; | |
941 | } | |
942 | if (i == *items) { | |
943 | ary[*items] = set; | |
944 | ++*items; | |
945 | return 0; | |
946 | } | |
947 | CPU_FREE(set); | |
948 | return 1; | |
949 | } | |
950 | ||
5dd7507c | 951 | static void |
4f642863 | 952 | read_topology(struct lscpu_desc *desc, int idx) |
5dd7507c | 953 | { |
b3adf6ef HC |
954 | cpu_set_t *thread_siblings, *core_siblings; |
955 | cpu_set_t *book_siblings, *drawer_siblings; | |
0d2b5d2a HC |
956 | int coreid, socketid, bookid, drawerid; |
957 | int i, num = real_cpu_num(desc, idx); | |
7e03f383 | 958 | |
6e509042 | 959 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/topology/thread_siblings", num) != 0) |
7e03f383 | 960 | return; |
5dd7507c | 961 | |
6e509042 KZ |
962 | ul_path_readf_cpuset(desc->syscpu, &thread_siblings, maxcpus, |
963 | "cpu%d/topology/thread_siblings", num); | |
964 | ul_path_readf_cpuset(desc->syscpu, &core_siblings, maxcpus, | |
965 | "cpu%d/topology/core_siblings", num); | |
966 | ul_path_readf_cpuset(desc->syscpu, &book_siblings, maxcpus, | |
967 | "cpu%d/topology/book_siblings", num); | |
968 | ul_path_readf_cpuset(desc->syscpu, &drawer_siblings, maxcpus, | |
969 | "cpu%d/topology/drawer_siblings", num); | |
970 | ||
971 | if (ul_path_readf_s32(desc->syscpu, &coreid, "cpu%d/topology/core_id", num) != 0) | |
972 | coreid = -1; | |
973 | ||
974 | if (ul_path_readf_s32(desc->syscpu, &socketid, "cpu%d/topology/physical_package_id", num) != 0) | |
975 | socketid = -1; | |
976 | ||
977 | if (ul_path_readf_s32(desc->syscpu, &bookid, "cpu%d/topology/book_id", num) != 0) | |
978 | bookid = -1; | |
979 | ||
980 | if (ul_path_readf_s32(desc->syscpu, &drawerid, "cpu%d/topology/drawer_id", num) != 0) | |
981 | drawerid = -1; | |
aac1e59e KZ |
982 | |
983 | if (!desc->coremaps) { | |
b3adf6ef | 984 | int ndrawers, nbooks, nsockets, ncores, nthreads; |
7e03f383 KZ |
985 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
986 | ||
987 | /* threads within one core */ | |
988 | nthreads = CPU_COUNT_S(setsize, thread_siblings); | |
d4bfa64d KZ |
989 | if (!nthreads) |
990 | nthreads = 1; | |
991 | ||
7e03f383 KZ |
992 | /* cores within one socket */ |
993 | ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads; | |
d4bfa64d KZ |
994 | if (!ncores) |
995 | ncores = 1; | |
996 | ||
997 | /* number of sockets within one book. Because of odd / | |
998 | * non-present cpu maps and to keep calculation easy we make | |
999 | * sure that nsockets and nbooks is at least 1. | |
32a46618 | 1000 | */ |
d4bfa64d KZ |
1001 | nsockets = desc->ncpus / nthreads / ncores; |
1002 | if (!nsockets) | |
1003 | nsockets = 1; | |
1004 | ||
56baaa4e | 1005 | /* number of books */ |
d4bfa64d KZ |
1006 | nbooks = desc->ncpus / nthreads / ncores / nsockets; |
1007 | if (!nbooks) | |
1008 | nbooks = 1; | |
e282eec2 | 1009 | |
b3adf6ef HC |
1010 | /* number of drawers */ |
1011 | ndrawers = desc->ncpus / nbooks / nthreads / ncores / nsockets; | |
1012 | if (!ndrawers) | |
1013 | ndrawers = 1; | |
1014 | ||
e282eec2 | 1015 | /* all threads, see also read_basicinfo() |
32a46618 | 1016 | * -- fallback for kernels without |
e282eec2 KZ |
1017 | * /sys/devices/system/cpu/online. |
1018 | */ | |
1019 | if (!desc->nthreads) | |
b3adf6ef | 1020 | desc->nthreads = ndrawers * nbooks * nsockets * ncores * nthreads; |
d4bfa64d | 1021 | |
a5cfffff | 1022 | /* For each map we make sure that it can have up to ncpuspos |
9d1a3a18 HC |
1023 | * entries. This is because we cannot reliably calculate the |
1024 | * number of cores, sockets and books on all architectures. | |
1025 | * E.g. completely virtualized architectures like s390 may | |
1026 | * have multiple sockets of different sizes. | |
1027 | */ | |
a5cfffff TK |
1028 | desc->coremaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
1029 | desc->socketmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); | |
0d2b5d2a HC |
1030 | desc->coreids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); |
1031 | desc->socketids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); | |
1032 | for (i = 0; i < desc->ncpuspos; i++) | |
1033 | desc->coreids[i] = desc->socketids[i] = -1; | |
1034 | if (book_siblings) { | |
a5cfffff | 1035 | desc->bookmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
0d2b5d2a HC |
1036 | desc->bookids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); |
1037 | for (i = 0; i < desc->ncpuspos; i++) | |
1038 | desc->bookids[i] = -1; | |
1039 | } | |
1040 | if (drawer_siblings) { | |
b3adf6ef | 1041 | desc->drawermaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
0d2b5d2a HC |
1042 | desc->drawerids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); |
1043 | for (i = 0; i < desc->ncpuspos; i++) | |
1044 | desc->drawerids[i] = -1; | |
1045 | } | |
7e03f383 | 1046 | } |
5dd7507c | 1047 | |
7e03f383 | 1048 | add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings); |
0d2b5d2a | 1049 | desc->coreids[idx] = coreid; |
7e03f383 | 1050 | add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings); |
0d2b5d2a HC |
1051 | desc->socketids[idx] = socketid; |
1052 | if (book_siblings) { | |
56baaa4e | 1053 | add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings); |
0d2b5d2a HC |
1054 | desc->bookids[idx] = bookid; |
1055 | } | |
1056 | if (drawer_siblings) { | |
b3adf6ef | 1057 | add_cpuset_to_array(desc->drawermaps, &desc->ndrawers, drawer_siblings); |
0d2b5d2a HC |
1058 | desc->drawerids[idx] = drawerid; |
1059 | } | |
7e03f383 | 1060 | } |
4f642863 | 1061 | |
2b8fcb81 | 1062 | static void |
4f642863 | 1063 | read_polarization(struct lscpu_desc *desc, int idx) |
2b8fcb81 HC |
1064 | { |
1065 | char mode[64]; | |
4f642863 | 1066 | int num = real_cpu_num(desc, idx); |
2b8fcb81 HC |
1067 | |
1068 | if (desc->dispatching < 0) | |
1069 | return; | |
6e509042 | 1070 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/polarization", num) != 0) |
2b8fcb81 HC |
1071 | return; |
1072 | if (!desc->polarization) | |
a5cfffff | 1073 | desc->polarization = xcalloc(desc->ncpuspos, sizeof(int)); |
6e509042 KZ |
1074 | |
1075 | ul_path_readf_buffer(desc->syscpu, mode, sizeof(mode), "cpu%d/polarization", num); | |
1076 | ||
2b8fcb81 | 1077 | if (strncmp(mode, "vertical:low", sizeof(mode)) == 0) |
4f642863 | 1078 | desc->polarization[idx] = POLAR_VLOW; |
2b8fcb81 | 1079 | else if (strncmp(mode, "vertical:medium", sizeof(mode)) == 0) |
4f642863 | 1080 | desc->polarization[idx] = POLAR_VMEDIUM; |
2b8fcb81 | 1081 | else if (strncmp(mode, "vertical:high", sizeof(mode)) == 0) |
4f642863 | 1082 | desc->polarization[idx] = POLAR_VHIGH; |
2b8fcb81 | 1083 | else if (strncmp(mode, "horizontal", sizeof(mode)) == 0) |
4f642863 | 1084 | desc->polarization[idx] = POLAR_HORIZONTAL; |
2b8fcb81 | 1085 | else |
4f642863 | 1086 | desc->polarization[idx] = POLAR_UNKNOWN; |
2b8fcb81 | 1087 | } |
7e03f383 | 1088 | |
596b8845 | 1089 | static void |
4f642863 | 1090 | read_address(struct lscpu_desc *desc, int idx) |
596b8845 | 1091 | { |
4f642863 AT |
1092 | int num = real_cpu_num(desc, idx); |
1093 | ||
6e509042 | 1094 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/address", num) != 0) |
596b8845 HC |
1095 | return; |
1096 | if (!desc->addresses) | |
a5cfffff | 1097 | desc->addresses = xcalloc(desc->ncpuspos, sizeof(int)); |
6e509042 | 1098 | ul_path_readf_s32(desc->syscpu, &desc->addresses[idx], "cpu%d/address", num); |
596b8845 | 1099 | } |
7e03f383 | 1100 | |
d231eea1 | 1101 | static void |
4f642863 | 1102 | read_configured(struct lscpu_desc *desc, int idx) |
d231eea1 | 1103 | { |
4f642863 AT |
1104 | int num = real_cpu_num(desc, idx); |
1105 | ||
6e509042 | 1106 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/configure", num) != 0) |
d231eea1 HC |
1107 | return; |
1108 | if (!desc->configured) | |
a5cfffff | 1109 | desc->configured = xcalloc(desc->ncpuspos, sizeof(int)); |
6e509042 | 1110 | ul_path_readf_s32(desc->syscpu, &desc->configured[idx], "cpu%d/configure", num); |
d231eea1 HC |
1111 | } |
1112 | ||
fc07d9f5 | 1113 | /* Read overall maximum frequency of cpu */ |
89222b17 KZ |
1114 | static char * |
1115 | cpu_max_mhz(struct lscpu_desc *desc, char *buf, size_t bufsz) | |
fc07d9f5 MI |
1116 | { |
1117 | int i; | |
95f09bc6 | 1118 | float cpu_freq = 0.0; |
538b50cb | 1119 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
fc07d9f5 MI |
1120 | |
1121 | if (desc->present) { | |
95f09bc6 | 1122 | for (i = 0; i < desc->ncpuspos; i++) { |
538b50cb | 1123 | if (CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present) |
0145d84a | 1124 | && desc->maxmhz[i]) { |
fc07d9f5 MI |
1125 | float freq = atof(desc->maxmhz[i]); |
1126 | ||
1127 | if (freq > cpu_freq) | |
1128 | cpu_freq = freq; | |
1129 | } | |
1130 | } | |
1131 | } | |
89222b17 KZ |
1132 | snprintf(buf, bufsz, "%.4f", cpu_freq); |
1133 | return buf; | |
fc07d9f5 MI |
1134 | } |
1135 | ||
1136 | /* Read overall minimum frequency of cpu */ | |
89222b17 KZ |
1137 | static char * |
1138 | cpu_min_mhz(struct lscpu_desc *desc, char *buf, size_t bufsz) | |
fc07d9f5 | 1139 | { |
95f09bc6 DM |
1140 | int i; |
1141 | float cpu_freq = -1.0; | |
538b50cb | 1142 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
fc07d9f5 MI |
1143 | |
1144 | if (desc->present) { | |
95f09bc6 | 1145 | for (i = 0; i < desc->ncpuspos; i++) { |
538b50cb | 1146 | if (CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present) |
0145d84a | 1147 | && desc->minmhz[i]) { |
fc07d9f5 MI |
1148 | float freq = atof(desc->minmhz[i]); |
1149 | ||
95f09bc6 | 1150 | if (cpu_freq < 0.0 || freq < cpu_freq) |
fc07d9f5 MI |
1151 | cpu_freq = freq; |
1152 | } | |
1153 | } | |
1154 | } | |
89222b17 KZ |
1155 | snprintf(buf, bufsz, "%.4f", cpu_freq); |
1156 | return buf; | |
fc07d9f5 MI |
1157 | } |
1158 | ||
1159 | ||
44320710 | 1160 | static void |
4f642863 | 1161 | read_max_mhz(struct lscpu_desc *desc, int idx) |
44320710 | 1162 | { |
4f642863 | 1163 | int num = real_cpu_num(desc, idx); |
6e509042 | 1164 | int mhz; |
4f642863 | 1165 | |
6e509042 | 1166 | if (ul_path_readf_s32(desc->syscpu, &mhz, "cpu%d/cpufreq/cpuinfo_max_freq", num) != 0) |
44320710 | 1167 | return; |
e065a597 DB |
1168 | if (!desc->maxmhz) |
1169 | desc->maxmhz = xcalloc(desc->ncpuspos, sizeof(char *)); | |
6e509042 | 1170 | xasprintf(&desc->maxmhz[idx], "%.4f", (float) mhz / 1000); |
44320710 SK |
1171 | } |
1172 | ||
e065a597 | 1173 | static void |
4f642863 | 1174 | read_min_mhz(struct lscpu_desc *desc, int idx) |
e065a597 | 1175 | { |
4f642863 | 1176 | int num = real_cpu_num(desc, idx); |
6e509042 | 1177 | int mhz; |
4f642863 | 1178 | |
6e509042 | 1179 | if (ul_path_readf_s32(desc->syscpu, &mhz, "cpu%d/cpufreq/cpuinfo_min_freq", num) != 0) |
e065a597 DB |
1180 | return; |
1181 | if (!desc->minmhz) | |
1182 | desc->minmhz = xcalloc(desc->ncpuspos, sizeof(char *)); | |
6e509042 | 1183 | xasprintf(&desc->minmhz[idx], "%.4f", (float) mhz / 1000); |
e065a597 DB |
1184 | } |
1185 | ||
7e03f383 KZ |
1186 | static int |
1187 | cachecmp(const void *a, const void *b) | |
1188 | { | |
1189 | struct cpu_cache *c1 = (struct cpu_cache *) a; | |
1190 | struct cpu_cache *c2 = (struct cpu_cache *) b; | |
1191 | ||
1192 | return strcmp(c2->name, c1->name); | |
5dd7507c CQ |
1193 | } |
1194 | ||
1195 | static void | |
4f642863 | 1196 | read_cache(struct lscpu_desc *desc, int idx) |
5dd7507c CQ |
1197 | { |
1198 | char buf[256]; | |
7e03f383 | 1199 | int i; |
4f642863 | 1200 | int num = real_cpu_num(desc, idx); |
5dd7507c | 1201 | |
aac1e59e | 1202 | if (!desc->ncaches) { |
6e509042 KZ |
1203 | while (ul_path_accessf(desc->syscpu, F_OK, |
1204 | "cpu%d/cache/index%d", | |
1205 | num, desc->ncaches) == 0) | |
7e03f383 | 1206 | desc->ncaches++; |
5dd7507c | 1207 | |
7e03f383 KZ |
1208 | if (!desc->ncaches) |
1209 | return; | |
08de16d0 | 1210 | desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches)); |
7e03f383 KZ |
1211 | } |
1212 | for (i = 0; i < desc->ncaches; i++) { | |
1213 | struct cpu_cache *ca = &desc->caches[i]; | |
1214 | cpu_set_t *map; | |
1215 | ||
6e509042 KZ |
1216 | if (ul_path_accessf(desc->syscpu, F_OK, |
1217 | "cpu%d/cache/index%d", num, i) != 0) | |
dcdead42 | 1218 | continue; |
7e03f383 | 1219 | if (!ca->name) { |
6e509042 | 1220 | int type = 0, level; |
7e03f383 KZ |
1221 | |
1222 | /* cache type */ | |
6e509042 KZ |
1223 | if (ul_path_readf_buffer(desc->syscpu, buf, sizeof(buf), |
1224 | "cpu%d/cache/index%d/type", num, i) > 0) { | |
1225 | if (!strcmp(buf, "Data")) | |
1226 | type = 'd'; | |
1227 | else if (!strcmp(buf, "Instruction")) | |
1228 | type = 'i'; | |
1229 | } | |
7e03f383 KZ |
1230 | |
1231 | /* cache level */ | |
6e509042 KZ |
1232 | ul_path_readf_s32(desc->syscpu, &level, |
1233 | "cpu%d/cache/index%d/level", num, i); | |
7e03f383 KZ |
1234 | if (type) |
1235 | snprintf(buf, sizeof(buf), "L%d%c", level, type); | |
1236 | else | |
1237 | snprintf(buf, sizeof(buf), "L%d", level); | |
1238 | ||
1239 | ca->name = xstrdup(buf); | |
1240 | ||
1241 | /* cache size */ | |
6e509042 KZ |
1242 | if (ul_path_readf_string(desc->syscpu, &ca->size, |
1243 | "cpu%d/cache/index%d/size", num, i) < 0) | |
10d927ab | 1244 | ca->size = xstrdup("unknown size"); |
7e03f383 | 1245 | } |
5dd7507c | 1246 | |
7e03f383 | 1247 | /* information about how CPUs share different caches */ |
6e509042 KZ |
1248 | ul_path_readf_cpuset(desc->syscpu, &map, maxcpus, |
1249 | "cpu%d/cache/index%d/shared_cpu_map", num, i); | |
5dd7507c | 1250 | |
08de16d0 | 1251 | if (!ca->sharedmaps) |
a5cfffff | 1252 | ca->sharedmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
7e03f383 | 1253 | add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map); |
5dd7507c CQ |
1254 | } |
1255 | } | |
1256 | ||
e9074a16 KZ |
1257 | static inline int is_node_dirent(struct dirent *d) |
1258 | { | |
1259 | return | |
1260 | d && | |
1261 | #ifdef _DIRENT_HAVE_D_TYPE | |
c0cf4ae9 | 1262 | (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) && |
e9074a16 KZ |
1263 | #endif |
1264 | strncmp(d->d_name, "node", 4) == 0 && | |
1265 | isdigit_string(d->d_name + 4); | |
1266 | } | |
1267 | ||
39c758d1 KZ |
1268 | static int |
1269 | nodecmp(const void *ap, const void *bp) | |
1270 | { | |
1271 | int *a = (int *) ap, *b = (int *) bp; | |
1272 | return *a - *b; | |
1273 | } | |
1274 | ||
5dd7507c | 1275 | static void |
e8aa16ee | 1276 | read_nodes(struct lscpu_desc *desc) |
5dd7507c | 1277 | { |
e9074a16 KZ |
1278 | int i = 0; |
1279 | DIR *dir; | |
1280 | struct dirent *d; | |
6e509042 | 1281 | struct path_cxt *sysnode; |
5dd7507c | 1282 | |
a25fb9e8 RM |
1283 | desc->nnodes = 0; |
1284 | ||
6e509042 KZ |
1285 | sysnode = ul_new_path(_PATH_SYS_NODE); |
1286 | if (!sysnode) | |
1287 | err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_NODE); | |
1288 | ul_path_set_prefix(sysnode, desc->prefix); | |
1289 | ||
1290 | dir = ul_path_opendir(sysnode, NULL); | |
1291 | if (!dir) | |
1292 | goto done; | |
1293 | ||
a25fb9e8 | 1294 | while ((d = readdir(dir))) { |
e9074a16 KZ |
1295 | if (is_node_dirent(d)) |
1296 | desc->nnodes++; | |
1297 | } | |
1298 | ||
1299 | if (!desc->nnodes) { | |
a25fb9e8 | 1300 | closedir(dir); |
6e509042 | 1301 | goto done; |
e9074a16 | 1302 | } |
5dd7507c | 1303 | |
08de16d0 | 1304 | desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *)); |
e9074a16 KZ |
1305 | desc->idx2nodenum = xmalloc(desc->nnodes * sizeof(int)); |
1306 | ||
a25fb9e8 RM |
1307 | rewinddir(dir); |
1308 | while ((d = readdir(dir)) && i < desc->nnodes) { | |
1309 | if (is_node_dirent(d)) | |
1310 | desc->idx2nodenum[i++] = strtol_or_err(((d->d_name) + 4), | |
1311 | _("Failed to extract the node number")); | |
e9074a16 | 1312 | } |
a25fb9e8 RM |
1313 | closedir(dir); |
1314 | qsort(desc->idx2nodenum, desc->nnodes, sizeof(int), nodecmp); | |
5dd7507c CQ |
1315 | |
1316 | /* information about how nodes share different CPUs */ | |
7e03f383 | 1317 | for (i = 0; i < desc->nnodes; i++) |
6e509042 KZ |
1318 | ul_path_readf_cpuset(sysnode, &desc->nodemaps[i], maxcpus, |
1319 | "node%d/cpumap", desc->idx2nodenum[i]); | |
1320 | done: | |
1321 | ul_unref_path(sysnode); | |
5dd7507c CQ |
1322 | } |
1323 | ||
e3b3a2f3 | 1324 | static char * |
4f642863 | 1325 | get_cell_data(struct lscpu_desc *desc, int idx, int col, |
e3b3a2f3 KZ |
1326 | struct lscpu_modifier *mod, |
1327 | char *buf, size_t bufsz) | |
5dd7507c | 1328 | { |
7e03f383 | 1329 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
4f642863 AT |
1330 | size_t i; |
1331 | int cpu = real_cpu_num(desc, idx); | |
e3b3a2f3 KZ |
1332 | |
1333 | *buf = '\0'; | |
5dd7507c | 1334 | |
477251f8 KZ |
1335 | switch (col) { |
1336 | case COL_CPU: | |
e3b3a2f3 | 1337 | snprintf(buf, bufsz, "%d", cpu); |
477251f8 KZ |
1338 | break; |
1339 | case COL_CORE: | |
0d2b5d2a HC |
1340 | if (mod->physical) { |
1341 | if (desc->coreids[idx] == -1) | |
1342 | snprintf(buf, bufsz, "-"); | |
1343 | else | |
1344 | snprintf(buf, bufsz, "%d", desc->coreids[idx]); | |
1345 | } else { | |
1346 | if (cpuset_ary_isset(cpu, desc->coremaps, | |
1347 | desc->ncores, setsize, &i) == 0) | |
1348 | snprintf(buf, bufsz, "%zu", i); | |
1349 | } | |
477251f8 KZ |
1350 | break; |
1351 | case COL_SOCKET: | |
0d2b5d2a HC |
1352 | if (mod->physical) { |
1353 | if (desc->socketids[idx] == -1) | |
1354 | snprintf(buf, bufsz, "-"); | |
1355 | else | |
1356 | snprintf(buf, bufsz, "%d", desc->socketids[idx]); | |
1357 | } else { | |
1358 | if (cpuset_ary_isset(cpu, desc->socketmaps, | |
1359 | desc->nsockets, setsize, &i) == 0) | |
1360 | snprintf(buf, bufsz, "%zu", i); | |
1361 | } | |
477251f8 KZ |
1362 | break; |
1363 | case COL_NODE: | |
e9d659ea | 1364 | if (cpuset_ary_isset(cpu, desc->nodemaps, |
4f642863 | 1365 | desc->nnodes, setsize, &i) == 0) |
e9074a16 | 1366 | snprintf(buf, bufsz, "%d", desc->idx2nodenum[i]); |
477251f8 | 1367 | break; |
b3adf6ef | 1368 | case COL_DRAWER: |
0d2b5d2a HC |
1369 | if (mod->physical) { |
1370 | if (desc->drawerids[idx] == -1) | |
1371 | snprintf(buf, bufsz, "-"); | |
1372 | else | |
1373 | snprintf(buf, bufsz, "%d", desc->drawerids[idx]); | |
1374 | } else { | |
1375 | if (cpuset_ary_isset(cpu, desc->drawermaps, | |
1376 | desc->ndrawers, setsize, &i) == 0) | |
1377 | snprintf(buf, bufsz, "%zu", i); | |
1378 | } | |
b3adf6ef | 1379 | break; |
477251f8 | 1380 | case COL_BOOK: |
0d2b5d2a HC |
1381 | if (mod->physical) { |
1382 | if (desc->bookids[idx] == -1) | |
1383 | snprintf(buf, bufsz, "-"); | |
1384 | else | |
1385 | snprintf(buf, bufsz, "%d", desc->bookids[idx]); | |
1386 | } else { | |
1387 | if (cpuset_ary_isset(cpu, desc->bookmaps, | |
1388 | desc->nbooks, setsize, &i) == 0) | |
1389 | snprintf(buf, bufsz, "%zu", i); | |
1390 | } | |
477251f8 KZ |
1391 | break; |
1392 | case COL_CACHE: | |
e3b3a2f3 KZ |
1393 | { |
1394 | char *p = buf; | |
1395 | size_t sz = bufsz; | |
1396 | int j; | |
1397 | ||
7e03f383 KZ |
1398 | for (j = desc->ncaches - 1; j >= 0; j--) { |
1399 | struct cpu_cache *ca = &desc->caches[j]; | |
e9d659ea KZ |
1400 | |
1401 | if (cpuset_ary_isset(cpu, ca->sharedmaps, | |
4f642863 | 1402 | ca->nsharedmaps, setsize, &i) == 0) { |
73f2bec5 | 1403 | int x = snprintf(p, sz, "%zu", i); |
06fa5817 | 1404 | if (x < 0 || (size_t) x >= sz) |
e3b3a2f3 KZ |
1405 | return NULL; |
1406 | p += x; | |
1407 | sz -= x; | |
1408 | } | |
1409 | if (j != 0) { | |
06fa5817 YK |
1410 | if (sz < 2) |
1411 | return NULL; | |
e3b3a2f3 KZ |
1412 | *p++ = mod->compat ? ',' : ':'; |
1413 | *p = '\0'; | |
0c5bbafa | 1414 | sz--; |
e3b3a2f3 | 1415 | } |
5dd7507c | 1416 | } |
477251f8 | 1417 | break; |
e3b3a2f3 | 1418 | } |
2b8fcb81 | 1419 | case COL_POLARIZATION: |
ba45d8c1 | 1420 | if (desc->polarization) { |
4f642863 | 1421 | int x = desc->polarization[idx]; |
ba45d8c1 | 1422 | |
e3b3a2f3 | 1423 | snprintf(buf, bufsz, "%s", |
ba45d8c1 KZ |
1424 | mod->mode == OUTPUT_PARSABLE ? |
1425 | polar_modes[x].parsable : | |
1426 | polar_modes[x].readable); | |
1427 | } | |
2b8fcb81 | 1428 | break; |
596b8845 HC |
1429 | case COL_ADDRESS: |
1430 | if (desc->addresses) | |
4f642863 | 1431 | snprintf(buf, bufsz, "%d", desc->addresses[idx]); |
596b8845 | 1432 | break; |
d231eea1 | 1433 | case COL_CONFIGURED: |
e43fc13e HC |
1434 | if (!desc->configured) |
1435 | break; | |
1436 | if (mod->mode == OUTPUT_PARSABLE) | |
f205c90a | 1437 | snprintf(buf, bufsz, "%s", |
4f642863 | 1438 | desc->configured[idx] ? _("Y") : _("N")); |
e43fc13e | 1439 | else |
f205c90a | 1440 | snprintf(buf, bufsz, "%s", |
4f642863 | 1441 | desc->configured[idx] ? _("yes") : _("no")); |
d231eea1 | 1442 | break; |
a7e5300c | 1443 | case COL_ONLINE: |
e43fc13e HC |
1444 | if (!desc->online) |
1445 | break; | |
1446 | if (mod->mode == OUTPUT_PARSABLE) | |
f205c90a | 1447 | snprintf(buf, bufsz, "%s", |
e43fc13e HC |
1448 | is_cpu_online(desc, cpu) ? _("Y") : _("N")); |
1449 | else | |
f205c90a | 1450 | snprintf(buf, bufsz, "%s", |
847b982e | 1451 | is_cpu_online(desc, cpu) ? _("yes") : _("no")); |
a7e5300c | 1452 | break; |
e065a597 | 1453 | case COL_MAXMHZ: |
0145d84a | 1454 | if (desc->maxmhz && desc->maxmhz[idx]) |
4f642863 | 1455 | xstrncpy(buf, desc->maxmhz[idx], bufsz); |
e065a597 DB |
1456 | break; |
1457 | case COL_MINMHZ: | |
0145d84a | 1458 | if (desc->minmhz && desc->minmhz[idx]) |
4f642863 | 1459 | xstrncpy(buf, desc->minmhz[idx], bufsz); |
44320710 | 1460 | break; |
477251f8 | 1461 | } |
e3b3a2f3 KZ |
1462 | return buf; |
1463 | } | |
1464 | ||
1465 | static char * | |
1466 | get_cell_header(struct lscpu_desc *desc, int col, | |
1467 | struct lscpu_modifier *mod, | |
1468 | char *buf, size_t bufsz) | |
1469 | { | |
1470 | *buf = '\0'; | |
1471 | ||
1472 | if (col == COL_CACHE) { | |
1473 | char *p = buf; | |
1474 | size_t sz = bufsz; | |
1475 | int i; | |
1476 | ||
1477 | for (i = desc->ncaches - 1; i >= 0; i--) { | |
1478 | int x = snprintf(p, sz, "%s", desc->caches[i].name); | |
06fa5817 | 1479 | if (x < 0 || (size_t) x >= sz) |
e3b3a2f3 KZ |
1480 | return NULL; |
1481 | sz -= x; | |
1482 | p += x; | |
1483 | if (i > 0) { | |
06fa5817 YK |
1484 | if (sz < 2) |
1485 | return NULL; | |
e3b3a2f3 KZ |
1486 | *p++ = mod->compat ? ',' : ':'; |
1487 | *p = '\0'; | |
0c5bbafa | 1488 | sz--; |
e3b3a2f3 KZ |
1489 | } |
1490 | } | |
1491 | if (desc->ncaches) | |
1492 | return buf; | |
1493 | } | |
756d79cd | 1494 | snprintf(buf, bufsz, "%s", coldescs[col].name); |
e3b3a2f3 | 1495 | return buf; |
477251f8 KZ |
1496 | } |
1497 | ||
1498 | /* | |
ba45d8c1 | 1499 | * [-p] backend, we support two parsable formats: |
477251f8 KZ |
1500 | * |
1501 | * 1) "compatible" -- this format is compatible with the original lscpu(1) | |
1502 | * output and it contains fixed set of the columns. The CACHE columns are at | |
1503 | * the end of the line and the CACHE is not printed if the number of the caches | |
1504 | * is zero. The CACHE columns are separated by two commas, for example: | |
1505 | * | |
1506 | * $ lscpu --parse | |
1507 | * # CPU,Core,Socket,Node,,L1d,L1i,L2 | |
1508 | * 0,0,0,0,,0,0,0 | |
1509 | * 1,1,0,0,,1,1,0 | |
1510 | * | |
1511 | * 2) "user defined output" -- this format prints always all columns without | |
1512 | * special prefix for CACHE column. If there are not CACHEs then the column is | |
1513 | * empty and the header "Cache" is printed rather than a real name of the cache. | |
1514 | * The CACHE columns are separated by ':'. | |
1515 | * | |
1516 | * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE | |
1517 | * # CPU,Core,Socket,Node,L1d:L1i:L2 | |
1518 | * 0,0,0,0,0:0:0 | |
1519 | * 1,1,0,0,1:1:0 | |
1520 | */ | |
1521 | static void | |
8005924a KZ |
1522 | print_parsable(struct lscpu_desc *desc, int cols[], int ncols, |
1523 | struct lscpu_modifier *mod) | |
477251f8 | 1524 | { |
e3b3a2f3 KZ |
1525 | char buf[BUFSIZ], *data; |
1526 | int i; | |
477251f8 | 1527 | |
e3b3a2f3 KZ |
1528 | /* |
1529 | * Header | |
1530 | */ | |
477251f8 KZ |
1531 | printf(_( |
1532 | "# The following is the parsable format, which can be fed to other\n" | |
1533 | "# programs. Each different item in every column has an unique ID\n" | |
1534 | "# starting from zero.\n")); | |
1535 | ||
1536 | fputs("# ", stdout); | |
1537 | for (i = 0; i < ncols; i++) { | |
3d27b76a | 1538 | int col = cols[i]; |
b9d18bc3 | 1539 | |
3d27b76a | 1540 | if (col == COL_CACHE) { |
8005924a | 1541 | if (mod->compat && !desc->ncaches) |
477251f8 | 1542 | continue; |
8005924a | 1543 | if (mod->compat && i != 0) |
477251f8 | 1544 | putchar(','); |
477251f8 | 1545 | } |
e3b3a2f3 KZ |
1546 | if (i > 0) |
1547 | putchar(','); | |
1548 | ||
3d27b76a | 1549 | data = get_cell_header(desc, col, mod, buf, sizeof(buf)); |
b9d18bc3 | 1550 | |
3d27b76a KZ |
1551 | if (data && * data && col != COL_CACHE && |
1552 | !coldescs[col].is_abbr) { | |
1553 | /* | |
1554 | * For normal column names use mixed case (e.g. "Socket") | |
1555 | */ | |
1556 | char *p = data + 1; | |
1557 | ||
14e8be8a PU |
1558 | while (p && *p != '\0') { |
1559 | *p = tolower((unsigned int) *p); | |
1560 | p++; | |
1561 | } | |
3d27b76a | 1562 | } |
e3b3a2f3 | 1563 | fputs(data && *data ? data : "", stdout); |
477251f8 KZ |
1564 | } |
1565 | putchar('\n'); | |
1566 | ||
e3b3a2f3 KZ |
1567 | /* |
1568 | * Data | |
1569 | */ | |
a5cfffff | 1570 | for (i = 0; i < desc->ncpuspos; i++) { |
e3b3a2f3 | 1571 | int c; |
4f642863 | 1572 | int cpu = real_cpu_num(desc, i); |
e3b3a2f3 | 1573 | |
4f642863 | 1574 | if (!mod->offline && desc->online && !is_cpu_online(desc, cpu)) |
7afc2387 | 1575 | continue; |
4f642863 | 1576 | if (!mod->online && desc->online && is_cpu_online(desc, cpu)) |
477251f8 | 1577 | continue; |
4f642863 | 1578 | if (desc->present && !is_cpu_present(desc, cpu)) |
a5cfffff | 1579 | continue; |
477251f8 | 1580 | for (c = 0; c < ncols; c++) { |
8005924a | 1581 | if (mod->compat && cols[c] == COL_CACHE) { |
477251f8 KZ |
1582 | if (!desc->ncaches) |
1583 | continue; | |
1584 | if (c > 0) | |
1585 | putchar(','); | |
1586 | } | |
1587 | if (c > 0) | |
1588 | putchar(','); | |
e3b3a2f3 KZ |
1589 | |
1590 | data = get_cell_data(desc, i, cols[c], mod, | |
1591 | buf, sizeof(buf)); | |
1592 | fputs(data && *data ? data : "", stdout); | |
477251f8 | 1593 | } |
5dd7507c CQ |
1594 | putchar('\n'); |
1595 | } | |
1596 | } | |
1597 | ||
ba45d8c1 KZ |
1598 | /* |
1599 | * [-e] backend | |
1600 | */ | |
1601 | static void | |
1602 | print_readable(struct lscpu_desc *desc, int cols[], int ncols, | |
1603 | struct lscpu_modifier *mod) | |
1604 | { | |
1605 | int i; | |
e7213e34 KZ |
1606 | char buf[BUFSIZ]; |
1607 | const char *data; | |
710ed55d | 1608 | struct libscols_table *table; |
ba45d8c1 | 1609 | |
710ed55d KZ |
1610 | scols_init_debug(0); |
1611 | ||
1612 | table = scols_new_table(); | |
83db4eb2 | 1613 | if (!table) |
780ce22c | 1614 | err(EXIT_FAILURE, _("failed to allocate output table")); |
19a5510b KZ |
1615 | if (mod->json) { |
1616 | scols_table_enable_json(table, 1); | |
1617 | scols_table_set_name(table, "cpus"); | |
1618 | } | |
ba45d8c1 KZ |
1619 | |
1620 | for (i = 0; i < ncols; i++) { | |
b9d18bc3 | 1621 | data = get_cell_header(desc, cols[i], mod, buf, sizeof(buf)); |
c6f6afc1 | 1622 | if (!scols_table_new_column(table, data, 0, 0)) |
780ce22c | 1623 | err(EXIT_FAILURE, _("failed to allocate output column")); |
ba45d8c1 KZ |
1624 | } |
1625 | ||
a5cfffff | 1626 | for (i = 0; i < desc->ncpuspos; i++) { |
ba45d8c1 | 1627 | int c; |
83db4eb2 | 1628 | struct libscols_line *line; |
4f642863 | 1629 | int cpu = real_cpu_num(desc, i); |
ba45d8c1 | 1630 | |
4f642863 | 1631 | if (!mod->offline && desc->online && !is_cpu_online(desc, cpu)) |
7afc2387 | 1632 | continue; |
4f642863 | 1633 | if (!mod->online && desc->online && is_cpu_online(desc, cpu)) |
ba45d8c1 | 1634 | continue; |
4f642863 | 1635 | if (desc->present && !is_cpu_present(desc, cpu)) |
a5cfffff | 1636 | continue; |
ba45d8c1 | 1637 | |
83db4eb2 | 1638 | line = scols_table_new_line(table, NULL); |
e7213e34 | 1639 | if (!line) |
780ce22c | 1640 | err(EXIT_FAILURE, _("failed to allocate output line")); |
ba45d8c1 KZ |
1641 | |
1642 | for (c = 0; c < ncols; c++) { | |
1643 | data = get_cell_data(desc, i, cols[c], mod, | |
1644 | buf, sizeof(buf)); | |
e7213e34 KZ |
1645 | if (!data || !*data) |
1646 | data = "-"; | |
bf1eab07 | 1647 | if (scols_line_set_data(line, c, data)) |
780ce22c | 1648 | err(EXIT_FAILURE, _("failed to add output data")); |
ba45d8c1 KZ |
1649 | } |
1650 | } | |
1651 | ||
83db4eb2 OO |
1652 | scols_print_table(table); |
1653 | scols_unref_table(table); | |
ba45d8c1 | 1654 | } |
5dd7507c | 1655 | |
c82b12a0 KZ |
1656 | |
1657 | static void __attribute__ ((__format__(printf, 3, 4))) | |
1658 | add_summary_sprint(struct libscols_table *tb, | |
1659 | const char *txt, | |
1660 | const char *fmt, | |
1661 | ...) | |
1662 | { | |
1663 | struct libscols_line *ln = scols_table_new_line(tb, NULL); | |
1664 | char *data; | |
1665 | va_list args; | |
1666 | ||
1667 | if (!ln) | |
780ce22c | 1668 | err(EXIT_FAILURE, _("failed to allocate output line")); |
c82b12a0 KZ |
1669 | |
1670 | /* description column */ | |
1671 | scols_line_set_data(ln, 0, txt); | |
1672 | ||
1673 | /* data column */ | |
1674 | va_start(args, fmt); | |
1675 | xvasprintf(&data, fmt, args); | |
1676 | va_end(args); | |
1677 | ||
780ce22c KZ |
1678 | if (data && scols_line_refer_data(ln, 1, data)) |
1679 | err(EXIT_FAILURE, _("failed to add output data")); | |
c82b12a0 KZ |
1680 | } |
1681 | ||
1682 | #define add_summary_n(tb, txt, num) add_summary_sprint(tb, txt, "%d", num) | |
1683 | #define add_summary_s(tb, txt, str) add_summary_sprint(tb, txt, "%s", str) | |
5dd7507c CQ |
1684 | |
1685 | static void | |
c82b12a0 KZ |
1686 | print_cpuset(struct libscols_table *tb, |
1687 | const char *key, cpu_set_t *set, int hex) | |
4f912c6a KZ |
1688 | { |
1689 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1690 | size_t setbuflen = 7 * maxcpus; | |
1691 | char setbuf[setbuflen], *p; | |
1692 | ||
1693 | if (hex) { | |
1694 | p = cpumask_create(setbuf, setbuflen, set, setsize); | |
c82b12a0 | 1695 | add_summary_s(tb, key, p); |
4f912c6a KZ |
1696 | } else { |
1697 | p = cpulist_create(setbuf, setbuflen, set, setsize); | |
c82b12a0 | 1698 | add_summary_s(tb, key, p); |
4f912c6a | 1699 | } |
4f912c6a KZ |
1700 | } |
1701 | ||
e5f72113 KZ |
1702 | static int get_cache_full_size(struct lscpu_desc *desc, int idx, uint64_t *res) |
1703 | { | |
1704 | struct cpu_cache *ca = &desc->caches[idx]; | |
1705 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1706 | int i, nshares = 0, rc; | |
1707 | uint64_t sz; | |
1708 | ||
1709 | /* Convert size to number */ | |
1710 | rc = parse_size(ca->size, &sz, NULL); | |
1711 | if (rc) | |
1712 | return rc; | |
1713 | ||
1714 | /* Count number of CPUs which shares the cache */ | |
1715 | for (i = 0; i < desc->ncpuspos; i++) { | |
1716 | int cpu = real_cpu_num(desc, i); | |
1717 | ||
1718 | if (desc->present && !is_cpu_present(desc, cpu)) | |
1719 | continue; | |
1720 | if (CPU_ISSET_S(cpu, setsize, ca->sharedmaps[0])) | |
1721 | nshares++; | |
1722 | } | |
1723 | ||
1724 | /* Correction for CPU threads */ | |
1725 | if (desc->nthreads > desc->ncores) | |
1726 | nshares /= (desc->nthreads / desc->ncores); | |
1727 | ||
1728 | *res = (desc->ncores / nshares) * sz; | |
1729 | return 0; | |
1730 | } | |
1731 | ||
ba45d8c1 KZ |
1732 | /* |
1733 | * default output | |
1734 | */ | |
4f912c6a | 1735 | static void |
8005924a | 1736 | print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
5dd7507c | 1737 | { |
577d1a66 | 1738 | char buf[BUFSIZ]; |
fc07d9f5 | 1739 | int i = 0; |
aac1e59e | 1740 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
c82b12a0 KZ |
1741 | struct libscols_table *tb; |
1742 | ||
1743 | scols_init_debug(0); | |
1744 | ||
1745 | tb = scols_new_table(); | |
1746 | if (!tb) | |
780ce22c | 1747 | err(EXIT_FAILURE, _("failed to allocate output table")); |
7e03f383 | 1748 | |
c82b12a0 | 1749 | scols_table_enable_noheadings(tb, 1); |
19a5510b KZ |
1750 | if (mod->json) { |
1751 | scols_table_enable_json(tb, 1); | |
1752 | scols_table_set_name(tb, "lscpu"); | |
1753 | } | |
79e8b41a | 1754 | |
19a5510b | 1755 | if (scols_table_new_column(tb, "field", 0, 0) == NULL || |
c82b12a0 KZ |
1756 | scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES) == NULL) |
1757 | err(EXIT_FAILURE, _("failed to initialize output column")); | |
1758 | ||
1759 | add_summary_s(tb, _("Architecture:"), desc->arch); | |
f633ad4e | 1760 | if (desc->mode) { |
577d1a66 | 1761 | char *p = buf; |
79e8b41a | 1762 | |
f633ad4e | 1763 | if (desc->mode & MODE_32BIT) { |
79e8b41a KZ |
1764 | strcpy(p, "32-bit, "); |
1765 | p += 8; | |
1766 | } | |
f633ad4e | 1767 | if (desc->mode & MODE_64BIT) { |
79e8b41a KZ |
1768 | strcpy(p, "64-bit, "); |
1769 | p += 8; | |
1770 | } | |
1771 | *(p - 2) = '\0'; | |
577d1a66 | 1772 | add_summary_s(tb, _("CPU op-mode(s):"), buf); |
79e8b41a | 1773 | } |
aabe2441 | 1774 | #if !defined(WORDS_BIGENDIAN) |
c82b12a0 | 1775 | add_summary_s(tb, _("Byte Order:"), "Little Endian"); |
9b8d4d5f | 1776 | #else |
c82b12a0 | 1777 | add_summary_s(tb, _("Byte Order:"), "Big Endian"); |
9b8d4d5f | 1778 | #endif |
3e48ef72 KZ |
1779 | |
1780 | if (desc->addrsz) | |
1781 | add_summary_s(tb, _("Address sizes:"), desc->addrsz); | |
1782 | ||
c82b12a0 | 1783 | add_summary_n(tb, _("CPU(s):"), desc->ncpus); |
4f912c6a | 1784 | |
5d4ba40d | 1785 | if (desc->online) |
c82b12a0 KZ |
1786 | print_cpuset(tb, mod->hex ? _("On-line CPU(s) mask:") : |
1787 | _("On-line CPU(s) list:"), | |
8005924a | 1788 | desc->online, mod->hex); |
4f912c6a | 1789 | |
5d4ba40d | 1790 | if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) { |
4f912c6a KZ |
1791 | cpu_set_t *set; |
1792 | ||
1793 | /* Linux kernel provides cpuset of off-line CPUs that contains | |
1794 | * all configured CPUs (see /sys/devices/system/cpu/offline), | |
1795 | * but want to print real (present in system) off-line CPUs only. | |
1796 | */ | |
1797 | set = cpuset_alloc(maxcpus, NULL, NULL); | |
1798 | if (!set) | |
1799 | err(EXIT_FAILURE, _("failed to callocate cpu set")); | |
1800 | CPU_ZERO_S(setsize, set); | |
a5cfffff | 1801 | for (i = 0; i < desc->ncpuspos; i++) { |
4f642863 AT |
1802 | int cpu = real_cpu_num(desc, i); |
1803 | if (!is_cpu_online(desc, cpu) && is_cpu_present(desc, cpu)) | |
1804 | CPU_SET_S(cpu, setsize, set); | |
4f912c6a | 1805 | } |
c82b12a0 KZ |
1806 | print_cpuset(tb, mod->hex ? _("Off-line CPU(s) mask:") : |
1807 | _("Off-line CPU(s) list:"), | |
8005924a | 1808 | set, mod->hex); |
4f912c6a KZ |
1809 | cpuset_free(set); |
1810 | } | |
5dd7507c | 1811 | |
7e03f383 | 1812 | if (desc->nsockets) { |
2c497d32 HC |
1813 | int threads_per_core, cores_per_socket, sockets_per_book; |
1814 | int books_per_drawer, drawers; | |
904ffe1f | 1815 | FILE *fd; |
8648ca96 | 1816 | |
2c497d32 HC |
1817 | threads_per_core = cores_per_socket = sockets_per_book = 0; |
1818 | books_per_drawer = drawers = 0; | |
8648ca96 HC |
1819 | /* s390 detects its cpu topology via /proc/sysinfo, if present. |
1820 | * Using simply the cpu topology masks in sysfs will not give | |
1821 | * usable results since everything is virtualized. E.g. | |
1822 | * virtual core 0 may have only 1 cpu, but virtual core 2 may | |
1823 | * five cpus. | |
1824 | * If the cpu topology is not exported (e.g. 2nd level guest) | |
1825 | * fall back to old calculation scheme. | |
1826 | */ | |
6e509042 | 1827 | if ((fd = ul_path_fopen(desc->procfs, "r", "sysinfo"))) { |
b3adf6ef | 1828 | int t0, t1; |
8648ca96 | 1829 | |
577d1a66 KZ |
1830 | while (fd && fgets(buf, sizeof(buf), fd) != NULL) { |
1831 | if (sscanf(buf, "CPU Topology SW:%d%d%d%d%d%d", | |
b3adf6ef HC |
1832 | &t0, &t1, &drawers, &books_per_drawer, |
1833 | &sockets_per_book, | |
8648ca96 HC |
1834 | &cores_per_socket) == 6) |
1835 | break; | |
1836 | } | |
ad655c88 KZ |
1837 | if (fd) |
1838 | fclose(fd); | |
8648ca96 | 1839 | } |
2c497d32 HC |
1840 | if (desc->mtid) |
1841 | threads_per_core = atoi(desc->mtid) + 1; | |
c82b12a0 | 1842 | add_summary_n(tb, _("Thread(s) per core:"), |
2c497d32 | 1843 | threads_per_core ?: desc->nthreads / desc->ncores); |
c82b12a0 | 1844 | add_summary_n(tb, _("Core(s) per socket:"), |
8648ca96 | 1845 | cores_per_socket ?: desc->ncores / desc->nsockets); |
56baaa4e | 1846 | if (desc->nbooks) { |
c82b12a0 | 1847 | add_summary_n(tb, _("Socket(s) per book:"), |
8648ca96 | 1848 | sockets_per_book ?: desc->nsockets / desc->nbooks); |
b3adf6ef | 1849 | if (desc->ndrawers) { |
c82b12a0 | 1850 | add_summary_n(tb, _("Book(s) per drawer:"), |
b3adf6ef | 1851 | books_per_drawer ?: desc->nbooks / desc->ndrawers); |
c82b12a0 | 1852 | add_summary_n(tb, _("Drawer(s):"), drawers ?: desc->ndrawers); |
b3adf6ef | 1853 | } else { |
c82b12a0 | 1854 | add_summary_n(tb, _("Book(s):"), books_per_drawer ?: desc->nbooks); |
b3adf6ef | 1855 | } |
56baaa4e | 1856 | } else { |
c82b12a0 | 1857 | add_summary_n(tb, _("Socket(s):"), sockets_per_book ?: desc->nsockets); |
56baaa4e | 1858 | } |
5dd7507c | 1859 | } |
7e03f383 | 1860 | if (desc->nnodes) |
c82b12a0 | 1861 | add_summary_n(tb, _("NUMA node(s):"), desc->nnodes); |
e8aa16ee | 1862 | if (desc->vendor) |
c82b12a0 | 1863 | add_summary_s(tb, _("Vendor ID:"), desc->vendor); |
0c28f0c8 | 1864 | if (desc->machinetype) |
c82b12a0 | 1865 | add_summary_s(tb, _("Machine type:"), desc->machinetype); |
e8aa16ee | 1866 | if (desc->family) |
c82b12a0 | 1867 | add_summary_s(tb, _("CPU family:"), desc->family); |
c95e3889 | 1868 | if (desc->model || desc->revision) |
c82b12a0 | 1869 | add_summary_s(tb, _("Model:"), desc->revision ? desc->revision : desc->model); |
c95e3889 | 1870 | if (desc->modelname || desc->cpu) |
c82b12a0 | 1871 | add_summary_s(tb, _("Model name:"), desc->cpu ? desc->cpu : desc->modelname); |
e8aa16ee | 1872 | if (desc->stepping) |
c82b12a0 | 1873 | add_summary_s(tb, _("Stepping:"), desc->stepping); |
16ca0551 KZ |
1874 | if (desc->freqboost >= 0) |
1875 | add_summary_s(tb, _("Frequency boost:"), desc->freqboost ? | |
1876 | _("enabled") : _("disabled")); | |
e8aa16ee | 1877 | if (desc->mhz) |
c82b12a0 | 1878 | add_summary_s(tb, _("CPU MHz:"), desc->mhz); |
4632b288 | 1879 | if (desc->dynamic_mhz) |
c82b12a0 | 1880 | add_summary_s(tb, _("CPU dynamic MHz:"), desc->dynamic_mhz); |
4632b288 | 1881 | if (desc->static_mhz) |
c82b12a0 | 1882 | add_summary_s(tb, _("CPU static MHz:"), desc->static_mhz); |
89222b17 KZ |
1883 | if (desc->maxmhz) |
1884 | add_summary_s(tb, _("CPU max MHz:"), cpu_max_mhz(desc, buf, sizeof(buf))); | |
1885 | if (desc->minmhz) | |
1886 | add_summary_s(tb, _("CPU min MHz:"), cpu_min_mhz(desc, buf, sizeof(buf))); | |
9b8d4d5f | 1887 | if (desc->bogomips) |
c82b12a0 | 1888 | add_summary_s(tb, _("BogoMIPS:"), desc->bogomips); |
e8aa16ee KZ |
1889 | if (desc->virtflag) { |
1890 | if (!strcmp(desc->virtflag, "svm")) | |
c82b12a0 | 1891 | add_summary_s(tb, _("Virtualization:"), "AMD-V"); |
e8aa16ee | 1892 | else if (!strcmp(desc->virtflag, "vmx")) |
c82b12a0 | 1893 | add_summary_s(tb, _("Virtualization:"), "VT-x"); |
5dd7507c | 1894 | } |
10829cd7 | 1895 | if (desc->hypervisor) |
c82b12a0 | 1896 | add_summary_s(tb, _("Hypervisor:"), desc->hypervisor); |
e8aa16ee | 1897 | if (desc->hyper) { |
c82b12a0 KZ |
1898 | add_summary_s(tb, _("Hypervisor vendor:"), hv_vendors[desc->hyper]); |
1899 | add_summary_s(tb, _("Virtualization type:"), _(virt_types[desc->virtype])); | |
c8b64f6d | 1900 | } |
a0fff77e | 1901 | if (desc->dispatching >= 0) |
c82b12a0 | 1902 | add_summary_s(tb, _("Dispatching mode:"), _(disp_modes[desc->dispatching])); |
7e03f383 | 1903 | if (desc->ncaches) { |
7e03f383 | 1904 | for (i = desc->ncaches - 1; i >= 0; i--) { |
e5f72113 KZ |
1905 | uint64_t sz = 0; |
1906 | char *tmp; | |
1907 | ||
1908 | if (get_cache_full_size(desc, i, &sz) != 0) | |
1909 | continue; | |
1910 | tmp = size_to_human_string( | |
1911 | SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, | |
1912 | sz); | |
577d1a66 | 1913 | snprintf(buf, sizeof(buf), |
e5f72113 KZ |
1914 | _("%s cache: "), desc->caches[i].name); |
1915 | add_summary_s(tb, buf, tmp); | |
1916 | free(tmp); | |
7e03f383 KZ |
1917 | } |
1918 | } | |
28b1658f | 1919 | if (desc->necaches) { |
28b1658f | 1920 | for (i = desc->necaches - 1; i >= 0; i--) { |
577d1a66 | 1921 | snprintf(buf, sizeof(buf), |
28b1658f | 1922 | _("%s cache:"), desc->ecaches[i].name); |
577d1a66 | 1923 | add_summary_s(tb, buf, desc->ecaches[i].size); |
28b1658f HC |
1924 | } |
1925 | } | |
1926 | ||
4f912c6a | 1927 | for (i = 0; i < desc->nnodes; i++) { |
e9074a16 | 1928 | snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), desc->idx2nodenum[i]); |
c82b12a0 | 1929 | print_cpuset(tb, buf, desc->nodemaps[i], mod->hex); |
5dd7507c | 1930 | } |
ee1f1057 | 1931 | |
bd9b94d1 | 1932 | if (desc->physsockets) { |
c82b12a0 KZ |
1933 | add_summary_n(tb, _("Physical sockets:"), desc->physsockets); |
1934 | add_summary_n(tb, _("Physical chips:"), desc->physchips); | |
1935 | add_summary_n(tb, _("Physical cores/chip:"), desc->physcoresperchip); | |
bd9b94d1 | 1936 | } |
c82b12a0 KZ |
1937 | |
1938 | if (desc->flags) | |
1939 | add_summary_s(tb, _("Flags:"), desc->flags); | |
1940 | ||
1941 | scols_print_table(tb); | |
1942 | scols_unref_table(tb); | |
5dd7507c CQ |
1943 | } |
1944 | ||
6e1eda6f | 1945 | static void __attribute__((__noreturn__)) usage(void) |
5dd7507c | 1946 | { |
6e1eda6f | 1947 | FILE *out = stdout; |
b9d18bc3 KZ |
1948 | size_t i; |
1949 | ||
1950 | fputs(USAGE_HEADER, out); | |
c6f095cf | 1951 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); |
7f1ec5e8 | 1952 | |
451dbcfa BS |
1953 | fputs(USAGE_SEPARATOR, out); |
1954 | fputs(_("Display information about the CPU architecture.\n"), out); | |
1955 | ||
b9d18bc3 | 1956 | fputs(USAGE_OPTIONS, out); |
c6f095cf BS |
1957 | fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out); |
1958 | fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out); | |
1959 | fputs(_(" -c, --offline print offline CPUs only\n"), out); | |
19a5510b | 1960 | fputs(_(" -J, --json use JSON for default or extended format\n"), out); |
c6f095cf BS |
1961 | fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out); |
1962 | fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out); | |
1963 | fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out); | |
1964 | fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out); | |
0d2b5d2a | 1965 | fputs(_(" -y, --physical print physical instead of logical IDs\n"), out); |
c6f095cf | 1966 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 | 1967 | printf(USAGE_HELP_OPTIONS(25)); |
b9d18bc3 | 1968 | |
6e2d5a44 | 1969 | fputs(USAGE_COLUMNS, out); |
3d27b76a KZ |
1970 | for (i = 0; i < ARRAY_SIZE(coldescs); i++) |
1971 | fprintf(out, " %13s %s\n", coldescs[i].name, _(coldescs[i].help)); | |
1972 | ||
f45f3ec3 | 1973 | printf(USAGE_MAN_TAIL("lscpu(1)")); |
4f912c6a | 1974 | |
6e1eda6f | 1975 | exit(EXIT_SUCCESS); |
5dd7507c CQ |
1976 | } |
1977 | ||
1978 | int main(int argc, char *argv[]) | |
1979 | { | |
8005924a | 1980 | struct lscpu_modifier _mod = { .mode = OUTPUT_SUMMARY }, *mod = &_mod; |
87918040 | 1981 | struct lscpu_desc _desc = { .flags = NULL }, *desc = &_desc; |
8005924a | 1982 | int c, i; |
3d27b76a | 1983 | int columns[ARRAY_SIZE(coldescs)], ncolumns = 0; |
7fc12cd2 | 1984 | int cpu_modifier_specified = 0; |
538b50cb | 1985 | size_t setsize; |
5dd7507c | 1986 | |
fbf0619b SK |
1987 | enum { |
1988 | OPT_OUTPUT_ALL = CHAR_MAX + 1, | |
1989 | }; | |
6c7d5ae9 | 1990 | static const struct option longopts[] = { |
87918040 SK |
1991 | { "all", no_argument, NULL, 'a' }, |
1992 | { "online", no_argument, NULL, 'b' }, | |
1993 | { "offline", no_argument, NULL, 'c' }, | |
1994 | { "help", no_argument, NULL, 'h' }, | |
1995 | { "extended", optional_argument, NULL, 'e' }, | |
19a5510b | 1996 | { "json", no_argument, NULL, 'J' }, |
87918040 SK |
1997 | { "parse", optional_argument, NULL, 'p' }, |
1998 | { "sysroot", required_argument, NULL, 's' }, | |
1999 | { "physical", no_argument, NULL, 'y' }, | |
2000 | { "hex", no_argument, NULL, 'x' }, | |
2001 | { "version", no_argument, NULL, 'V' }, | |
fbf0619b | 2002 | { "output-all", no_argument, NULL, OPT_OUTPUT_ALL }, |
87918040 | 2003 | { NULL, 0, NULL, 0 } |
5dd7507c CQ |
2004 | }; |
2005 | ||
8e97eb4b KZ |
2006 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
2007 | { 'a','b','c' }, | |
2008 | { 'e','p' }, | |
2009 | { 0 } | |
2010 | }; | |
2011 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
2012 | ||
2f8f1388 | 2013 | setlocale(LC_ALL, ""); |
5dd7507c CQ |
2014 | bindtextdomain(PACKAGE, LOCALEDIR); |
2015 | textdomain(PACKAGE); | |
efb8854f | 2016 | atexit(close_stdout); |
5dd7507c | 2017 | |
19a5510b | 2018 | while ((c = getopt_long(argc, argv, "abce::hJp::s:xyV", longopts, NULL)) != -1) { |
8e97eb4b KZ |
2019 | |
2020 | err_exclusive_options(c, longopts, excl, excl_st); | |
2021 | ||
5dd7507c | 2022 | switch (c) { |
0ad29ff6 | 2023 | case 'a': |
7afc2387 | 2024 | mod->online = mod->offline = 1; |
7fc12cd2 | 2025 | cpu_modifier_specified = 1; |
0ad29ff6 | 2026 | break; |
23e9e95a KZ |
2027 | case 'b': |
2028 | mod->online = 1; | |
7fc12cd2 | 2029 | cpu_modifier_specified = 1; |
23e9e95a | 2030 | break; |
7afc2387 HC |
2031 | case 'c': |
2032 | mod->offline = 1; | |
7fc12cd2 | 2033 | cpu_modifier_specified = 1; |
7afc2387 | 2034 | break; |
5dd7507c | 2035 | case 'h': |
6e1eda6f | 2036 | usage(); |
19a5510b KZ |
2037 | case 'J': |
2038 | mod->json = 1; | |
2039 | break; | |
5dd7507c | 2040 | case 'p': |
ba45d8c1 | 2041 | case 'e': |
477251f8 KZ |
2042 | if (optarg) { |
2043 | if (*optarg == '=') | |
2044 | optarg++; | |
2045 | ncolumns = string_to_idarray(optarg, | |
2046 | columns, ARRAY_SIZE(columns), | |
2047 | column_name_to_id); | |
2048 | if (ncolumns < 0) | |
2049 | return EXIT_FAILURE; | |
477251f8 | 2050 | } |
ba45d8c1 | 2051 | mod->mode = c == 'p' ? OUTPUT_PARSABLE : OUTPUT_READABLE; |
5dd7507c | 2052 | break; |
47b6e8b6 | 2053 | case 's': |
6e509042 | 2054 | desc->prefix = optarg; |
8148217b | 2055 | mod->system = SYSTEM_SNAPSHOT; |
47b6e8b6 | 2056 | break; |
4f912c6a | 2057 | case 'x': |
8005924a | 2058 | mod->hex = 1; |
4f912c6a | 2059 | break; |
0d2b5d2a HC |
2060 | case 'y': |
2061 | mod->physical = 1; | |
2062 | break; | |
44de912c | 2063 | case 'V': |
f6277500 | 2064 | printf(UTIL_LINUX_VERSION); |
44de912c | 2065 | return EXIT_SUCCESS; |
fbf0619b SK |
2066 | case OPT_OUTPUT_ALL: |
2067 | { | |
2068 | size_t sz; | |
2069 | for (sz = 0; sz < ARRAY_SIZE(coldescs); sz++) | |
2070 | columns[sz] = 1; | |
2071 | break; | |
2072 | } | |
5dd7507c | 2073 | default: |
677ec86c | 2074 | errtryhelp(EXIT_FAILURE); |
5dd7507c CQ |
2075 | } |
2076 | } | |
7bbb7829 | 2077 | |
7fc12cd2 HC |
2078 | if (cpu_modifier_specified && mod->mode == OUTPUT_SUMMARY) { |
2079 | fprintf(stderr, | |
2080 | _("%s: options --all, --online and --offline may only " | |
ac56e555 | 2081 | "be used with options --extended or --parse.\n"), |
7fc12cd2 HC |
2082 | program_invocation_short_name); |
2083 | return EXIT_FAILURE; | |
2084 | } | |
2085 | ||
6e1eda6f RM |
2086 | if (argc != optind) { |
2087 | warnx(_("bad usage")); | |
2088 | errtryhelp(EXIT_FAILURE); | |
2089 | } | |
7bbb7829 | 2090 | |
7afc2387 HC |
2091 | /* set default cpu display mode if none was specified */ |
2092 | if (!mod->online && !mod->offline) { | |
2093 | mod->online = 1; | |
2094 | mod->offline = mod->mode == OUTPUT_READABLE ? 1 : 0; | |
2095 | } | |
5dd7507c | 2096 | |
6e509042 KZ |
2097 | ul_path_init_debug(); |
2098 | ||
2099 | /* /sys/devices/system/cpu */ | |
2100 | desc->syscpu = ul_new_path(_PATH_SYS_CPU); | |
2101 | if (!desc->syscpu) | |
2102 | err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler")); | |
2103 | if (desc->prefix) | |
2104 | ul_path_set_prefix(desc->syscpu, desc->prefix); | |
2105 | ||
2106 | /* /proc */ | |
2107 | desc->procfs = ul_new_path("/proc"); | |
2108 | if (!desc->procfs) | |
2109 | err(EXIT_FAILURE, _("failed to initialize procfs handler")); | |
2110 | if (desc->prefix) | |
2111 | ul_path_set_prefix(desc->procfs, desc->prefix); | |
2112 | ||
8148217b | 2113 | read_basicinfo(desc, mod); |
5dd7507c | 2114 | |
538b50cb SB |
2115 | setsize = CPU_ALLOC_SIZE(maxcpus); |
2116 | ||
a5cfffff | 2117 | for (i = 0; i < desc->ncpuspos; i++) { |
0002704e HC |
2118 | /* only consider present CPUs */ |
2119 | if (desc->present && | |
538b50cb | 2120 | !CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present)) |
0002704e | 2121 | continue; |
7e03f383 KZ |
2122 | read_topology(desc, i); |
2123 | read_cache(desc, i); | |
2b8fcb81 | 2124 | read_polarization(desc, i); |
596b8845 | 2125 | read_address(desc, i); |
d231eea1 | 2126 | read_configured(desc, i); |
44320710 | 2127 | read_max_mhz(desc, i); |
e065a597 | 2128 | read_min_mhz(desc, i); |
47b6e8b6 | 2129 | } |
7e03f383 | 2130 | |
960bf130 KZ |
2131 | if (desc->caches) |
2132 | qsort(desc->caches, desc->ncaches, | |
2133 | sizeof(struct cpu_cache), cachecmp); | |
7e03f383 | 2134 | |
28b1658f HC |
2135 | if (desc->ecaches) |
2136 | qsort(desc->ecaches, desc->necaches, | |
2137 | sizeof(struct cpu_cache), cachecmp); | |
2138 | ||
7e03f383 | 2139 | read_nodes(desc); |
eff79ceb | 2140 | read_hypervisor(desc, mod); |
744d62ee | 2141 | arm_cpu_decode(desc); |
c8b64f6d | 2142 | |
8005924a | 2143 | switch(mod->mode) { |
ba45d8c1 KZ |
2144 | case OUTPUT_SUMMARY: |
2145 | print_summary(desc, mod); | |
2146 | break; | |
2147 | case OUTPUT_PARSABLE: | |
2148 | if (!ncolumns) { | |
2149 | columns[ncolumns++] = COL_CPU; | |
2150 | columns[ncolumns++] = COL_CORE; | |
2151 | columns[ncolumns++] = COL_SOCKET; | |
2152 | columns[ncolumns++] = COL_NODE; | |
2153 | columns[ncolumns++] = COL_CACHE; | |
2154 | mod->compat = 1; | |
2155 | } | |
2156 | print_parsable(desc, columns, ncolumns, mod); | |
2157 | break; | |
2158 | case OUTPUT_READABLE: | |
2159 | if (!ncolumns) { | |
2160 | /* No list was given. Just print whatever is there. */ | |
2161 | columns[ncolumns++] = COL_CPU; | |
2162 | if (desc->nodemaps) | |
8005924a | 2163 | columns[ncolumns++] = COL_NODE; |
b3adf6ef HC |
2164 | if (desc->drawermaps) |
2165 | columns[ncolumns++] = COL_DRAWER; | |
ba45d8c1 KZ |
2166 | if (desc->bookmaps) |
2167 | columns[ncolumns++] = COL_BOOK; | |
2168 | if (desc->socketmaps) | |
2169 | columns[ncolumns++] = COL_SOCKET; | |
2170 | if (desc->coremaps) | |
2171 | columns[ncolumns++] = COL_CORE; | |
2172 | if (desc->caches) | |
8005924a | 2173 | columns[ncolumns++] = COL_CACHE; |
a7e5300c HC |
2174 | if (desc->online) |
2175 | columns[ncolumns++] = COL_ONLINE; | |
d231eea1 HC |
2176 | if (desc->configured) |
2177 | columns[ncolumns++] = COL_CONFIGURED; | |
ba45d8c1 KZ |
2178 | if (desc->polarization) |
2179 | columns[ncolumns++] = COL_POLARIZATION; | |
2180 | if (desc->addresses) | |
2181 | columns[ncolumns++] = COL_ADDRESS; | |
e065a597 DB |
2182 | if (desc->maxmhz) |
2183 | columns[ncolumns++] = COL_MAXMHZ; | |
2184 | if (desc->minmhz) | |
2185 | columns[ncolumns++] = COL_MINMHZ; | |
ba45d8c1 KZ |
2186 | } |
2187 | print_readable(desc, columns, ncolumns, mod); | |
2188 | break; | |
8005924a | 2189 | } |
5dd7507c | 2190 | |
6e509042 KZ |
2191 | ul_unref_path(desc->syscpu); |
2192 | ul_unref_path(desc->procfs); | |
cf474aac | 2193 | return EXIT_SUCCESS; |
5dd7507c | 2194 | } |