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