]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
Merge branch 'patch-1' of https://github.com/edupont/util-linux
[thirdparty/util-linux.git] / sys-utils / lscpu.c
CommitLineData
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 84static 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 91static 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 110static 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 119static 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 130static const char *disp_modes[] = {
a0fff77e
HC
131 [DISP_HORIZONTAL] = N_("horizontal"),
132 [DISP_VERTICAL] = N_("vertical")
133};
134
2ba641e5 135static 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 143static 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 */
157enum {
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 */
175struct 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
182static 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
199static int
200column_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
219static int
220lookup(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 */
266static int
267lookup_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 */
317static int
8148217b 318init_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
354static int strbe16toh(const char *buf, int offset)
355{
356 return (buf[offset] << 8) + buf[offset+1];
357}
358
359static 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
385static void read_physical_info_powerpc(
386 struct lscpu_desc *desc __attribute__((__unused__)))
387{
388}
389#endif
390
744d62ee 391
5dd7507c 392static void
8148217b 393read_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
SB
510
511 if (mod->system == SYSTEM_LIVE)
512 read_physical_info_powerpc(desc);
0c28f0c8 513
6e509042 514 if ((fp = ul_path_fopen(desc->procfs, "r", "sysinfo"))) {
904ffe1f 515 while (fgets(buf, sizeof(buf), fp) != NULL && !desc->machinetype)
0c28f0c8 516 lookup(buf, "Type", &desc->machinetype);
904ffe1f 517 fclose(fp);
0c28f0c8 518 }
5dd7507c
CQ
519}
520
c8b64f6d 521static int
6e509042 522has_pci_device(struct lscpu_desc *desc, unsigned int vendor, unsigned int device)
c8b64f6d
KZ
523{
524 FILE *f;
13cf6fc8 525 unsigned int num, fn, ven, dev;
c8b64f6d
KZ
526 int res = 1;
527
6e509042 528 f = ul_path_fopen(desc->procfs, "r", "bus/pci/devices");
c8b64f6d
KZ
529 if (!f)
530 return 0;
531
532 /* for more details about bus/pci/devices format see
533 * drivers/pci/proc.c in linux kernel
534 */
535 while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]",
536 &num, &fn, &ven, &dev) == 4) {
537
538 if (ven == vendor && dev == device)
539 goto found;
540 }
541
542 res = 0;
543found:
544 fclose(f);
545 return res;
546}
547
548#if defined(__x86_64__) || defined(__i386__)
549
550/*
551 * This CPUID leaf returns the information about the hypervisor.
552 * EAX : maximum input value for CPUID supported by the hypervisor.
553 * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware.
554 */
555#define HYPERVISOR_INFO_LEAF 0x40000000
556
557static inline void
558cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
559 unsigned int *ecx, unsigned int *edx)
560{
c9239f23
MF
561 __asm__(
562#if defined(__PIC__) && defined(__i386__)
563 /* x86 PIC cannot clobber ebx -- gcc bitches */
7845b91d 564 "xchg %%ebx, %%esi;"
c9239f23 565 "cpuid;"
7845b91d 566 "xchg %%esi, %%ebx;"
c9239f23
MF
567 : "=S" (*ebx),
568#else
569 "cpuid;"
570 : "=b" (*ebx),
571#endif
572 "=a" (*eax),
c8b64f6d
KZ
573 "=c" (*ecx),
574 "=d" (*edx)
bc54770d 575 : "1" (op), "c"(0));
c8b64f6d
KZ
576}
577
578static void
e8aa16ee 579read_hypervisor_cpuid(struct lscpu_desc *desc)
c8b64f6d
KZ
580{
581 unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
582 char hyper_vendor_id[13];
583
584 memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id));
585
586 cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx);
587 memcpy(hyper_vendor_id + 0, &ebx, 4);
588 memcpy(hyper_vendor_id + 4, &ecx, 4);
589 memcpy(hyper_vendor_id + 8, &edx, 4);
590 hyper_vendor_id[12] = '\0';
591
592 if (!hyper_vendor_id[0])
593 return;
594
595 if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12))
e8aa16ee 596 desc->hyper = HYPER_XEN;
c8b64f6d 597 else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9))
e8aa16ee 598 desc->hyper = HYPER_KVM;
c8b64f6d 599 else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12))
e8aa16ee 600 desc->hyper = HYPER_MSHV;
99fbc877
SH
601 else if (!strncmp("VMwareVMware", hyper_vendor_id, 12))
602 desc->hyper = HYPER_VMWARE;
4597b692
BR
603 else if (!strncmp("UnisysSpar64", hyper_vendor_id, 12))
604 desc->hyper = HYPER_SPAR;
c8b64f6d
KZ
605}
606
b7744730 607#else /* ! (__x86_64__ || __i386__) */
c8b64f6d 608static void
6d791b4c 609read_hypervisor_cpuid(struct lscpu_desc *desc __attribute__((__unused__)))
c8b64f6d
KZ
610{
611}
612#endif
613
6e509042 614static int is_devtree_compatible(struct lscpu_desc *desc, const char *str)
3565bd7a 615{
6e509042 616 FILE *fd = ul_path_fopen(desc->procfs, "r", "device-tree/compatible");
3565bd7a 617
3565bd7a
KZ
618 if (fd) {
619 char buf[256];
620 size_t i, len;
621
622 memset(buf, 0, sizeof(buf));
623 len = fread(buf, 1, sizeof(buf) - 1, fd);
624 fclose(fd);
625
626 for (i = 0; i < len;) {
627 if (!strcmp(&buf[i], str))
628 return 1;
629 i += strlen(&buf[i]);
630 i++;
631 }
632 }
633
634 return 0;
635}
636
5bd31c6d
RM
637static int
638read_hypervisor_powerpc(struct lscpu_desc *desc)
639{
640 assert(!desc->hyper);
641
79738105 642 /* IBM iSeries: legacy, para-virtualized on top of OS/400 */
6e509042 643 if (ul_path_access(desc->procfs, F_OK, "iSeries") == 0) {
5bd31c6d
RM
644 desc->hyper = HYPER_OS400;
645 desc->virtype = VIRT_PARA;
79738105
KZ
646
647 /* PowerNV (POWER Non-Virtualized, bare-metal) */
6e509042 648 } else if (is_devtree_compatible(desc, "ibm,powernv")) {
e03b613e
KZ
649 desc->hyper = HYPER_NONE;
650 desc->virtype = VIRT_NONE;
79738105 651
d45984be 652 /* PowerVM (IBM's proprietary hypervisor, aka pHyp) */
6e509042
KZ
653 } else if (ul_path_access(desc->procfs, F_OK, "device-tree/ibm,partition-name") == 0
654 && ul_path_access(desc->procfs, F_OK, "device-tree/hmc-managed?") == 0
655 && ul_path_access(desc->procfs, F_OK, "device-tree/chosen/qemu,graphic-width") != 0) {
656
5bd31c6d
RM
657 FILE *fd;
658 desc->hyper = HYPER_PHYP;
659 desc->virtype = VIRT_PARA;
6e509042
KZ
660
661 fd = ul_path_fopen(desc->procfs, "r", "device-tree/ibm,partition-name");
5bd31c6d
RM
662 if (fd) {
663 char buf[256];
6cbf75e5 664 if (fscanf(fd, "%255s", buf) == 1 && !strcmp(buf, "full"))
5bd31c6d
RM
665 desc->virtype = VIRT_NONE;
666 fclose(fd);
667 }
79738105
KZ
668
669 /* Qemu */
6e509042 670 } else if (is_devtree_compatible(desc, "qemu,pseries")) {
e03b613e
KZ
671 desc->hyper = HYPER_KVM;
672 desc->virtype = VIRT_PARA;
5bd31c6d 673 }
5bd31c6d
RM
674 return desc->hyper;
675}
676
b7744730
RM
677#ifdef INCLUDE_VMWARE_BDOOR
678
679#define VMWARE_BDOOR_MAGIC 0x564D5868
680#define VMWARE_BDOOR_PORT 0x5658
681#define VMWARE_BDOOR_CMD_GETVERSION 10
682
77242032 683static UL_ASAN_BLACKLIST
7845b91d
MF
684void vmware_bdoor(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
685{
686 __asm__(
687#if defined(__PIC__) && defined(__i386__)
688 /* x86 PIC cannot clobber ebx -- gcc bitches */
689 "xchg %%ebx, %%esi;"
690 "inl (%%dx), %%eax;"
691 "xchg %%esi, %%ebx;"
692 : "=S" (*ebx),
693#else
694 "inl (%%dx), %%eax;"
695 : "=b" (*ebx),
696#endif
697 "=a" (*eax),
698 "=c" (*ecx),
699 "=d" (*edx)
700 : "0" (VMWARE_BDOOR_MAGIC),
701 "1" (VMWARE_BDOOR_CMD_GETVERSION),
702 "2" (VMWARE_BDOOR_PORT),
703 "3" (0)
704 : "memory");
705}
b7744730
RM
706
707static jmp_buf segv_handler_env;
708
709static void
710segv_handler(__attribute__((__unused__)) int sig,
711 __attribute__((__unused__)) siginfo_t *info,
712 __attribute__((__unused__)) void *ignored)
713{
714 siglongjmp(segv_handler_env, 1);
715}
716
717static int
718is_vmware_platform(void)
719{
720 uint32_t eax, ebx, ecx, edx;
721 struct sigaction act, oact;
722
b5d61108
KZ
723 /*
724 * FIXME: Not reliable for non-root users. Note it works as expected if
725 * vmware_bdoor() is not optimized for PIE, but then it fails to build
726 * on 32bit x86 systems. See lscpu git log for more details (commit
727 * 7845b91dbc7690064a2be6df690e4aaba728fb04). kzak [3-Nov-2016]
728 */
729 if (getuid() != 0)
730 return 0;
731
b7744730
RM
732 /*
733 * The assembly routine for vmware detection works
734 * fine under vmware, even if ran as regular user. But
735 * on real HW or under other hypervisors, it segfaults (which is
736 * expected). So we temporarily install SIGSEGV handler to catch
737 * the signal. All this magic is needed because lscpu
738 * isn't supposed to require root privileges.
739 */
740 if (sigsetjmp(segv_handler_env, 1))
741 return 0;
742
9dd2d497 743 memset(&act, 0, sizeof(act));
b7744730
RM
744 act.sa_sigaction = segv_handler;
745 act.sa_flags = SA_SIGINFO;
746
747 if (sigaction(SIGSEGV, &act, &oact))
223939d9 748 err(EXIT_FAILURE, _("cannot set signal handler"));
b7744730 749
7845b91d 750 vmware_bdoor(&eax, &ebx, &ecx, &edx);
b7744730
RM
751
752 if (sigaction(SIGSEGV, &oact, NULL))
223939d9 753 err(EXIT_FAILURE, _("cannot restore signal handler"));
b7744730
RM
754
755 return eax != (uint32_t)-1 && ebx == VMWARE_BDOOR_MAGIC;
756}
757
758#else /* ! INCLUDE_VMWARE_BDOOR */
759
760static int
761is_vmware_platform(void)
762{
763 return 0;
764}
765
766#endif /* INCLUDE_VMWARE_BDOOR */
767
c8b64f6d 768static void
eff79ceb 769read_hypervisor(struct lscpu_desc *desc, struct lscpu_modifier *mod)
c8b64f6d 770{
fb2627ce
OO
771 FILE *fd;
772
7572fb2b
SB
773 /* We have to detect WSL first. is_vmware_platform() crashes on Windows 10. */
774
6e509042 775 if ((fd = ul_path_fopen(desc->procfs, "r", "sys/kernel/osrelease"))) {
7572fb2b
SB
776 char buf[256];
777
778 if (fgets(buf, sizeof(buf), fd) != NULL) {
779 if (strstr(buf, "Microsoft")) {
780 desc->hyper = HYPER_WSL;
781 desc->virtype = VIRT_CONT;
782 }
783 }
784 fclose(fd);
785 if (desc->virtype)
786 return;
787 }
788
fb2627ce 789 if (mod->system != SYSTEM_SNAPSHOT) {
eff79ceb 790 read_hypervisor_cpuid(desc);
fb2627ce
OO
791 if (!desc->hyper)
792 desc->hyper = read_hypervisor_dmi();
b7744730
RM
793 if (!desc->hyper && is_vmware_platform())
794 desc->hyper = HYPER_VMWARE;
fb2627ce 795 }
c8b64f6d 796
0ebbe9f1 797 if (desc->hyper) {
e8aa16ee 798 desc->virtype = VIRT_FULL;
c8b64f6d 799
b4d1fbda 800 if (desc->hyper == HYPER_XEN) {
0ebbe9f1
DZ
801 uint32_t features;
802
6e509042
KZ
803 fd = ul_prefix_fopen(desc->prefix, "r", _PATH_SYS_HYP_FEATURES);
804
0ebbe9f1
DZ
805 if (fd && fscanf(fd, "%x", &features) == 1) {
806 /* Xen PV domain */
807 if (features & XEN_FEATURES_PV_MASK)
808 desc->virtype = VIRT_PARA;
809 /* Xen PVH domain */
810 else if ((features & XEN_FEATURES_PVH_MASK)
811 == XEN_FEATURES_PVH_MASK)
812 desc->virtype = VIRT_PARA;
0ebbe9f1 813 }
d29b7ced
KZ
814 if (fd)
815 fclose(fd);
0ebbe9f1
DZ
816 }
817 } else if (read_hypervisor_powerpc(desc) > 0) {}
5bd31c6d 818
fb2627ce 819 /* Xen para-virt or dom0 */
6e509042 820 else if (ul_path_access(desc->procfs, F_OK, "xen") == 0) {
c8b64f6d
KZ
821 int dom0 = 0;
822
6e509042 823 fd = ul_path_fopen(desc->procfs, "r", "xen/capabilities");
c8b64f6d
KZ
824 if (fd) {
825 char buf[256];
826
6cbf75e5 827 if (fscanf(fd, "%255s", buf) == 1 &&
c8b64f6d
KZ
828 !strcmp(buf, "control_d"))
829 dom0 = 1;
830 fclose(fd);
831 }
e8aa16ee
KZ
832 desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA;
833 desc->hyper = HYPER_XEN;
c8b64f6d 834
fb2627ce 835 /* Xen full-virt on non-x86_64 */
6e509042 836 } else if (has_pci_device(desc, hv_vendor_pci[HYPER_XEN], hv_graphics_pci[HYPER_XEN])) {
e8aa16ee
KZ
837 desc->hyper = HYPER_XEN;
838 desc->virtype = VIRT_FULL;
6e509042 839 } else if (has_pci_device(desc, hv_vendor_pci[HYPER_VMWARE], hv_graphics_pci[HYPER_VMWARE])) {
96ce475f
RM
840 desc->hyper = HYPER_VMWARE;
841 desc->virtype = VIRT_FULL;
6e509042 842 } else if (has_pci_device(desc, hv_vendor_pci[HYPER_VBOX], hv_graphics_pci[HYPER_VBOX])) {
96ce475f
RM
843 desc->hyper = HYPER_VBOX;
844 desc->virtype = VIRT_FULL;
fb2627ce
OO
845
846 /* IBM PR/SM */
6e509042 847 } else if ((fd = ul_path_fopen(desc->procfs, "r", "sysinfo"))) {
b8ec7bdf
HC
848 char buf[BUFSIZ];
849
850 desc->hyper = HYPER_IBM;
10829cd7 851 desc->hypervisor = "PR/SM";
b8ec7bdf 852 desc->virtype = VIRT_FULL;
904ffe1f 853 while (fgets(buf, sizeof(buf), fd) != NULL) {
d0b75b03 854 char *str, *p;
10829cd7 855
b8ec7bdf
HC
856 if (!strstr(buf, "Control Program:"))
857 continue;
858 if (!strstr(buf, "KVM"))
859 desc->hyper = HYPER_IBM;
860 else
861 desc->hyper = HYPER_KVM;
d0b75b03
KZ
862 p = strchr(buf, ':');
863 if (!p)
10829cd7 864 continue;
d0b75b03 865 xasprintf(&str, "%s", p + 1);
9d8537ff 866
10829cd7
HC
867 /* remove leading, trailing and repeating whitespace */
868 while (*str == ' ')
869 str++;
870 desc->hypervisor = str;
871 str += strlen(str) - 1;
872 while ((*str == '\n') || (*str == ' '))
873 *(str--) = '\0';
874 while ((str = strstr(desc->hypervisor, " ")))
875 memmove(str, str + 1, strlen(str));
350f5c8d 876 break;
b8ec7bdf 877 }
904ffe1f 878 fclose(fd);
c8b64f6d 879 }
fb2627ce
OO
880
881 /* OpenVZ/Virtuozzo - /proc/vz dir should exist
882 * /proc/bc should not */
6e509042
KZ
883 else if (ul_path_access(desc->procfs, F_OK, "vz") == 0 &&
884 ul_path_access(desc->procfs, F_OK, "bc") != 0) {
fb2627ce 885 desc->hyper = HYPER_PARALLELS;
857d9cdf 886 desc->virtype = VIRT_CONT;
fb2627ce
OO
887
888 /* IBM */
857d9cdf 889 } else if (desc->vendor &&
fb2627ce 890 (strcmp(desc->vendor, "PowerVM Lx86") == 0 ||
857d9cdf 891 strcmp(desc->vendor, "IBM/S390") == 0)) {
fb2627ce 892 desc->hyper = HYPER_IBM;
857d9cdf 893 desc->virtype = VIRT_FULL;
fb2627ce
OO
894
895 /* User-mode-linux */
857d9cdf 896 } else if (desc->modelname && strstr(desc->modelname, "UML")) {
fb2627ce 897 desc->hyper = HYPER_UML;
857d9cdf 898 desc->virtype = VIRT_PARA;
fb2627ce
OO
899
900 /* Linux-VServer */
6e509042 901 } else if ((fd = ul_path_fopen(desc->procfs, "r", "self/status"))) {
fb2627ce
OO
902 char buf[BUFSIZ];
903 char *val = NULL;
904
fb2627ce
OO
905 while (fgets(buf, sizeof(buf), fd) != NULL) {
906 if (lookup(buf, "VxID", &val))
907 break;
908 }
909 fclose(fd);
910
911 if (val) {
c6f6afc1
KZ
912 char *org = val;
913
fb2627ce
OO
914 while (isdigit(*val))
915 ++val;
857d9cdf 916 if (!*val) {
fb2627ce 917 desc->hyper = HYPER_VSERVER;
857d9cdf
KZ
918 desc->virtype = VIRT_CONT;
919 }
c6f6afc1 920 free(org);
fb2627ce
OO
921 }
922 }
c8b64f6d
KZ
923}
924
455fe9a0 925/* add @set to the @ary, unnecessary set is deallocated. */
7e03f383
KZ
926static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set)
927{
928 int i;
929 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
930
931 if (!ary)
932 return -1;
933
934 for (i = 0; i < *items; i++) {
935 if (CPU_EQUAL_S(setsize, set, ary[i]))
936 break;
937 }
938 if (i == *items) {
939 ary[*items] = set;
940 ++*items;
941 return 0;
942 }
943 CPU_FREE(set);
944 return 1;
945}
946
5dd7507c 947static void
4f642863 948read_topology(struct lscpu_desc *desc, int idx)
5dd7507c 949{
b3adf6ef
HC
950 cpu_set_t *thread_siblings, *core_siblings;
951 cpu_set_t *book_siblings, *drawer_siblings;
0d2b5d2a
HC
952 int coreid, socketid, bookid, drawerid;
953 int i, num = real_cpu_num(desc, idx);
7e03f383 954
6e509042 955 if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/topology/thread_siblings", num) != 0)
7e03f383 956 return;
5dd7507c 957
6e509042
KZ
958 ul_path_readf_cpuset(desc->syscpu, &thread_siblings, maxcpus,
959 "cpu%d/topology/thread_siblings", num);
960 ul_path_readf_cpuset(desc->syscpu, &core_siblings, maxcpus,
961 "cpu%d/topology/core_siblings", num);
962 ul_path_readf_cpuset(desc->syscpu, &book_siblings, maxcpus,
963 "cpu%d/topology/book_siblings", num);
964 ul_path_readf_cpuset(desc->syscpu, &drawer_siblings, maxcpus,
965 "cpu%d/topology/drawer_siblings", num);
966
967 if (ul_path_readf_s32(desc->syscpu, &coreid, "cpu%d/topology/core_id", num) != 0)
968 coreid = -1;
969
970 if (ul_path_readf_s32(desc->syscpu, &socketid, "cpu%d/topology/physical_package_id", num) != 0)
971 socketid = -1;
972
973 if (ul_path_readf_s32(desc->syscpu, &bookid, "cpu%d/topology/book_id", num) != 0)
974 bookid = -1;
975
976 if (ul_path_readf_s32(desc->syscpu, &drawerid, "cpu%d/topology/drawer_id", num) != 0)
977 drawerid = -1;
aac1e59e
KZ
978
979 if (!desc->coremaps) {
b3adf6ef 980 int ndrawers, nbooks, nsockets, ncores, nthreads;
7e03f383
KZ
981 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
982
983 /* threads within one core */
984 nthreads = CPU_COUNT_S(setsize, thread_siblings);
d4bfa64d
KZ
985 if (!nthreads)
986 nthreads = 1;
987
7e03f383
KZ
988 /* cores within one socket */
989 ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads;
d4bfa64d
KZ
990 if (!ncores)
991 ncores = 1;
992
993 /* number of sockets within one book. Because of odd /
994 * non-present cpu maps and to keep calculation easy we make
995 * sure that nsockets and nbooks is at least 1.
32a46618 996 */
d4bfa64d
KZ
997 nsockets = desc->ncpus / nthreads / ncores;
998 if (!nsockets)
999 nsockets = 1;
1000
56baaa4e 1001 /* number of books */
d4bfa64d
KZ
1002 nbooks = desc->ncpus / nthreads / ncores / nsockets;
1003 if (!nbooks)
1004 nbooks = 1;
e282eec2 1005
b3adf6ef
HC
1006 /* number of drawers */
1007 ndrawers = desc->ncpus / nbooks / nthreads / ncores / nsockets;
1008 if (!ndrawers)
1009 ndrawers = 1;
1010
e282eec2 1011 /* all threads, see also read_basicinfo()
32a46618 1012 * -- fallback for kernels without
e282eec2
KZ
1013 * /sys/devices/system/cpu/online.
1014 */
1015 if (!desc->nthreads)
b3adf6ef 1016 desc->nthreads = ndrawers * nbooks * nsockets * ncores * nthreads;
d4bfa64d 1017
a5cfffff 1018 /* For each map we make sure that it can have up to ncpuspos
9d1a3a18
HC
1019 * entries. This is because we cannot reliably calculate the
1020 * number of cores, sockets and books on all architectures.
1021 * E.g. completely virtualized architectures like s390 may
1022 * have multiple sockets of different sizes.
1023 */
a5cfffff
TK
1024 desc->coremaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *));
1025 desc->socketmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *));
0d2b5d2a
HC
1026 desc->coreids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids));
1027 desc->socketids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids));
1028 for (i = 0; i < desc->ncpuspos; i++)
1029 desc->coreids[i] = desc->socketids[i] = -1;
1030 if (book_siblings) {
a5cfffff 1031 desc->bookmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *));
0d2b5d2a
HC
1032 desc->bookids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids));
1033 for (i = 0; i < desc->ncpuspos; i++)
1034 desc->bookids[i] = -1;
1035 }
1036 if (drawer_siblings) {
b3adf6ef 1037 desc->drawermaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *));
0d2b5d2a
HC
1038 desc->drawerids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids));
1039 for (i = 0; i < desc->ncpuspos; i++)
1040 desc->drawerids[i] = -1;
1041 }
7e03f383 1042 }
5dd7507c 1043
7e03f383 1044 add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings);
0d2b5d2a 1045 desc->coreids[idx] = coreid;
7e03f383 1046 add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings);
0d2b5d2a
HC
1047 desc->socketids[idx] = socketid;
1048 if (book_siblings) {
56baaa4e 1049 add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings);
0d2b5d2a
HC
1050 desc->bookids[idx] = bookid;
1051 }
1052 if (drawer_siblings) {
b3adf6ef 1053 add_cpuset_to_array(desc->drawermaps, &desc->ndrawers, drawer_siblings);
0d2b5d2a
HC
1054 desc->drawerids[idx] = drawerid;
1055 }
7e03f383 1056}
4f642863 1057
2b8fcb81 1058static void
4f642863 1059read_polarization(struct lscpu_desc *desc, int idx)
2b8fcb81
HC
1060{
1061 char mode[64];
4f642863 1062 int num = real_cpu_num(desc, idx);
2b8fcb81
HC
1063
1064 if (desc->dispatching < 0)
1065 return;
6e509042 1066 if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/polarization", num) != 0)
2b8fcb81
HC
1067 return;
1068 if (!desc->polarization)
a5cfffff 1069 desc->polarization = xcalloc(desc->ncpuspos, sizeof(int));
6e509042
KZ
1070
1071 ul_path_readf_buffer(desc->syscpu, mode, sizeof(mode), "cpu%d/polarization", num);
1072
2b8fcb81 1073 if (strncmp(mode, "vertical:low", sizeof(mode)) == 0)
4f642863 1074 desc->polarization[idx] = POLAR_VLOW;
2b8fcb81 1075 else if (strncmp(mode, "vertical:medium", sizeof(mode)) == 0)
4f642863 1076 desc->polarization[idx] = POLAR_VMEDIUM;
2b8fcb81 1077 else if (strncmp(mode, "vertical:high", sizeof(mode)) == 0)
4f642863 1078 desc->polarization[idx] = POLAR_VHIGH;
2b8fcb81 1079 else if (strncmp(mode, "horizontal", sizeof(mode)) == 0)
4f642863 1080 desc->polarization[idx] = POLAR_HORIZONTAL;
2b8fcb81 1081 else
4f642863 1082 desc->polarization[idx] = POLAR_UNKNOWN;
2b8fcb81 1083}
7e03f383 1084
596b8845 1085static void
4f642863 1086read_address(struct lscpu_desc *desc, int idx)
596b8845 1087{
4f642863
AT
1088 int num = real_cpu_num(desc, idx);
1089
6e509042 1090 if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/address", num) != 0)
596b8845
HC
1091 return;
1092 if (!desc->addresses)
a5cfffff 1093 desc->addresses = xcalloc(desc->ncpuspos, sizeof(int));
6e509042 1094 ul_path_readf_s32(desc->syscpu, &desc->addresses[idx], "cpu%d/address", num);
596b8845 1095}
7e03f383 1096
d231eea1 1097static void
4f642863 1098read_configured(struct lscpu_desc *desc, int idx)
d231eea1 1099{
4f642863
AT
1100 int num = real_cpu_num(desc, idx);
1101
6e509042 1102 if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/configure", num) != 0)
d231eea1
HC
1103 return;
1104 if (!desc->configured)
a5cfffff 1105 desc->configured = xcalloc(desc->ncpuspos, sizeof(int));
6e509042 1106 ul_path_readf_s32(desc->syscpu, &desc->configured[idx], "cpu%d/configure", num);
d231eea1
HC
1107}
1108
fc07d9f5 1109/* Read overall maximum frequency of cpu */
89222b17
KZ
1110static char *
1111cpu_max_mhz(struct lscpu_desc *desc, char *buf, size_t bufsz)
fc07d9f5
MI
1112{
1113 int i;
95f09bc6 1114 float cpu_freq = 0.0;
538b50cb 1115 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
fc07d9f5
MI
1116
1117 if (desc->present) {
95f09bc6 1118 for (i = 0; i < desc->ncpuspos; i++) {
538b50cb 1119 if (CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present)
0145d84a 1120 && desc->maxmhz[i]) {
fc07d9f5
MI
1121 float freq = atof(desc->maxmhz[i]);
1122
1123 if (freq > cpu_freq)
1124 cpu_freq = freq;
1125 }
1126 }
1127 }
89222b17
KZ
1128 snprintf(buf, bufsz, "%.4f", cpu_freq);
1129 return buf;
fc07d9f5
MI
1130}
1131
1132/* Read overall minimum frequency of cpu */
89222b17
KZ
1133static char *
1134cpu_min_mhz(struct lscpu_desc *desc, char *buf, size_t bufsz)
fc07d9f5 1135{
95f09bc6
DM
1136 int i;
1137 float cpu_freq = -1.0;
538b50cb 1138 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
fc07d9f5
MI
1139
1140 if (desc->present) {
95f09bc6 1141 for (i = 0; i < desc->ncpuspos; i++) {
538b50cb 1142 if (CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present)
0145d84a 1143 && desc->minmhz[i]) {
fc07d9f5
MI
1144 float freq = atof(desc->minmhz[i]);
1145
95f09bc6 1146 if (cpu_freq < 0.0 || freq < cpu_freq)
fc07d9f5
MI
1147 cpu_freq = freq;
1148 }
1149 }
1150 }
89222b17
KZ
1151 snprintf(buf, bufsz, "%.4f", cpu_freq);
1152 return buf;
fc07d9f5
MI
1153}
1154
1155
44320710 1156static void
4f642863 1157read_max_mhz(struct lscpu_desc *desc, int idx)
44320710 1158{
4f642863 1159 int num = real_cpu_num(desc, idx);
6e509042 1160 int mhz;
4f642863 1161
6e509042 1162 if (ul_path_readf_s32(desc->syscpu, &mhz, "cpu%d/cpufreq/cpuinfo_max_freq", num) != 0)
44320710 1163 return;
e065a597
DB
1164 if (!desc->maxmhz)
1165 desc->maxmhz = xcalloc(desc->ncpuspos, sizeof(char *));
6e509042 1166 xasprintf(&desc->maxmhz[idx], "%.4f", (float) mhz / 1000);
44320710
SK
1167}
1168
e065a597 1169static void
4f642863 1170read_min_mhz(struct lscpu_desc *desc, int idx)
e065a597 1171{
4f642863 1172 int num = real_cpu_num(desc, idx);
6e509042 1173 int mhz;
4f642863 1174
6e509042 1175 if (ul_path_readf_s32(desc->syscpu, &mhz, "cpu%d/cpufreq/cpuinfo_min_freq", num) != 0)
e065a597
DB
1176 return;
1177 if (!desc->minmhz)
1178 desc->minmhz = xcalloc(desc->ncpuspos, sizeof(char *));
6e509042 1179 xasprintf(&desc->minmhz[idx], "%.4f", (float) mhz / 1000);
e065a597
DB
1180}
1181
7e03f383
KZ
1182static int
1183cachecmp(const void *a, const void *b)
1184{
1185 struct cpu_cache *c1 = (struct cpu_cache *) a;
1186 struct cpu_cache *c2 = (struct cpu_cache *) b;
1187
1188 return strcmp(c2->name, c1->name);
5dd7507c
CQ
1189}
1190
1191static void
4f642863 1192read_cache(struct lscpu_desc *desc, int idx)
5dd7507c
CQ
1193{
1194 char buf[256];
7e03f383 1195 int i;
4f642863 1196 int num = real_cpu_num(desc, idx);
5dd7507c 1197
aac1e59e 1198 if (!desc->ncaches) {
6e509042
KZ
1199 while (ul_path_accessf(desc->syscpu, F_OK,
1200 "cpu%d/cache/index%d",
1201 num, desc->ncaches) == 0)
7e03f383 1202 desc->ncaches++;
5dd7507c 1203
7e03f383
KZ
1204 if (!desc->ncaches)
1205 return;
08de16d0 1206 desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches));
7e03f383
KZ
1207 }
1208 for (i = 0; i < desc->ncaches; i++) {
1209 struct cpu_cache *ca = &desc->caches[i];
1210 cpu_set_t *map;
1211
6e509042
KZ
1212 if (ul_path_accessf(desc->syscpu, F_OK,
1213 "cpu%d/cache/index%d", num, i) != 0)
dcdead42 1214 continue;
7e03f383 1215 if (!ca->name) {
6e509042 1216 int type = 0, level;
7e03f383
KZ
1217
1218 /* cache type */
6e509042
KZ
1219 if (ul_path_readf_buffer(desc->syscpu, buf, sizeof(buf),
1220 "cpu%d/cache/index%d/type", num, i) > 0) {
1221 if (!strcmp(buf, "Data"))
1222 type = 'd';
1223 else if (!strcmp(buf, "Instruction"))
1224 type = 'i';
1225 }
7e03f383
KZ
1226
1227 /* cache level */
6e509042
KZ
1228 ul_path_readf_s32(desc->syscpu, &level,
1229 "cpu%d/cache/index%d/level", num, i);
7e03f383
KZ
1230 if (type)
1231 snprintf(buf, sizeof(buf), "L%d%c", level, type);
1232 else
1233 snprintf(buf, sizeof(buf), "L%d", level);
1234
1235 ca->name = xstrdup(buf);
1236
1237 /* cache size */
6e509042
KZ
1238 if (ul_path_readf_string(desc->syscpu, &ca->size,
1239 "cpu%d/cache/index%d/size", num, i) < 0)
10d927ab 1240 ca->size = xstrdup("unknown size");
7e03f383 1241 }
5dd7507c 1242
7e03f383 1243 /* information about how CPUs share different caches */
6e509042
KZ
1244 ul_path_readf_cpuset(desc->syscpu, &map, maxcpus,
1245 "cpu%d/cache/index%d/shared_cpu_map", num, i);
5dd7507c 1246
08de16d0 1247 if (!ca->sharedmaps)
a5cfffff 1248 ca->sharedmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *));
7e03f383 1249 add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map);
5dd7507c
CQ
1250 }
1251}
1252
e9074a16
KZ
1253static inline int is_node_dirent(struct dirent *d)
1254{
1255 return
1256 d &&
1257#ifdef _DIRENT_HAVE_D_TYPE
c0cf4ae9 1258 (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) &&
e9074a16
KZ
1259#endif
1260 strncmp(d->d_name, "node", 4) == 0 &&
1261 isdigit_string(d->d_name + 4);
1262}
1263
39c758d1
KZ
1264static int
1265nodecmp(const void *ap, const void *bp)
1266{
1267 int *a = (int *) ap, *b = (int *) bp;
1268 return *a - *b;
1269}
1270
5dd7507c 1271static void
e8aa16ee 1272read_nodes(struct lscpu_desc *desc)
5dd7507c 1273{
e9074a16
KZ
1274 int i = 0;
1275 DIR *dir;
1276 struct dirent *d;
6e509042 1277 struct path_cxt *sysnode;
5dd7507c 1278
a25fb9e8
RM
1279 desc->nnodes = 0;
1280
6e509042
KZ
1281 sysnode = ul_new_path(_PATH_SYS_NODE);
1282 if (!sysnode)
1283 err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_NODE);
1284 ul_path_set_prefix(sysnode, desc->prefix);
1285
1286 dir = ul_path_opendir(sysnode, NULL);
1287 if (!dir)
1288 goto done;
1289
a25fb9e8 1290 while ((d = readdir(dir))) {
e9074a16
KZ
1291 if (is_node_dirent(d))
1292 desc->nnodes++;
1293 }
1294
1295 if (!desc->nnodes) {
a25fb9e8 1296 closedir(dir);
6e509042 1297 goto done;
e9074a16 1298 }
5dd7507c 1299
08de16d0 1300 desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *));
e9074a16
KZ
1301 desc->idx2nodenum = xmalloc(desc->nnodes * sizeof(int));
1302
a25fb9e8
RM
1303 rewinddir(dir);
1304 while ((d = readdir(dir)) && i < desc->nnodes) {
1305 if (is_node_dirent(d))
1306 desc->idx2nodenum[i++] = strtol_or_err(((d->d_name) + 4),
1307 _("Failed to extract the node number"));
e9074a16 1308 }
a25fb9e8
RM
1309 closedir(dir);
1310 qsort(desc->idx2nodenum, desc->nnodes, sizeof(int), nodecmp);
5dd7507c
CQ
1311
1312 /* information about how nodes share different CPUs */
7e03f383 1313 for (i = 0; i < desc->nnodes; i++)
6e509042
KZ
1314 ul_path_readf_cpuset(sysnode, &desc->nodemaps[i], maxcpus,
1315 "node%d/cpumap", desc->idx2nodenum[i]);
1316done:
1317 ul_unref_path(sysnode);
5dd7507c
CQ
1318}
1319
e3b3a2f3 1320static char *
4f642863 1321get_cell_data(struct lscpu_desc *desc, int idx, int col,
e3b3a2f3
KZ
1322 struct lscpu_modifier *mod,
1323 char *buf, size_t bufsz)
5dd7507c 1324{
7e03f383 1325 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
4f642863
AT
1326 size_t i;
1327 int cpu = real_cpu_num(desc, idx);
e3b3a2f3
KZ
1328
1329 *buf = '\0';
5dd7507c 1330
477251f8
KZ
1331 switch (col) {
1332 case COL_CPU:
e3b3a2f3 1333 snprintf(buf, bufsz, "%d", cpu);
477251f8
KZ
1334 break;
1335 case COL_CORE:
0d2b5d2a
HC
1336 if (mod->physical) {
1337 if (desc->coreids[idx] == -1)
1338 snprintf(buf, bufsz, "-");
1339 else
1340 snprintf(buf, bufsz, "%d", desc->coreids[idx]);
1341 } else {
1342 if (cpuset_ary_isset(cpu, desc->coremaps,
1343 desc->ncores, setsize, &i) == 0)
1344 snprintf(buf, bufsz, "%zu", i);
1345 }
477251f8
KZ
1346 break;
1347 case COL_SOCKET:
0d2b5d2a
HC
1348 if (mod->physical) {
1349 if (desc->socketids[idx] == -1)
1350 snprintf(buf, bufsz, "-");
1351 else
1352 snprintf(buf, bufsz, "%d", desc->socketids[idx]);
1353 } else {
1354 if (cpuset_ary_isset(cpu, desc->socketmaps,
1355 desc->nsockets, setsize, &i) == 0)
1356 snprintf(buf, bufsz, "%zu", i);
1357 }
477251f8
KZ
1358 break;
1359 case COL_NODE:
e9d659ea 1360 if (cpuset_ary_isset(cpu, desc->nodemaps,
4f642863 1361 desc->nnodes, setsize, &i) == 0)
e9074a16 1362 snprintf(buf, bufsz, "%d", desc->idx2nodenum[i]);
477251f8 1363 break;
b3adf6ef 1364 case COL_DRAWER:
0d2b5d2a
HC
1365 if (mod->physical) {
1366 if (desc->drawerids[idx] == -1)
1367 snprintf(buf, bufsz, "-");
1368 else
1369 snprintf(buf, bufsz, "%d", desc->drawerids[idx]);
1370 } else {
1371 if (cpuset_ary_isset(cpu, desc->drawermaps,
1372 desc->ndrawers, setsize, &i) == 0)
1373 snprintf(buf, bufsz, "%zu", i);
1374 }
b3adf6ef 1375 break;
477251f8 1376 case COL_BOOK:
0d2b5d2a
HC
1377 if (mod->physical) {
1378 if (desc->bookids[idx] == -1)
1379 snprintf(buf, bufsz, "-");
1380 else
1381 snprintf(buf, bufsz, "%d", desc->bookids[idx]);
1382 } else {
1383 if (cpuset_ary_isset(cpu, desc->bookmaps,
1384 desc->nbooks, setsize, &i) == 0)
1385 snprintf(buf, bufsz, "%zu", i);
1386 }
477251f8
KZ
1387 break;
1388 case COL_CACHE:
e3b3a2f3
KZ
1389 {
1390 char *p = buf;
1391 size_t sz = bufsz;
1392 int j;
1393
7e03f383
KZ
1394 for (j = desc->ncaches - 1; j >= 0; j--) {
1395 struct cpu_cache *ca = &desc->caches[j];
e9d659ea
KZ
1396
1397 if (cpuset_ary_isset(cpu, ca->sharedmaps,
4f642863 1398 ca->nsharedmaps, setsize, &i) == 0) {
73f2bec5 1399 int x = snprintf(p, sz, "%zu", i);
06fa5817 1400 if (x < 0 || (size_t) x >= sz)
e3b3a2f3
KZ
1401 return NULL;
1402 p += x;
1403 sz -= x;
1404 }
1405 if (j != 0) {
06fa5817
YK
1406 if (sz < 2)
1407 return NULL;
e3b3a2f3
KZ
1408 *p++ = mod->compat ? ',' : ':';
1409 *p = '\0';
0c5bbafa 1410 sz--;
e3b3a2f3 1411 }
5dd7507c 1412 }
477251f8 1413 break;
e3b3a2f3 1414 }
2b8fcb81 1415 case COL_POLARIZATION:
ba45d8c1 1416 if (desc->polarization) {
4f642863 1417 int x = desc->polarization[idx];
ba45d8c1 1418
e3b3a2f3 1419 snprintf(buf, bufsz, "%s",
ba45d8c1
KZ
1420 mod->mode == OUTPUT_PARSABLE ?
1421 polar_modes[x].parsable :
1422 polar_modes[x].readable);
1423 }
2b8fcb81 1424 break;
596b8845
HC
1425 case COL_ADDRESS:
1426 if (desc->addresses)
4f642863 1427 snprintf(buf, bufsz, "%d", desc->addresses[idx]);
596b8845 1428 break;
d231eea1 1429 case COL_CONFIGURED:
e43fc13e
HC
1430 if (!desc->configured)
1431 break;
1432 if (mod->mode == OUTPUT_PARSABLE)
f205c90a 1433 snprintf(buf, bufsz, "%s",
4f642863 1434 desc->configured[idx] ? _("Y") : _("N"));
e43fc13e 1435 else
f205c90a 1436 snprintf(buf, bufsz, "%s",
4f642863 1437 desc->configured[idx] ? _("yes") : _("no"));
d231eea1 1438 break;
a7e5300c 1439 case COL_ONLINE:
e43fc13e
HC
1440 if (!desc->online)
1441 break;
1442 if (mod->mode == OUTPUT_PARSABLE)
f205c90a 1443 snprintf(buf, bufsz, "%s",
e43fc13e
HC
1444 is_cpu_online(desc, cpu) ? _("Y") : _("N"));
1445 else
f205c90a 1446 snprintf(buf, bufsz, "%s",
847b982e 1447 is_cpu_online(desc, cpu) ? _("yes") : _("no"));
a7e5300c 1448 break;
e065a597 1449 case COL_MAXMHZ:
0145d84a 1450 if (desc->maxmhz && desc->maxmhz[idx])
4f642863 1451 xstrncpy(buf, desc->maxmhz[idx], bufsz);
e065a597
DB
1452 break;
1453 case COL_MINMHZ:
0145d84a 1454 if (desc->minmhz && desc->minmhz[idx])
4f642863 1455 xstrncpy(buf, desc->minmhz[idx], bufsz);
44320710 1456 break;
477251f8 1457 }
e3b3a2f3
KZ
1458 return buf;
1459}
1460
1461static char *
1462get_cell_header(struct lscpu_desc *desc, int col,
1463 struct lscpu_modifier *mod,
1464 char *buf, size_t bufsz)
1465{
1466 *buf = '\0';
1467
1468 if (col == COL_CACHE) {
1469 char *p = buf;
1470 size_t sz = bufsz;
1471 int i;
1472
1473 for (i = desc->ncaches - 1; i >= 0; i--) {
1474 int x = snprintf(p, sz, "%s", desc->caches[i].name);
06fa5817 1475 if (x < 0 || (size_t) x >= sz)
e3b3a2f3
KZ
1476 return NULL;
1477 sz -= x;
1478 p += x;
1479 if (i > 0) {
06fa5817
YK
1480 if (sz < 2)
1481 return NULL;
e3b3a2f3
KZ
1482 *p++ = mod->compat ? ',' : ':';
1483 *p = '\0';
0c5bbafa 1484 sz--;
e3b3a2f3
KZ
1485 }
1486 }
1487 if (desc->ncaches)
1488 return buf;
1489 }
756d79cd 1490 snprintf(buf, bufsz, "%s", coldescs[col].name);
e3b3a2f3 1491 return buf;
477251f8
KZ
1492}
1493
1494/*
ba45d8c1 1495 * [-p] backend, we support two parsable formats:
477251f8
KZ
1496 *
1497 * 1) "compatible" -- this format is compatible with the original lscpu(1)
1498 * output and it contains fixed set of the columns. The CACHE columns are at
1499 * the end of the line and the CACHE is not printed if the number of the caches
1500 * is zero. The CACHE columns are separated by two commas, for example:
1501 *
1502 * $ lscpu --parse
1503 * # CPU,Core,Socket,Node,,L1d,L1i,L2
1504 * 0,0,0,0,,0,0,0
1505 * 1,1,0,0,,1,1,0
1506 *
1507 * 2) "user defined output" -- this format prints always all columns without
1508 * special prefix for CACHE column. If there are not CACHEs then the column is
1509 * empty and the header "Cache" is printed rather than a real name of the cache.
1510 * The CACHE columns are separated by ':'.
1511 *
1512 * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE
1513 * # CPU,Core,Socket,Node,L1d:L1i:L2
1514 * 0,0,0,0,0:0:0
1515 * 1,1,0,0,1:1:0
1516 */
1517static void
8005924a
KZ
1518print_parsable(struct lscpu_desc *desc, int cols[], int ncols,
1519 struct lscpu_modifier *mod)
477251f8 1520{
e3b3a2f3
KZ
1521 char buf[BUFSIZ], *data;
1522 int i;
477251f8 1523
e3b3a2f3
KZ
1524 /*
1525 * Header
1526 */
477251f8
KZ
1527 printf(_(
1528 "# The following is the parsable format, which can be fed to other\n"
1529 "# programs. Each different item in every column has an unique ID\n"
1530 "# starting from zero.\n"));
1531
1532 fputs("# ", stdout);
1533 for (i = 0; i < ncols; i++) {
3d27b76a 1534 int col = cols[i];
b9d18bc3 1535
3d27b76a 1536 if (col == COL_CACHE) {
8005924a 1537 if (mod->compat && !desc->ncaches)
477251f8 1538 continue;
8005924a 1539 if (mod->compat && i != 0)
477251f8 1540 putchar(',');
477251f8 1541 }
e3b3a2f3
KZ
1542 if (i > 0)
1543 putchar(',');
1544
3d27b76a 1545 data = get_cell_header(desc, col, mod, buf, sizeof(buf));
b9d18bc3 1546
3d27b76a
KZ
1547 if (data && * data && col != COL_CACHE &&
1548 !coldescs[col].is_abbr) {
1549 /*
1550 * For normal column names use mixed case (e.g. "Socket")
1551 */
1552 char *p = data + 1;
1553
14e8be8a
PU
1554 while (p && *p != '\0') {
1555 *p = tolower((unsigned int) *p);
1556 p++;
1557 }
3d27b76a 1558 }
e3b3a2f3 1559 fputs(data && *data ? data : "", stdout);
477251f8
KZ
1560 }
1561 putchar('\n');
1562
e3b3a2f3
KZ
1563 /*
1564 * Data
1565 */
a5cfffff 1566 for (i = 0; i < desc->ncpuspos; i++) {
e3b3a2f3 1567 int c;
4f642863 1568 int cpu = real_cpu_num(desc, i);
e3b3a2f3 1569
4f642863 1570 if (!mod->offline && desc->online && !is_cpu_online(desc, cpu))
7afc2387 1571 continue;
4f642863 1572 if (!mod->online && desc->online && is_cpu_online(desc, cpu))
477251f8 1573 continue;
4f642863 1574 if (desc->present && !is_cpu_present(desc, cpu))
a5cfffff 1575 continue;
477251f8 1576 for (c = 0; c < ncols; c++) {
8005924a 1577 if (mod->compat && cols[c] == COL_CACHE) {
477251f8
KZ
1578 if (!desc->ncaches)
1579 continue;
1580 if (c > 0)
1581 putchar(',');
1582 }
1583 if (c > 0)
1584 putchar(',');
e3b3a2f3
KZ
1585
1586 data = get_cell_data(desc, i, cols[c], mod,
1587 buf, sizeof(buf));
1588 fputs(data && *data ? data : "", stdout);
477251f8 1589 }
5dd7507c
CQ
1590 putchar('\n');
1591 }
1592}
1593
ba45d8c1
KZ
1594/*
1595 * [-e] backend
1596 */
1597static void
1598print_readable(struct lscpu_desc *desc, int cols[], int ncols,
1599 struct lscpu_modifier *mod)
1600{
1601 int i;
e7213e34
KZ
1602 char buf[BUFSIZ];
1603 const char *data;
710ed55d 1604 struct libscols_table *table;
ba45d8c1 1605
710ed55d
KZ
1606 scols_init_debug(0);
1607
1608 table = scols_new_table();
83db4eb2 1609 if (!table)
780ce22c 1610 err(EXIT_FAILURE, _("failed to allocate output table"));
19a5510b
KZ
1611 if (mod->json) {
1612 scols_table_enable_json(table, 1);
1613 scols_table_set_name(table, "cpus");
1614 }
ba45d8c1
KZ
1615
1616 for (i = 0; i < ncols; i++) {
b9d18bc3 1617 data = get_cell_header(desc, cols[i], mod, buf, sizeof(buf));
c6f6afc1 1618 if (!scols_table_new_column(table, data, 0, 0))
780ce22c 1619 err(EXIT_FAILURE, _("failed to allocate output column"));
ba45d8c1
KZ
1620 }
1621
a5cfffff 1622 for (i = 0; i < desc->ncpuspos; i++) {
ba45d8c1 1623 int c;
83db4eb2 1624 struct libscols_line *line;
4f642863 1625 int cpu = real_cpu_num(desc, i);
ba45d8c1 1626
4f642863 1627 if (!mod->offline && desc->online && !is_cpu_online(desc, cpu))
7afc2387 1628 continue;
4f642863 1629 if (!mod->online && desc->online && is_cpu_online(desc, cpu))
ba45d8c1 1630 continue;
4f642863 1631 if (desc->present && !is_cpu_present(desc, cpu))
a5cfffff 1632 continue;
ba45d8c1 1633
83db4eb2 1634 line = scols_table_new_line(table, NULL);
e7213e34 1635 if (!line)
780ce22c 1636 err(EXIT_FAILURE, _("failed to allocate output line"));
ba45d8c1
KZ
1637
1638 for (c = 0; c < ncols; c++) {
1639 data = get_cell_data(desc, i, cols[c], mod,
1640 buf, sizeof(buf));
e7213e34
KZ
1641 if (!data || !*data)
1642 data = "-";
bf1eab07 1643 if (scols_line_set_data(line, c, data))
780ce22c 1644 err(EXIT_FAILURE, _("failed to add output data"));
ba45d8c1
KZ
1645 }
1646 }
1647
83db4eb2
OO
1648 scols_print_table(table);
1649 scols_unref_table(table);
ba45d8c1 1650}
5dd7507c 1651
c82b12a0
KZ
1652
1653static void __attribute__ ((__format__(printf, 3, 4)))
1654 add_summary_sprint(struct libscols_table *tb,
1655 const char *txt,
1656 const char *fmt,
1657 ...)
1658{
1659 struct libscols_line *ln = scols_table_new_line(tb, NULL);
1660 char *data;
1661 va_list args;
1662
1663 if (!ln)
780ce22c 1664 err(EXIT_FAILURE, _("failed to allocate output line"));
c82b12a0
KZ
1665
1666 /* description column */
1667 scols_line_set_data(ln, 0, txt);
1668
1669 /* data column */
1670 va_start(args, fmt);
1671 xvasprintf(&data, fmt, args);
1672 va_end(args);
1673
780ce22c
KZ
1674 if (data && scols_line_refer_data(ln, 1, data))
1675 err(EXIT_FAILURE, _("failed to add output data"));
c82b12a0
KZ
1676}
1677
1678#define add_summary_n(tb, txt, num) add_summary_sprint(tb, txt, "%d", num)
1679#define add_summary_s(tb, txt, str) add_summary_sprint(tb, txt, "%s", str)
5dd7507c
CQ
1680
1681static void
c82b12a0
KZ
1682print_cpuset(struct libscols_table *tb,
1683 const char *key, cpu_set_t *set, int hex)
4f912c6a
KZ
1684{
1685 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
1686 size_t setbuflen = 7 * maxcpus;
1687 char setbuf[setbuflen], *p;
1688
1689 if (hex) {
1690 p = cpumask_create(setbuf, setbuflen, set, setsize);
c82b12a0 1691 add_summary_s(tb, key, p);
4f912c6a
KZ
1692 } else {
1693 p = cpulist_create(setbuf, setbuflen, set, setsize);
c82b12a0 1694 add_summary_s(tb, key, p);
4f912c6a 1695 }
4f912c6a
KZ
1696}
1697
ba45d8c1
KZ
1698/*
1699 * default output
1700 */
4f912c6a 1701static void
8005924a 1702print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod)
5dd7507c 1703{
577d1a66 1704 char buf[BUFSIZ];
fc07d9f5 1705 int i = 0;
aac1e59e 1706 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
c82b12a0
KZ
1707 struct libscols_table *tb;
1708
1709 scols_init_debug(0);
1710
1711 tb = scols_new_table();
1712 if (!tb)
780ce22c 1713 err(EXIT_FAILURE, _("failed to allocate output table"));
7e03f383 1714
c82b12a0 1715 scols_table_enable_noheadings(tb, 1);
19a5510b
KZ
1716 if (mod->json) {
1717 scols_table_enable_json(tb, 1);
1718 scols_table_set_name(tb, "lscpu");
1719 }
79e8b41a 1720
19a5510b 1721 if (scols_table_new_column(tb, "field", 0, 0) == NULL ||
c82b12a0
KZ
1722 scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES) == NULL)
1723 err(EXIT_FAILURE, _("failed to initialize output column"));
1724
1725 add_summary_s(tb, _("Architecture:"), desc->arch);
f633ad4e 1726 if (desc->mode) {
577d1a66 1727 char *p = buf;
79e8b41a 1728
f633ad4e 1729 if (desc->mode & MODE_32BIT) {
79e8b41a
KZ
1730 strcpy(p, "32-bit, ");
1731 p += 8;
1732 }
f633ad4e 1733 if (desc->mode & MODE_64BIT) {
79e8b41a
KZ
1734 strcpy(p, "64-bit, ");
1735 p += 8;
1736 }
1737 *(p - 2) = '\0';
577d1a66 1738 add_summary_s(tb, _("CPU op-mode(s):"), buf);
79e8b41a 1739 }
aabe2441 1740#if !defined(WORDS_BIGENDIAN)
c82b12a0 1741 add_summary_s(tb, _("Byte Order:"), "Little Endian");
9b8d4d5f 1742#else
c82b12a0 1743 add_summary_s(tb, _("Byte Order:"), "Big Endian");
9b8d4d5f 1744#endif
3e48ef72
KZ
1745
1746 if (desc->addrsz)
1747 add_summary_s(tb, _("Address sizes:"), desc->addrsz);
1748
c82b12a0 1749 add_summary_n(tb, _("CPU(s):"), desc->ncpus);
4f912c6a 1750
5d4ba40d 1751 if (desc->online)
c82b12a0
KZ
1752 print_cpuset(tb, mod->hex ? _("On-line CPU(s) mask:") :
1753 _("On-line CPU(s) list:"),
8005924a 1754 desc->online, mod->hex);
4f912c6a 1755
5d4ba40d 1756 if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) {
4f912c6a
KZ
1757 cpu_set_t *set;
1758
1759 /* Linux kernel provides cpuset of off-line CPUs that contains
1760 * all configured CPUs (see /sys/devices/system/cpu/offline),
1761 * but want to print real (present in system) off-line CPUs only.
1762 */
1763 set = cpuset_alloc(maxcpus, NULL, NULL);
1764 if (!set)
1765 err(EXIT_FAILURE, _("failed to callocate cpu set"));
1766 CPU_ZERO_S(setsize, set);
a5cfffff 1767 for (i = 0; i < desc->ncpuspos; i++) {
4f642863
AT
1768 int cpu = real_cpu_num(desc, i);
1769 if (!is_cpu_online(desc, cpu) && is_cpu_present(desc, cpu))
1770 CPU_SET_S(cpu, setsize, set);
4f912c6a 1771 }
c82b12a0
KZ
1772 print_cpuset(tb, mod->hex ? _("Off-line CPU(s) mask:") :
1773 _("Off-line CPU(s) list:"),
8005924a 1774 set, mod->hex);
4f912c6a
KZ
1775 cpuset_free(set);
1776 }
5dd7507c 1777
7e03f383 1778 if (desc->nsockets) {
2c497d32
HC
1779 int threads_per_core, cores_per_socket, sockets_per_book;
1780 int books_per_drawer, drawers;
904ffe1f 1781 FILE *fd;
8648ca96 1782
2c497d32
HC
1783 threads_per_core = cores_per_socket = sockets_per_book = 0;
1784 books_per_drawer = drawers = 0;
8648ca96
HC
1785 /* s390 detects its cpu topology via /proc/sysinfo, if present.
1786 * Using simply the cpu topology masks in sysfs will not give
1787 * usable results since everything is virtualized. E.g.
1788 * virtual core 0 may have only 1 cpu, but virtual core 2 may
1789 * five cpus.
1790 * If the cpu topology is not exported (e.g. 2nd level guest)
1791 * fall back to old calculation scheme.
1792 */
6e509042 1793 if ((fd = ul_path_fopen(desc->procfs, "r", "sysinfo"))) {
b3adf6ef 1794 int t0, t1;
8648ca96 1795
577d1a66
KZ
1796 while (fd && fgets(buf, sizeof(buf), fd) != NULL) {
1797 if (sscanf(buf, "CPU Topology SW:%d%d%d%d%d%d",
b3adf6ef
HC
1798 &t0, &t1, &drawers, &books_per_drawer,
1799 &sockets_per_book,
8648ca96
HC
1800 &cores_per_socket) == 6)
1801 break;
1802 }
ad655c88
KZ
1803 if (fd)
1804 fclose(fd);
8648ca96 1805 }
2c497d32
HC
1806 if (desc->mtid)
1807 threads_per_core = atoi(desc->mtid) + 1;
c82b12a0 1808 add_summary_n(tb, _("Thread(s) per core:"),
2c497d32 1809 threads_per_core ?: desc->nthreads / desc->ncores);
c82b12a0 1810 add_summary_n(tb, _("Core(s) per socket:"),
8648ca96 1811 cores_per_socket ?: desc->ncores / desc->nsockets);
56baaa4e 1812 if (desc->nbooks) {
c82b12a0 1813 add_summary_n(tb, _("Socket(s) per book:"),
8648ca96 1814 sockets_per_book ?: desc->nsockets / desc->nbooks);
b3adf6ef 1815 if (desc->ndrawers) {
c82b12a0 1816 add_summary_n(tb, _("Book(s) per drawer:"),
b3adf6ef 1817 books_per_drawer ?: desc->nbooks / desc->ndrawers);
c82b12a0 1818 add_summary_n(tb, _("Drawer(s):"), drawers ?: desc->ndrawers);
b3adf6ef 1819 } else {
c82b12a0 1820 add_summary_n(tb, _("Book(s):"), books_per_drawer ?: desc->nbooks);
b3adf6ef 1821 }
56baaa4e 1822 } else {
c82b12a0 1823 add_summary_n(tb, _("Socket(s):"), sockets_per_book ?: desc->nsockets);
56baaa4e 1824 }
5dd7507c 1825 }
7e03f383 1826 if (desc->nnodes)
c82b12a0 1827 add_summary_n(tb, _("NUMA node(s):"), desc->nnodes);
e8aa16ee 1828 if (desc->vendor)
c82b12a0 1829 add_summary_s(tb, _("Vendor ID:"), desc->vendor);
0c28f0c8 1830 if (desc->machinetype)
c82b12a0 1831 add_summary_s(tb, _("Machine type:"), desc->machinetype);
e8aa16ee 1832 if (desc->family)
c82b12a0 1833 add_summary_s(tb, _("CPU family:"), desc->family);
c95e3889 1834 if (desc->model || desc->revision)
c82b12a0 1835 add_summary_s(tb, _("Model:"), desc->revision ? desc->revision : desc->model);
c95e3889 1836 if (desc->modelname || desc->cpu)
c82b12a0 1837 add_summary_s(tb, _("Model name:"), desc->cpu ? desc->cpu : desc->modelname);
e8aa16ee 1838 if (desc->stepping)
c82b12a0 1839 add_summary_s(tb, _("Stepping:"), desc->stepping);
e8aa16ee 1840 if (desc->mhz)
c82b12a0 1841 add_summary_s(tb, _("CPU MHz:"), desc->mhz);
4632b288 1842 if (desc->dynamic_mhz)
c82b12a0 1843 add_summary_s(tb, _("CPU dynamic MHz:"), desc->dynamic_mhz);
4632b288 1844 if (desc->static_mhz)
c82b12a0 1845 add_summary_s(tb, _("CPU static MHz:"), desc->static_mhz);
89222b17
KZ
1846 if (desc->maxmhz)
1847 add_summary_s(tb, _("CPU max MHz:"), cpu_max_mhz(desc, buf, sizeof(buf)));
1848 if (desc->minmhz)
1849 add_summary_s(tb, _("CPU min MHz:"), cpu_min_mhz(desc, buf, sizeof(buf)));
9b8d4d5f 1850 if (desc->bogomips)
c82b12a0 1851 add_summary_s(tb, _("BogoMIPS:"), desc->bogomips);
e8aa16ee
KZ
1852 if (desc->virtflag) {
1853 if (!strcmp(desc->virtflag, "svm"))
c82b12a0 1854 add_summary_s(tb, _("Virtualization:"), "AMD-V");
e8aa16ee 1855 else if (!strcmp(desc->virtflag, "vmx"))
c82b12a0 1856 add_summary_s(tb, _("Virtualization:"), "VT-x");
5dd7507c 1857 }
10829cd7 1858 if (desc->hypervisor)
c82b12a0 1859 add_summary_s(tb, _("Hypervisor:"), desc->hypervisor);
e8aa16ee 1860 if (desc->hyper) {
c82b12a0
KZ
1861 add_summary_s(tb, _("Hypervisor vendor:"), hv_vendors[desc->hyper]);
1862 add_summary_s(tb, _("Virtualization type:"), _(virt_types[desc->virtype]));
c8b64f6d 1863 }
a0fff77e 1864 if (desc->dispatching >= 0)
c82b12a0 1865 add_summary_s(tb, _("Dispatching mode:"), _(disp_modes[desc->dispatching]));
7e03f383 1866 if (desc->ncaches) {
7e03f383 1867 for (i = desc->ncaches - 1; i >= 0; i--) {
577d1a66 1868 snprintf(buf, sizeof(buf),
7e03f383 1869 _("%s cache:"), desc->caches[i].name);
577d1a66 1870 add_summary_s(tb, buf, desc->caches[i].size);
7e03f383
KZ
1871 }
1872 }
28b1658f 1873 if (desc->necaches) {
28b1658f 1874 for (i = desc->necaches - 1; i >= 0; i--) {
577d1a66 1875 snprintf(buf, sizeof(buf),
28b1658f 1876 _("%s cache:"), desc->ecaches[i].name);
577d1a66 1877 add_summary_s(tb, buf, desc->ecaches[i].size);
28b1658f
HC
1878 }
1879 }
1880
4f912c6a 1881 for (i = 0; i < desc->nnodes; i++) {
e9074a16 1882 snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), desc->idx2nodenum[i]);
c82b12a0 1883 print_cpuset(tb, buf, desc->nodemaps[i], mod->hex);
5dd7507c 1884 }
ee1f1057 1885
bd9b94d1 1886 if (desc->physsockets) {
c82b12a0
KZ
1887 add_summary_n(tb, _("Physical sockets:"), desc->physsockets);
1888 add_summary_n(tb, _("Physical chips:"), desc->physchips);
1889 add_summary_n(tb, _("Physical cores/chip:"), desc->physcoresperchip);
bd9b94d1 1890 }
c82b12a0
KZ
1891
1892 if (desc->flags)
1893 add_summary_s(tb, _("Flags:"), desc->flags);
1894
1895 scols_print_table(tb);
1896 scols_unref_table(tb);
5dd7507c
CQ
1897}
1898
6e1eda6f 1899static void __attribute__((__noreturn__)) usage(void)
5dd7507c 1900{
6e1eda6f 1901 FILE *out = stdout;
b9d18bc3
KZ
1902 size_t i;
1903
1904 fputs(USAGE_HEADER, out);
c6f095cf 1905 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
7f1ec5e8 1906
451dbcfa
BS
1907 fputs(USAGE_SEPARATOR, out);
1908 fputs(_("Display information about the CPU architecture.\n"), out);
1909
b9d18bc3 1910 fputs(USAGE_OPTIONS, out);
c6f095cf
BS
1911 fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out);
1912 fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out);
1913 fputs(_(" -c, --offline print offline CPUs only\n"), out);
19a5510b 1914 fputs(_(" -J, --json use JSON for default or extended format\n"), out);
c6f095cf
BS
1915 fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out);
1916 fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out);
1917 fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out);
1918 fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out);
0d2b5d2a 1919 fputs(_(" -y, --physical print physical instead of logical IDs\n"), out);
c6f095cf 1920 fputs(USAGE_SEPARATOR, out);
f45f3ec3 1921 printf(USAGE_HELP_OPTIONS(25));
b9d18bc3 1922
6e2d5a44 1923 fputs(USAGE_COLUMNS, out);
3d27b76a
KZ
1924 for (i = 0; i < ARRAY_SIZE(coldescs); i++)
1925 fprintf(out, " %13s %s\n", coldescs[i].name, _(coldescs[i].help));
1926
f45f3ec3 1927 printf(USAGE_MAN_TAIL("lscpu(1)"));
4f912c6a 1928
6e1eda6f 1929 exit(EXIT_SUCCESS);
5dd7507c
CQ
1930}
1931
1932int main(int argc, char *argv[])
1933{
8005924a 1934 struct lscpu_modifier _mod = { .mode = OUTPUT_SUMMARY }, *mod = &_mod;
87918040 1935 struct lscpu_desc _desc = { .flags = NULL }, *desc = &_desc;
8005924a 1936 int c, i;
3d27b76a 1937 int columns[ARRAY_SIZE(coldescs)], ncolumns = 0;
7fc12cd2 1938 int cpu_modifier_specified = 0;
538b50cb 1939 size_t setsize;
5dd7507c 1940
fbf0619b
SK
1941 enum {
1942 OPT_OUTPUT_ALL = CHAR_MAX + 1,
1943 };
6c7d5ae9 1944 static const struct option longopts[] = {
87918040
SK
1945 { "all", no_argument, NULL, 'a' },
1946 { "online", no_argument, NULL, 'b' },
1947 { "offline", no_argument, NULL, 'c' },
1948 { "help", no_argument, NULL, 'h' },
1949 { "extended", optional_argument, NULL, 'e' },
19a5510b 1950 { "json", no_argument, NULL, 'J' },
87918040
SK
1951 { "parse", optional_argument, NULL, 'p' },
1952 { "sysroot", required_argument, NULL, 's' },
1953 { "physical", no_argument, NULL, 'y' },
1954 { "hex", no_argument, NULL, 'x' },
1955 { "version", no_argument, NULL, 'V' },
fbf0619b 1956 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
87918040 1957 { NULL, 0, NULL, 0 }
5dd7507c
CQ
1958 };
1959
8e97eb4b
KZ
1960 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1961 { 'a','b','c' },
1962 { 'e','p' },
1963 { 0 }
1964 };
1965 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1966
2f8f1388 1967 setlocale(LC_ALL, "");
5dd7507c
CQ
1968 bindtextdomain(PACKAGE, LOCALEDIR);
1969 textdomain(PACKAGE);
efb8854f 1970 atexit(close_stdout);
5dd7507c 1971
19a5510b 1972 while ((c = getopt_long(argc, argv, "abce::hJp::s:xyV", longopts, NULL)) != -1) {
8e97eb4b
KZ
1973
1974 err_exclusive_options(c, longopts, excl, excl_st);
1975
5dd7507c 1976 switch (c) {
0ad29ff6 1977 case 'a':
7afc2387 1978 mod->online = mod->offline = 1;
7fc12cd2 1979 cpu_modifier_specified = 1;
0ad29ff6 1980 break;
23e9e95a
KZ
1981 case 'b':
1982 mod->online = 1;
7fc12cd2 1983 cpu_modifier_specified = 1;
23e9e95a 1984 break;
7afc2387
HC
1985 case 'c':
1986 mod->offline = 1;
7fc12cd2 1987 cpu_modifier_specified = 1;
7afc2387 1988 break;
5dd7507c 1989 case 'h':
6e1eda6f 1990 usage();
19a5510b
KZ
1991 case 'J':
1992 mod->json = 1;
1993 break;
5dd7507c 1994 case 'p':
ba45d8c1 1995 case 'e':
477251f8
KZ
1996 if (optarg) {
1997 if (*optarg == '=')
1998 optarg++;
1999 ncolumns = string_to_idarray(optarg,
2000 columns, ARRAY_SIZE(columns),
2001 column_name_to_id);
2002 if (ncolumns < 0)
2003 return EXIT_FAILURE;
477251f8 2004 }
ba45d8c1 2005 mod->mode = c == 'p' ? OUTPUT_PARSABLE : OUTPUT_READABLE;
5dd7507c 2006 break;
47b6e8b6 2007 case 's':
6e509042 2008 desc->prefix = optarg;
8148217b 2009 mod->system = SYSTEM_SNAPSHOT;
47b6e8b6 2010 break;
4f912c6a 2011 case 'x':
8005924a 2012 mod->hex = 1;
4f912c6a 2013 break;
0d2b5d2a
HC
2014 case 'y':
2015 mod->physical = 1;
2016 break;
44de912c 2017 case 'V':
f6277500 2018 printf(UTIL_LINUX_VERSION);
44de912c 2019 return EXIT_SUCCESS;
fbf0619b
SK
2020 case OPT_OUTPUT_ALL:
2021 {
2022 size_t sz;
2023 for (sz = 0; sz < ARRAY_SIZE(coldescs); sz++)
2024 columns[sz] = 1;
2025 break;
2026 }
5dd7507c 2027 default:
677ec86c 2028 errtryhelp(EXIT_FAILURE);
5dd7507c
CQ
2029 }
2030 }
7bbb7829 2031
7fc12cd2
HC
2032 if (cpu_modifier_specified && mod->mode == OUTPUT_SUMMARY) {
2033 fprintf(stderr,
2034 _("%s: options --all, --online and --offline may only "
ac56e555 2035 "be used with options --extended or --parse.\n"),
7fc12cd2
HC
2036 program_invocation_short_name);
2037 return EXIT_FAILURE;
2038 }
2039
6e1eda6f
RM
2040 if (argc != optind) {
2041 warnx(_("bad usage"));
2042 errtryhelp(EXIT_FAILURE);
2043 }
7bbb7829 2044
7afc2387
HC
2045 /* set default cpu display mode if none was specified */
2046 if (!mod->online && !mod->offline) {
2047 mod->online = 1;
2048 mod->offline = mod->mode == OUTPUT_READABLE ? 1 : 0;
2049 }
5dd7507c 2050
6e509042
KZ
2051 ul_path_init_debug();
2052
2053 /* /sys/devices/system/cpu */
2054 desc->syscpu = ul_new_path(_PATH_SYS_CPU);
2055 if (!desc->syscpu)
2056 err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler"));
2057 if (desc->prefix)
2058 ul_path_set_prefix(desc->syscpu, desc->prefix);
2059
2060 /* /proc */
2061 desc->procfs = ul_new_path("/proc");
2062 if (!desc->procfs)
2063 err(EXIT_FAILURE, _("failed to initialize procfs handler"));
2064 if (desc->prefix)
2065 ul_path_set_prefix(desc->procfs, desc->prefix);
2066
8148217b 2067 read_basicinfo(desc, mod);
5dd7507c 2068
538b50cb
SB
2069 setsize = CPU_ALLOC_SIZE(maxcpus);
2070
a5cfffff 2071 for (i = 0; i < desc->ncpuspos; i++) {
0002704e
HC
2072 /* only consider present CPUs */
2073 if (desc->present &&
538b50cb 2074 !CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present))
0002704e 2075 continue;
7e03f383
KZ
2076 read_topology(desc, i);
2077 read_cache(desc, i);
2b8fcb81 2078 read_polarization(desc, i);
596b8845 2079 read_address(desc, i);
d231eea1 2080 read_configured(desc, i);
44320710 2081 read_max_mhz(desc, i);
e065a597 2082 read_min_mhz(desc, i);
47b6e8b6 2083 }
7e03f383 2084
960bf130
KZ
2085 if (desc->caches)
2086 qsort(desc->caches, desc->ncaches,
2087 sizeof(struct cpu_cache), cachecmp);
7e03f383 2088
28b1658f
HC
2089 if (desc->ecaches)
2090 qsort(desc->ecaches, desc->necaches,
2091 sizeof(struct cpu_cache), cachecmp);
2092
7e03f383 2093 read_nodes(desc);
eff79ceb 2094 read_hypervisor(desc, mod);
744d62ee 2095 arm_cpu_decode(desc);
c8b64f6d 2096
8005924a 2097 switch(mod->mode) {
ba45d8c1
KZ
2098 case OUTPUT_SUMMARY:
2099 print_summary(desc, mod);
2100 break;
2101 case OUTPUT_PARSABLE:
2102 if (!ncolumns) {
2103 columns[ncolumns++] = COL_CPU;
2104 columns[ncolumns++] = COL_CORE;
2105 columns[ncolumns++] = COL_SOCKET;
2106 columns[ncolumns++] = COL_NODE;
2107 columns[ncolumns++] = COL_CACHE;
2108 mod->compat = 1;
2109 }
2110 print_parsable(desc, columns, ncolumns, mod);
2111 break;
2112 case OUTPUT_READABLE:
2113 if (!ncolumns) {
2114 /* No list was given. Just print whatever is there. */
2115 columns[ncolumns++] = COL_CPU;
2116 if (desc->nodemaps)
8005924a 2117 columns[ncolumns++] = COL_NODE;
b3adf6ef
HC
2118 if (desc->drawermaps)
2119 columns[ncolumns++] = COL_DRAWER;
ba45d8c1
KZ
2120 if (desc->bookmaps)
2121 columns[ncolumns++] = COL_BOOK;
2122 if (desc->socketmaps)
2123 columns[ncolumns++] = COL_SOCKET;
2124 if (desc->coremaps)
2125 columns[ncolumns++] = COL_CORE;
2126 if (desc->caches)
8005924a 2127 columns[ncolumns++] = COL_CACHE;
a7e5300c
HC
2128 if (desc->online)
2129 columns[ncolumns++] = COL_ONLINE;
d231eea1
HC
2130 if (desc->configured)
2131 columns[ncolumns++] = COL_CONFIGURED;
ba45d8c1
KZ
2132 if (desc->polarization)
2133 columns[ncolumns++] = COL_POLARIZATION;
2134 if (desc->addresses)
2135 columns[ncolumns++] = COL_ADDRESS;
e065a597
DB
2136 if (desc->maxmhz)
2137 columns[ncolumns++] = COL_MAXMHZ;
2138 if (desc->minmhz)
2139 columns[ncolumns++] = COL_MINMHZ;
ba45d8c1
KZ
2140 }
2141 print_readable(desc, columns, ncolumns, mod);
2142 break;
8005924a 2143 }
5dd7507c 2144
6e509042
KZ
2145 ul_unref_path(desc->syscpu);
2146 ul_unref_path(desc->procfs);
cf474aac 2147 return EXIT_SUCCESS;
5dd7507c 2148}