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