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