]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
lscpu: fix for sparc64
[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
63c5e7f8
KZ
281static void __fill_id( struct lscpu_cxt *cxt,
282 struct lscpu_cpu *cpu,
283 int id, cpu_set_t **map,
284 size_t nitems,
285 char *buf, size_t bufsz)
286{
287 *buf = '\0';
288
289 if (cxt->show_physical) {
290 if (id < 0)
291 snprintf(buf, bufsz, "-");
292 else
293 snprintf(buf, bufsz, "%d", id);
294 } else if (map) {
295 size_t i;
296
297 if (cpuset_ary_isset(cpu->logical_id, map, nitems,
298 cxt->setsize, &i) == 0)
299 snprintf(buf, bufsz, "%zu", i);
300 }
301}
5dd7507c 302
63c5e7f8
KZ
303#define fill_id(_cxt, _cpu, NAME, _buf, _bufsz) \
304 __fill_id(_cxt, (_cpu), \
305 (_cpu)-> NAME ## id, \
306 (_cpu)->type-> NAME ## maps, \
307 (_cpu)->type->n ## NAME ## s, \
308 _buf, _bufsz)
309
310static char *get_cell_data(
311 struct lscpu_cxt *cxt,
312 struct lscpu_cpu *cpu, int col,
313 char *buf, size_t bufsz)
5dd7507c 314{
4f642863 315 size_t i;
e3b3a2f3
KZ
316
317 *buf = '\0';
5dd7507c 318
63c5e7f8
KZ
319 if (!cpu->type)
320 return NULL;
321
477251f8 322 switch (col) {
cc07239d 323 case COL_CPU_CPU:
63c5e7f8 324 snprintf(buf, bufsz, "%d", cpu->logical_id);
477251f8 325 break;
cc07239d 326 case COL_CPU_CORE:
63c5e7f8 327 fill_id(cxt, cpu, core, buf, bufsz);
477251f8 328 break;
cc07239d 329 case COL_CPU_SOCKET:
63c5e7f8 330 fill_id(cxt, cpu, socket, buf, bufsz);
477251f8 331 break;
cc07239d 332 case COL_CPU_DRAWER:
63c5e7f8 333 fill_id(cxt, cpu, drawer, buf, bufsz);
b3adf6ef 334 break;
cc07239d 335 case COL_CPU_BOOK:
63c5e7f8
KZ
336 fill_id(cxt, cpu, book, buf, bufsz);
337 break;
338 case COL_CPU_NODE:
339 if (cpuset_ary_isset(cpu->logical_id, cxt->nodemaps,
340 cxt->nnodes, cxt->setsize, &i) == 0)
341 snprintf(buf, bufsz, "%d", cxt->idx2nodenum[i]);
477251f8 342 break;
cc07239d 343 case COL_CPU_CACHE:
e3b3a2f3 344 {
63c5e7f8 345 const char *last = NULL;
e3b3a2f3
KZ
346 char *p = buf;
347 size_t sz = bufsz;
e3b3a2f3 348
63c5e7f8
KZ
349 for (i = 0; i < cxt->ncaches; i++) {
350 int x;
351 struct lscpu_cache *ca;
352 const char *name = cxt->caches[i].name;
e9d659ea 353
63c5e7f8
KZ
354 if (last && strcmp(last, name) == 0)
355 continue;
356 last = name;
357 ca = lscpu_cpu_get_cache(cxt, cpu, name);
358 if (!ca)
359 continue;
360 x = snprintf(p, sz, "%d", ca->id);
361 if (x < 0 || (size_t) x >= sz)
362 return NULL;
363 p += x;
364 sz -= x;
e07cca6b
KZ
365 if (sz < 2)
366 return NULL;
367 *p++ = cxt->show_compatible ? ',' : ':';
368 *p = '\0';
369 sz--;
5dd7507c 370 }
e07cca6b
KZ
371 if (p > buf && (*(p - 1) == ',' || *(p - 1) == ':'))
372 *(p - 1) = '\0';
477251f8 373 break;
e3b3a2f3 374 }
cc07239d 375 case COL_CPU_POLARIZATION:
63c5e7f8
KZ
376 if (cpu->polarization < 0)
377 break;
378 snprintf(buf, bufsz, "%s",
379 cxt->mode == LSCPU_OUTPUT_PARSABLE ?
380 polar_modes[cpu->polarization].parsable :
381 polar_modes[cpu->polarization].readable);
2b8fcb81 382 break;
cc07239d 383 case COL_CPU_ADDRESS:
63c5e7f8
KZ
384 if (cpu->address < 0)
385 break;
386 snprintf(buf, bufsz, "%d", cpu->address);
596b8845 387 break;
cc07239d 388 case COL_CPU_CONFIGURED:
63c5e7f8 389 if (cpu->configured < 0)
e43fc13e 390 break;
63c5e7f8 391 if (cxt->mode == LSCPU_OUTPUT_PARSABLE)
f205c90a 392 snprintf(buf, bufsz, "%s",
63c5e7f8 393 cpu->configured ? _("Y") : _("N"));
e43fc13e 394 else
f205c90a 395 snprintf(buf, bufsz, "%s",
63c5e7f8 396 cpu->configured ? _("yes") : _("no"));
d231eea1 397 break;
cc07239d 398 case COL_CPU_ONLINE:
63c5e7f8 399 if (!cxt->online)
e43fc13e 400 break;
63c5e7f8 401 if (cxt->mode == LSCPU_OUTPUT_PARSABLE)
f205c90a 402 snprintf(buf, bufsz, "%s",
63c5e7f8 403 is_cpu_online(cxt, cpu) ? _("Y") : _("N"));
e43fc13e 404 else
f205c90a 405 snprintf(buf, bufsz, "%s",
63c5e7f8 406 is_cpu_online(cxt, cpu) ? _("yes") : _("no"));
a7e5300c 407 break;
cc07239d 408 case COL_CPU_MAXMHZ:
63c5e7f8
KZ
409 if (cpu->mhz_max_freq)
410 snprintf(buf, bufsz, "%.4f", cpu->mhz_max_freq);
e065a597 411 break;
cc07239d 412 case COL_CPU_MINMHZ:
63c5e7f8
KZ
413 if (cpu->mhz_min_freq)
414 snprintf(buf, bufsz, "%.4f", cpu->mhz_min_freq);
44320710 415 break;
477251f8 416 }
e3b3a2f3
KZ
417 return buf;
418}
419
63c5e7f8
KZ
420static char *get_cell_header(
421 struct lscpu_cxt *cxt, int col,
422 char *buf, size_t bufsz)
e3b3a2f3
KZ
423{
424 *buf = '\0';
425
cc07239d 426 if (col == COL_CPU_CACHE) {
63c5e7f8 427 const char *last = NULL;
e3b3a2f3
KZ
428 char *p = buf;
429 size_t sz = bufsz;
63c5e7f8
KZ
430 size_t i;
431
432 for (i = 0; i < cxt->ncaches; i++) {
433 struct lscpu_cache *ca = &cxt->caches[i];
434 int x;
435
436 if (last && strcmp(last, ca->name) == 0)
437 continue;
438 last = ca->name;
e3b3a2f3 439
63c5e7f8 440 x = snprintf(p, sz, "%s", ca->name);
06fa5817 441 if (x < 0 || (size_t) x >= sz)
e3b3a2f3
KZ
442 return NULL;
443 sz -= x;
444 p += x;
e07cca6b
KZ
445 if (sz < 2)
446 return NULL;
447 *p++ = cxt->show_compatible ? ',' : ':';
448 *p = '\0';
449 sz--;
e3b3a2f3 450 }
e07cca6b
KZ
451 if (p > buf && (*(p - 1) == ',' || *(p - 1) == ':'))
452 *(p - 1) = '\0';
63c5e7f8 453 if (cxt->ncaches)
e3b3a2f3
KZ
454 return buf;
455 }
cc07239d 456 snprintf(buf, bufsz, "%s", coldescs_cpu[col].name);
e3b3a2f3 457 return buf;
477251f8
KZ
458}
459
1766641a 460
9d480e57
KZ
461static void caches_add_line(struct lscpu_cxt *cxt,
462 struct libscols_table *tb,
463 struct lscpu_cache *ca,
464 int cols[], size_t ncols)
465{
466 struct libscols_line *ln;
467 size_t c;
468
469 ln = scols_table_new_line(tb, NULL);
470 if (!ln)
471 err(EXIT_FAILURE, _("failed to allocate output line"));
472
473 for (c = 0; c < ncols; c++) {
474 char *data = NULL;
475 int col = cols[c];
476
477 switch (col) {
478 case COL_CACHE_NAME:
479 if (ca->name)
480 data = xstrdup(ca->name);
481 break;
482 case COL_CACHE_ONESIZE:
483 if (!ca->size)
484 break;
485 if (cxt->bytes)
486 xasprintf(&data, "%" PRIu64, ca->size);
487 else
488 data = size_to_human_string(SIZE_SUFFIX_1LETTER, ca->size);
489 break;
490 case COL_CACHE_ALLSIZE:
491 {
492 uint64_t sz = lscpu_get_cache_full_size(cxt, ca->name);
493 if (!sz)
494 break;
495 if (cxt->bytes)
496 xasprintf(&data, "%" PRIu64, sz);
497 else
498 data = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
499 break;
500 }
501 case COL_CACHE_WAYS:
502 if (ca->ways_of_associativity)
503 xasprintf(&data, "%u", ca->ways_of_associativity);
504 break;
505
506 case COL_CACHE_TYPE:
507 if (ca->type)
508 data = xstrdup(ca->type);
509 break;
510 case COL_CACHE_LEVEL:
511 if (ca->level)
512 xasprintf(&data, "%d", ca->level);
513 break;
514 case COL_CACHE_ALLOCPOL:
515 if (ca->allocation_policy)
516 data = xstrdup(ca->allocation_policy);
517 break;
518 case COL_CACHE_WRITEPOL:
519 if (ca->write_policy)
520 data = xstrdup(ca->write_policy);
521 break;
522 case COL_CACHE_PHYLINE:
523 if (ca->physical_line_partition)
524 xasprintf(&data, "%u", ca->physical_line_partition);
525 break;
526 case COL_CACHE_SETS:
527 if (ca->number_of_sets)
528 xasprintf(&data, "%u", ca->number_of_sets);
529 break;
530 case COL_CACHE_COHERENCYSIZE:
531 if (ca->coherency_line_size)
532 xasprintf(&data, "%u", ca->coherency_line_size);
533 break;
534 }
535
536 if (data && scols_line_refer_data(ln, c, data))
537 err(EXIT_FAILURE, _("failed to add output data"));
538 }
539}
540
541
0e86bc84
KZ
542/*
543 * [-C] backend
544 */
1766641a 545static void print_caches_readable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
0e86bc84 546{
1766641a
KZ
547 size_t i;
548 struct libscols_table *tb;
549 const char *last = NULL;
0e86bc84
KZ
550
551 scols_init_debug(0);
552
1766641a
KZ
553 tb = scols_new_table();
554 if (!tb)
0e86bc84 555 err(EXIT_FAILURE, _("failed to allocate output table"));
1766641a
KZ
556 if (cxt->json) {
557 scols_table_enable_json(tb, 1);
558 scols_table_set_name(tb, "caches");
0e86bc84
KZ
559 }
560
561 for (i = 0; i < ncols; i++) {
562 struct lscpu_coldesc *cd = &coldescs_cache[cols[i]];
1766641a 563 if (!scols_table_new_column(tb, cd->name, 0, cd->flags))
0e86bc84
KZ
564 err(EXIT_FAILURE, _("failed to allocate output column"));
565 }
566
9d480e57 567 /* standard caches */
1766641a
KZ
568 for (i = 0; i < cxt->ncaches; i++) {
569 struct lscpu_cache *ca = &cxt->caches[i];
0e86bc84 570
1766641a
KZ
571 if (last && strcmp(last, ca->name) == 0)
572 continue;
1766641a 573 last = ca->name;
9d480e57
KZ
574 caches_add_line(cxt, tb, ca, cols, ncols);
575 }
1766641a 576
9d480e57
KZ
577 /* extra caches */
578 for (i = 0; i < cxt->necaches; i++) {
579 struct lscpu_cache *ca = &cxt->ecaches[i];
318542e0 580
9d480e57
KZ
581 if (last && strcmp(last, ca->name) == 0)
582 continue;
583 last = ca->name;
584 caches_add_line(cxt, tb, ca, cols, ncols);
0e86bc84
KZ
585 }
586
1766641a
KZ
587 scols_print_table(tb);
588 scols_unref_table(tb);
0e86bc84
KZ
589}
590
477251f8 591/*
ba45d8c1 592 * [-p] backend, we support two parsable formats:
477251f8
KZ
593 *
594 * 1) "compatible" -- this format is compatible with the original lscpu(1)
595 * output and it contains fixed set of the columns. The CACHE columns are at
596 * the end of the line and the CACHE is not printed if the number of the caches
597 * is zero. The CACHE columns are separated by two commas, for example:
598 *
599 * $ lscpu --parse
600 * # CPU,Core,Socket,Node,,L1d,L1i,L2
601 * 0,0,0,0,,0,0,0
602 * 1,1,0,0,,1,1,0
603 *
604 * 2) "user defined output" -- this format prints always all columns without
605 * special prefix for CACHE column. If there are not CACHEs then the column is
606 * empty and the header "Cache" is printed rather than a real name of the cache.
607 * The CACHE columns are separated by ':'.
608 *
609 * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE
610 * # CPU,Core,Socket,Node,L1d:L1i:L2
611 * 0,0,0,0,0:0:0
612 * 1,1,0,0,1:1:0
613 */
0710bb13 614static void print_cpus_parsable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
477251f8 615{
e3b3a2f3 616 char buf[BUFSIZ], *data;
0710bb13 617 size_t i;
477251f8 618
e3b3a2f3
KZ
619 /*
620 * Header
621 */
477251f8
KZ
622 printf(_(
623 "# The following is the parsable format, which can be fed to other\n"
624 "# programs. Each different item in every column has an unique ID\n"
625 "# starting from zero.\n"));
626
627 fputs("# ", stdout);
628 for (i = 0; i < ncols; i++) {
3d27b76a 629 int col = cols[i];
b9d18bc3 630
cc07239d 631 if (col == COL_CPU_CACHE) {
0710bb13 632 if (cxt->show_compatible && !cxt->ncaches)
477251f8 633 continue;
0710bb13 634 if (cxt->show_compatible && i != 0)
477251f8 635 putchar(',');
477251f8 636 }
e3b3a2f3
KZ
637 if (i > 0)
638 putchar(',');
639
0710bb13 640 data = get_cell_header(cxt, col, buf, sizeof(buf));
cc07239d
KZ
641 if (data && * data && col != COL_CPU_CACHE &&
642 !coldescs_cpu[col].is_abbr) {
3d27b76a
KZ
643 /*
644 * For normal column names use mixed case (e.g. "Socket")
645 */
646 char *p = data + 1;
647
14e8be8a
PU
648 while (p && *p != '\0') {
649 *p = tolower((unsigned int) *p);
650 p++;
651 }
3d27b76a 652 }
e3b3a2f3 653 fputs(data && *data ? data : "", stdout);
477251f8
KZ
654 }
655 putchar('\n');
656
e3b3a2f3
KZ
657 /*
658 * Data
659 */
0710bb13
KZ
660 for (i = 0; i < cxt->npossibles; i++) {
661 struct lscpu_cpu *cpu = cxt->cpus[i];
662 size_t c;
e3b3a2f3 663
0710bb13
KZ
664 if (cxt->online) {
665 if (!cxt->show_offline && !is_cpu_online(cxt, cpu))
6dd7b74b 666 continue;
0710bb13 667 if (!cxt->show_online && is_cpu_online(cxt, cpu))
6dd7b74b
SK
668 continue;
669 }
0710bb13 670 if (cxt->present && !is_cpu_present(cxt, cpu))
a5cfffff 671 continue;
0710bb13 672
477251f8 673 for (c = 0; c < ncols; c++) {
0710bb13
KZ
674 if (cxt->show_compatible && cols[c] == COL_CPU_CACHE) {
675 if (!cxt->ncaches)
477251f8
KZ
676 continue;
677 if (c > 0)
678 putchar(',');
679 }
680 if (c > 0)
681 putchar(',');
e3b3a2f3 682
0710bb13 683 data = get_cell_data(cxt, cpu, cols[c], buf, sizeof(buf));
e3b3a2f3 684 fputs(data && *data ? data : "", stdout);
bdda3543 685 *buf = '\0';
477251f8 686 }
5dd7507c
CQ
687 putchar('\n');
688 }
689}
690
ba45d8c1
KZ
691/*
692 * [-e] backend
693 */
63c5e7f8 694static void print_cpus_readable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
ba45d8c1 695{
63c5e7f8 696 size_t i;
e7213e34
KZ
697 char buf[BUFSIZ];
698 const char *data;
63c5e7f8 699 struct libscols_table *tb;
ba45d8c1 700
710ed55d
KZ
701 scols_init_debug(0);
702
63c5e7f8
KZ
703 tb = scols_new_table();
704 if (!tb)
780ce22c 705 err(EXIT_FAILURE, _("failed to allocate output table"));
63c5e7f8
KZ
706 if (cxt->json) {
707 scols_table_enable_json(tb, 1);
708 scols_table_set_name(tb, "cpus");
19a5510b 709 }
ba45d8c1
KZ
710
711 for (i = 0; i < ncols; i++) {
63c5e7f8
KZ
712 data = get_cell_header(cxt, cols[i], buf, sizeof(buf));
713 if (!scols_table_new_column(tb, data, 0, coldescs_cpu[cols[i]].flags))
780ce22c 714 err(EXIT_FAILURE, _("failed to allocate output column"));
ba45d8c1
KZ
715 }
716
63c5e7f8
KZ
717 for (i = 0; i < cxt->npossibles; i++) {
718 size_t c;
719 struct libscols_line *ln;
720 struct lscpu_cpu *cpu = cxt->cpus[i];
ba45d8c1 721
63c5e7f8
KZ
722 if (cxt->online) {
723 if (!cxt->show_offline && !is_cpu_online(cxt, cpu))
6dd7b74b 724 continue;
63c5e7f8 725 if (!cxt->show_online && is_cpu_online(cxt, cpu))
6dd7b74b
SK
726 continue;
727 }
63c5e7f8
KZ
728
729 if (cxt->present && !is_cpu_present(cxt, cpu))
a5cfffff 730 continue;
ba45d8c1 731
63c5e7f8
KZ
732 ln = scols_table_new_line(tb, NULL);
733 if (!ln)
780ce22c 734 err(EXIT_FAILURE, _("failed to allocate output line"));
ba45d8c1
KZ
735
736 for (c = 0; c < ncols; c++) {
63c5e7f8 737 data = get_cell_data(cxt, cpu, cols[c], buf, sizeof(buf));
e7213e34
KZ
738 if (!data || !*data)
739 data = "-";
63c5e7f8 740 if (scols_line_set_data(ln, c, data))
780ce22c 741 err(EXIT_FAILURE, _("failed to add output data"));
ba45d8c1
KZ
742 }
743 }
744
63c5e7f8
KZ
745 scols_print_table(tb);
746 scols_unref_table(tb);
ba45d8c1 747}
5dd7507c 748
e3f21318 749static struct libscols_line *
91eef60c 750 __attribute__ ((__format__(printf, 4, 5)))
c82b12a0 751 add_summary_sprint(struct libscols_table *tb,
e3f21318 752 struct libscols_line *sec,
c82b12a0
KZ
753 const char *txt,
754 const char *fmt,
755 ...)
756{
91eef60c 757 struct libscols_line *ln;
c82b12a0
KZ
758 va_list args;
759
91eef60c
KZ
760 /* Don't print section lines without data on non-terminal output */
761 if (!is_term && fmt == NULL)
762 return NULL;
763
764 ln = scols_table_new_line(tb, sec);
c82b12a0 765 if (!ln)
780ce22c 766 err(EXIT_FAILURE, _("failed to allocate output line"));
c82b12a0
KZ
767
768 /* description column */
a0661407
KZ
769 if (txt && scols_line_set_data(ln, 0, txt))
770 err(EXIT_FAILURE, _("failed to add output data"));
c82b12a0
KZ
771
772 /* data column */
91eef60c
KZ
773 if (fmt) {
774 char *data;
775 va_start(args, fmt);
776 xvasprintf(&data, fmt, args);
777 va_end(args);
778
779 if (data && scols_line_refer_data(ln, 1, data))
780 err(EXIT_FAILURE, _("failed to add output data"));
781 }
e3f21318
KZ
782
783 return ln;
c82b12a0
KZ
784}
785
91eef60c 786#define add_summary_e(tb, sec, txt) add_summary_sprint(tb, sec, txt, NULL)
e3f21318
KZ
787#define add_summary_n(tb, sec, txt, num) add_summary_sprint(tb, sec, txt, "%zu", num)
788#define add_summary_s(tb, sec, txt, str) add_summary_sprint(tb, sec, txt, "%s", str)
789#define add_summary_x(tb, sec, txt, fmt, x) add_summary_sprint(tb, sec, txt, fmt, x)
5dd7507c
CQ
790
791static void
d8813bb3
KZ
792print_cpuset(struct lscpu_cxt *cxt,
793 struct libscols_table *tb,
91eef60c 794 struct libscols_line *sec,
d8813bb3 795 const char *key, cpu_set_t *set)
4f912c6a 796{
d8813bb3 797 size_t setbuflen = 7 * cxt->maxcpus;
4f912c6a
KZ
798 char setbuf[setbuflen], *p;
799
93a1bb10
KZ
800 assert(set);
801 assert(key);
802 assert(tb);
803 assert(cxt);
804
d8813bb3
KZ
805 if (cxt->hex) {
806 p = cpumask_create(setbuf, setbuflen, set, cxt->setsize);
91eef60c 807 add_summary_s(tb, sec, key, p);
4f912c6a 808 } else {
d8813bb3 809 p = cpulist_create(setbuf, setbuflen, set, cxt->setsize);
91eef60c 810 add_summary_s(tb, sec, key, p);
4f912c6a 811 }
4f912c6a
KZ
812}
813
2f5e2730
KZ
814static void
815print_summary_cputype(struct lscpu_cxt *cxt,
816 struct lscpu_cputype *ct,
91eef60c
KZ
817 struct libscols_table *tb,
818 struct libscols_line *sec)
5dd7507c 819{
91eef60c
KZ
820 if (ct->modelname) {
821 struct libscols_line *tmp = add_summary_s(tb, sec, _("Model name:"), ct->modelname);
822 if (!sec)
823 sec= tmp;
824 }
825
2f5e2730 826 if (ct->machinetype)
91eef60c 827 add_summary_s(tb, sec, _("Machine type:"), ct->machinetype);
2f5e2730 828 if (ct->family)
91eef60c 829 add_summary_s(tb, sec, _("CPU family:"), ct->family);
2f5e2730 830 if (ct->model || ct->revision)
91eef60c 831 add_summary_s(tb, sec, _("Model:"), ct->revision ? ct->revision : ct->model);
2f5e2730 832
91eef60c
KZ
833 add_summary_n(tb, sec, _("Thread(s) per core:"), ct->nthreads_per_core);
834 add_summary_n(tb, sec, _("Core(s) per socket:"), ct->ncores_per_socket);
19ddc05e 835
2f5e2730 836 if (ct->nbooks) {
91eef60c 837 add_summary_n(tb, sec, _("Socket(s) per book:"), ct->nsockets_per_book);
19ddc05e 838 if (ct->ndrawers_per_system || ct->ndrawers) {
91eef60c 839 add_summary_n(tb, sec, _("Book(s) per drawer:"), ct->nbooks_per_drawer);
19ddc05e 840 add_summary_n(tb, sec, _("Drawer(s):"), ct->ndrawers_per_system ?: ct->ndrawers);
2f5e2730 841 } else
19ddc05e 842 add_summary_n(tb, sec, _("Book(s):"), ct->nbooks_per_drawer ?: ct->nbooks);
2f5e2730 843 } else
19ddc05e 844 add_summary_n(tb, sec, _("Socket(s):"), ct->nsockets_per_book ?: 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;
30b912d3 1289 case LSCPU_OUTPUT_CACHES:
0e86bc84
KZ
1290 if (!ncolumns) {
1291 columns[ncolumns++] = COL_CACHE_NAME;
1292 columns[ncolumns++] = COL_CACHE_ONESIZE;
1293 columns[ncolumns++] = COL_CACHE_ALLSIZE;
1294 columns[ncolumns++] = COL_CACHE_WAYS;
1295 columns[ncolumns++] = COL_CACHE_TYPE;
1296 columns[ncolumns++] = COL_CACHE_LEVEL;
cf3b6b71
KZ
1297 columns[ncolumns++] = COL_CACHE_SETS;
1298 columns[ncolumns++] = COL_CACHE_PHYLINE;
1299 columns[ncolumns++] = COL_CACHE_COHERENCYSIZE;
0e86bc84 1300 }
1766641a 1301 print_caches_readable(cxt, columns, ncolumns);
0e86bc84 1302 break;
30b912d3 1303 case LSCPU_OUTPUT_READABLE:
ba45d8c1
KZ
1304 if (!ncolumns) {
1305 /* No list was given. Just print whatever is there. */
63c5e7f8
KZ
1306 struct lscpu_cputype *ct = lscpu_cputype_get_default(cxt);
1307
cc07239d 1308 columns[ncolumns++] = COL_CPU_CPU;
63c5e7f8 1309 if (cxt->nnodes)
cc07239d 1310 columns[ncolumns++] = COL_CPU_NODE;
63c5e7f8 1311 if (ct && ct->ndrawers)
cc07239d 1312 columns[ncolumns++] = COL_CPU_DRAWER;
63c5e7f8 1313 if (ct && ct->nbooks)
cc07239d 1314 columns[ncolumns++] = COL_CPU_BOOK;
63c5e7f8 1315 if (ct && ct->nsockets)
cc07239d 1316 columns[ncolumns++] = COL_CPU_SOCKET;
63c5e7f8 1317 if (ct && ct->ncores)
cc07239d 1318 columns[ncolumns++] = COL_CPU_CORE;
63c5e7f8 1319 if (cxt->ncaches)
cc07239d 1320 columns[ncolumns++] = COL_CPU_CACHE;
63c5e7f8 1321 if (cxt->online)
cc07239d 1322 columns[ncolumns++] = COL_CPU_ONLINE;
63c5e7f8 1323 if (ct && ct->has_configured)
cc07239d 1324 columns[ncolumns++] = COL_CPU_CONFIGURED;
63c5e7f8 1325 if (ct && ct->has_polarization)
cc07239d 1326 columns[ncolumns++] = COL_CPU_POLARIZATION;
63c5e7f8 1327 if (ct && ct->has_addresses)
cc07239d 1328 columns[ncolumns++] = COL_CPU_ADDRESS;
63c5e7f8 1329 if (ct && ct->has_freq) {
cc07239d 1330 columns[ncolumns++] = COL_CPU_MAXMHZ;
cc07239d 1331 columns[ncolumns++] = COL_CPU_MINMHZ;
63c5e7f8 1332 }
ba45d8c1 1333 }
63c5e7f8
KZ
1334 print_cpus_readable(cxt, columns, ncolumns);
1335 break;
63c5e7f8
KZ
1336 case LSCPU_OUTPUT_PARSABLE:
1337 if (!ncolumns) {
1338 columns[ncolumns++] = COL_CPU_CPU;
1339 columns[ncolumns++] = COL_CPU_CORE;
1340 columns[ncolumns++] = COL_CPU_SOCKET;
1341 columns[ncolumns++] = COL_CPU_NODE;
1342 columns[ncolumns++] = COL_CPU_CACHE;
0710bb13 1343 cxt->show_compatible = 1;
63c5e7f8 1344 }
0710bb13 1345 print_cpus_parsable(cxt, columns, ncolumns);
ba45d8c1 1346 break;
8005924a 1347 }
5dd7507c 1348
27c349f9
KZ
1349 lscpu_free_context(cxt);
1350
cf474aac 1351 return EXIT_SUCCESS;
5dd7507c 1352}