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