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