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