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