]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
lscpu: split output to sections
[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
83db4eb2
OO
38#include <libsmartcols.h>
39
efb8854f 40#include "closestream.h"
41a8940d 41#include "optutils.h"
71061694 42
fb2627ce 43#include "lscpu.h"
0ebbe9f1 44
2ba641e5 45static const char *virt_types[] = {
7454b598
KZ
46 [VIRT_TYPE_NONE] = N_("none"),
47 [VIRT_TYPE_PARA] = N_("para"),
48 [VIRT_TYPE_FULL] = N_("full"),
49 [VIRT_TYPE_CONTAINER] = N_("container"),
c8b64f6d
KZ
50};
51
2ba641e5 52static const char *hv_vendors[] = {
7454b598
KZ
53 [VIRT_VENDOR_NONE] = NULL,
54 [VIRT_VENDOR_XEN] = "Xen",
55 [VIRT_VENDOR_KVM] = "KVM",
56 [VIRT_VENDOR_MSHV] = "Microsoft",
57 [VIRT_VENDOR_VMWARE] = "VMware",
58 [VIRT_VENDOR_IBM] = "IBM",
59 [VIRT_VENDOR_VSERVER] = "Linux-VServer",
60 [VIRT_VENDOR_UML] = "User-mode Linux",
61 [VIRT_VENDOR_INNOTEK] = "Innotek GmbH",
62 [VIRT_VENDOR_HITACHI] = "Hitachi",
63 [VIRT_VENDOR_PARALLELS] = "Parallels",
64 [VIRT_VENDOR_VBOX] = "Oracle",
65 [VIRT_VENDOR_OS400] = "OS/400",
66 [VIRT_VENDOR_PHYP] = "pHyp",
67 [VIRT_VENDOR_SPAR] = "Unisys s-Par",
68 [VIRT_VENDOR_WSL] = "Windows Subsystem for Linux"
96ce475f
RM
69};
70
a0fff77e 71/* dispatching modes */
2ba641e5 72static const char *disp_modes[] = {
a0fff77e
HC
73 [DISP_HORIZONTAL] = N_("horizontal"),
74 [DISP_VERTICAL] = N_("vertical")
75};
76
f9ac0210
KZ
77struct polarization_modes {
78 char *parsable;
79 char *readable;
80};
81
2ba641e5 82static struct polarization_modes polar_modes[] = {
8005924a
KZ
83 [POLAR_UNKNOWN] = {"U", "-"},
84 [POLAR_VLOW] = {"VL", "vert-low"},
85 [POLAR_VMEDIUM] = {"VM", "vert-medium"},
86 [POLAR_VHIGH] = {"VH", "vert-high"},
87 [POLAR_HORIZONTAL] = {"H", "horizontal"},
2b8fcb81
HC
88};
89
477251f8 90/*
3d27b76a 91 * IDs
477251f8
KZ
92 */
93enum {
cc07239d
KZ
94 COL_CPU_CPU,
95 COL_CPU_CORE,
96 COL_CPU_SOCKET,
97 COL_CPU_NODE,
98 COL_CPU_BOOK,
99 COL_CPU_DRAWER,
100 COL_CPU_CACHE,
101 COL_CPU_POLARIZATION,
102 COL_CPU_ADDRESS,
103 COL_CPU_CONFIGURED,
104 COL_CPU_ONLINE,
105 COL_CPU_MAXMHZ,
106 COL_CPU_MINMHZ,
477251f8
KZ
107};
108
0e86bc84
KZ
109enum {
110 COL_CACHE_ALLSIZE,
111 COL_CACHE_LEVEL,
112 COL_CACHE_NAME,
113 COL_CACHE_ONESIZE,
114 COL_CACHE_TYPE,
115 COL_CACHE_WAYS,
cf3b6b71
KZ
116 COL_CACHE_ALLOCPOL,
117 COL_CACHE_WRITEPOL,
118 COL_CACHE_PHYLINE,
119 COL_CACHE_SETS,
120 COL_CACHE_COHERENCYSIZE
0e86bc84
KZ
121};
122
123
3d27b76a
KZ
124/* column description
125 */
126struct lscpu_coldesc {
127 const char *name;
128 const char *help;
129
0e86bc84 130 int flags;
3d27b76a 131 unsigned int is_abbr:1; /* name is abbreviation */
477251f8
KZ
132};
133
cc07239d 134static struct lscpu_coldesc coldescs_cpu[] =
3d27b76a 135{
2ec00a10
KZ
136 [COL_CPU_CPU] = { "CPU", N_("logical CPU number"), SCOLS_FL_RIGHT, 1 },
137 [COL_CPU_CORE] = { "CORE", N_("logical core number"), SCOLS_FL_RIGHT },
138 [COL_CPU_SOCKET] = { "SOCKET", N_("logical socket number"), SCOLS_FL_RIGHT },
139 [COL_CPU_NODE] = { "NODE", N_("logical NUMA node number"), SCOLS_FL_RIGHT },
140 [COL_CPU_BOOK] = { "BOOK", N_("logical book number"), SCOLS_FL_RIGHT },
141 [COL_CPU_DRAWER] = { "DRAWER", N_("logical drawer number"), SCOLS_FL_RIGHT },
cc07239d
KZ
142 [COL_CPU_CACHE] = { "CACHE", N_("shows how caches are shared between CPUs") },
143 [COL_CPU_POLARIZATION] = { "POLARIZATION", N_("CPU dispatching mode on virtual hardware") },
144 [COL_CPU_ADDRESS] = { "ADDRESS", N_("physical address of a CPU") },
145 [COL_CPU_CONFIGURED] = { "CONFIGURED", N_("shows if the hypervisor has allocated the CPU") },
2ec00a10
KZ
146 [COL_CPU_ONLINE] = { "ONLINE", N_("shows if Linux currently makes use of the CPU"), SCOLS_FL_RIGHT },
147 [COL_CPU_MAXMHZ] = { "MAXMHZ", N_("shows the maximum MHz of the CPU"), SCOLS_FL_RIGHT },
148 [COL_CPU_MINMHZ] = { "MINMHZ", N_("shows the minimum MHz of the CPU"), SCOLS_FL_RIGHT }
0e86bc84
KZ
149};
150
151static struct lscpu_coldesc coldescs_cache[] =
152{
153 [COL_CACHE_ALLSIZE] = { "ALL-SIZE", N_("size of all system caches"), SCOLS_FL_RIGHT },
154 [COL_CACHE_LEVEL] = { "LEVEL", N_("cache level"), SCOLS_FL_RIGHT },
155 [COL_CACHE_NAME] = { "NAME", N_("cache name") },
156 [COL_CACHE_ONESIZE] = { "ONE-SIZE", N_("size of one cache"), SCOLS_FL_RIGHT },
157 [COL_CACHE_TYPE] = { "TYPE", N_("cache type") },
cf3b6b71
KZ
158 [COL_CACHE_WAYS] = { "WAYS", N_("ways of associativity"), SCOLS_FL_RIGHT },
159 [COL_CACHE_ALLOCPOL] = { "ALLOC-POLICY", N_("allocation policy") },
160 [COL_CACHE_WRITEPOL] = { "WRITE-POLICY", N_("write policy") },
161 [COL_CACHE_PHYLINE] = { "PHY-LINE", N_("number of physical cache line per cache t"), SCOLS_FL_RIGHT },
162 [COL_CACHE_SETS] = { "SETS", N_("number of sets in the cache; set lines has the same cache index"), SCOLS_FL_RIGHT },
163 [COL_CACHE_COHERENCYSIZE] = { "COHERENCY-SIZE", N_("minimum amount of data in bytes transferred from memory to cache"), SCOLS_FL_RIGHT }
3d27b76a 164};
477251f8 165
91eef60c
KZ
166static int is_term = 0;
167
43715b4e
KZ
168UL_DEBUG_DEFINE_MASK(lscpu);
169UL_DEBUG_DEFINE_MASKNAMES(lscpu) = UL_DEBUG_EMPTY_MASKNAMES;
170
171static void lscpu_init_debug(void)
172{
173 __UL_INIT_DEBUG_FROM_ENV(lscpu, LSCPU_DEBUG_, 0, LSCPU_DEBUG);
174}
175
583f14cc 176static int
cc07239d 177cpu_column_name_to_id(const char *name, size_t namesz)
477251f8 178{
329fd1c3 179 size_t i;
477251f8 180
cc07239d
KZ
181 for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) {
182 const char *cn = coldescs_cpu[i].name;
477251f8
KZ
183
184 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
185 return i;
186 }
187 warnx(_("unknown column: %s"), name);
188 return -1;
189}
190
0e86bc84
KZ
191static int
192cache_column_name_to_id(const char *name, size_t namesz)
193{
194 size_t i;
195
196 for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++) {
197 const char *cn = coldescs_cache[i].name;
198
199 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
200 return i;
201 }
202 warnx(_("unknown column: %s"), name);
203 return -1;
204}
205
27c349f9
KZ
206static void lscpu_context_init_paths(struct lscpu_cxt *cxt)
207{
208 DBG(MISC, ul_debugobj(cxt, "initialize paths"));
209 ul_path_init_debug();
210
211 /* /sys/devices/system/cpu */
212 cxt->syscpu = ul_new_path(_PATH_SYS_CPU);
213 if (!cxt->syscpu)
214 err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler"));
215 if (cxt->prefix)
216 ul_path_set_prefix(cxt->syscpu, cxt->prefix);
217
218 /* /proc */
219 cxt->procfs = ul_new_path("/proc");
220 if (!cxt->procfs)
221 err(EXIT_FAILURE, _("failed to initialize procfs handler"));
222 if (cxt->prefix)
223 ul_path_set_prefix(cxt->procfs, cxt->prefix);
224}
225
226static struct lscpu_cxt *lscpu_new_context(void)
227{
228 return xcalloc(1, sizeof(struct lscpu_cxt));
229}
230
231static void lscpu_free_context(struct lscpu_cxt *cxt)
232{
233 size_t i;
234
235 if (!cxt)
236 return;
237
238 DBG(MISC, ul_debugobj(cxt, "freeing context"));
239
240 DBG(MISC, ul_debugobj(cxt, " de-initialize paths"));
241 ul_unref_path(cxt->syscpu);
242 ul_unref_path(cxt->procfs);
243
244 DBG(MISC, ul_debugobj(cxt, " freeing cpus"));
245 for (i = 0; i < cxt->npossibles; i++) {
246 lscpu_unref_cpu(cxt->cpus[i]);
247 cxt->cpus[i] = NULL;
248 }
249 DBG(MISC, ul_debugobj(cxt, " freeing types"));
250 for (i = 0; i < cxt->ncputypes; i++) {
251 lscpu_unref_cputype(cxt->cputypes[i]);
252 cxt->cputypes[i] = NULL;
253 }
254
255 free(cxt->present);
256 free(cxt->online);
257 free(cxt->cputypes);
258 free(cxt->cpus);
259
260 for (i = 0; i < cxt->nvuls; i++) {
261 free(cxt->vuls[i].name);
262 free(cxt->vuls[i].text);
263 }
264 free(cxt->vuls);
265
266 for (i = 0; i < cxt->nnodes; i++)
267 free(cxt->nodemaps[i]);
268
269 free(cxt->nodemaps);
270 free(cxt->idx2nodenum);
271
272 lscpu_free_virtualization(cxt->virt);
273 lscpu_free_architecture(cxt->arch);
6fbb5328 274
27c349f9 275 lscpu_free_caches(cxt->ecaches, cxt->necaches);
6fbb5328 276 lscpu_free_caches(cxt->caches, cxt->ncaches);
27c349f9
KZ
277
278 free(cxt);
279}
280
f9ac0210 281#ifdef LSCPU_OLD_OUTPUT_CODE /* temporary disabled for revrite */
5dd7507c 282
e3b3a2f3 283static char *
4f642863 284get_cell_data(struct lscpu_desc *desc, int idx, int col,
e3b3a2f3
KZ
285 struct lscpu_modifier *mod,
286 char *buf, size_t bufsz)
5dd7507c 287{
7e03f383 288 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
4f642863
AT
289 size_t i;
290 int cpu = real_cpu_num(desc, idx);
e3b3a2f3
KZ
291
292 *buf = '\0';
5dd7507c 293
477251f8 294 switch (col) {
cc07239d 295 case COL_CPU_CPU:
e3b3a2f3 296 snprintf(buf, bufsz, "%d", cpu);
477251f8 297 break;
cc07239d 298 case COL_CPU_CORE:
0d2b5d2a
HC
299 if (mod->physical) {
300 if (desc->coreids[idx] == -1)
301 snprintf(buf, bufsz, "-");
302 else
303 snprintf(buf, bufsz, "%d", desc->coreids[idx]);
304 } else {
305 if (cpuset_ary_isset(cpu, desc->coremaps,
306 desc->ncores, setsize, &i) == 0)
307 snprintf(buf, bufsz, "%zu", i);
308 }
477251f8 309 break;
cc07239d 310 case COL_CPU_SOCKET:
0d2b5d2a
HC
311 if (mod->physical) {
312 if (desc->socketids[idx] == -1)
313 snprintf(buf, bufsz, "-");
314 else
315 snprintf(buf, bufsz, "%d", desc->socketids[idx]);
316 } else {
317 if (cpuset_ary_isset(cpu, desc->socketmaps,
318 desc->nsockets, setsize, &i) == 0)
319 snprintf(buf, bufsz, "%zu", i);
320 }
477251f8 321 break;
cc07239d 322 case COL_CPU_NODE:
e9d659ea 323 if (cpuset_ary_isset(cpu, desc->nodemaps,
4f642863 324 desc->nnodes, setsize, &i) == 0)
e9074a16 325 snprintf(buf, bufsz, "%d", desc->idx2nodenum[i]);
477251f8 326 break;
cc07239d 327 case COL_CPU_DRAWER:
bdda3543
KZ
328 if (!desc->drawerids || !desc->drawermaps)
329 break;
0d2b5d2a
HC
330 if (mod->physical) {
331 if (desc->drawerids[idx] == -1)
332 snprintf(buf, bufsz, "-");
333 else
334 snprintf(buf, bufsz, "%d", desc->drawerids[idx]);
335 } else {
336 if (cpuset_ary_isset(cpu, desc->drawermaps,
337 desc->ndrawers, setsize, &i) == 0)
338 snprintf(buf, bufsz, "%zu", i);
339 }
b3adf6ef 340 break;
cc07239d 341 case COL_CPU_BOOK:
bdda3543
KZ
342 if (!desc->bookids || !desc->bookmaps)
343 break;
0d2b5d2a
HC
344 if (mod->physical) {
345 if (desc->bookids[idx] == -1)
346 snprintf(buf, bufsz, "-");
347 else
348 snprintf(buf, bufsz, "%d", desc->bookids[idx]);
349 } else {
350 if (cpuset_ary_isset(cpu, desc->bookmaps,
351 desc->nbooks, setsize, &i) == 0)
352 snprintf(buf, bufsz, "%zu", i);
353 }
477251f8 354 break;
cc07239d 355 case COL_CPU_CACHE:
e3b3a2f3
KZ
356 {
357 char *p = buf;
358 size_t sz = bufsz;
359 int j;
360
7e03f383
KZ
361 for (j = desc->ncaches - 1; j >= 0; j--) {
362 struct cpu_cache *ca = &desc->caches[j];
e9d659ea
KZ
363
364 if (cpuset_ary_isset(cpu, ca->sharedmaps,
4f642863 365 ca->nsharedmaps, setsize, &i) == 0) {
73f2bec5 366 int x = snprintf(p, sz, "%zu", i);
06fa5817 367 if (x < 0 || (size_t) x >= sz)
e3b3a2f3
KZ
368 return NULL;
369 p += x;
370 sz -= x;
371 }
372 if (j != 0) {
06fa5817
YK
373 if (sz < 2)
374 return NULL;
e3b3a2f3
KZ
375 *p++ = mod->compat ? ',' : ':';
376 *p = '\0';
0c5bbafa 377 sz--;
e3b3a2f3 378 }
5dd7507c 379 }
477251f8 380 break;
e3b3a2f3 381 }
cc07239d 382 case COL_CPU_POLARIZATION:
ba45d8c1 383 if (desc->polarization) {
4f642863 384 int x = desc->polarization[idx];
ba45d8c1 385
e3b3a2f3 386 snprintf(buf, bufsz, "%s",
30b912d3 387 mod->mode == LSCPU_OUTPUT_PARSABLE ?
ba45d8c1
KZ
388 polar_modes[x].parsable :
389 polar_modes[x].readable);
390 }
2b8fcb81 391 break;
cc07239d 392 case COL_CPU_ADDRESS:
596b8845 393 if (desc->addresses)
4f642863 394 snprintf(buf, bufsz, "%d", desc->addresses[idx]);
596b8845 395 break;
cc07239d 396 case COL_CPU_CONFIGURED:
e43fc13e
HC
397 if (!desc->configured)
398 break;
30b912d3 399 if (mod->mode == LSCPU_OUTPUT_PARSABLE)
f205c90a 400 snprintf(buf, bufsz, "%s",
4f642863 401 desc->configured[idx] ? _("Y") : _("N"));
e43fc13e 402 else
f205c90a 403 snprintf(buf, bufsz, "%s",
4f642863 404 desc->configured[idx] ? _("yes") : _("no"));
d231eea1 405 break;
cc07239d 406 case COL_CPU_ONLINE:
e43fc13e
HC
407 if (!desc->online)
408 break;
30b912d3 409 if (mod->mode == LSCPU_OUTPUT_PARSABLE)
f205c90a 410 snprintf(buf, bufsz, "%s",
e43fc13e
HC
411 is_cpu_online(desc, cpu) ? _("Y") : _("N"));
412 else
f205c90a 413 snprintf(buf, bufsz, "%s",
847b982e 414 is_cpu_online(desc, cpu) ? _("yes") : _("no"));
a7e5300c 415 break;
cc07239d 416 case COL_CPU_MAXMHZ:
0145d84a 417 if (desc->maxmhz && desc->maxmhz[idx])
4f642863 418 xstrncpy(buf, desc->maxmhz[idx], bufsz);
e065a597 419 break;
cc07239d 420 case COL_CPU_MINMHZ:
0145d84a 421 if (desc->minmhz && desc->minmhz[idx])
4f642863 422 xstrncpy(buf, desc->minmhz[idx], bufsz);
44320710 423 break;
477251f8 424 }
e3b3a2f3
KZ
425 return buf;
426}
427
428static char *
429get_cell_header(struct lscpu_desc *desc, int col,
430 struct lscpu_modifier *mod,
431 char *buf, size_t bufsz)
432{
433 *buf = '\0';
434
cc07239d 435 if (col == COL_CPU_CACHE) {
e3b3a2f3
KZ
436 char *p = buf;
437 size_t sz = bufsz;
438 int i;
439
440 for (i = desc->ncaches - 1; i >= 0; i--) {
441 int x = snprintf(p, sz, "%s", desc->caches[i].name);
06fa5817 442 if (x < 0 || (size_t) x >= sz)
e3b3a2f3
KZ
443 return NULL;
444 sz -= x;
445 p += x;
446 if (i > 0) {
06fa5817
YK
447 if (sz < 2)
448 return NULL;
e3b3a2f3
KZ
449 *p++ = mod->compat ? ',' : ':';
450 *p = '\0';
0c5bbafa 451 sz--;
e3b3a2f3
KZ
452 }
453 }
454 if (desc->ncaches)
455 return buf;
456 }
cc07239d 457 snprintf(buf, bufsz, "%s", coldescs_cpu[col].name);
e3b3a2f3 458 return buf;
477251f8
KZ
459}
460
0e86bc84
KZ
461/*
462 * [-C] backend
463 */
464static void
465print_caches_readable(struct lscpu_desc *desc, int cols[], int ncols,
466 struct lscpu_modifier *mod)
467{
0e86bc84 468 struct libscols_table *table;
ac26b19b
KZ
469 struct cpu_cache *cachesrc;
470 int i, end, j, shared_allsize;
0e86bc84
KZ
471
472 scols_init_debug(0);
473
474 table = scols_new_table();
475 if (!table)
476 err(EXIT_FAILURE, _("failed to allocate output table"));
477 if (mod->json) {
478 scols_table_enable_json(table, 1);
479 scols_table_set_name(table, "caches");
480 }
481
482 for (i = 0; i < ncols; i++) {
483 struct lscpu_coldesc *cd = &coldescs_cache[cols[i]];
484 if (!scols_table_new_column(table, cd->name, 0, cd->flags))
485 err(EXIT_FAILURE, _("failed to allocate output column"));
486 }
487
318542e0
SK
488 for (j = 0; j < 2; j++) {
489 /* First check the caches from /sys/devices */
490 if (j == 0) {
491 cachesrc = desc->caches;
492 end = desc->ncaches - 1;
493 shared_allsize = 0;
ac26b19b 494 } else {
318542e0
SK
495 /* Check shared caches from /proc/cpuinfo s390 */
496 cachesrc = desc->ecaches;
497 end = desc->necaches - 1;
498 /* Dont use get_cache_full_size */
499 shared_allsize = 1;
500 }
0e86bc84 501
318542e0 502 for (i = end; i >= 0; i--) {
318542e0 503 struct libscols_line *line;
ac26b19b 504 struct cpu_cache *ca = &cachesrc[i];
318542e0 505 int c;
0e86bc84 506
318542e0
SK
507 line = scols_table_new_line(table, NULL);
508 if (!line)
509 err(EXIT_FAILURE, _("failed to allocate output line"));
0e86bc84 510
318542e0
SK
511 for (c = 0; c < ncols; c++) {
512 char *data = NULL;
318542e0 513
ac26b19b 514 switch (cols[c]) {
318542e0
SK
515 case COL_CACHE_NAME:
516 if (ca->name)
517 data = xstrdup(ca->name);
518 break;
519 case COL_CACHE_ONESIZE:
520 if (!ca->size)
521 break;
522 if (mod->bytes)
523 xasprintf(&data, "%" PRIu64, ca->size);
524 else
525 data = size_to_human_string(SIZE_SUFFIX_1LETTER, ca->size);
526 break;
527 case COL_CACHE_ALLSIZE:
528 {
529 uint64_t sz = 0;
530 if (shared_allsize)
531 break;
532 if (get_cache_full_size(desc, ca, &sz) != 0)
533 break;
534 if (mod->bytes)
535 xasprintf(&data, "%" PRIu64, sz);
536 else
537 data = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
538 break;
539 }
540 case COL_CACHE_WAYS:
541 if (ca->ways_of_associativity)
542 xasprintf(&data, "%u", ca->ways_of_associativity);
0e86bc84 543 break;
318542e0
SK
544 case COL_CACHE_TYPE:
545 if (ca->type)
546 data = xstrdup(ca->type);
0e86bc84 547 break;
318542e0
SK
548 case COL_CACHE_LEVEL:
549 if (ca->level)
550 xasprintf(&data, "%d", ca->level);
551 break;
552 case COL_CACHE_ALLOCPOL:
553 if (ca->allocation_policy)
554 data = xstrdup(ca->allocation_policy);
555 break;
556 case COL_CACHE_WRITEPOL:
557 if (ca->write_policy)
558 data = xstrdup(ca->write_policy);
559 break;
560 case COL_CACHE_PHYLINE:
561 if (ca->physical_line_partition)
562 xasprintf(&data, "%u", ca->physical_line_partition);
563 break;
564 case COL_CACHE_SETS:
565 if (ca->number_of_sets)
566 xasprintf(&data, "%u", ca->number_of_sets);
567 break;
568 case COL_CACHE_COHERENCYSIZE:
569 if (ca->coherency_line_size)
570 xasprintf(&data, "%u", ca->coherency_line_size);
571 break;
572 }
cf3b6b71 573
318542e0
SK
574 if (data && scols_line_refer_data(line, c, data))
575 err(EXIT_FAILURE, _("failed to add output data"));
0e86bc84 576 }
0e86bc84 577 }
318542e0 578
0e86bc84
KZ
579 }
580
581 scols_print_table(table);
582 scols_unref_table(table);
583}
584
477251f8 585/*
ba45d8c1 586 * [-p] backend, we support two parsable formats:
477251f8
KZ
587 *
588 * 1) "compatible" -- this format is compatible with the original lscpu(1)
589 * output and it contains fixed set of the columns. The CACHE columns are at
590 * the end of the line and the CACHE is not printed if the number of the caches
591 * is zero. The CACHE columns are separated by two commas, for example:
592 *
593 * $ lscpu --parse
594 * # CPU,Core,Socket,Node,,L1d,L1i,L2
595 * 0,0,0,0,,0,0,0
596 * 1,1,0,0,,1,1,0
597 *
598 * 2) "user defined output" -- this format prints always all columns without
599 * special prefix for CACHE column. If there are not CACHEs then the column is
600 * empty and the header "Cache" is printed rather than a real name of the cache.
601 * The CACHE columns are separated by ':'.
602 *
603 * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE
604 * # CPU,Core,Socket,Node,L1d:L1i:L2
605 * 0,0,0,0,0:0:0
606 * 1,1,0,0,1:1:0
607 */
608static void
cc07239d 609print_cpus_parsable(struct lscpu_desc *desc, int cols[], int ncols,
8005924a 610 struct lscpu_modifier *mod)
477251f8 611{
e3b3a2f3
KZ
612 char buf[BUFSIZ], *data;
613 int i;
477251f8 614
e3b3a2f3
KZ
615 /*
616 * Header
617 */
477251f8
KZ
618 printf(_(
619 "# The following is the parsable format, which can be fed to other\n"
620 "# programs. Each different item in every column has an unique ID\n"
621 "# starting from zero.\n"));
622
623 fputs("# ", stdout);
624 for (i = 0; i < ncols; i++) {
3d27b76a 625 int col = cols[i];
b9d18bc3 626
cc07239d 627 if (col == COL_CPU_CACHE) {
8005924a 628 if (mod->compat && !desc->ncaches)
477251f8 629 continue;
8005924a 630 if (mod->compat && i != 0)
477251f8 631 putchar(',');
477251f8 632 }
e3b3a2f3
KZ
633 if (i > 0)
634 putchar(',');
635
3d27b76a 636 data = get_cell_header(desc, col, mod, buf, sizeof(buf));
b9d18bc3 637
cc07239d
KZ
638 if (data && * data && col != COL_CPU_CACHE &&
639 !coldescs_cpu[col].is_abbr) {
3d27b76a
KZ
640 /*
641 * For normal column names use mixed case (e.g. "Socket")
642 */
643 char *p = data + 1;
644
14e8be8a
PU
645 while (p && *p != '\0') {
646 *p = tolower((unsigned int) *p);
647 p++;
648 }
3d27b76a 649 }
e3b3a2f3 650 fputs(data && *data ? data : "", stdout);
477251f8
KZ
651 }
652 putchar('\n');
653
e3b3a2f3
KZ
654 /*
655 * Data
656 */
a5cfffff 657 for (i = 0; i < desc->ncpuspos; i++) {
e3b3a2f3 658 int c;
4f642863 659 int cpu = real_cpu_num(desc, i);
e3b3a2f3 660
6dd7b74b
SK
661 if (desc->online) {
662 if (!mod->offline && !is_cpu_online(desc, cpu))
663 continue;
664 if (!mod->online && is_cpu_online(desc, cpu))
665 continue;
666 }
4f642863 667 if (desc->present && !is_cpu_present(desc, cpu))
a5cfffff 668 continue;
477251f8 669 for (c = 0; c < ncols; c++) {
cc07239d 670 if (mod->compat && cols[c] == COL_CPU_CACHE) {
477251f8
KZ
671 if (!desc->ncaches)
672 continue;
673 if (c > 0)
674 putchar(',');
675 }
676 if (c > 0)
677 putchar(',');
e3b3a2f3
KZ
678
679 data = get_cell_data(desc, i, cols[c], mod,
680 buf, sizeof(buf));
681 fputs(data && *data ? data : "", stdout);
bdda3543 682 *buf = '\0';
477251f8 683 }
5dd7507c
CQ
684 putchar('\n');
685 }
686}
687
ba45d8c1
KZ
688/*
689 * [-e] backend
690 */
691static void
cc07239d 692print_cpus_readable(struct lscpu_desc *desc, int cols[], int ncols,
ba45d8c1
KZ
693 struct lscpu_modifier *mod)
694{
695 int i;
e7213e34
KZ
696 char buf[BUFSIZ];
697 const char *data;
710ed55d 698 struct libscols_table *table;
ba45d8c1 699
710ed55d
KZ
700 scols_init_debug(0);
701
702 table = scols_new_table();
83db4eb2 703 if (!table)
780ce22c 704 err(EXIT_FAILURE, _("failed to allocate output table"));
19a5510b
KZ
705 if (mod->json) {
706 scols_table_enable_json(table, 1);
707 scols_table_set_name(table, "cpus");
708 }
ba45d8c1
KZ
709
710 for (i = 0; i < ncols; i++) {
b9d18bc3 711 data = get_cell_header(desc, cols[i], mod, buf, sizeof(buf));
2ec00a10 712 if (!scols_table_new_column(table, data, 0, coldescs_cpu[cols[i]].flags))
780ce22c 713 err(EXIT_FAILURE, _("failed to allocate output column"));
ba45d8c1
KZ
714 }
715
a5cfffff 716 for (i = 0; i < desc->ncpuspos; i++) {
ba45d8c1 717 int c;
83db4eb2 718 struct libscols_line *line;
4f642863 719 int cpu = real_cpu_num(desc, i);
ba45d8c1 720
6dd7b74b
SK
721 if (desc->online) {
722 if (!mod->offline && !is_cpu_online(desc, cpu))
723 continue;
724 if (!mod->online && is_cpu_online(desc, cpu))
725 continue;
726 }
4f642863 727 if (desc->present && !is_cpu_present(desc, cpu))
a5cfffff 728 continue;
ba45d8c1 729
83db4eb2 730 line = scols_table_new_line(table, NULL);
e7213e34 731 if (!line)
780ce22c 732 err(EXIT_FAILURE, _("failed to allocate output line"));
ba45d8c1
KZ
733
734 for (c = 0; c < ncols; c++) {
735 data = get_cell_data(desc, i, cols[c], mod,
736 buf, sizeof(buf));
e7213e34
KZ
737 if (!data || !*data)
738 data = "-";
bf1eab07 739 if (scols_line_set_data(line, c, data))
780ce22c 740 err(EXIT_FAILURE, _("failed to add output data"));
ba45d8c1
KZ
741 }
742 }
743
83db4eb2
OO
744 scols_print_table(table);
745 scols_unref_table(table);
ba45d8c1 746}
5dd7507c 747
d8813bb3 748#endif
c82b12a0 749
e3f21318 750static struct libscols_line *
91eef60c 751 __attribute__ ((__format__(printf, 4, 5)))
c82b12a0 752 add_summary_sprint(struct libscols_table *tb,
e3f21318 753 struct libscols_line *sec,
c82b12a0
KZ
754 const char *txt,
755 const char *fmt,
756 ...)
757{
91eef60c 758 struct libscols_line *ln;
c82b12a0
KZ
759 va_list args;
760
91eef60c
KZ
761 /* Don't print section lines without data on non-terminal output */
762 if (!is_term && fmt == NULL)
763 return NULL;
764
765 ln = scols_table_new_line(tb, sec);
c82b12a0 766 if (!ln)
780ce22c 767 err(EXIT_FAILURE, _("failed to allocate output line"));
c82b12a0
KZ
768
769 /* description column */
a0661407
KZ
770 if (txt && scols_line_set_data(ln, 0, txt))
771 err(EXIT_FAILURE, _("failed to add output data"));
c82b12a0
KZ
772
773 /* data column */
91eef60c
KZ
774 if (fmt) {
775 char *data;
776 va_start(args, fmt);
777 xvasprintf(&data, fmt, args);
778 va_end(args);
779
780 if (data && scols_line_refer_data(ln, 1, data))
781 err(EXIT_FAILURE, _("failed to add output data"));
782 }
e3f21318
KZ
783
784 return ln;
c82b12a0
KZ
785}
786
91eef60c 787#define add_summary_e(tb, sec, txt) add_summary_sprint(tb, sec, txt, NULL)
e3f21318
KZ
788#define add_summary_n(tb, sec, txt, num) add_summary_sprint(tb, sec, txt, "%zu", num)
789#define add_summary_s(tb, sec, txt, str) add_summary_sprint(tb, sec, txt, "%s", str)
790#define add_summary_x(tb, sec, txt, fmt, x) add_summary_sprint(tb, sec, txt, fmt, x)
5dd7507c
CQ
791
792static void
d8813bb3
KZ
793print_cpuset(struct lscpu_cxt *cxt,
794 struct libscols_table *tb,
91eef60c 795 struct libscols_line *sec,
d8813bb3 796 const char *key, cpu_set_t *set)
4f912c6a 797{
d8813bb3 798 size_t setbuflen = 7 * cxt->maxcpus;
4f912c6a
KZ
799 char setbuf[setbuflen], *p;
800
93a1bb10
KZ
801 assert(set);
802 assert(key);
803 assert(tb);
804 assert(cxt);
805
d8813bb3
KZ
806 if (cxt->hex) {
807 p = cpumask_create(setbuf, setbuflen, set, cxt->setsize);
91eef60c 808 add_summary_s(tb, sec, key, p);
4f912c6a 809 } else {
d8813bb3 810 p = cpulist_create(setbuf, setbuflen, set, cxt->setsize);
91eef60c 811 add_summary_s(tb, sec, key, p);
4f912c6a 812 }
4f912c6a
KZ
813}
814
2f5e2730
KZ
815static void
816print_summary_cputype(struct lscpu_cxt *cxt,
817 struct lscpu_cputype *ct,
91eef60c
KZ
818 struct libscols_table *tb,
819 struct libscols_line *sec)
5dd7507c 820{
91eef60c
KZ
821 if (ct->modelname) {
822 struct libscols_line *tmp = add_summary_s(tb, sec, _("Model name:"), ct->modelname);
823 if (!sec)
824 sec= tmp;
825 }
826
2f5e2730 827 if (ct->machinetype)
91eef60c 828 add_summary_s(tb, sec, _("Machine type:"), ct->machinetype);
2f5e2730 829 if (ct->family)
91eef60c 830 add_summary_s(tb, sec, _("CPU family:"), ct->family);
2f5e2730 831 if (ct->model || ct->revision)
91eef60c 832 add_summary_s(tb, sec, _("Model:"), ct->revision ? ct->revision : ct->model);
2f5e2730 833
91eef60c
KZ
834 add_summary_n(tb, sec, _("Thread(s) per core:"), ct->nthreads_per_core);
835 add_summary_n(tb, sec, _("Core(s) per socket:"), ct->ncores_per_socket);
2f5e2730 836 if (ct->nbooks) {
91eef60c 837 add_summary_n(tb, sec, _("Socket(s) per book:"), ct->nsockets_per_book);
2f5e2730 838 if (ct->ndrawers_per_system) {
91eef60c
KZ
839 add_summary_n(tb, sec, _("Book(s) per drawer:"), ct->nbooks_per_drawer);
840 add_summary_n(tb, sec, _("Drawer(s):"), ct->ndrawers_per_system);
2f5e2730 841 } else
91eef60c 842 add_summary_n(tb, sec, _("Book(s):"), ct->nbooks);
2f5e2730 843 } else
91eef60c 844 add_summary_n(tb, sec, _("Socket(s):"), ct->nsockets);
2f5e2730 845
93a1bb10 846 if (ct->stepping)
91eef60c 847 add_summary_s(tb, sec, _("Stepping:"), ct->stepping);
93a1bb10 848 if (ct->freqboost >= 0)
91eef60c 849 add_summary_s(tb, sec, _("Frequency boost:"), ct->freqboost ?
16ca0551 850 _("enabled") : _("disabled"));
01bea871
KZ
851
852 /* s390 -- from the first CPU where is dynamic/static MHz */
93a1bb10 853 if (ct->dynamic_mhz)
91eef60c 854 add_summary_s(tb, sec, _("CPU dynamic MHz:"), ct->dynamic_mhz);
93a1bb10 855 if (ct->static_mhz)
91eef60c 856 add_summary_s(tb, sec, _("CPU static MHz:"), ct->static_mhz);
01bea871 857
93a1bb10 858 if (ct->has_freq) {
91eef60c
KZ
859 add_summary_x(tb, sec, _("CPU max MHz:"), "%.4f", lsblk_cputype_get_maxmhz(cxt, ct));
860 add_summary_x(tb, sec, _("CPU min MHz:"), "%.4f", lsblk_cputype_get_minmhz(cxt, ct));
bd9b94d1 861 }
93a1bb10 862 if (ct->bogomips)
91eef60c
KZ
863 add_summary_s(tb, sec, _("BogoMIPS:"), ct->bogomips);
864
93a1bb10 865 if (ct->dispatching >= 0)
91eef60c 866 add_summary_s(tb, sec, _("Dispatching mode:"), _(disp_modes[ct->dispatching]));
93a1bb10
KZ
867
868 if (ct->physsockets) {
91eef60c
KZ
869 add_summary_n(tb, sec, _("Physical sockets:"), ct->physsockets);
870 add_summary_n(tb, sec, _("Physical chips:"), ct->physchips);
871 add_summary_n(tb, sec, _("Physical cores/chip:"), ct->physcoresperchip);
639eeb28
KZ
872 }
873
93a1bb10 874 if (ct->flags)
91eef60c 875 add_summary_s(tb, sec, _("Flags:"), ct->flags);
2f5e2730
KZ
876}
877
878/*
879 * default output
880 */
881static void print_summary(struct lscpu_cxt *cxt)
882{
883 struct lscpu_cputype *ct;
91eef60c 884 char field[256];
2f5e2730
KZ
885 size_t i = 0;
886 struct libscols_table *tb;
e3f21318 887 struct libscols_line *sec = NULL;
2f5e2730
KZ
888
889 scols_init_debug(0);
890
891 tb = scols_new_table();
892 if (!tb)
893 err(EXIT_FAILURE, _("failed to allocate output table"));
894
895 scols_table_enable_noheadings(tb, 1);
896 if (cxt->json) {
897 scols_table_enable_json(tb, 1);
898 scols_table_set_name(tb, "lscpu");
91eef60c
KZ
899 } else if (is_term) {
900 struct libscols_symbols *sy = scols_new_symbols();
901
902 if (!sy)
903 err_oom();
904 scols_symbols_set_branch(sy, " ");
905 scols_symbols_set_vertical(sy, " ");
906 scols_symbols_set_right(sy, " ");
907 scols_table_set_symbols(tb, sy);
2f5e2730
KZ
908 }
909
91eef60c 910 if (scols_table_new_column(tb, "field", 0, is_term ? SCOLS_FL_TREE : 0) == NULL ||
2f5e2730
KZ
911 scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES | SCOLS_FL_WRAP) == NULL)
912 err(EXIT_FAILURE, _("failed to initialize output column"));
913
914 ct = lscpu_cputype_get_default(cxt);
915
91eef60c 916 /* Section: architecture */
2f5e2730 917 if (cxt->arch)
e3f21318 918 sec = add_summary_s(tb, NULL, _("Architecture:"), cxt->arch->name);
2f5e2730
KZ
919 if (cxt->arch && (cxt->arch->bit32 || cxt->arch->bit64)) {
920 char buf[32], *p = buf;
921
922 if (cxt->arch->bit32) {
923 strcpy(p, "32-bit, ");
924 p += 8;
925 }
926 if (cxt->arch->bit64) {
927 strcpy(p, "64-bit, ");
928 p += 8;
929 }
930 *(p - 2) = '\0';
e3f21318 931 add_summary_s(tb, sec, _("CPU op-mode(s):"), buf);
2f5e2730
KZ
932 }
933 if (ct->addrsz)
e3f21318 934 add_summary_s(tb, sec, _("Address sizes:"), ct->addrsz);
2f5e2730 935#if !defined(WORDS_BIGENDIAN)
e3f21318 936 add_summary_s(tb, sec, _("Byte Order:"), "Little Endian");
2f5e2730 937#else
e3f21318 938 add_summary_s(tb, sec, _("Byte Order:"), "Big Endian");
2f5e2730 939#endif
91eef60c
KZ
940
941 /* Section: CPU lists */
942 sec = add_summary_n(tb, NULL, _("CPU(s):"), cxt->npresents);
943
2f5e2730 944 if (cxt->online)
91eef60c 945 print_cpuset(cxt, tb, sec,
2f5e2730
KZ
946 cxt->hex ? _("On-line CPU(s) mask:") :
947 _("On-line CPU(s) list:"),
948 cxt->online);
949
950 if (cxt->online && cxt->nonlines != cxt->npresents) {
951 cpu_set_t *set;
952
953 /* Linux kernel provides cpuset of off-line CPUs that contains
954 * all configured CPUs (see /sys/devices/system/cpu/offline),
955 * but want to print real (present in system) off-line CPUs only.
956 */
957 set = cpuset_alloc(cxt->maxcpus, NULL, NULL);
958 if (!set)
959 err(EXIT_FAILURE, _("failed to callocate cpu set"));
960 CPU_ZERO_S(cxt->setsize, set);
961 for (i = 0; i < cxt->npossibles; i++) {
962 struct lscpu_cpu *cpu = cxt->cpus[i];
963
964 if (cpu && is_cpu_present(cxt, cpu) && !is_cpu_online(cxt, cpu))
965 CPU_SET_S(cpu->logical_id, cxt->setsize, set);
966 }
91eef60c 967 print_cpuset(cxt, tb, sec,
2f5e2730
KZ
968 cxt->hex ? _("Off-line CPU(s) mask:") :
969 _("Off-line CPU(s) list:"), set);
970 cpuset_free(set);
971 }
972
91eef60c
KZ
973 /* Section: cpu type description */
974 if (ct->vendor)
975 sec = add_summary_s(tb, NULL, _("Vendor ID:"), ct->vendor);
976
977 for (i = 0; i < cxt->ncputypes; i++) {
978 print_summary_cputype(cxt, cxt->cputypes[i],
979 tb, cxt->ncputypes == 1 ? sec : NULL);
e3f21318 980
91eef60c
KZ
981 }
982 sec = NULL;
2f5e2730 983
91eef60c 984 /* Section: vitualiazation */
93a1bb10 985 if (cxt->virt) {
91eef60c 986 sec = add_summary_e(tb, NULL, _("Virtualization features:"));
01bea871 987 if (cxt->virt->cpuflag && !strcmp(cxt->virt->cpuflag, "svm"))
e3f21318 988 add_summary_s(tb, sec, _("Virtualization:"), "AMD-V");
01bea871 989 else if (cxt->virt->cpuflag && !strcmp(cxt->virt->cpuflag, "vmx"))
e3f21318 990 add_summary_s(tb, sec, _("Virtualization:"), "VT-x");
93a1bb10
KZ
991
992 if (cxt->virt->hypervisor)
e3f21318 993 add_summary_s(tb, sec, _("Hypervisor:"), cxt->virt->hypervisor);
93a1bb10 994 if (cxt->virt->vendor) {
e3f21318
KZ
995 add_summary_s(tb, sec, _("Hypervisor vendor:"), hv_vendors[cxt->virt->vendor]);
996 add_summary_s(tb, sec, _("Virtualization type:"), _(virt_types[cxt->virt->type]));
93a1bb10 997 }
e3f21318 998 sec = NULL;
93a1bb10 999 }
91eef60c
KZ
1000
1001 /* Section: caches */
01bea871
KZ
1002 if (cxt->ncaches) {
1003 const char *last = NULL;
93a1bb10 1004
01bea871
KZ
1005 /* The caches are sorted by name, cxt->caches[] may contains
1006 * multiple instances for the same name.
1007 */
91eef60c 1008 sec = add_summary_e(tb, NULL, _("Caches:"));
e3f21318 1009
01bea871
KZ
1010 for (i = 0; i < cxt->ncaches; i++) {
1011 const char *name = cxt->caches[i].name;
1012 uint64_t sz;
93a1bb10 1013
01bea871 1014 if (last && strcmp(last, name) == 0)
93a1bb10 1015 continue;
01bea871
KZ
1016 sz = lscpu_get_cache_full_size(cxt, name);
1017 if (!sz)
93a1bb10 1018 continue;
91eef60c 1019 snprintf(field, sizeof(field), is_term ? _("%s:") : _("%s cache:"), name);
01bea871 1020 if (cxt->bytes)
91eef60c 1021 add_summary_x(tb, sec, field, "%" PRIu64, sz);
01bea871
KZ
1022 else {
1023 char *tmp = size_to_human_string(
1024 SIZE_SUFFIX_3LETTER |
1025 SIZE_SUFFIX_SPACE,
1026 sz);
91eef60c 1027 add_summary_s(tb, sec, field, tmp);
01bea871
KZ
1028 free(tmp);
1029 }
1030 last = name;
93a1bb10 1031 }
e3f21318 1032 sec = NULL;
93a1bb10 1033 }
01bea871 1034
91eef60c 1035 /* Section: extra caches (s390, ...) */
01bea871 1036 if (cxt->necaches) {
91eef60c 1037 sec = add_summary_e(tb, NULL, _("Extra caches:"));
e3f21318 1038
01bea871
KZ
1039 for (i = 0; i < cxt->necaches; i++) {
1040 struct lscpu_cache *ca = &cxt->ecaches[i];
93a1bb10
KZ
1041
1042 if (ca->size == 0)
1043 continue;
91eef60c 1044 snprintf(field, sizeof(field), is_term ? _("%s:") : _("%s cache:"), ca->name);
01bea871 1045 if (cxt->bytes)
91eef60c 1046 add_summary_x(tb, sec, field, "%" PRIu64, ca->size);
01bea871
KZ
1047 else {
1048 char *tmp = size_to_human_string(
1049 SIZE_SUFFIX_3LETTER |
1050 SIZE_SUFFIX_SPACE,
1051 ca->size);
91eef60c 1052 add_summary_s(tb, sec, field, tmp);
01bea871
KZ
1053 free(tmp);
1054 }
1055 }
1056 }
1057
91eef60c 1058 /* Section: NUMA modes */
01bea871 1059 if (cxt->nnodes) {
91eef60c 1060 sec = add_summary_e(tb, NULL, _("NUMA:"));
e3f21318
KZ
1061
1062 add_summary_n(tb, sec,_("NUMA node(s):"), cxt->nnodes);
01bea871 1063 for (i = 0; i < cxt->nnodes; i++) {
91eef60c
KZ
1064 snprintf(field, sizeof(field), _("NUMA node%d CPU(s):"), cxt->idx2nodenum[i]);
1065 print_cpuset(cxt, tb, sec, field, cxt->nodemaps[i]);
93a1bb10 1066 }
e3f21318 1067 sec = NULL;
93a1bb10
KZ
1068 }
1069
91eef60c 1070 /* Section: Vulnerabilities */
01bea871 1071 if (cxt->vuls) {
91eef60c 1072 sec = add_summary_e(tb, NULL, _("Vulnerabilities:"));
e3f21318 1073
01bea871 1074 for (i = 0; i < cxt->nvuls; i++) {
91eef60c
KZ
1075 snprintf(field, sizeof(field), is_term ?
1076 _("%s:") : _("Vulnerability %s:"), cxt->vuls[i].name);
1077 add_summary_s(tb, sec, field, cxt->vuls[i].text);
93a1bb10 1078 }
e3f21318 1079 sec = NULL;
93a1bb10 1080 }
c82b12a0
KZ
1081 scols_print_table(tb);
1082 scols_unref_table(tb);
5dd7507c
CQ
1083}
1084
6e1eda6f 1085static void __attribute__((__noreturn__)) usage(void)
5dd7507c 1086{
6e1eda6f 1087 FILE *out = stdout;
b9d18bc3
KZ
1088 size_t i;
1089
1090 fputs(USAGE_HEADER, out);
c6f095cf 1091 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
7f1ec5e8 1092
451dbcfa
BS
1093 fputs(USAGE_SEPARATOR, out);
1094 fputs(_("Display information about the CPU architecture.\n"), out);
1095
b9d18bc3 1096 fputs(USAGE_OPTIONS, out);
c6f095cf
BS
1097 fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out);
1098 fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out);
2011528b 1099 fputs(_(" -B, --bytes print sizes in bytes rather than in human readable format\n"), out);
0e86bc84 1100 fputs(_(" -C, --caches[=<list>] info about caches in extended readable format\n"), out);
c6f095cf 1101 fputs(_(" -c, --offline print offline CPUs only\n"), out);
19a5510b 1102 fputs(_(" -J, --json use JSON for default or extended format\n"), out);
c6f095cf
BS
1103 fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out);
1104 fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out);
1105 fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out);
1106 fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out);
0d2b5d2a 1107 fputs(_(" -y, --physical print physical instead of logical IDs\n"), out);
0a31a242 1108 fputs(_(" --output-all print all available columns for -e, -p or -C\n"), out);
c6f095cf 1109 fputs(USAGE_SEPARATOR, out);
f45f3ec3 1110 printf(USAGE_HELP_OPTIONS(25));
b9d18bc3 1111
cc07239d
KZ
1112 fputs(_("\nAvailable output columns for -e or -p:\n"), out);
1113 for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++)
1114 fprintf(out, " %13s %s\n", coldescs_cpu[i].name, _(coldescs_cpu[i].help));
3d27b76a 1115
0e86bc84
KZ
1116 fputs(_("\nAvailable output columns for -C:\n"), out);
1117 for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++)
1118 fprintf(out, " %13s %s\n", coldescs_cache[i].name, _(coldescs_cache[i].help));
1119
f45f3ec3 1120 printf(USAGE_MAN_TAIL("lscpu(1)"));
4f912c6a 1121
6e1eda6f 1122 exit(EXIT_SUCCESS);
5dd7507c
CQ
1123}
1124
1125int main(int argc, char *argv[])
1126{
27c349f9 1127 struct lscpu_cxt *cxt;
b73d38b1 1128 int c, all = 0;
cc07239d 1129 int columns[ARRAY_SIZE(coldescs_cpu)], ncolumns = 0;
7fc12cd2 1130 int cpu_modifier_specified = 0;
b73d38b1 1131 size_t i;
fbf0619b
SK
1132 enum {
1133 OPT_OUTPUT_ALL = CHAR_MAX + 1,
1134 };
6c7d5ae9 1135 static const struct option longopts[] = {
87918040
SK
1136 { "all", no_argument, NULL, 'a' },
1137 { "online", no_argument, NULL, 'b' },
2011528b 1138 { "bytes", no_argument, NULL, 'B' },
0e86bc84 1139 { "caches", optional_argument, NULL, 'C' },
87918040
SK
1140 { "offline", no_argument, NULL, 'c' },
1141 { "help", no_argument, NULL, 'h' },
1142 { "extended", optional_argument, NULL, 'e' },
19a5510b 1143 { "json", no_argument, NULL, 'J' },
87918040
SK
1144 { "parse", optional_argument, NULL, 'p' },
1145 { "sysroot", required_argument, NULL, 's' },
1146 { "physical", no_argument, NULL, 'y' },
1147 { "hex", no_argument, NULL, 'x' },
1148 { "version", no_argument, NULL, 'V' },
fbf0619b 1149 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
87918040 1150 { NULL, 0, NULL, 0 }
5dd7507c
CQ
1151 };
1152
8e97eb4b 1153 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
0e86bc84 1154 { 'C','e','p' },
a44cd891 1155 { 'a','b','c' },
8e97eb4b
KZ
1156 { 0 }
1157 };
1158 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1159
2f8f1388 1160 setlocale(LC_ALL, "");
5dd7507c
CQ
1161 bindtextdomain(PACKAGE, LOCALEDIR);
1162 textdomain(PACKAGE);
25b7045e 1163 close_stdout_atexit();
5dd7507c 1164
b73d38b1
KZ
1165 cxt = lscpu_new_context();
1166
2011528b 1167 while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::s:xyV", longopts, NULL)) != -1) {
8e97eb4b
KZ
1168
1169 err_exclusive_options(c, longopts, excl, excl_st);
1170
5dd7507c 1171 switch (c) {
0ad29ff6 1172 case 'a':
b73d38b1 1173 cxt->show_online = cxt->show_offline = 1;
7fc12cd2 1174 cpu_modifier_specified = 1;
0ad29ff6 1175 break;
2011528b 1176 case 'B':
b73d38b1 1177 cxt->bytes = 1;
2011528b 1178 break;
23e9e95a 1179 case 'b':
b73d38b1 1180 cxt->show_online = 1;
7fc12cd2 1181 cpu_modifier_specified = 1;
23e9e95a 1182 break;
7afc2387 1183 case 'c':
b73d38b1 1184 cxt->show_offline = 1;
7fc12cd2 1185 cpu_modifier_specified = 1;
7afc2387 1186 break;
0e86bc84
KZ
1187 case 'C':
1188 if (optarg) {
1189 if (*optarg == '=')
1190 optarg++;
1191 ncolumns = string_to_idarray(optarg,
1192 columns, ARRAY_SIZE(columns),
1193 cache_column_name_to_id);
1194 if (ncolumns < 0)
1195 return EXIT_FAILURE;
1196 }
b73d38b1 1197 cxt->mode = LSCPU_OUTPUT_CACHES;
0e86bc84 1198 break;
19a5510b 1199 case 'J':
b73d38b1 1200 cxt->json = 1;
19a5510b 1201 break;
5dd7507c 1202 case 'p':
ba45d8c1 1203 case 'e':
477251f8
KZ
1204 if (optarg) {
1205 if (*optarg == '=')
1206 optarg++;
1207 ncolumns = string_to_idarray(optarg,
1208 columns, ARRAY_SIZE(columns),
cc07239d 1209 cpu_column_name_to_id);
477251f8
KZ
1210 if (ncolumns < 0)
1211 return EXIT_FAILURE;
477251f8 1212 }
b73d38b1 1213 cxt->mode = c == 'p' ? LSCPU_OUTPUT_PARSABLE : LSCPU_OUTPUT_READABLE;
5dd7507c 1214 break;
47b6e8b6 1215 case 's':
b73d38b1
KZ
1216 cxt->prefix = optarg;
1217 cxt->noalive = 1;
47b6e8b6 1218 break;
4f912c6a 1219 case 'x':
b73d38b1 1220 cxt->hex = 1;
4f912c6a 1221 break;
0d2b5d2a 1222 case 'y':
b73d38b1 1223 cxt->show_physical = 1;
0d2b5d2a 1224 break;
fbf0619b 1225 case OPT_OUTPUT_ALL:
0e86bc84 1226 all = 1;
fbf0619b 1227 break;
2c308875
KZ
1228
1229 case 'h':
1230 usage();
1231 case 'V':
1232 print_version(EXIT_SUCCESS);
5dd7507c 1233 default:
677ec86c 1234 errtryhelp(EXIT_FAILURE);
5dd7507c
CQ
1235 }
1236 }
7bbb7829 1237
0a31a242 1238 if (all && ncolumns == 0) {
b73d38b1 1239 size_t maxsz = cxt->mode == LSCPU_OUTPUT_CACHES ?
0e86bc84
KZ
1240 ARRAY_SIZE(coldescs_cache) :
1241 ARRAY_SIZE(coldescs_cpu);
1242
b73d38b1
KZ
1243 for (i = 0; i < maxsz; i++)
1244 columns[ncolumns++] = i;
0e86bc84
KZ
1245 }
1246
b73d38b1 1247 if (cpu_modifier_specified && cxt->mode == LSCPU_OUTPUT_SUMMARY) {
7fc12cd2
HC
1248 fprintf(stderr,
1249 _("%s: options --all, --online and --offline may only "
ac56e555 1250 "be used with options --extended or --parse.\n"),
7fc12cd2
HC
1251 program_invocation_short_name);
1252 return EXIT_FAILURE;
1253 }
1254
6e1eda6f
RM
1255 if (argc != optind) {
1256 warnx(_("bad usage"));
1257 errtryhelp(EXIT_FAILURE);
1258 }
7bbb7829 1259
7afc2387 1260 /* set default cpu display mode if none was specified */
b73d38b1
KZ
1261 if (!cxt->show_online && !cxt->show_offline) {
1262 cxt->show_online = 1;
1263 cxt->show_offline = cxt->mode == LSCPU_OUTPUT_READABLE ? 1 : 0;
7afc2387 1264 }
6e509042 1265
91eef60c
KZ
1266 is_term = isatty(STDOUT_FILENO); /* global variable */
1267
4b9cbc38 1268 lscpu_init_debug();
6e509042 1269
4b9cbc38 1270 lscpu_context_init_paths(cxt);
5dd7507c 1271
4b9cbc38
KZ
1272 lscpu_read_cpulists(cxt);
1273 lscpu_read_cpuinfo(cxt);
1274 cxt->arch = lscpu_read_architecture(cxt);
538b50cb 1275
4b9cbc38
KZ
1276 lscpu_read_archext(cxt);
1277 lscpu_read_vulnerabilities(cxt);
1278 lscpu_read_numas(cxt);
1279 lscpu_read_topology(cxt);
7e03f383 1280
4b9cbc38 1281 lscpu_decode_arm(cxt);
28b1658f 1282
4b9cbc38 1283 cxt->virt = lscpu_read_virtualization(cxt);
c8b64f6d 1284
d8813bb3 1285 switch(cxt->mode) {
30b912d3 1286 case LSCPU_OUTPUT_SUMMARY:
d8813bb3 1287 print_summary(cxt);
ba45d8c1 1288 break;
d8813bb3
KZ
1289 }
1290
1291#ifdef LSCPU_OLD_OUTPUT_CODE
30b912d3 1292 case LSCPU_OUTPUT_CACHES:
0e86bc84
KZ
1293 if (!ncolumns) {
1294 columns[ncolumns++] = COL_CACHE_NAME;
1295 columns[ncolumns++] = COL_CACHE_ONESIZE;
1296 columns[ncolumns++] = COL_CACHE_ALLSIZE;
1297 columns[ncolumns++] = COL_CACHE_WAYS;
1298 columns[ncolumns++] = COL_CACHE_TYPE;
1299 columns[ncolumns++] = COL_CACHE_LEVEL;
cf3b6b71
KZ
1300 columns[ncolumns++] = COL_CACHE_SETS;
1301 columns[ncolumns++] = COL_CACHE_PHYLINE;
1302 columns[ncolumns++] = COL_CACHE_COHERENCYSIZE;
0e86bc84
KZ
1303 }
1304 print_caches_readable(desc, columns, ncolumns, mod);
1305 break;
30b912d3 1306 case LSCPU_OUTPUT_PARSABLE:
ba45d8c1 1307 if (!ncolumns) {
cc07239d
KZ
1308 columns[ncolumns++] = COL_CPU_CPU;
1309 columns[ncolumns++] = COL_CPU_CORE;
1310 columns[ncolumns++] = COL_CPU_SOCKET;
1311 columns[ncolumns++] = COL_CPU_NODE;
1312 columns[ncolumns++] = COL_CPU_CACHE;
ba45d8c1
KZ
1313 mod->compat = 1;
1314 }
cc07239d 1315 print_cpus_parsable(desc, columns, ncolumns, mod);
ba45d8c1 1316 break;
30b912d3 1317 case LSCPU_OUTPUT_READABLE:
ba45d8c1
KZ
1318 if (!ncolumns) {
1319 /* No list was given. Just print whatever is there. */
cc07239d 1320 columns[ncolumns++] = COL_CPU_CPU;
ba45d8c1 1321 if (desc->nodemaps)
cc07239d 1322 columns[ncolumns++] = COL_CPU_NODE;
b3adf6ef 1323 if (desc->drawermaps)
cc07239d 1324 columns[ncolumns++] = COL_CPU_DRAWER;
ba45d8c1 1325 if (desc->bookmaps)
cc07239d 1326 columns[ncolumns++] = COL_CPU_BOOK;
ba45d8c1 1327 if (desc->socketmaps)
cc07239d 1328 columns[ncolumns++] = COL_CPU_SOCKET;
ba45d8c1 1329 if (desc->coremaps)
cc07239d 1330 columns[ncolumns++] = COL_CPU_CORE;
ba45d8c1 1331 if (desc->caches)
cc07239d 1332 columns[ncolumns++] = COL_CPU_CACHE;
a7e5300c 1333 if (desc->online)
cc07239d 1334 columns[ncolumns++] = COL_CPU_ONLINE;
d231eea1 1335 if (desc->configured)
cc07239d 1336 columns[ncolumns++] = COL_CPU_CONFIGURED;
ba45d8c1 1337 if (desc->polarization)
cc07239d 1338 columns[ncolumns++] = COL_CPU_POLARIZATION;
ba45d8c1 1339 if (desc->addresses)
cc07239d 1340 columns[ncolumns++] = COL_CPU_ADDRESS;
e065a597 1341 if (desc->maxmhz)
cc07239d 1342 columns[ncolumns++] = COL_CPU_MAXMHZ;
e065a597 1343 if (desc->minmhz)
cc07239d 1344 columns[ncolumns++] = COL_CPU_MINMHZ;
ba45d8c1 1345 }
cc07239d 1346 print_cpus_readable(desc, columns, ncolumns, mod);
ba45d8c1 1347 break;
8005924a 1348 }
5dd7507c 1349
f9ac0210 1350#endif /* LSCPU_OLD_OUTPUT_CODE */
27c349f9
KZ
1351
1352 lscpu_free_context(cxt);
1353
cf474aac 1354 return EXIT_SUCCESS;
5dd7507c 1355}