]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
lscpu: add --hierarchic
[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") },
b956bc86 170 [COL_CACHE_PHYLINE] = { "PHY-LINE", N_("number of physical cache line per cache tag"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
17353ee0
TW
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
c9121791 175static int hierarchic = -1;
91eef60c 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
c9121791
KZ
802 /* Don't print section lines without data */
803 if (!hierarchic && fmt == NULL)
91eef60c
KZ
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{
38e93216 862 sec = add_summary_s(tb, sec, _("Model name:"), ct->modelname ? ct->modelname : "-");
8014104b
MM
863 if (ct->bios_modelname)
864 add_summary_s(tb, sec, _("BIOS Model name:"), ct->bios_modelname);
4cae2104
HS
865 if (ct->bios_family)
866 add_summary_s(tb, sec, _("BIOS CPU family:"), ct->bios_family);
2f5e2730 867 if (ct->machinetype)
91eef60c 868 add_summary_s(tb, sec, _("Machine type:"), ct->machinetype);
2f5e2730 869 if (ct->family)
91eef60c 870 add_summary_s(tb, sec, _("CPU family:"), ct->family);
2f5e2730 871 if (ct->model || ct->revision)
91eef60c 872 add_summary_s(tb, sec, _("Model:"), ct->revision ? ct->revision : ct->model);
2f5e2730 873
91eef60c 874 add_summary_n(tb, sec, _("Thread(s) per core:"), ct->nthreads_per_core);
73c0a766
MM
875 if (cxt->is_cluster)
876 add_summary_n(tb, sec, _("Core(s) per cluster:"), ct->ncores_per_socket);
877 else
878 add_summary_n(tb, sec, _("Core(s) per socket:"), ct->ncores_per_socket);
19ddc05e 879
2f5e2730 880 if (ct->nbooks) {
91eef60c 881 add_summary_n(tb, sec, _("Socket(s) per book:"), ct->nsockets_per_book);
19ddc05e 882 if (ct->ndrawers_per_system || ct->ndrawers) {
91eef60c 883 add_summary_n(tb, sec, _("Book(s) per drawer:"), ct->nbooks_per_drawer);
19ddc05e 884 add_summary_n(tb, sec, _("Drawer(s):"), ct->ndrawers_per_system ?: ct->ndrawers);
2f5e2730 885 } else
19ddc05e 886 add_summary_n(tb, sec, _("Book(s):"), ct->nbooks_per_drawer ?: ct->nbooks);
73c0a766 887 } else {
f42f105b
MM
888 if (cxt->is_cluster) {
889 if (ct->nr_socket_on_cluster > 0)
890 add_summary_n(tb, sec, _("Socket(s):"), ct->nr_socket_on_cluster);
891 else
892 add_summary_s(tb, sec, _("Socket(s):"), "-");
893
73c0a766
MM
894 add_summary_n(tb, sec, _("Cluster(s):"),
895 ct->nsockets_per_book ?: ct->nsockets);
f42f105b 896 } else
73c0a766
MM
897 add_summary_n(tb, sec, _("Socket(s):"),
898 ct->nsockets_per_book ?: ct->nsockets);
899 }
2f5e2730 900
93a1bb10 901 if (ct->stepping)
91eef60c 902 add_summary_s(tb, sec, _("Stepping:"), ct->stepping);
93a1bb10 903 if (ct->freqboost >= 0)
91eef60c 904 add_summary_s(tb, sec, _("Frequency boost:"), ct->freqboost ?
16ca0551 905 _("enabled") : _("disabled"));
01bea871
KZ
906
907 /* s390 -- from the first CPU where is dynamic/static MHz */
93a1bb10 908 if (ct->dynamic_mhz)
91eef60c 909 add_summary_s(tb, sec, _("CPU dynamic MHz:"), ct->dynamic_mhz);
93a1bb10 910 if (ct->static_mhz)
91eef60c 911 add_summary_s(tb, sec, _("CPU static MHz:"), ct->static_mhz);
01bea871 912
93a1bb10 913 if (ct->has_freq) {
9b9e4f5d
KZ
914 float scal = lsblk_cputype_get_scalmhz(cxt, ct);
915 if (scal > 0.0)
916 add_summary_x(tb, sec, _("CPU(s) scaling MHz:"), "%.0f%%", scal);
91eef60c
KZ
917 add_summary_x(tb, sec, _("CPU max MHz:"), "%.4f", lsblk_cputype_get_maxmhz(cxt, ct));
918 add_summary_x(tb, sec, _("CPU min MHz:"), "%.4f", lsblk_cputype_get_minmhz(cxt, ct));
bd9b94d1 919 }
93a1bb10 920 if (ct->bogomips)
91eef60c
KZ
921 add_summary_s(tb, sec, _("BogoMIPS:"), ct->bogomips);
922
93a1bb10 923 if (ct->dispatching >= 0)
91eef60c 924 add_summary_s(tb, sec, _("Dispatching mode:"), _(disp_modes[ct->dispatching]));
93a1bb10
KZ
925
926 if (ct->physsockets) {
91eef60c
KZ
927 add_summary_n(tb, sec, _("Physical sockets:"), ct->physsockets);
928 add_summary_n(tb, sec, _("Physical chips:"), ct->physchips);
929 add_summary_n(tb, sec, _("Physical cores/chip:"), ct->physcoresperchip);
639eeb28
KZ
930 }
931
93a1bb10 932 if (ct->flags)
91eef60c 933 add_summary_s(tb, sec, _("Flags:"), ct->flags);
2f5e2730
KZ
934}
935
936/*
937 * default output
938 */
939static void print_summary(struct lscpu_cxt *cxt)
940{
941 struct lscpu_cputype *ct;
91eef60c 942 char field[256];
2f5e2730
KZ
943 size_t i = 0;
944 struct libscols_table *tb;
e3f21318 945 struct libscols_line *sec = NULL;
071d3135 946 int hdr_caches = 0;
2f5e2730
KZ
947
948 scols_init_debug(0);
949
950 tb = scols_new_table();
951 if (!tb)
952 err(EXIT_FAILURE, _("failed to allocate output table"));
953
954 scols_table_enable_noheadings(tb, 1);
955 if (cxt->json) {
956 scols_table_enable_json(tb, 1);
957 scols_table_set_name(tb, "lscpu");
c9121791 958 } else if (hierarchic) {
91eef60c
KZ
959 struct libscols_symbols *sy = scols_new_symbols();
960
961 if (!sy)
962 err_oom();
963 scols_symbols_set_branch(sy, " ");
964 scols_symbols_set_vertical(sy, " ");
965 scols_symbols_set_right(sy, " ");
966 scols_table_set_symbols(tb, sy);
d4cb6a03 967 scols_unref_symbols(sy);
2f5e2730
KZ
968 }
969
c9121791 970 if (scols_table_new_column(tb, "field", 0, hierarchic ? SCOLS_FL_TREE : 0) == NULL ||
2f5e2730
KZ
971 scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES | SCOLS_FL_WRAP) == NULL)
972 err(EXIT_FAILURE, _("failed to initialize output column"));
973
974 ct = lscpu_cputype_get_default(cxt);
975
91eef60c 976 /* Section: architecture */
2f5e2730 977 if (cxt->arch)
e3f21318 978 sec = add_summary_s(tb, NULL, _("Architecture:"), cxt->arch->name);
2f5e2730
KZ
979 if (cxt->arch && (cxt->arch->bit32 || cxt->arch->bit64)) {
980 char buf[32], *p = buf;
981
982 if (cxt->arch->bit32) {
983 strcpy(p, "32-bit, ");
984 p += 8;
985 }
986 if (cxt->arch->bit64) {
987 strcpy(p, "64-bit, ");
988 p += 8;
989 }
990 *(p - 2) = '\0';
e3f21318 991 add_summary_s(tb, sec, _("CPU op-mode(s):"), buf);
2f5e2730 992 }
0d7cef3d 993 if (ct && ct->addrsz)
e3f21318 994 add_summary_s(tb, sec, _("Address sizes:"), ct->addrsz);
2f5e2730 995#if !defined(WORDS_BIGENDIAN)
e3f21318 996 add_summary_s(tb, sec, _("Byte Order:"), "Little Endian");
2f5e2730 997#else
e3f21318 998 add_summary_s(tb, sec, _("Byte Order:"), "Big Endian");
2f5e2730 999#endif
91eef60c
KZ
1000
1001 /* Section: CPU lists */
1002 sec = add_summary_n(tb, NULL, _("CPU(s):"), cxt->npresents);
1003
2f5e2730 1004 if (cxt->online)
91eef60c 1005 print_cpuset(cxt, tb, sec,
2f5e2730
KZ
1006 cxt->hex ? _("On-line CPU(s) mask:") :
1007 _("On-line CPU(s) list:"),
1008 cxt->online);
1009
1010 if (cxt->online && cxt->nonlines != cxt->npresents) {
1011 cpu_set_t *set;
1012
1013 /* Linux kernel provides cpuset of off-line CPUs that contains
1014 * all configured CPUs (see /sys/devices/system/cpu/offline),
1015 * but want to print real (present in system) off-line CPUs only.
1016 */
1017 set = cpuset_alloc(cxt->maxcpus, NULL, NULL);
1018 if (!set)
1019 err(EXIT_FAILURE, _("failed to callocate cpu set"));
1020 CPU_ZERO_S(cxt->setsize, set);
1021 for (i = 0; i < cxt->npossibles; i++) {
1022 struct lscpu_cpu *cpu = cxt->cpus[i];
1023
1024 if (cpu && is_cpu_present(cxt, cpu) && !is_cpu_online(cxt, cpu))
1025 CPU_SET_S(cpu->logical_id, cxt->setsize, set);
1026 }
91eef60c 1027 print_cpuset(cxt, tb, sec,
2f5e2730
KZ
1028 cxt->hex ? _("Off-line CPU(s) mask:") :
1029 _("Off-line CPU(s) list:"), set);
1030 cpuset_free(set);
1031 }
8014104b 1032 sec = NULL;
2f5e2730 1033
91eef60c 1034 /* Section: cpu type description */
0d7cef3d 1035 if (ct && ct->vendor)
91eef60c 1036 sec = add_summary_s(tb, NULL, _("Vendor ID:"), ct->vendor);
0d7cef3d 1037 if (ct && ct->bios_vendor)
8014104b 1038 add_summary_s(tb, sec, _("BIOS Vendor ID:"), ct->bios_vendor);
91eef60c 1039
8014104b
MM
1040 for (i = 0; i < cxt->ncputypes; i++)
1041 print_summary_cputype(cxt, cxt->cputypes[i], tb, sec);
91eef60c 1042 sec = NULL;
2f5e2730 1043
91eef60c 1044 /* Section: vitualiazation */
93a1bb10 1045 if (cxt->virt) {
91eef60c 1046 sec = add_summary_e(tb, NULL, _("Virtualization features:"));
01bea871 1047 if (cxt->virt->cpuflag && !strcmp(cxt->virt->cpuflag, "svm"))
e3f21318 1048 add_summary_s(tb, sec, _("Virtualization:"), "AMD-V");
01bea871 1049 else if (cxt->virt->cpuflag && !strcmp(cxt->virt->cpuflag, "vmx"))
e3f21318 1050 add_summary_s(tb, sec, _("Virtualization:"), "VT-x");
93a1bb10
KZ
1051
1052 if (cxt->virt->hypervisor)
e3f21318 1053 add_summary_s(tb, sec, _("Hypervisor:"), cxt->virt->hypervisor);
93a1bb10 1054 if (cxt->virt->vendor) {
e3f21318
KZ
1055 add_summary_s(tb, sec, _("Hypervisor vendor:"), hv_vendors[cxt->virt->vendor]);
1056 add_summary_s(tb, sec, _("Virtualization type:"), _(virt_types[cxt->virt->type]));
93a1bb10 1057 }
e3f21318 1058 sec = NULL;
93a1bb10 1059 }
91eef60c
KZ
1060
1061 /* Section: caches */
01bea871
KZ
1062 if (cxt->ncaches) {
1063 const char *last = NULL;
93a1bb10 1064
01bea871
KZ
1065 /* The caches are sorted by name, cxt->caches[] may contains
1066 * multiple instances for the same name.
1067 */
1068 for (i = 0; i < cxt->ncaches; i++) {
1069 const char *name = cxt->caches[i].name;
1070 uint64_t sz;
9dadd3e6 1071 int n = 0;
93a1bb10 1072
01bea871 1073 if (last && strcmp(last, name) == 0)
93a1bb10 1074 continue;
9dadd3e6 1075 sz = lscpu_get_cache_full_size(cxt, name, &n);
01bea871 1076 if (!sz)
93a1bb10 1077 continue;
071d3135
KZ
1078 if (!hdr_caches) {
1079 sec = add_summary_e(tb, NULL, _("Caches (sum of all):"));
1080 hdr_caches = 1;
1081 }
1082
c9121791 1083 snprintf(field, sizeof(field), hierarchic ? _("%s:") : _("%s cache:"), name);
01bea871 1084 if (cxt->bytes)
9dadd3e6
KZ
1085 add_summary_sprint(tb, sec, field,
1086 P_("%" PRIu64 " (%d instance)",
1087 "%" PRIu64 " (%d instances)", n),
1088 sz, n);
01bea871
KZ
1089 else {
1090 char *tmp = size_to_human_string(
1091 SIZE_SUFFIX_3LETTER |
1092 SIZE_SUFFIX_SPACE,
1093 sz);
9dadd3e6
KZ
1094 add_summary_sprint(tb, sec, field,
1095 P_("%s (%d instance)",
1096 "%s (%d instances)", n),
1097 tmp, n);
01bea871
KZ
1098 free(tmp);
1099 }
1100 last = name;
93a1bb10
KZ
1101 }
1102 }
01bea871 1103
071d3135
KZ
1104 for (i = 0; i < cxt->necaches; i++) {
1105 struct lscpu_cache *ca = &cxt->ecaches[i];
93a1bb10 1106
071d3135
KZ
1107 if (ca->size == 0)
1108 continue;
1109 if (!hdr_caches) {
1110 sec = add_summary_e(tb, NULL, _("Caches:"));
1111 hdr_caches = 1;
1112 }
c9121791 1113 snprintf(field, sizeof(field), hierarchic ? _("%s:") : _("%s cache:"), ca->name);
071d3135
KZ
1114 if (cxt->bytes)
1115 add_summary_x(tb, sec, field, "%" PRIu64, ca->size);
1116 else {
1117 char *tmp = size_to_human_string(
1118 SIZE_SUFFIX_3LETTER |
1119 SIZE_SUFFIX_SPACE,
1120 ca->size);
1121 add_summary_s(tb, sec, field, tmp);
1122 free(tmp);
01bea871
KZ
1123 }
1124 }
7155a57d 1125 sec = NULL;
01bea871 1126
91eef60c 1127 /* Section: NUMA modes */
01bea871 1128 if (cxt->nnodes) {
91eef60c 1129 sec = add_summary_e(tb, NULL, _("NUMA:"));
e3f21318
KZ
1130
1131 add_summary_n(tb, sec,_("NUMA node(s):"), cxt->nnodes);
01bea871 1132 for (i = 0; i < cxt->nnodes; i++) {
91eef60c
KZ
1133 snprintf(field, sizeof(field), _("NUMA node%d CPU(s):"), cxt->idx2nodenum[i]);
1134 print_cpuset(cxt, tb, sec, field, cxt->nodemaps[i]);
93a1bb10 1135 }
e3f21318 1136 sec = NULL;
93a1bb10
KZ
1137 }
1138
91eef60c 1139 /* Section: Vulnerabilities */
01bea871 1140 if (cxt->vuls) {
91eef60c 1141 sec = add_summary_e(tb, NULL, _("Vulnerabilities:"));
e3f21318 1142
01bea871 1143 for (i = 0; i < cxt->nvuls; i++) {
c9121791 1144 snprintf(field, sizeof(field), hierarchic ?
91eef60c
KZ
1145 _("%s:") : _("Vulnerability %s:"), cxt->vuls[i].name);
1146 add_summary_s(tb, sec, field, cxt->vuls[i].text);
93a1bb10 1147 }
e3f21318 1148 sec = NULL;
93a1bb10 1149 }
c82b12a0
KZ
1150 scols_print_table(tb);
1151 scols_unref_table(tb);
5dd7507c
CQ
1152}
1153
6e1eda6f 1154static void __attribute__((__noreturn__)) usage(void)
5dd7507c 1155{
6e1eda6f 1156 FILE *out = stdout;
b9d18bc3
KZ
1157 size_t i;
1158
1159 fputs(USAGE_HEADER, out);
c6f095cf 1160 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
7f1ec5e8 1161
451dbcfa
BS
1162 fputs(USAGE_SEPARATOR, out);
1163 fputs(_("Display information about the CPU architecture.\n"), out);
1164
b9d18bc3 1165 fputs(USAGE_OPTIONS, out);
c6f095cf
BS
1166 fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out);
1167 fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out);
2011528b 1168 fputs(_(" -B, --bytes print sizes in bytes rather than in human readable format\n"), out);
0e86bc84 1169 fputs(_(" -C, --caches[=<list>] info about caches in extended readable format\n"), out);
c6f095cf 1170 fputs(_(" -c, --offline print offline CPUs only\n"), out);
19a5510b 1171 fputs(_(" -J, --json use JSON for default or extended format\n"), out);
c6f095cf
BS
1172 fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out);
1173 fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out);
1174 fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out);
1175 fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out);
0d2b5d2a 1176 fputs(_(" -y, --physical print physical instead of logical IDs\n"), out);
c9121791 1177 fputs(_(" --hierarchic[=when] use subsections in summary (auto, never, always)\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,
c9121791 1205 OPT_HIERARCHIC,
fbf0619b 1206 };
6c7d5ae9 1207 static const struct option longopts[] = {
87918040
SK
1208 { "all", no_argument, NULL, 'a' },
1209 { "online", no_argument, NULL, 'b' },
2011528b 1210 { "bytes", no_argument, NULL, 'B' },
0e86bc84 1211 { "caches", optional_argument, NULL, 'C' },
87918040
SK
1212 { "offline", no_argument, NULL, 'c' },
1213 { "help", no_argument, NULL, 'h' },
1214 { "extended", optional_argument, NULL, 'e' },
19a5510b 1215 { "json", no_argument, NULL, 'J' },
87918040
SK
1216 { "parse", optional_argument, NULL, 'p' },
1217 { "sysroot", required_argument, NULL, 's' },
1218 { "physical", no_argument, NULL, 'y' },
1219 { "hex", no_argument, NULL, 'x' },
1220 { "version", no_argument, NULL, 'V' },
fbf0619b 1221 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
c9121791 1222 { "hierarchic", optional_argument, NULL, OPT_HIERARCHIC },
87918040 1223 { NULL, 0, NULL, 0 }
5dd7507c
CQ
1224 };
1225
8e97eb4b 1226 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
0e86bc84 1227 { 'C','e','p' },
a44cd891 1228 { 'a','b','c' },
8e97eb4b
KZ
1229 { 0 }
1230 };
1231 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1232
2f8f1388 1233 setlocale(LC_ALL, "");
5dd7507c
CQ
1234 bindtextdomain(PACKAGE, LOCALEDIR);
1235 textdomain(PACKAGE);
25b7045e 1236 close_stdout_atexit();
5dd7507c 1237
b73d38b1
KZ
1238 cxt = lscpu_new_context();
1239
2011528b 1240 while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::s:xyV", longopts, NULL)) != -1) {
8e97eb4b
KZ
1241
1242 err_exclusive_options(c, longopts, excl, excl_st);
1243
5dd7507c 1244 switch (c) {
0ad29ff6 1245 case 'a':
b73d38b1 1246 cxt->show_online = cxt->show_offline = 1;
7fc12cd2 1247 cpu_modifier_specified = 1;
0ad29ff6 1248 break;
2011528b 1249 case 'B':
b73d38b1 1250 cxt->bytes = 1;
2011528b 1251 break;
23e9e95a 1252 case 'b':
b73d38b1 1253 cxt->show_online = 1;
7fc12cd2 1254 cpu_modifier_specified = 1;
23e9e95a 1255 break;
7afc2387 1256 case 'c':
b73d38b1 1257 cxt->show_offline = 1;
7fc12cd2 1258 cpu_modifier_specified = 1;
7afc2387 1259 break;
0e86bc84
KZ
1260 case 'C':
1261 if (optarg) {
1262 if (*optarg == '=')
1263 optarg++;
1242c3fd 1264 outarg = optarg;
0e86bc84 1265 }
b73d38b1 1266 cxt->mode = LSCPU_OUTPUT_CACHES;
0e86bc84 1267 break;
19a5510b 1268 case 'J':
b73d38b1 1269 cxt->json = 1;
19a5510b 1270 break;
5dd7507c 1271 case 'p':
ba45d8c1 1272 case 'e':
477251f8
KZ
1273 if (optarg) {
1274 if (*optarg == '=')
1275 optarg++;
1242c3fd 1276 outarg = optarg;
477251f8 1277 }
b73d38b1 1278 cxt->mode = c == 'p' ? LSCPU_OUTPUT_PARSABLE : LSCPU_OUTPUT_READABLE;
5dd7507c 1279 break;
47b6e8b6 1280 case 's':
b73d38b1
KZ
1281 cxt->prefix = optarg;
1282 cxt->noalive = 1;
47b6e8b6 1283 break;
4f912c6a 1284 case 'x':
b73d38b1 1285 cxt->hex = 1;
4f912c6a 1286 break;
0d2b5d2a 1287 case 'y':
b73d38b1 1288 cxt->show_physical = 1;
0d2b5d2a 1289 break;
fbf0619b 1290 case OPT_OUTPUT_ALL:
0e86bc84 1291 all = 1;
fbf0619b 1292 break;
c9121791
KZ
1293 case OPT_HIERARCHIC:
1294 if (optarg) {
1295 if (strcmp(optarg, "auto") == 0)
1296 hierarchic = -1;
1297 else if (strcmp(optarg, "never") == 0)
1298 hierarchic = 0;
1299 else if (strcmp(optarg, "always") == 0)
1300 hierarchic = 1;
1301 else
1302 errx(EXIT_FAILURE, _("unsupported --flat argument"));
1303 } else
1304 hierarchic = 1;
1305 break;
2c308875
KZ
1306 case 'h':
1307 usage();
1308 case 'V':
1309 print_version(EXIT_SUCCESS);
5dd7507c 1310 default:
677ec86c 1311 errtryhelp(EXIT_FAILURE);
5dd7507c
CQ
1312 }
1313 }
7bbb7829 1314
0a31a242 1315 if (all && ncolumns == 0) {
b73d38b1 1316 size_t maxsz = cxt->mode == LSCPU_OUTPUT_CACHES ?
0e86bc84
KZ
1317 ARRAY_SIZE(coldescs_cache) :
1318 ARRAY_SIZE(coldescs_cpu);
1319
b73d38b1
KZ
1320 for (i = 0; i < maxsz; i++)
1321 columns[ncolumns++] = i;
0e86bc84
KZ
1322 }
1323
b73d38b1 1324 if (cpu_modifier_specified && cxt->mode == LSCPU_OUTPUT_SUMMARY) {
7fc12cd2
HC
1325 fprintf(stderr,
1326 _("%s: options --all, --online and --offline may only "
ac56e555 1327 "be used with options --extended or --parse.\n"),
7fc12cd2
HC
1328 program_invocation_short_name);
1329 return EXIT_FAILURE;
1330 }
1331
6e1eda6f
RM
1332 if (argc != optind) {
1333 warnx(_("bad usage"));
1334 errtryhelp(EXIT_FAILURE);
1335 }
7bbb7829 1336
7afc2387 1337 /* set default cpu display mode if none was specified */
b73d38b1
KZ
1338 if (!cxt->show_online && !cxt->show_offline) {
1339 cxt->show_online = 1;
1340 cxt->show_offline = cxt->mode == LSCPU_OUTPUT_READABLE ? 1 : 0;
7afc2387 1341 }
6e509042 1342
91eef60c 1343
4b9cbc38 1344 lscpu_init_debug();
6e509042 1345
4b9cbc38 1346 lscpu_context_init_paths(cxt);
5dd7507c 1347
4b9cbc38
KZ
1348 lscpu_read_cpulists(cxt);
1349 lscpu_read_cpuinfo(cxt);
1350 cxt->arch = lscpu_read_architecture(cxt);
538b50cb 1351
4b9cbc38
KZ
1352 lscpu_read_archext(cxt);
1353 lscpu_read_vulnerabilities(cxt);
1354 lscpu_read_numas(cxt);
1355 lscpu_read_topology(cxt);
7e03f383 1356
4b9cbc38 1357 lscpu_decode_arm(cxt);
28b1658f 1358
4b9cbc38 1359 cxt->virt = lscpu_read_virtualization(cxt);
c8b64f6d 1360
c9121791
KZ
1361 if (hierarchic == -1)
1362 hierarchic = isatty(STDOUT_FILENO); /* default */
1363
d8813bb3 1364 switch(cxt->mode) {
30b912d3 1365 case LSCPU_OUTPUT_SUMMARY:
d8813bb3 1366 print_summary(cxt);
ba45d8c1 1367 break;
30b912d3 1368 case LSCPU_OUTPUT_CACHES:
0e86bc84
KZ
1369 if (!ncolumns) {
1370 columns[ncolumns++] = COL_CACHE_NAME;
1371 columns[ncolumns++] = COL_CACHE_ONESIZE;
1372 columns[ncolumns++] = COL_CACHE_ALLSIZE;
1373 columns[ncolumns++] = COL_CACHE_WAYS;
1374 columns[ncolumns++] = COL_CACHE_TYPE;
1375 columns[ncolumns++] = COL_CACHE_LEVEL;
cf3b6b71
KZ
1376 columns[ncolumns++] = COL_CACHE_SETS;
1377 columns[ncolumns++] = COL_CACHE_PHYLINE;
1378 columns[ncolumns++] = COL_CACHE_COHERENCYSIZE;
0e86bc84 1379 }
1242c3fd
KZ
1380 if (outarg && string_add_to_idarray(outarg, columns,
1381 ARRAY_SIZE(columns),
1382 &ncolumns, cache_column_name_to_id) < 0)
1383 return EXIT_FAILURE;
1384
1766641a 1385 print_caches_readable(cxt, columns, ncolumns);
0e86bc84 1386 break;
30b912d3 1387 case LSCPU_OUTPUT_READABLE:
ba45d8c1
KZ
1388 if (!ncolumns) {
1389 /* No list was given. Just print whatever is there. */
63c5e7f8
KZ
1390 struct lscpu_cputype *ct = lscpu_cputype_get_default(cxt);
1391
cc07239d 1392 columns[ncolumns++] = COL_CPU_CPU;
63c5e7f8 1393 if (cxt->nnodes)
cc07239d 1394 columns[ncolumns++] = COL_CPU_NODE;
63c5e7f8 1395 if (ct && ct->ndrawers)
cc07239d 1396 columns[ncolumns++] = COL_CPU_DRAWER;
63c5e7f8 1397 if (ct && ct->nbooks)
cc07239d 1398 columns[ncolumns++] = COL_CPU_BOOK;
73c0a766
MM
1399 if (ct && ct->nsockets) {
1400 if (cxt->is_cluster)
1401 columns[ncolumns++] = COL_CPU_CLUSTER;
1402 else
1403 columns[ncolumns++] = COL_CPU_SOCKET;
1404 }
63c5e7f8 1405 if (ct && ct->ncores)
cc07239d 1406 columns[ncolumns++] = COL_CPU_CORE;
63c5e7f8 1407 if (cxt->ncaches)
cc07239d 1408 columns[ncolumns++] = COL_CPU_CACHE;
63c5e7f8 1409 if (cxt->online)
cc07239d 1410 columns[ncolumns++] = COL_CPU_ONLINE;
63c5e7f8 1411 if (ct && ct->has_configured)
cc07239d 1412 columns[ncolumns++] = COL_CPU_CONFIGURED;
63c5e7f8 1413 if (ct && ct->has_polarization)
cc07239d 1414 columns[ncolumns++] = COL_CPU_POLARIZATION;
63c5e7f8 1415 if (ct && ct->has_addresses)
cc07239d 1416 columns[ncolumns++] = COL_CPU_ADDRESS;
63c5e7f8 1417 if (ct && ct->has_freq) {
cc07239d 1418 columns[ncolumns++] = COL_CPU_MAXMHZ;
cc07239d 1419 columns[ncolumns++] = COL_CPU_MINMHZ;
aa049eab 1420 columns[ncolumns++] = COL_CPU_MHZ;
63c5e7f8 1421 }
ba45d8c1 1422 }
1242c3fd
KZ
1423 if (outarg && string_add_to_idarray(outarg, columns,
1424 ARRAY_SIZE(columns),
1425 &ncolumns, cpu_column_name_to_id) < 0)
1426 return EXIT_FAILURE;
63c5e7f8
KZ
1427 print_cpus_readable(cxt, columns, ncolumns);
1428 break;
63c5e7f8
KZ
1429 case LSCPU_OUTPUT_PARSABLE:
1430 if (!ncolumns) {
1431 columns[ncolumns++] = COL_CPU_CPU;
1432 columns[ncolumns++] = COL_CPU_CORE;
73c0a766
MM
1433 if (cxt->is_cluster)
1434 columns[ncolumns++] = COL_CPU_CLUSTER;
1435 else
1436 columns[ncolumns++] = COL_CPU_SOCKET;
63c5e7f8
KZ
1437 columns[ncolumns++] = COL_CPU_NODE;
1438 columns[ncolumns++] = COL_CPU_CACHE;
0710bb13 1439 cxt->show_compatible = 1;
63c5e7f8 1440 }
1242c3fd
KZ
1441 if (outarg && string_add_to_idarray(outarg, columns,
1442 ARRAY_SIZE(columns),
1443 &ncolumns, cpu_column_name_to_id) < 0)
1444 return EXIT_FAILURE;
1445
0710bb13 1446 print_cpus_parsable(cxt, columns, ncolumns);
ba45d8c1 1447 break;
8005924a 1448 }
5dd7507c 1449
27c349f9
KZ
1450 lscpu_free_context(cxt);
1451
cf474aac 1452 return EXIT_SUCCESS;
5dd7507c 1453}