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