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