]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
lscpu: (arm) don't use space in names
[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"
aff94add 42#include "c_strtod.h"
71061694 43
fb2627ce 44#include "lscpu.h"
0ebbe9f1 45
2ba641e5 46static const char *virt_types[] = {
7454b598
KZ
47 [VIRT_TYPE_NONE] = N_("none"),
48 [VIRT_TYPE_PARA] = N_("para"),
49 [VIRT_TYPE_FULL] = N_("full"),
50 [VIRT_TYPE_CONTAINER] = N_("container"),
c8b64f6d
KZ
51};
52
2ba641e5 53static const char *hv_vendors[] = {
7454b598
KZ
54 [VIRT_VENDOR_NONE] = NULL,
55 [VIRT_VENDOR_XEN] = "Xen",
56 [VIRT_VENDOR_KVM] = "KVM",
57 [VIRT_VENDOR_MSHV] = "Microsoft",
f2aadc11 58 [VIRT_VENDOR_VMWARE] = "VMware",
7454b598
KZ
59 [VIRT_VENDOR_IBM] = "IBM",
60 [VIRT_VENDOR_VSERVER] = "Linux-VServer",
61 [VIRT_VENDOR_UML] = "User-mode Linux",
62 [VIRT_VENDOR_INNOTEK] = "Innotek GmbH",
63 [VIRT_VENDOR_HITACHI] = "Hitachi",
f2aadc11 64 [VIRT_VENDOR_PARALLELS] = "Parallels",
7454b598
KZ
65 [VIRT_VENDOR_VBOX] = "Oracle",
66 [VIRT_VENDOR_OS400] = "OS/400",
67 [VIRT_VENDOR_PHYP] = "pHyp",
68 [VIRT_VENDOR_SPAR] = "Unisys s-Par",
69 [VIRT_VENDOR_WSL] = "Windows Subsystem for Linux"
96ce475f
RM
70};
71
a0fff77e 72/* dispatching modes */
2ba641e5 73static const char *disp_modes[] = {
a0fff77e
HC
74 [DISP_HORIZONTAL] = N_("horizontal"),
75 [DISP_VERTICAL] = N_("vertical")
76};
77
f9ac0210
KZ
78struct polarization_modes {
79 char *parsable;
80 char *readable;
81};
82
2ba641e5 83static struct polarization_modes polar_modes[] = {
0a0af616
KZ
84 [POLAR_UNKNOWN] = {"U", "-"},
85 [POLAR_VLOW] = {"VL", "vert-low"},
86 [POLAR_VMEDIUM] = {"VM", "vert-medium"},
87 [POLAR_VHIGH] = {"VH", "vert-high"},
8005924a 88 [POLAR_HORIZONTAL] = {"H", "horizontal"},
2b8fcb81
HC
89};
90
477251f8 91/*
3d27b76a 92 * IDs
477251f8
KZ
93 */
94enum {
cc94324e 95 COL_CPU_BOGOMIPS,
cc07239d
KZ
96 COL_CPU_CPU,
97 COL_CPU_CORE,
98 COL_CPU_SOCKET,
73c0a766 99 COL_CPU_CLUSTER,
cc07239d
KZ
100 COL_CPU_NODE,
101 COL_CPU_BOOK,
102 COL_CPU_DRAWER,
103 COL_CPU_CACHE,
104 COL_CPU_POLARIZATION,
105 COL_CPU_ADDRESS,
106 COL_CPU_CONFIGURED,
107 COL_CPU_ONLINE,
6d880d3d 108 COL_CPU_MHZ,
9b9e4f5d 109 COL_CPU_SCALMHZ,
cc07239d
KZ
110 COL_CPU_MAXMHZ,
111 COL_CPU_MINMHZ,
477251f8
KZ
112};
113
0e86bc84
KZ
114enum {
115 COL_CACHE_ALLSIZE,
116 COL_CACHE_LEVEL,
117 COL_CACHE_NAME,
118 COL_CACHE_ONESIZE,
119 COL_CACHE_TYPE,
120 COL_CACHE_WAYS,
cf3b6b71
KZ
121 COL_CACHE_ALLOCPOL,
122 COL_CACHE_WRITEPOL,
123 COL_CACHE_PHYLINE,
124 COL_CACHE_SETS,
125 COL_CACHE_COHERENCYSIZE
0e86bc84
KZ
126};
127
128
3d27b76a
KZ
129/* column description
130 */
131struct lscpu_coldesc {
132 const char *name;
133 const char *help;
134
0e86bc84 135 int flags;
3d27b76a 136 unsigned int is_abbr:1; /* name is abbreviation */
17353ee0 137 int json_type;
477251f8
KZ
138};
139
cc07239d 140static struct lscpu_coldesc coldescs_cpu[] =
3d27b76a 141{
17353ee0
TW
142 [COL_CPU_BOGOMIPS] = { "BOGOMIPS", N_("crude measurement of CPU speed"), SCOLS_FL_RIGHT, 1, SCOLS_JSON_NUMBER },
143 [COL_CPU_CPU] = { "CPU", N_("logical CPU number"), SCOLS_FL_RIGHT, 1, SCOLS_JSON_NUMBER },
144 [COL_CPU_CORE] = { "CORE", N_("logical core number"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
145 [COL_CPU_CLUSTER] = { "CLUSTER", N_("logical cluster number"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
146 [COL_CPU_SOCKET] = { "SOCKET", N_("logical socket number"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
147 [COL_CPU_NODE] = { "NODE", N_("logical NUMA node number"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
148 [COL_CPU_BOOK] = { "BOOK", N_("logical book number"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
149 [COL_CPU_DRAWER] = { "DRAWER", N_("logical drawer number"), SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER },
cc07239d
KZ
150 [COL_CPU_CACHE] = { "CACHE", N_("shows how caches are shared between CPUs") },
151 [COL_CPU_POLARIZATION] = { "POLARIZATION", N_("CPU dispatching mode on virtual hardware") },
152 [COL_CPU_ADDRESS] = { "ADDRESS", N_("physical address of a CPU") },
87579a96
TW
153 [COL_CPU_CONFIGURED] = { "CONFIGURED", N_("shows if the hypervisor has allocated the CPU"), 0, 0, SCOLS_JSON_BOOLEAN_OPTIONAL },
154 [COL_CPU_ONLINE] = { "ONLINE", N_("shows if Linux currently makes use of the CPU"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_BOOLEAN_OPTIONAL },
79884c5d 155 [COL_CPU_MHZ] = { "MHZ", N_("shows the currently MHz of the CPU"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
17353ee0
TW
156 [COL_CPU_SCALMHZ] = { "SCALMHZ%", N_("shows scaling percentage of the CPU frequency"), SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER },
157 [COL_CPU_MAXMHZ] = { "MAXMHZ", N_("shows the maximum MHz of the CPU"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
158 [COL_CPU_MINMHZ] = { "MINMHZ", N_("shows the minimum MHz of the CPU"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER }
0e86bc84
KZ
159};
160
161static struct lscpu_coldesc coldescs_cache[] =
162{
163 [COL_CACHE_ALLSIZE] = { "ALL-SIZE", N_("size of all system caches"), SCOLS_FL_RIGHT },
17353ee0 164 [COL_CACHE_LEVEL] = { "LEVEL", N_("cache level"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
0e86bc84
KZ
165 [COL_CACHE_NAME] = { "NAME", N_("cache name") },
166 [COL_CACHE_ONESIZE] = { "ONE-SIZE", N_("size of one cache"), SCOLS_FL_RIGHT },
167 [COL_CACHE_TYPE] = { "TYPE", N_("cache type") },
17353ee0 168 [COL_CACHE_WAYS] = { "WAYS", N_("ways of associativity"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
cf3b6b71
KZ
169 [COL_CACHE_ALLOCPOL] = { "ALLOC-POLICY", N_("allocation policy") },
170 [COL_CACHE_WRITEPOL] = { "WRITE-POLICY", N_("write policy") },
b956bc86 171 [COL_CACHE_PHYLINE] = { "PHY-LINE", N_("number of physical cache line per cache tag"), SCOLS_FL_RIGHT, 0, SCOLS_JSON_NUMBER },
17353ee0
TW
172 [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 },
173 [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 174};
477251f8 175
c9121791 176static int hierarchic = -1;
91eef60c 177
43715b4e
KZ
178UL_DEBUG_DEFINE_MASK(lscpu);
179UL_DEBUG_DEFINE_MASKNAMES(lscpu) = UL_DEBUG_EMPTY_MASKNAMES;
180
181static void lscpu_init_debug(void)
182{
183 __UL_INIT_DEBUG_FROM_ENV(lscpu, LSCPU_DEBUG_, 0, LSCPU_DEBUG);
184}
185
583f14cc 186static int
cc07239d 187cpu_column_name_to_id(const char *name, size_t namesz)
477251f8 188{
329fd1c3 189 size_t i;
477251f8 190
cc07239d
KZ
191 for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) {
192 const char *cn = coldescs_cpu[i].name;
477251f8
KZ
193
194 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
195 return i;
196 }
197 warnx(_("unknown column: %s"), name);
198 return -1;
199}
200
0e86bc84
KZ
201static int
202cache_column_name_to_id(const char *name, size_t namesz)
203{
204 size_t i;
205
206 for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++) {
207 const char *cn = coldescs_cache[i].name;
208
209 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
210 return i;
211 }
212 warnx(_("unknown column: %s"), name);
213 return -1;
214}
215
27c349f9
KZ
216static void lscpu_context_init_paths(struct lscpu_cxt *cxt)
217{
218 DBG(MISC, ul_debugobj(cxt, "initialize paths"));
219 ul_path_init_debug();
220
221 /* /sys/devices/system/cpu */
222 cxt->syscpu = ul_new_path(_PATH_SYS_CPU);
223 if (!cxt->syscpu)
224 err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler"));
225 if (cxt->prefix)
226 ul_path_set_prefix(cxt->syscpu, cxt->prefix);
227
228 /* /proc */
229 cxt->procfs = ul_new_path("/proc");
230 if (!cxt->procfs)
231 err(EXIT_FAILURE, _("failed to initialize procfs handler"));
232 if (cxt->prefix)
233 ul_path_set_prefix(cxt->procfs, cxt->prefix);
234}
235
236static struct lscpu_cxt *lscpu_new_context(void)
237{
238 return xcalloc(1, sizeof(struct lscpu_cxt));
239}
240
241static void lscpu_free_context(struct lscpu_cxt *cxt)
242{
243 size_t i;
244
245 if (!cxt)
246 return;
247
248 DBG(MISC, ul_debugobj(cxt, "freeing context"));
249
250 DBG(MISC, ul_debugobj(cxt, " de-initialize paths"));
251 ul_unref_path(cxt->syscpu);
252 ul_unref_path(cxt->procfs);
253
254 DBG(MISC, ul_debugobj(cxt, " freeing cpus"));
255 for (i = 0; i < cxt->npossibles; i++) {
256 lscpu_unref_cpu(cxt->cpus[i]);
257 cxt->cpus[i] = NULL;
258 }
259 DBG(MISC, ul_debugobj(cxt, " freeing types"));
260 for (i = 0; i < cxt->ncputypes; i++) {
261 lscpu_unref_cputype(cxt->cputypes[i]);
262 cxt->cputypes[i] = NULL;
263 }
264
265 free(cxt->present);
266 free(cxt->online);
267 free(cxt->cputypes);
268 free(cxt->cpus);
269
270 for (i = 0; i < cxt->nvuls; i++) {
271 free(cxt->vuls[i].name);
272 free(cxt->vuls[i].text);
273 }
274 free(cxt->vuls);
275
276 for (i = 0; i < cxt->nnodes; i++)
277 free(cxt->nodemaps[i]);
278
279 free(cxt->nodemaps);
280 free(cxt->idx2nodenum);
281
282 lscpu_free_virtualization(cxt->virt);
283 lscpu_free_architecture(cxt->arch);
6fbb5328 284
27c349f9 285 lscpu_free_caches(cxt->ecaches, cxt->necaches);
6fbb5328 286 lscpu_free_caches(cxt->caches, cxt->ncaches);
27c349f9
KZ
287
288 free(cxt);
289}
290
63c5e7f8
KZ
291static void __fill_id( struct lscpu_cxt *cxt,
292 struct lscpu_cpu *cpu,
293 int id, cpu_set_t **map,
294 size_t nitems,
295 char *buf, size_t bufsz)
296{
297 *buf = '\0';
298
299 if (cxt->show_physical) {
300 if (id < 0)
301 snprintf(buf, bufsz, "-");
302 else
303 snprintf(buf, bufsz, "%d", id);
304 } else if (map) {
305 size_t i;
306
307 if (cpuset_ary_isset(cpu->logical_id, map, nitems,
308 cxt->setsize, &i) == 0)
309 snprintf(buf, bufsz, "%zu", i);
310 }
311}
5dd7507c 312
17353ee0
TW
313static void get_cell_boolean(
314 struct lscpu_cxt *cxt,
315 int has_data, int data,
316 char *buf, size_t bufsz)
317{
318 if (!has_data)
319 return;
320
321 if (cxt->mode == LSCPU_OUTPUT_PARSABLE || cxt->json)
322 snprintf(buf, bufsz, "%s",
323 data ? _("Y") : _("N"));
324 else
325 snprintf(buf, bufsz, "%s",
326 data ? _("yes") : _("no"));
327}
328
63c5e7f8
KZ
329#define fill_id(_cxt, _cpu, NAME, _buf, _bufsz) \
330 __fill_id(_cxt, (_cpu), \
331 (_cpu)-> NAME ## id, \
332 (_cpu)->type-> NAME ## maps, \
333 (_cpu)->type->n ## NAME ## s, \
334 _buf, _bufsz)
335
336static char *get_cell_data(
337 struct lscpu_cxt *cxt,
338 struct lscpu_cpu *cpu, int col,
339 char *buf, size_t bufsz)
5dd7507c 340{
4f642863 341 size_t i;
e3b3a2f3
KZ
342
343 *buf = '\0';
5dd7507c 344
63c5e7f8
KZ
345 if (!cpu->type)
346 return NULL;
347
477251f8 348 switch (col) {
cc07239d 349 case COL_CPU_CPU:
63c5e7f8 350 snprintf(buf, bufsz, "%d", cpu->logical_id);
477251f8 351 break;
cc94324e 352 case COL_CPU_BOGOMIPS:
aff94add
KZ
353 if (!cpu->bogomips && !cpu->type->bogomips)
354 break;
355 snprintf(buf, bufsz, "%.2f", (float) c_strtod(
356 cpu->bogomips ? : cpu->type->bogomips, NULL));
cc94324e 357 break;
cc07239d 358 case COL_CPU_CORE:
63c5e7f8 359 fill_id(cxt, cpu, core, buf, bufsz);
477251f8 360 break;
cc07239d 361 case COL_CPU_SOCKET:
63c5e7f8 362 fill_id(cxt, cpu, socket, buf, bufsz);
477251f8 363 break;
73c0a766
MM
364 case COL_CPU_CLUSTER:
365 if (cxt->is_cluster)
366 fill_id(cxt, cpu, socket, buf, bufsz);
367 break;
cc07239d 368 case COL_CPU_DRAWER:
63c5e7f8 369 fill_id(cxt, cpu, drawer, buf, bufsz);
b3adf6ef 370 break;
cc07239d 371 case COL_CPU_BOOK:
63c5e7f8
KZ
372 fill_id(cxt, cpu, book, buf, bufsz);
373 break;
374 case COL_CPU_NODE:
375 if (cpuset_ary_isset(cpu->logical_id, cxt->nodemaps,
376 cxt->nnodes, cxt->setsize, &i) == 0)
377 snprintf(buf, bufsz, "%d", cxt->idx2nodenum[i]);
477251f8 378 break;
cc07239d 379 case COL_CPU_CACHE:
e3b3a2f3 380 {
63c5e7f8 381 const char *last = NULL;
e3b3a2f3
KZ
382 char *p = buf;
383 size_t sz = bufsz;
e3b3a2f3 384
63c5e7f8
KZ
385 for (i = 0; i < cxt->ncaches; i++) {
386 int x;
387 struct lscpu_cache *ca;
388 const char *name = cxt->caches[i].name;
e9d659ea 389
63c5e7f8
KZ
390 if (last && strcmp(last, name) == 0)
391 continue;
392 last = name;
393 ca = lscpu_cpu_get_cache(cxt, cpu, name);
394 if (!ca)
395 continue;
396 x = snprintf(p, sz, "%d", ca->id);
397 if (x < 0 || (size_t) x >= sz)
398 return NULL;
399 p += x;
400 sz -= x;
e07cca6b
KZ
401 if (sz < 2)
402 return NULL;
403 *p++ = cxt->show_compatible ? ',' : ':';
404 *p = '\0';
405 sz--;
5dd7507c 406 }
e07cca6b
KZ
407 if (p > buf && (*(p - 1) == ',' || *(p - 1) == ':'))
408 *(p - 1) = '\0';
477251f8 409 break;
e3b3a2f3 410 }
cc07239d 411 case COL_CPU_POLARIZATION:
63c5e7f8
KZ
412 if (cpu->polarization < 0)
413 break;
414 snprintf(buf, bufsz, "%s",
415 cxt->mode == LSCPU_OUTPUT_PARSABLE ?
416 polar_modes[cpu->polarization].parsable :
417 polar_modes[cpu->polarization].readable);
2b8fcb81 418 break;
cc07239d 419 case COL_CPU_ADDRESS:
63c5e7f8
KZ
420 if (cpu->address < 0)
421 break;
422 snprintf(buf, bufsz, "%d", cpu->address);
596b8845 423 break;
cc07239d 424 case COL_CPU_CONFIGURED:
17353ee0 425 get_cell_boolean(cxt, cpu->configured >= 0, cpu->configured, buf, bufsz);
d231eea1 426 break;
cc07239d 427 case COL_CPU_ONLINE:
17353ee0 428 get_cell_boolean(cxt, !!cxt->online, is_cpu_online(cxt, cpu), buf, bufsz);
a7e5300c 429 break;
6d880d3d 430 case COL_CPU_MHZ:
ff6513c7
KZ
431 if (cpu->mhz_cur_freq)
432 snprintf(buf, bufsz, "%.4f", cpu->mhz_cur_freq);
6d880d3d 433 break;
9b9e4f5d
KZ
434 case COL_CPU_SCALMHZ:
435 if (cpu->mhz_cur_freq && cpu->mhz_max_freq)
436 snprintf(buf, bufsz, "%.0f%%", cpu->mhz_cur_freq / cpu->mhz_max_freq * 100);
6d880d3d 437 break;
cc07239d 438 case COL_CPU_MAXMHZ:
63c5e7f8
KZ
439 if (cpu->mhz_max_freq)
440 snprintf(buf, bufsz, "%.4f", cpu->mhz_max_freq);
e065a597 441 break;
cc07239d 442 case COL_CPU_MINMHZ:
63c5e7f8
KZ
443 if (cpu->mhz_min_freq)
444 snprintf(buf, bufsz, "%.4f", cpu->mhz_min_freq);
44320710 445 break;
477251f8 446 }
e3b3a2f3
KZ
447 return buf;
448}
449
63c5e7f8
KZ
450static char *get_cell_header(
451 struct lscpu_cxt *cxt, int col,
452 char *buf, size_t bufsz)
e3b3a2f3
KZ
453{
454 *buf = '\0';
455
cc07239d 456 if (col == COL_CPU_CACHE) {
63c5e7f8 457 const char *last = NULL;
e3b3a2f3
KZ
458 char *p = buf;
459 size_t sz = bufsz;
63c5e7f8
KZ
460 size_t i;
461
462 for (i = 0; i < cxt->ncaches; i++) {
463 struct lscpu_cache *ca = &cxt->caches[i];
464 int x;
465
466 if (last && strcmp(last, ca->name) == 0)
467 continue;
468 last = ca->name;
e3b3a2f3 469
63c5e7f8 470 x = snprintf(p, sz, "%s", ca->name);
06fa5817 471 if (x < 0 || (size_t) x >= sz)
e3b3a2f3
KZ
472 return NULL;
473 sz -= x;
474 p += x;
e07cca6b
KZ
475 if (sz < 2)
476 return NULL;
477 *p++ = cxt->show_compatible ? ',' : ':';
478 *p = '\0';
479 sz--;
e3b3a2f3 480 }
e07cca6b
KZ
481 if (p > buf && (*(p - 1) == ',' || *(p - 1) == ':'))
482 *(p - 1) = '\0';
63c5e7f8 483 if (cxt->ncaches)
e3b3a2f3
KZ
484 return buf;
485 }
cc07239d 486 snprintf(buf, bufsz, "%s", coldescs_cpu[col].name);
e3b3a2f3 487 return buf;
477251f8
KZ
488}
489
1766641a 490
9d480e57
KZ
491static void caches_add_line(struct lscpu_cxt *cxt,
492 struct libscols_table *tb,
493 struct lscpu_cache *ca,
494 int cols[], size_t ncols)
495{
496 struct libscols_line *ln;
497 size_t c;
498
499 ln = scols_table_new_line(tb, NULL);
500 if (!ln)
501 err(EXIT_FAILURE, _("failed to allocate output line"));
502
503 for (c = 0; c < ncols; c++) {
504 char *data = NULL;
505 int col = cols[c];
506
507 switch (col) {
508 case COL_CACHE_NAME:
509 if (ca->name)
510 data = xstrdup(ca->name);
511 break;
512 case COL_CACHE_ONESIZE:
513 if (!ca->size)
514 break;
515 if (cxt->bytes)
516 xasprintf(&data, "%" PRIu64, ca->size);
517 else
518 data = size_to_human_string(SIZE_SUFFIX_1LETTER, ca->size);
519 break;
520 case COL_CACHE_ALLSIZE:
521 {
b9b28b64
KZ
522 uint64_t sz = 0;
523 if (ca->name)
9dadd3e6 524 sz = lscpu_get_cache_full_size(cxt, ca->name, NULL);
9d480e57
KZ
525 if (!sz)
526 break;
527 if (cxt->bytes)
528 xasprintf(&data, "%" PRIu64, sz);
529 else
530 data = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
531 break;
532 }
533 case COL_CACHE_WAYS:
534 if (ca->ways_of_associativity)
535 xasprintf(&data, "%u", ca->ways_of_associativity);
536 break;
537
538 case COL_CACHE_TYPE:
539 if (ca->type)
540 data = xstrdup(ca->type);
541 break;
542 case COL_CACHE_LEVEL:
543 if (ca->level)
544 xasprintf(&data, "%d", ca->level);
545 break;
546 case COL_CACHE_ALLOCPOL:
547 if (ca->allocation_policy)
548 data = xstrdup(ca->allocation_policy);
549 break;
550 case COL_CACHE_WRITEPOL:
551 if (ca->write_policy)
552 data = xstrdup(ca->write_policy);
553 break;
554 case COL_CACHE_PHYLINE:
555 if (ca->physical_line_partition)
556 xasprintf(&data, "%u", ca->physical_line_partition);
557 break;
558 case COL_CACHE_SETS:
559 if (ca->number_of_sets)
560 xasprintf(&data, "%u", ca->number_of_sets);
561 break;
562 case COL_CACHE_COHERENCYSIZE:
563 if (ca->coherency_line_size)
564 xasprintf(&data, "%u", ca->coherency_line_size);
565 break;
566 }
567
568 if (data && scols_line_refer_data(ln, c, data))
569 err(EXIT_FAILURE, _("failed to add output data"));
570 }
571}
572
573
0e86bc84
KZ
574/*
575 * [-C] backend
576 */
1766641a 577static void print_caches_readable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
0e86bc84 578{
1766641a
KZ
579 size_t i;
580 struct libscols_table *tb;
581 const char *last = NULL;
0e86bc84
KZ
582
583 scols_init_debug(0);
584
1766641a
KZ
585 tb = scols_new_table();
586 if (!tb)
0e86bc84 587 err(EXIT_FAILURE, _("failed to allocate output table"));
1766641a
KZ
588 if (cxt->json) {
589 scols_table_enable_json(tb, 1);
590 scols_table_set_name(tb, "caches");
0e86bc84
KZ
591 }
592
593 for (i = 0; i < ncols; i++) {
594 struct lscpu_coldesc *cd = &coldescs_cache[cols[i]];
17353ee0
TW
595 struct libscols_column *cl;
596
597 cl = scols_table_new_column(tb, cd->name, 0, cd->flags);
17353ee0 598 if (cl == NULL)
0e86bc84 599 err(EXIT_FAILURE, _("failed to allocate output column"));
17353ee0
TW
600 if (cxt->json)
601 scols_column_set_json_type(cl, cd->json_type);
0e86bc84
KZ
602 }
603
9d480e57 604 /* standard caches */
1766641a
KZ
605 for (i = 0; i < cxt->ncaches; i++) {
606 struct lscpu_cache *ca = &cxt->caches[i];
0e86bc84 607
1766641a
KZ
608 if (last && strcmp(last, ca->name) == 0)
609 continue;
1766641a 610 last = ca->name;
9d480e57
KZ
611 caches_add_line(cxt, tb, ca, cols, ncols);
612 }
1766641a 613
9d480e57
KZ
614 /* extra caches */
615 for (i = 0; i < cxt->necaches; i++) {
616 struct lscpu_cache *ca = &cxt->ecaches[i];
318542e0 617
9d480e57
KZ
618 if (last && strcmp(last, ca->name) == 0)
619 continue;
620 last = ca->name;
621 caches_add_line(cxt, tb, ca, cols, ncols);
0e86bc84
KZ
622 }
623
1766641a
KZ
624 scols_print_table(tb);
625 scols_unref_table(tb);
0e86bc84
KZ
626}
627
477251f8 628/*
ba45d8c1 629 * [-p] backend, we support two parsable formats:
477251f8
KZ
630 *
631 * 1) "compatible" -- this format is compatible with the original lscpu(1)
632 * output and it contains fixed set of the columns. The CACHE columns are at
633 * the end of the line and the CACHE is not printed if the number of the caches
634 * is zero. The CACHE columns are separated by two commas, for example:
635 *
636 * $ lscpu --parse
637 * # CPU,Core,Socket,Node,,L1d,L1i,L2
638 * 0,0,0,0,,0,0,0
639 * 1,1,0,0,,1,1,0
640 *
641 * 2) "user defined output" -- this format prints always all columns without
642 * special prefix for CACHE column. If there are not CACHEs then the column is
643 * empty and the header "Cache" is printed rather than a real name of the cache.
644 * The CACHE columns are separated by ':'.
645 *
646 * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE
647 * # CPU,Core,Socket,Node,L1d:L1i:L2
648 * 0,0,0,0,0:0:0
649 * 1,1,0,0,1:1:0
650 */
0710bb13 651static void print_cpus_parsable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
477251f8 652{
e3b3a2f3 653 char buf[BUFSIZ], *data;
0710bb13 654 size_t i;
477251f8 655
e3b3a2f3
KZ
656 /*
657 * Header
658 */
477251f8
KZ
659 printf(_(
660 "# The following is the parsable format, which can be fed to other\n"
661 "# programs. Each different item in every column has an unique ID\n"
6321d34f 662 "# starting usually from zero.\n"));
477251f8
KZ
663
664 fputs("# ", stdout);
665 for (i = 0; i < ncols; i++) {
3d27b76a 666 int col = cols[i];
b9d18bc3 667
cc07239d 668 if (col == COL_CPU_CACHE) {
0710bb13 669 if (cxt->show_compatible && !cxt->ncaches)
477251f8 670 continue;
0710bb13 671 if (cxt->show_compatible && i != 0)
477251f8 672 putchar(',');
477251f8 673 }
e3b3a2f3
KZ
674 if (i > 0)
675 putchar(',');
676
0710bb13 677 data = get_cell_header(cxt, col, buf, sizeof(buf));
cc07239d
KZ
678 if (data && * data && col != COL_CPU_CACHE &&
679 !coldescs_cpu[col].is_abbr) {
3d27b76a
KZ
680 /*
681 * For normal column names use mixed case (e.g. "Socket")
682 */
683 char *p = data + 1;
684
14e8be8a
PU
685 while (p && *p != '\0') {
686 *p = tolower((unsigned int) *p);
687 p++;
688 }
3d27b76a 689 }
e3b3a2f3 690 fputs(data && *data ? data : "", stdout);
477251f8
KZ
691 }
692 putchar('\n');
693
e3b3a2f3
KZ
694 /*
695 * Data
696 */
0710bb13
KZ
697 for (i = 0; i < cxt->npossibles; i++) {
698 struct lscpu_cpu *cpu = cxt->cpus[i];
699 size_t c;
e3b3a2f3 700
0710bb13
KZ
701 if (cxt->online) {
702 if (!cxt->show_offline && !is_cpu_online(cxt, cpu))
6dd7b74b 703 continue;
0710bb13 704 if (!cxt->show_online && is_cpu_online(cxt, cpu))
6dd7b74b
SK
705 continue;
706 }
0710bb13 707 if (cxt->present && !is_cpu_present(cxt, cpu))
a5cfffff 708 continue;
0710bb13 709
477251f8 710 for (c = 0; c < ncols; c++) {
0710bb13
KZ
711 if (cxt->show_compatible && cols[c] == COL_CPU_CACHE) {
712 if (!cxt->ncaches)
477251f8
KZ
713 continue;
714 if (c > 0)
715 putchar(',');
716 }
717 if (c > 0)
718 putchar(',');
e3b3a2f3 719
0710bb13 720 data = get_cell_data(cxt, cpu, cols[c], buf, sizeof(buf));
e3b3a2f3 721 fputs(data && *data ? data : "", stdout);
bdda3543 722 *buf = '\0';
477251f8 723 }
5dd7507c
CQ
724 putchar('\n');
725 }
726}
727
ba45d8c1
KZ
728/*
729 * [-e] backend
730 */
63c5e7f8 731static void print_cpus_readable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
ba45d8c1 732{
63c5e7f8 733 size_t i;
e7213e34
KZ
734 char buf[BUFSIZ];
735 const char *data;
63c5e7f8 736 struct libscols_table *tb;
ba45d8c1 737
710ed55d
KZ
738 scols_init_debug(0);
739
63c5e7f8
KZ
740 tb = scols_new_table();
741 if (!tb)
780ce22c 742 err(EXIT_FAILURE, _("failed to allocate output table"));
63c5e7f8
KZ
743 if (cxt->json) {
744 scols_table_enable_json(tb, 1);
745 scols_table_set_name(tb, "cpus");
19a5510b 746 }
ba45d8c1
KZ
747
748 for (i = 0; i < ncols; i++) {
63c5e7f8 749 data = get_cell_header(cxt, cols[i], buf, sizeof(buf));
17353ee0
TW
750 struct lscpu_coldesc *cd = &coldescs_cpu[cols[i]];
751 struct libscols_column *cl;
752
753 cl = scols_table_new_column(tb, data, 0, cd->flags);
754 if (cl == NULL)
780ce22c 755 err(EXIT_FAILURE, _("failed to allocate output column"));
17353ee0
TW
756 if (cxt->json)
757 scols_column_set_json_type(cl, cd->json_type);
ba45d8c1
KZ
758 }
759
63c5e7f8
KZ
760 for (i = 0; i < cxt->npossibles; i++) {
761 size_t c;
762 struct libscols_line *ln;
763 struct lscpu_cpu *cpu = cxt->cpus[i];
ba45d8c1 764
63c5e7f8
KZ
765 if (cxt->online) {
766 if (!cxt->show_offline && !is_cpu_online(cxt, cpu))
6dd7b74b 767 continue;
63c5e7f8 768 if (!cxt->show_online && is_cpu_online(cxt, cpu))
6dd7b74b
SK
769 continue;
770 }
63c5e7f8
KZ
771
772 if (cxt->present && !is_cpu_present(cxt, cpu))
a5cfffff 773 continue;
ba45d8c1 774
63c5e7f8
KZ
775 ln = scols_table_new_line(tb, NULL);
776 if (!ln)
780ce22c 777 err(EXIT_FAILURE, _("failed to allocate output line"));
ba45d8c1
KZ
778
779 for (c = 0; c < ncols; c++) {
63c5e7f8 780 data = get_cell_data(cxt, cpu, cols[c], buf, sizeof(buf));
e7213e34
KZ
781 if (!data || !*data)
782 data = "-";
63c5e7f8 783 if (scols_line_set_data(ln, c, data))
780ce22c 784 err(EXIT_FAILURE, _("failed to add output data"));
ba45d8c1
KZ
785 }
786 }
787
63c5e7f8
KZ
788 scols_print_table(tb);
789 scols_unref_table(tb);
ba45d8c1 790}
5dd7507c 791
e3f21318 792static struct libscols_line *
91eef60c 793 __attribute__ ((__format__(printf, 4, 5)))
c82b12a0 794 add_summary_sprint(struct libscols_table *tb,
e3f21318 795 struct libscols_line *sec,
c82b12a0
KZ
796 const char *txt,
797 const char *fmt,
798 ...)
799{
91eef60c 800 struct libscols_line *ln;
c82b12a0
KZ
801 va_list args;
802
c9121791
KZ
803 /* Don't print section lines without data */
804 if (!hierarchic && fmt == NULL)
91eef60c
KZ
805 return NULL;
806
807 ln = scols_table_new_line(tb, sec);
c82b12a0 808 if (!ln)
780ce22c 809 err(EXIT_FAILURE, _("failed to allocate output line"));
c82b12a0
KZ
810
811 /* description column */
a0661407
KZ
812 if (txt && scols_line_set_data(ln, 0, txt))
813 err(EXIT_FAILURE, _("failed to add output data"));
c82b12a0
KZ
814
815 /* data column */
91eef60c
KZ
816 if (fmt) {
817 char *data;
818 va_start(args, fmt);
819 xvasprintf(&data, fmt, args);
820 va_end(args);
821
822 if (data && scols_line_refer_data(ln, 1, data))
823 err(EXIT_FAILURE, _("failed to add output data"));
824 }
e3f21318
KZ
825
826 return ln;
c82b12a0
KZ
827}
828
91eef60c 829#define add_summary_e(tb, sec, txt) add_summary_sprint(tb, sec, txt, NULL)
e3f21318
KZ
830#define add_summary_n(tb, sec, txt, num) add_summary_sprint(tb, sec, txt, "%zu", num)
831#define add_summary_s(tb, sec, txt, str) add_summary_sprint(tb, sec, txt, "%s", str)
832#define add_summary_x(tb, sec, txt, fmt, x) add_summary_sprint(tb, sec, txt, fmt, x)
5dd7507c
CQ
833
834static void
d8813bb3
KZ
835print_cpuset(struct lscpu_cxt *cxt,
836 struct libscols_table *tb,
91eef60c 837 struct libscols_line *sec,
d8813bb3 838 const char *key, cpu_set_t *set)
4f912c6a 839{
d8813bb3 840 size_t setbuflen = 7 * cxt->maxcpus;
4f912c6a
KZ
841 char setbuf[setbuflen], *p;
842
93a1bb10
KZ
843 assert(set);
844 assert(key);
845 assert(tb);
846 assert(cxt);
847
d8813bb3
KZ
848 if (cxt->hex) {
849 p = cpumask_create(setbuf, setbuflen, set, cxt->setsize);
91eef60c 850 add_summary_s(tb, sec, key, p);
4f912c6a 851 } else {
d8813bb3 852 p = cpulist_create(setbuf, setbuflen, set, cxt->setsize);
91eef60c 853 add_summary_s(tb, sec, key, p);
4f912c6a 854 }
4f912c6a
KZ
855}
856
2f5e2730
KZ
857static void
858print_summary_cputype(struct lscpu_cxt *cxt,
859 struct lscpu_cputype *ct,
91eef60c
KZ
860 struct libscols_table *tb,
861 struct libscols_line *sec)
5dd7507c 862{
38e93216 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)
aff94add 922 add_summary_x(tb, sec, _("BogoMIPS:"), "%.2f", (float) c_strtod(ct->bogomips, NULL));
91eef60c 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");
c9121791 959 } else if (hierarchic) {
91eef60c
KZ
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
c9121791 971 if (scols_table_new_column(tb, "field", 0, hierarchic ? 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
c9121791 1084 snprintf(field, sizeof(field), hierarchic ? _("%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 }
c9121791 1114 snprintf(field, sizeof(field), hierarchic ? _("%s:") : _("%s cache:"), ca->name);
071d3135
KZ
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++) {
c9121791 1145 snprintf(field, sizeof(field), hierarchic ?
91eef60c
KZ
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);
c9121791 1178 fputs(_(" --hierarchic[=when] use subsections in summary (auto, never, always)\n"), out);
0a31a242 1179 fputs(_(" --output-all print all available columns for -e, -p or -C\n"), out);
c6f095cf 1180 fputs(USAGE_SEPARATOR, out);
f45f3ec3 1181 printf(USAGE_HELP_OPTIONS(25));
b9d18bc3 1182
cc07239d
KZ
1183 fputs(_("\nAvailable output columns for -e or -p:\n"), out);
1184 for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++)
1185 fprintf(out, " %13s %s\n", coldescs_cpu[i].name, _(coldescs_cpu[i].help));
3d27b76a 1186
0e86bc84
KZ
1187 fputs(_("\nAvailable output columns for -C:\n"), out);
1188 for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++)
1189 fprintf(out, " %13s %s\n", coldescs_cache[i].name, _(coldescs_cache[i].help));
1190
f45f3ec3 1191 printf(USAGE_MAN_TAIL("lscpu(1)"));
4f912c6a 1192
6e1eda6f 1193 exit(EXIT_SUCCESS);
5dd7507c
CQ
1194}
1195
1196int main(int argc, char *argv[])
1197{
27c349f9 1198 struct lscpu_cxt *cxt;
b73d38b1 1199 int c, all = 0;
b4f60062 1200 int columns[ARRAY_SIZE(coldescs_cpu)];
7fc12cd2 1201 int cpu_modifier_specified = 0;
1242c3fd 1202 char *outarg = NULL;
b4f60062 1203 size_t i, ncolumns = 0;
fbf0619b
SK
1204 enum {
1205 OPT_OUTPUT_ALL = CHAR_MAX + 1,
c9121791 1206 OPT_HIERARCHIC,
fbf0619b 1207 };
6c7d5ae9 1208 static const struct option longopts[] = {
87918040
SK
1209 { "all", no_argument, NULL, 'a' },
1210 { "online", no_argument, NULL, 'b' },
2011528b 1211 { "bytes", no_argument, NULL, 'B' },
0e86bc84 1212 { "caches", optional_argument, NULL, 'C' },
87918040
SK
1213 { "offline", no_argument, NULL, 'c' },
1214 { "help", no_argument, NULL, 'h' },
1215 { "extended", optional_argument, NULL, 'e' },
19a5510b 1216 { "json", no_argument, NULL, 'J' },
87918040
SK
1217 { "parse", optional_argument, NULL, 'p' },
1218 { "sysroot", required_argument, NULL, 's' },
1219 { "physical", no_argument, NULL, 'y' },
1220 { "hex", no_argument, NULL, 'x' },
1221 { "version", no_argument, NULL, 'V' },
fbf0619b 1222 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
c9121791 1223 { "hierarchic", optional_argument, NULL, OPT_HIERARCHIC },
87918040 1224 { NULL, 0, NULL, 0 }
5dd7507c
CQ
1225 };
1226
8e97eb4b 1227 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
0e86bc84 1228 { 'C','e','p' },
a44cd891 1229 { 'a','b','c' },
8e97eb4b
KZ
1230 { 0 }
1231 };
1232 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1233
2f8f1388 1234 setlocale(LC_ALL, "");
5dd7507c
CQ
1235 bindtextdomain(PACKAGE, LOCALEDIR);
1236 textdomain(PACKAGE);
25b7045e 1237 close_stdout_atexit();
5dd7507c 1238
b73d38b1
KZ
1239 cxt = lscpu_new_context();
1240
2011528b 1241 while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::s:xyV", longopts, NULL)) != -1) {
8e97eb4b
KZ
1242
1243 err_exclusive_options(c, longopts, excl, excl_st);
1244
5dd7507c 1245 switch (c) {
0ad29ff6 1246 case 'a':
b73d38b1 1247 cxt->show_online = cxt->show_offline = 1;
7fc12cd2 1248 cpu_modifier_specified = 1;
0ad29ff6 1249 break;
2011528b 1250 case 'B':
b73d38b1 1251 cxt->bytes = 1;
2011528b 1252 break;
23e9e95a 1253 case 'b':
b73d38b1 1254 cxt->show_online = 1;
7fc12cd2 1255 cpu_modifier_specified = 1;
23e9e95a 1256 break;
7afc2387 1257 case 'c':
b73d38b1 1258 cxt->show_offline = 1;
7fc12cd2 1259 cpu_modifier_specified = 1;
7afc2387 1260 break;
0e86bc84
KZ
1261 case 'C':
1262 if (optarg) {
1263 if (*optarg == '=')
1264 optarg++;
1242c3fd 1265 outarg = optarg;
0e86bc84 1266 }
b73d38b1 1267 cxt->mode = LSCPU_OUTPUT_CACHES;
0e86bc84 1268 break;
19a5510b 1269 case 'J':
b73d38b1 1270 cxt->json = 1;
19a5510b 1271 break;
5dd7507c 1272 case 'p':
ba45d8c1 1273 case 'e':
477251f8
KZ
1274 if (optarg) {
1275 if (*optarg == '=')
1276 optarg++;
1242c3fd 1277 outarg = optarg;
477251f8 1278 }
b73d38b1 1279 cxt->mode = c == 'p' ? LSCPU_OUTPUT_PARSABLE : LSCPU_OUTPUT_READABLE;
5dd7507c 1280 break;
47b6e8b6 1281 case 's':
b73d38b1
KZ
1282 cxt->prefix = optarg;
1283 cxt->noalive = 1;
47b6e8b6 1284 break;
4f912c6a 1285 case 'x':
b73d38b1 1286 cxt->hex = 1;
4f912c6a 1287 break;
0d2b5d2a 1288 case 'y':
b73d38b1 1289 cxt->show_physical = 1;
0d2b5d2a 1290 break;
fbf0619b 1291 case OPT_OUTPUT_ALL:
0e86bc84 1292 all = 1;
fbf0619b 1293 break;
c9121791
KZ
1294 case OPT_HIERARCHIC:
1295 if (optarg) {
1296 if (strcmp(optarg, "auto") == 0)
1297 hierarchic = -1;
1298 else if (strcmp(optarg, "never") == 0)
1299 hierarchic = 0;
1300 else if (strcmp(optarg, "always") == 0)
1301 hierarchic = 1;
1302 else
1303 errx(EXIT_FAILURE, _("unsupported --flat argument"));
1304 } else
1305 hierarchic = 1;
1306 break;
2c308875
KZ
1307 case 'h':
1308 usage();
1309 case 'V':
1310 print_version(EXIT_SUCCESS);
5dd7507c 1311 default:
677ec86c 1312 errtryhelp(EXIT_FAILURE);
5dd7507c
CQ
1313 }
1314 }
7bbb7829 1315
0a31a242 1316 if (all && ncolumns == 0) {
b73d38b1 1317 size_t maxsz = cxt->mode == LSCPU_OUTPUT_CACHES ?
0e86bc84
KZ
1318 ARRAY_SIZE(coldescs_cache) :
1319 ARRAY_SIZE(coldescs_cpu);
1320
b73d38b1
KZ
1321 for (i = 0; i < maxsz; i++)
1322 columns[ncolumns++] = i;
0e86bc84
KZ
1323 }
1324
b73d38b1 1325 if (cpu_modifier_specified && cxt->mode == LSCPU_OUTPUT_SUMMARY) {
7fc12cd2
HC
1326 fprintf(stderr,
1327 _("%s: options --all, --online and --offline may only "
ac56e555 1328 "be used with options --extended or --parse.\n"),
7fc12cd2
HC
1329 program_invocation_short_name);
1330 return EXIT_FAILURE;
1331 }
1332
6e1eda6f
RM
1333 if (argc != optind) {
1334 warnx(_("bad usage"));
1335 errtryhelp(EXIT_FAILURE);
1336 }
7bbb7829 1337
7afc2387 1338 /* set default cpu display mode if none was specified */
b73d38b1
KZ
1339 if (!cxt->show_online && !cxt->show_offline) {
1340 cxt->show_online = 1;
1341 cxt->show_offline = cxt->mode == LSCPU_OUTPUT_READABLE ? 1 : 0;
7afc2387 1342 }
6e509042 1343
91eef60c 1344
4b9cbc38 1345 lscpu_init_debug();
6e509042 1346
4b9cbc38 1347 lscpu_context_init_paths(cxt);
5dd7507c 1348
4b9cbc38
KZ
1349 lscpu_read_cpulists(cxt);
1350 lscpu_read_cpuinfo(cxt);
1351 cxt->arch = lscpu_read_architecture(cxt);
538b50cb 1352
4b9cbc38
KZ
1353 lscpu_read_archext(cxt);
1354 lscpu_read_vulnerabilities(cxt);
1355 lscpu_read_numas(cxt);
1356 lscpu_read_topology(cxt);
7e03f383 1357
4b9cbc38 1358 lscpu_decode_arm(cxt);
28b1658f 1359
4b9cbc38 1360 cxt->virt = lscpu_read_virtualization(cxt);
c8b64f6d 1361
c9121791
KZ
1362 if (hierarchic == -1)
1363 hierarchic = isatty(STDOUT_FILENO); /* default */
1364
d8813bb3 1365 switch(cxt->mode) {
30b912d3 1366 case LSCPU_OUTPUT_SUMMARY:
d8813bb3 1367 print_summary(cxt);
ba45d8c1 1368 break;
30b912d3 1369 case LSCPU_OUTPUT_CACHES:
0e86bc84
KZ
1370 if (!ncolumns) {
1371 columns[ncolumns++] = COL_CACHE_NAME;
1372 columns[ncolumns++] = COL_CACHE_ONESIZE;
1373 columns[ncolumns++] = COL_CACHE_ALLSIZE;
1374 columns[ncolumns++] = COL_CACHE_WAYS;
1375 columns[ncolumns++] = COL_CACHE_TYPE;
1376 columns[ncolumns++] = COL_CACHE_LEVEL;
cf3b6b71
KZ
1377 columns[ncolumns++] = COL_CACHE_SETS;
1378 columns[ncolumns++] = COL_CACHE_PHYLINE;
1379 columns[ncolumns++] = COL_CACHE_COHERENCYSIZE;
0e86bc84 1380 }
1242c3fd
KZ
1381 if (outarg && string_add_to_idarray(outarg, columns,
1382 ARRAY_SIZE(columns),
1383 &ncolumns, cache_column_name_to_id) < 0)
1384 return EXIT_FAILURE;
1385
1766641a 1386 print_caches_readable(cxt, columns, ncolumns);
0e86bc84 1387 break;
30b912d3 1388 case LSCPU_OUTPUT_READABLE:
ba45d8c1
KZ
1389 if (!ncolumns) {
1390 /* No list was given. Just print whatever is there. */
63c5e7f8
KZ
1391 struct lscpu_cputype *ct = lscpu_cputype_get_default(cxt);
1392
cc07239d 1393 columns[ncolumns++] = COL_CPU_CPU;
63c5e7f8 1394 if (cxt->nnodes)
cc07239d 1395 columns[ncolumns++] = COL_CPU_NODE;
63c5e7f8 1396 if (ct && ct->ndrawers)
cc07239d 1397 columns[ncolumns++] = COL_CPU_DRAWER;
63c5e7f8 1398 if (ct && ct->nbooks)
cc07239d 1399 columns[ncolumns++] = COL_CPU_BOOK;
73c0a766
MM
1400 if (ct && ct->nsockets) {
1401 if (cxt->is_cluster)
1402 columns[ncolumns++] = COL_CPU_CLUSTER;
1403 else
1404 columns[ncolumns++] = COL_CPU_SOCKET;
1405 }
63c5e7f8 1406 if (ct && ct->ncores)
cc07239d 1407 columns[ncolumns++] = COL_CPU_CORE;
63c5e7f8 1408 if (cxt->ncaches)
cc07239d 1409 columns[ncolumns++] = COL_CPU_CACHE;
63c5e7f8 1410 if (cxt->online)
cc07239d 1411 columns[ncolumns++] = COL_CPU_ONLINE;
63c5e7f8 1412 if (ct && ct->has_configured)
cc07239d 1413 columns[ncolumns++] = COL_CPU_CONFIGURED;
63c5e7f8 1414 if (ct && ct->has_polarization)
cc07239d 1415 columns[ncolumns++] = COL_CPU_POLARIZATION;
63c5e7f8 1416 if (ct && ct->has_addresses)
cc07239d 1417 columns[ncolumns++] = COL_CPU_ADDRESS;
63c5e7f8 1418 if (ct && ct->has_freq) {
cc07239d 1419 columns[ncolumns++] = COL_CPU_MAXMHZ;
cc07239d 1420 columns[ncolumns++] = COL_CPU_MINMHZ;
aa049eab 1421 columns[ncolumns++] = COL_CPU_MHZ;
63c5e7f8 1422 }
ba45d8c1 1423 }
1242c3fd
KZ
1424 if (outarg && string_add_to_idarray(outarg, columns,
1425 ARRAY_SIZE(columns),
1426 &ncolumns, cpu_column_name_to_id) < 0)
1427 return EXIT_FAILURE;
63c5e7f8
KZ
1428 print_cpus_readable(cxt, columns, ncolumns);
1429 break;
63c5e7f8
KZ
1430 case LSCPU_OUTPUT_PARSABLE:
1431 if (!ncolumns) {
1432 columns[ncolumns++] = COL_CPU_CPU;
1433 columns[ncolumns++] = COL_CPU_CORE;
73c0a766
MM
1434 if (cxt->is_cluster)
1435 columns[ncolumns++] = COL_CPU_CLUSTER;
1436 else
1437 columns[ncolumns++] = COL_CPU_SOCKET;
63c5e7f8
KZ
1438 columns[ncolumns++] = COL_CPU_NODE;
1439 columns[ncolumns++] = COL_CPU_CACHE;
0710bb13 1440 cxt->show_compatible = 1;
63c5e7f8 1441 }
1242c3fd
KZ
1442 if (outarg && string_add_to_idarray(outarg, columns,
1443 ARRAY_SIZE(columns),
1444 &ncolumns, cpu_column_name_to_id) < 0)
1445 return EXIT_FAILURE;
1446
0710bb13 1447 print_cpus_parsable(cxt, columns, ncolumns);
ba45d8c1 1448 break;
8005924a 1449 }
5dd7507c 1450
27c349f9
KZ
1451 lscpu_free_context(cxt);
1452
cf474aac 1453 return EXIT_SUCCESS;
5dd7507c 1454}