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