]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lscpu.c
lscpu: Replace space with tabs
[thirdparty/util-linux.git] / sys-utils / lscpu.c
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it would be useful,
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 *
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.
20 */
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <dirent.h>
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>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/personality.h>
37
38 #include <libsmartcols.h>
39
40 #include "closestream.h"
41 #include "optutils.h"
42
43 #include "lscpu.h"
44
45 static const char *virt_types[] = {
46 [VIRT_TYPE_NONE] = N_("none"),
47 [VIRT_TYPE_PARA] = N_("para"),
48 [VIRT_TYPE_FULL] = N_("full"),
49 [VIRT_TYPE_CONTAINER] = N_("container"),
50 };
51
52 static const char *hv_vendors[] = {
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"
69 };
70
71 /* dispatching modes */
72 static const char *disp_modes[] = {
73 [DISP_HORIZONTAL] = N_("horizontal"),
74 [DISP_VERTICAL] = N_("vertical")
75 };
76
77 struct polarization_modes {
78 char *parsable;
79 char *readable;
80 };
81
82 static struct polarization_modes polar_modes[] = {
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"},
88 };
89
90 /*
91 * IDs
92 */
93 enum {
94 COL_CPU_BOGOMIPS,
95 COL_CPU_CPU,
96 COL_CPU_CORE,
97 COL_CPU_SOCKET,
98 COL_CPU_CLUSTER,
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,
107 COL_CPU_MHZ,
108 COL_CPU_MAXMHZ,
109 COL_CPU_MINMHZ,
110 };
111
112 enum {
113 COL_CACHE_ALLSIZE,
114 COL_CACHE_LEVEL,
115 COL_CACHE_NAME,
116 COL_CACHE_ONESIZE,
117 COL_CACHE_TYPE,
118 COL_CACHE_WAYS,
119 COL_CACHE_ALLOCPOL,
120 COL_CACHE_WRITEPOL,
121 COL_CACHE_PHYLINE,
122 COL_CACHE_SETS,
123 COL_CACHE_COHERENCYSIZE
124 };
125
126
127 /* column description
128 */
129 struct lscpu_coldesc {
130 const char *name;
131 const char *help;
132
133 int flags;
134 unsigned int is_abbr:1; /* name is abbreviation */
135 };
136
137 static struct lscpu_coldesc coldescs_cpu[] =
138 {
139 [COL_CPU_BOGOMIPS] = { "BOGOMIPS", N_("crude measurement of CPU speed"), SCOLS_FL_RIGHT, 1 },
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 },
142 [COL_CPU_CLUSTER] = { "CLUSTER", N_("logical cluster number"), SCOLS_FL_RIGHT },
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 },
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") },
151 [COL_CPU_ONLINE] = { "ONLINE", N_("shows if Linux currently makes use of the CPU"), SCOLS_FL_RIGHT },
152 [COL_CPU_MHZ] = { "MHZ", N_("shows the currently MHz of the CPU"), SCOLS_FL_RIGHT },
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 }
155 };
156
157 static 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") },
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 }
170 };
171
172 static int is_term = 0;
173
174 UL_DEBUG_DEFINE_MASK(lscpu);
175 UL_DEBUG_DEFINE_MASKNAMES(lscpu) = UL_DEBUG_EMPTY_MASKNAMES;
176
177 static void lscpu_init_debug(void)
178 {
179 __UL_INIT_DEBUG_FROM_ENV(lscpu, LSCPU_DEBUG_, 0, LSCPU_DEBUG);
180 }
181
182 static int
183 cpu_column_name_to_id(const char *name, size_t namesz)
184 {
185 size_t i;
186
187 for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) {
188 const char *cn = coldescs_cpu[i].name;
189
190 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
191 return i;
192 }
193 warnx(_("unknown column: %s"), name);
194 return -1;
195 }
196
197 static int
198 cache_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
212 static 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
232 static struct lscpu_cxt *lscpu_new_context(void)
233 {
234 return xcalloc(1, sizeof(struct lscpu_cxt));
235 }
236
237 static 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);
280
281 lscpu_free_caches(cxt->ecaches, cxt->necaches);
282 lscpu_free_caches(cxt->caches, cxt->ncaches);
283
284 free(cxt);
285 }
286
287 static 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 }
308
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
316 static char *get_cell_data(
317 struct lscpu_cxt *cxt,
318 struct lscpu_cpu *cpu, int col,
319 char *buf, size_t bufsz)
320 {
321 size_t i;
322
323 *buf = '\0';
324
325 if (!cpu->type)
326 return NULL;
327
328 switch (col) {
329 case COL_CPU_CPU:
330 snprintf(buf, bufsz, "%d", cpu->logical_id);
331 break;
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;
338 case COL_CPU_CORE:
339 fill_id(cxt, cpu, core, buf, bufsz);
340 break;
341 case COL_CPU_SOCKET:
342 fill_id(cxt, cpu, socket, buf, bufsz);
343 break;
344 case COL_CPU_CLUSTER:
345 if (cxt->is_cluster)
346 fill_id(cxt, cpu, socket, buf, bufsz);
347 break;
348 case COL_CPU_DRAWER:
349 fill_id(cxt, cpu, drawer, buf, bufsz);
350 break;
351 case COL_CPU_BOOK:
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]);
358 break;
359 case COL_CPU_CACHE:
360 {
361 const char *last = NULL;
362 char *p = buf;
363 size_t sz = bufsz;
364
365 for (i = 0; i < cxt->ncaches; i++) {
366 int x;
367 struct lscpu_cache *ca;
368 const char *name = cxt->caches[i].name;
369
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;
381 if (sz < 2)
382 return NULL;
383 *p++ = cxt->show_compatible ? ',' : ':';
384 *p = '\0';
385 sz--;
386 }
387 if (p > buf && (*(p - 1) == ',' || *(p - 1) == ':'))
388 *(p - 1) = '\0';
389 break;
390 }
391 case COL_CPU_POLARIZATION:
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);
398 break;
399 case COL_CPU_ADDRESS:
400 if (cpu->address < 0)
401 break;
402 snprintf(buf, bufsz, "%d", cpu->address);
403 break;
404 case COL_CPU_CONFIGURED:
405 if (cpu->configured < 0)
406 break;
407 if (cxt->mode == LSCPU_OUTPUT_PARSABLE)
408 snprintf(buf, bufsz, "%s",
409 cpu->configured ? _("Y") : _("N"));
410 else
411 snprintf(buf, bufsz, "%s",
412 cpu->configured ? _("yes") : _("no"));
413 break;
414 case COL_CPU_ONLINE:
415 if (!cxt->online)
416 break;
417 if (cxt->mode == LSCPU_OUTPUT_PARSABLE)
418 snprintf(buf, bufsz, "%s",
419 is_cpu_online(cxt, cpu) ? _("Y") : _("N"));
420 else
421 snprintf(buf, bufsz, "%s",
422 is_cpu_online(cxt, cpu) ? _("yes") : _("no"));
423 break;
424 case COL_CPU_MHZ:
425 if (cpu->mhz)
426 xstrncpy(buf, cpu->mhz, bufsz);
427 break;
428 case COL_CPU_MAXMHZ:
429 if (cpu->mhz_max_freq)
430 snprintf(buf, bufsz, "%.4f", cpu->mhz_max_freq);
431 break;
432 case COL_CPU_MINMHZ:
433 if (cpu->mhz_min_freq)
434 snprintf(buf, bufsz, "%.4f", cpu->mhz_min_freq);
435 break;
436 }
437 return buf;
438 }
439
440 static char *get_cell_header(
441 struct lscpu_cxt *cxt, int col,
442 char *buf, size_t bufsz)
443 {
444 *buf = '\0';
445
446 if (col == COL_CPU_CACHE) {
447 const char *last = NULL;
448 char *p = buf;
449 size_t sz = bufsz;
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;
459
460 x = snprintf(p, sz, "%s", ca->name);
461 if (x < 0 || (size_t) x >= sz)
462 return NULL;
463 sz -= x;
464 p += x;
465 if (sz < 2)
466 return NULL;
467 *p++ = cxt->show_compatible ? ',' : ':';
468 *p = '\0';
469 sz--;
470 }
471 if (p > buf && (*(p - 1) == ',' || *(p - 1) == ':'))
472 *(p - 1) = '\0';
473 if (cxt->ncaches)
474 return buf;
475 }
476 snprintf(buf, bufsz, "%s", coldescs_cpu[col].name);
477 return buf;
478 }
479
480
481 static 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 {
512 uint64_t sz = 0;
513 if (ca->name)
514 sz = lscpu_get_cache_full_size(cxt, ca->name, NULL);
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
564 /*
565 * [-C] backend
566 */
567 static void print_caches_readable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
568 {
569 size_t i;
570 struct libscols_table *tb;
571 const char *last = NULL;
572
573 scols_init_debug(0);
574
575 tb = scols_new_table();
576 if (!tb)
577 err(EXIT_FAILURE, _("failed to allocate output table"));
578 if (cxt->json) {
579 scols_table_enable_json(tb, 1);
580 scols_table_set_name(tb, "caches");
581 }
582
583 for (i = 0; i < ncols; i++) {
584 struct lscpu_coldesc *cd = &coldescs_cache[cols[i]];
585 if (!scols_table_new_column(tb, cd->name, 0, cd->flags))
586 err(EXIT_FAILURE, _("failed to allocate output column"));
587 }
588
589 /* standard caches */
590 for (i = 0; i < cxt->ncaches; i++) {
591 struct lscpu_cache *ca = &cxt->caches[i];
592
593 if (last && strcmp(last, ca->name) == 0)
594 continue;
595 last = ca->name;
596 caches_add_line(cxt, tb, ca, cols, ncols);
597 }
598
599 /* extra caches */
600 for (i = 0; i < cxt->necaches; i++) {
601 struct lscpu_cache *ca = &cxt->ecaches[i];
602
603 if (last && strcmp(last, ca->name) == 0)
604 continue;
605 last = ca->name;
606 caches_add_line(cxt, tb, ca, cols, ncols);
607 }
608
609 scols_print_table(tb);
610 scols_unref_table(tb);
611 }
612
613 /*
614 * [-p] backend, we support two parsable formats:
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 */
636 static void print_cpus_parsable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
637 {
638 char buf[BUFSIZ], *data;
639 size_t i;
640
641 /*
642 * Header
643 */
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"
647 "# starting usually from zero.\n"));
648
649 fputs("# ", stdout);
650 for (i = 0; i < ncols; i++) {
651 int col = cols[i];
652
653 if (col == COL_CPU_CACHE) {
654 if (cxt->show_compatible && !cxt->ncaches)
655 continue;
656 if (cxt->show_compatible && i != 0)
657 putchar(',');
658 }
659 if (i > 0)
660 putchar(',');
661
662 data = get_cell_header(cxt, col, buf, sizeof(buf));
663 if (data && * data && col != COL_CPU_CACHE &&
664 !coldescs_cpu[col].is_abbr) {
665 /*
666 * For normal column names use mixed case (e.g. "Socket")
667 */
668 char *p = data + 1;
669
670 while (p && *p != '\0') {
671 *p = tolower((unsigned int) *p);
672 p++;
673 }
674 }
675 fputs(data && *data ? data : "", stdout);
676 }
677 putchar('\n');
678
679 /*
680 * Data
681 */
682 for (i = 0; i < cxt->npossibles; i++) {
683 struct lscpu_cpu *cpu = cxt->cpus[i];
684 size_t c;
685
686 if (cxt->online) {
687 if (!cxt->show_offline && !is_cpu_online(cxt, cpu))
688 continue;
689 if (!cxt->show_online && is_cpu_online(cxt, cpu))
690 continue;
691 }
692 if (cxt->present && !is_cpu_present(cxt, cpu))
693 continue;
694
695 for (c = 0; c < ncols; c++) {
696 if (cxt->show_compatible && cols[c] == COL_CPU_CACHE) {
697 if (!cxt->ncaches)
698 continue;
699 if (c > 0)
700 putchar(',');
701 }
702 if (c > 0)
703 putchar(',');
704
705 data = get_cell_data(cxt, cpu, cols[c], buf, sizeof(buf));
706 fputs(data && *data ? data : "", stdout);
707 *buf = '\0';
708 }
709 putchar('\n');
710 }
711 }
712
713 /*
714 * [-e] backend
715 */
716 static void print_cpus_readable(struct lscpu_cxt *cxt, int cols[], size_t ncols)
717 {
718 size_t i;
719 char buf[BUFSIZ];
720 const char *data;
721 struct libscols_table *tb;
722
723 scols_init_debug(0);
724
725 tb = scols_new_table();
726 if (!tb)
727 err(EXIT_FAILURE, _("failed to allocate output table"));
728 if (cxt->json) {
729 scols_table_enable_json(tb, 1);
730 scols_table_set_name(tb, "cpus");
731 }
732
733 for (i = 0; i < ncols; i++) {
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))
736 err(EXIT_FAILURE, _("failed to allocate output column"));
737 }
738
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];
743
744 if (cxt->online) {
745 if (!cxt->show_offline && !is_cpu_online(cxt, cpu))
746 continue;
747 if (!cxt->show_online && is_cpu_online(cxt, cpu))
748 continue;
749 }
750
751 if (cxt->present && !is_cpu_present(cxt, cpu))
752 continue;
753
754 ln = scols_table_new_line(tb, NULL);
755 if (!ln)
756 err(EXIT_FAILURE, _("failed to allocate output line"));
757
758 for (c = 0; c < ncols; c++) {
759 data = get_cell_data(cxt, cpu, cols[c], buf, sizeof(buf));
760 if (!data || !*data)
761 data = "-";
762 if (scols_line_set_data(ln, c, data))
763 err(EXIT_FAILURE, _("failed to add output data"));
764 }
765 }
766
767 scols_print_table(tb);
768 scols_unref_table(tb);
769 }
770
771 static struct libscols_line *
772 __attribute__ ((__format__(printf, 4, 5)))
773 add_summary_sprint(struct libscols_table *tb,
774 struct libscols_line *sec,
775 const char *txt,
776 const char *fmt,
777 ...)
778 {
779 struct libscols_line *ln;
780 va_list args;
781
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);
787 if (!ln)
788 err(EXIT_FAILURE, _("failed to allocate output line"));
789
790 /* description column */
791 if (txt && scols_line_set_data(ln, 0, txt))
792 err(EXIT_FAILURE, _("failed to add output data"));
793
794 /* data column */
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 }
804
805 return ln;
806 }
807
808 #define add_summary_e(tb, sec, txt) add_summary_sprint(tb, sec, txt, NULL)
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)
812
813 static void
814 print_cpuset(struct lscpu_cxt *cxt,
815 struct libscols_table *tb,
816 struct libscols_line *sec,
817 const char *key, cpu_set_t *set)
818 {
819 size_t setbuflen = 7 * cxt->maxcpus;
820 char setbuf[setbuflen], *p;
821
822 assert(set);
823 assert(key);
824 assert(tb);
825 assert(cxt);
826
827 if (cxt->hex) {
828 p = cpumask_create(setbuf, setbuflen, set, cxt->setsize);
829 add_summary_s(tb, sec, key, p);
830 } else {
831 p = cpulist_create(setbuf, setbuflen, set, cxt->setsize);
832 add_summary_s(tb, sec, key, p);
833 }
834 }
835
836 static void
837 print_summary_cputype(struct lscpu_cxt *cxt,
838 struct lscpu_cputype *ct,
839 struct libscols_table *tb,
840 struct libscols_line *sec)
841 {
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);
846 if (ct->machinetype)
847 add_summary_s(tb, sec, _("Machine type:"), ct->machinetype);
848 if (ct->family)
849 add_summary_s(tb, sec, _("CPU family:"), ct->family);
850 if (ct->model || ct->revision)
851 add_summary_s(tb, sec, _("Model:"), ct->revision ? ct->revision : ct->model);
852
853 add_summary_n(tb, sec, _("Thread(s) per core:"), ct->nthreads_per_core);
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);
858
859 if (ct->nbooks) {
860 add_summary_n(tb, sec, _("Socket(s) per book:"), ct->nsockets_per_book);
861 if (ct->ndrawers_per_system || ct->ndrawers) {
862 add_summary_n(tb, sec, _("Book(s) per drawer:"), ct->nbooks_per_drawer);
863 add_summary_n(tb, sec, _("Drawer(s):"), ct->ndrawers_per_system ?: ct->ndrawers);
864 } else
865 add_summary_n(tb, sec, _("Book(s):"), ct->nbooks_per_drawer ?: ct->nbooks);
866 } else {
867 if (cxt->is_cluster) {
868 if (ct->nr_socket_on_cluster > 0)
869 add_summary_n(tb, sec, _("Socket(s):"), ct->nr_socket_on_cluster);
870 else
871 add_summary_s(tb, sec, _("Socket(s):"), "-");
872
873 add_summary_n(tb, sec, _("Cluster(s):"),
874 ct->nsockets_per_book ?: ct->nsockets);
875 } else
876 add_summary_n(tb, sec, _("Socket(s):"),
877 ct->nsockets_per_book ?: ct->nsockets);
878 }
879
880 if (ct->stepping)
881 add_summary_s(tb, sec, _("Stepping:"), ct->stepping);
882 if (ct->freqboost >= 0)
883 add_summary_s(tb, sec, _("Frequency boost:"), ct->freqboost ?
884 _("enabled") : _("disabled"));
885
886 /* s390 -- from the first CPU where is dynamic/static MHz */
887 if (ct->dynamic_mhz)
888 add_summary_s(tb, sec, _("CPU dynamic MHz:"), ct->dynamic_mhz);
889 if (ct->static_mhz)
890 add_summary_s(tb, sec, _("CPU static MHz:"), ct->static_mhz);
891
892 if (ct->has_freq) {
893 add_summary_x(tb, sec, _("CPU max MHz:"), "%.4f", lsblk_cputype_get_maxmhz(cxt, ct));
894 add_summary_x(tb, sec, _("CPU min MHz:"), "%.4f", lsblk_cputype_get_minmhz(cxt, ct));
895 }
896 if (ct->bogomips)
897 add_summary_s(tb, sec, _("BogoMIPS:"), ct->bogomips);
898
899 if (ct->dispatching >= 0)
900 add_summary_s(tb, sec, _("Dispatching mode:"), _(disp_modes[ct->dispatching]));
901
902 if (ct->physsockets) {
903 add_summary_n(tb, sec, _("Physical sockets:"), ct->physsockets);
904 add_summary_n(tb, sec, _("Physical chips:"), ct->physchips);
905 add_summary_n(tb, sec, _("Physical cores/chip:"), ct->physcoresperchip);
906 }
907
908 if (ct->flags)
909 add_summary_s(tb, sec, _("Flags:"), ct->flags);
910 }
911
912 /*
913 * default output
914 */
915 static void print_summary(struct lscpu_cxt *cxt)
916 {
917 struct lscpu_cputype *ct;
918 char field[256];
919 size_t i = 0;
920 struct libscols_table *tb;
921 struct libscols_line *sec = NULL;
922 int hdr_caches = 0;
923
924 scols_init_debug(0);
925
926 tb = scols_new_table();
927 if (!tb)
928 err(EXIT_FAILURE, _("failed to allocate output table"));
929
930 scols_table_enable_noheadings(tb, 1);
931 if (cxt->json) {
932 scols_table_enable_json(tb, 1);
933 scols_table_set_name(tb, "lscpu");
934 } else if (is_term) {
935 struct libscols_symbols *sy = scols_new_symbols();
936
937 if (!sy)
938 err_oom();
939 scols_symbols_set_branch(sy, " ");
940 scols_symbols_set_vertical(sy, " ");
941 scols_symbols_set_right(sy, " ");
942 scols_table_set_symbols(tb, sy);
943 scols_unref_symbols(sy);
944 }
945
946 if (scols_table_new_column(tb, "field", 0, is_term ? SCOLS_FL_TREE : 0) == NULL ||
947 scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES | SCOLS_FL_WRAP) == NULL)
948 err(EXIT_FAILURE, _("failed to initialize output column"));
949
950 ct = lscpu_cputype_get_default(cxt);
951
952 /* Section: architecture */
953 if (cxt->arch)
954 sec = add_summary_s(tb, NULL, _("Architecture:"), cxt->arch->name);
955 if (cxt->arch && (cxt->arch->bit32 || cxt->arch->bit64)) {
956 char buf[32], *p = buf;
957
958 if (cxt->arch->bit32) {
959 strcpy(p, "32-bit, ");
960 p += 8;
961 }
962 if (cxt->arch->bit64) {
963 strcpy(p, "64-bit, ");
964 p += 8;
965 }
966 *(p - 2) = '\0';
967 add_summary_s(tb, sec, _("CPU op-mode(s):"), buf);
968 }
969 if (ct->addrsz)
970 add_summary_s(tb, sec, _("Address sizes:"), ct->addrsz);
971 #if !defined(WORDS_BIGENDIAN)
972 add_summary_s(tb, sec, _("Byte Order:"), "Little Endian");
973 #else
974 add_summary_s(tb, sec, _("Byte Order:"), "Big Endian");
975 #endif
976
977 /* Section: CPU lists */
978 sec = add_summary_n(tb, NULL, _("CPU(s):"), cxt->npresents);
979
980 if (cxt->online)
981 print_cpuset(cxt, tb, sec,
982 cxt->hex ? _("On-line CPU(s) mask:") :
983 _("On-line CPU(s) list:"),
984 cxt->online);
985
986 if (cxt->online && cxt->nonlines != cxt->npresents) {
987 cpu_set_t *set;
988
989 /* Linux kernel provides cpuset of off-line CPUs that contains
990 * all configured CPUs (see /sys/devices/system/cpu/offline),
991 * but want to print real (present in system) off-line CPUs only.
992 */
993 set = cpuset_alloc(cxt->maxcpus, NULL, NULL);
994 if (!set)
995 err(EXIT_FAILURE, _("failed to callocate cpu set"));
996 CPU_ZERO_S(cxt->setsize, set);
997 for (i = 0; i < cxt->npossibles; i++) {
998 struct lscpu_cpu *cpu = cxt->cpus[i];
999
1000 if (cpu && is_cpu_present(cxt, cpu) && !is_cpu_online(cxt, cpu))
1001 CPU_SET_S(cpu->logical_id, cxt->setsize, set);
1002 }
1003 print_cpuset(cxt, tb, sec,
1004 cxt->hex ? _("Off-line CPU(s) mask:") :
1005 _("Off-line CPU(s) list:"), set);
1006 cpuset_free(set);
1007 }
1008 sec = NULL;
1009
1010 /* Section: cpu type description */
1011 if (ct->vendor)
1012 sec = add_summary_s(tb, NULL, _("Vendor ID:"), ct->vendor);
1013 if (ct->bios_vendor)
1014 add_summary_s(tb, sec, _("BIOS Vendor ID:"), ct->bios_vendor);
1015
1016 for (i = 0; i < cxt->ncputypes; i++)
1017 print_summary_cputype(cxt, cxt->cputypes[i], tb, sec);
1018 sec = NULL;
1019
1020 /* Section: vitualiazation */
1021 if (cxt->virt) {
1022 sec = add_summary_e(tb, NULL, _("Virtualization features:"));
1023 if (cxt->virt->cpuflag && !strcmp(cxt->virt->cpuflag, "svm"))
1024 add_summary_s(tb, sec, _("Virtualization:"), "AMD-V");
1025 else if (cxt->virt->cpuflag && !strcmp(cxt->virt->cpuflag, "vmx"))
1026 add_summary_s(tb, sec, _("Virtualization:"), "VT-x");
1027
1028 if (cxt->virt->hypervisor)
1029 add_summary_s(tb, sec, _("Hypervisor:"), cxt->virt->hypervisor);
1030 if (cxt->virt->vendor) {
1031 add_summary_s(tb, sec, _("Hypervisor vendor:"), hv_vendors[cxt->virt->vendor]);
1032 add_summary_s(tb, sec, _("Virtualization type:"), _(virt_types[cxt->virt->type]));
1033 }
1034 sec = NULL;
1035 }
1036
1037 /* Section: caches */
1038 if (cxt->ncaches) {
1039 const char *last = NULL;
1040
1041 /* The caches are sorted by name, cxt->caches[] may contains
1042 * multiple instances for the same name.
1043 */
1044 for (i = 0; i < cxt->ncaches; i++) {
1045 const char *name = cxt->caches[i].name;
1046 uint64_t sz;
1047 int n = 0;
1048
1049 if (last && strcmp(last, name) == 0)
1050 continue;
1051 sz = lscpu_get_cache_full_size(cxt, name, &n);
1052 if (!sz)
1053 continue;
1054 if (!hdr_caches) {
1055 sec = add_summary_e(tb, NULL, _("Caches (sum of all):"));
1056 hdr_caches = 1;
1057 }
1058
1059 snprintf(field, sizeof(field), is_term ? _("%s:") : _("%s cache:"), name);
1060 if (cxt->bytes)
1061 add_summary_sprint(tb, sec, field,
1062 P_("%" PRIu64 " (%d instance)",
1063 "%" PRIu64 " (%d instances)", n),
1064 sz, n);
1065 else {
1066 char *tmp = size_to_human_string(
1067 SIZE_SUFFIX_3LETTER |
1068 SIZE_SUFFIX_SPACE,
1069 sz);
1070 add_summary_sprint(tb, sec, field,
1071 P_("%s (%d instance)",
1072 "%s (%d instances)", n),
1073 tmp, n);
1074 free(tmp);
1075 }
1076 last = name;
1077 }
1078 }
1079
1080 for (i = 0; i < cxt->necaches; i++) {
1081 struct lscpu_cache *ca = &cxt->ecaches[i];
1082
1083 if (ca->size == 0)
1084 continue;
1085 if (!hdr_caches) {
1086 sec = add_summary_e(tb, NULL, _("Caches:"));
1087 hdr_caches = 1;
1088 }
1089 snprintf(field, sizeof(field), is_term ? _("%s:") : _("%s cache:"), ca->name);
1090 if (cxt->bytes)
1091 add_summary_x(tb, sec, field, "%" PRIu64, ca->size);
1092 else {
1093 char *tmp = size_to_human_string(
1094 SIZE_SUFFIX_3LETTER |
1095 SIZE_SUFFIX_SPACE,
1096 ca->size);
1097 add_summary_s(tb, sec, field, tmp);
1098 free(tmp);
1099 }
1100 }
1101 sec = NULL;
1102
1103 /* Section: NUMA modes */
1104 if (cxt->nnodes) {
1105 sec = add_summary_e(tb, NULL, _("NUMA:"));
1106
1107 add_summary_n(tb, sec,_("NUMA node(s):"), cxt->nnodes);
1108 for (i = 0; i < cxt->nnodes; i++) {
1109 snprintf(field, sizeof(field), _("NUMA node%d CPU(s):"), cxt->idx2nodenum[i]);
1110 print_cpuset(cxt, tb, sec, field, cxt->nodemaps[i]);
1111 }
1112 sec = NULL;
1113 }
1114
1115 /* Section: Vulnerabilities */
1116 if (cxt->vuls) {
1117 sec = add_summary_e(tb, NULL, _("Vulnerabilities:"));
1118
1119 for (i = 0; i < cxt->nvuls; i++) {
1120 snprintf(field, sizeof(field), is_term ?
1121 _("%s:") : _("Vulnerability %s:"), cxt->vuls[i].name);
1122 add_summary_s(tb, sec, field, cxt->vuls[i].text);
1123 }
1124 sec = NULL;
1125 }
1126 scols_print_table(tb);
1127 scols_unref_table(tb);
1128 }
1129
1130 static void __attribute__((__noreturn__)) usage(void)
1131 {
1132 FILE *out = stdout;
1133 size_t i;
1134
1135 fputs(USAGE_HEADER, out);
1136 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
1137
1138 fputs(USAGE_SEPARATOR, out);
1139 fputs(_("Display information about the CPU architecture.\n"), out);
1140
1141 fputs(USAGE_OPTIONS, out);
1142 fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out);
1143 fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out);
1144 fputs(_(" -B, --bytes print sizes in bytes rather than in human readable format\n"), out);
1145 fputs(_(" -C, --caches[=<list>] info about caches in extended readable format\n"), out);
1146 fputs(_(" -c, --offline print offline CPUs only\n"), out);
1147 fputs(_(" -J, --json use JSON for default or extended format\n"), out);
1148 fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out);
1149 fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out);
1150 fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out);
1151 fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out);
1152 fputs(_(" -y, --physical print physical instead of logical IDs\n"), out);
1153 fputs(_(" --output-all print all available columns for -e, -p or -C\n"), out);
1154 fputs(USAGE_SEPARATOR, out);
1155 printf(USAGE_HELP_OPTIONS(25));
1156
1157 fputs(_("\nAvailable output columns for -e or -p:\n"), out);
1158 for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++)
1159 fprintf(out, " %13s %s\n", coldescs_cpu[i].name, _(coldescs_cpu[i].help));
1160
1161 fputs(_("\nAvailable output columns for -C:\n"), out);
1162 for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++)
1163 fprintf(out, " %13s %s\n", coldescs_cache[i].name, _(coldescs_cache[i].help));
1164
1165 printf(USAGE_MAN_TAIL("lscpu(1)"));
1166
1167 exit(EXIT_SUCCESS);
1168 }
1169
1170 int main(int argc, char *argv[])
1171 {
1172 struct lscpu_cxt *cxt;
1173 int c, all = 0;
1174 int columns[ARRAY_SIZE(coldescs_cpu)];
1175 int cpu_modifier_specified = 0;
1176 char *outarg = NULL;
1177 size_t i, ncolumns = 0;
1178 enum {
1179 OPT_OUTPUT_ALL = CHAR_MAX + 1,
1180 };
1181 static const struct option longopts[] = {
1182 { "all", no_argument, NULL, 'a' },
1183 { "online", no_argument, NULL, 'b' },
1184 { "bytes", no_argument, NULL, 'B' },
1185 { "caches", optional_argument, NULL, 'C' },
1186 { "offline", no_argument, NULL, 'c' },
1187 { "help", no_argument, NULL, 'h' },
1188 { "extended", optional_argument, NULL, 'e' },
1189 { "json", no_argument, NULL, 'J' },
1190 { "parse", optional_argument, NULL, 'p' },
1191 { "sysroot", required_argument, NULL, 's' },
1192 { "physical", no_argument, NULL, 'y' },
1193 { "hex", no_argument, NULL, 'x' },
1194 { "version", no_argument, NULL, 'V' },
1195 { "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
1196 { NULL, 0, NULL, 0 }
1197 };
1198
1199 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1200 { 'C','e','p' },
1201 { 'a','b','c' },
1202 { 0 }
1203 };
1204 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1205
1206 setlocale(LC_ALL, "");
1207 bindtextdomain(PACKAGE, LOCALEDIR);
1208 textdomain(PACKAGE);
1209 close_stdout_atexit();
1210
1211 cxt = lscpu_new_context();
1212
1213 while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::s:xyV", longopts, NULL)) != -1) {
1214
1215 err_exclusive_options(c, longopts, excl, excl_st);
1216
1217 switch (c) {
1218 case 'a':
1219 cxt->show_online = cxt->show_offline = 1;
1220 cpu_modifier_specified = 1;
1221 break;
1222 case 'B':
1223 cxt->bytes = 1;
1224 break;
1225 case 'b':
1226 cxt->show_online = 1;
1227 cpu_modifier_specified = 1;
1228 break;
1229 case 'c':
1230 cxt->show_offline = 1;
1231 cpu_modifier_specified = 1;
1232 break;
1233 case 'C':
1234 if (optarg) {
1235 if (*optarg == '=')
1236 optarg++;
1237 outarg = optarg;
1238 }
1239 cxt->mode = LSCPU_OUTPUT_CACHES;
1240 break;
1241 case 'J':
1242 cxt->json = 1;
1243 break;
1244 case 'p':
1245 case 'e':
1246 if (optarg) {
1247 if (*optarg == '=')
1248 optarg++;
1249 outarg = optarg;
1250 }
1251 cxt->mode = c == 'p' ? LSCPU_OUTPUT_PARSABLE : LSCPU_OUTPUT_READABLE;
1252 break;
1253 case 's':
1254 cxt->prefix = optarg;
1255 cxt->noalive = 1;
1256 break;
1257 case 'x':
1258 cxt->hex = 1;
1259 break;
1260 case 'y':
1261 cxt->show_physical = 1;
1262 break;
1263 case OPT_OUTPUT_ALL:
1264 all = 1;
1265 break;
1266
1267 case 'h':
1268 usage();
1269 case 'V':
1270 print_version(EXIT_SUCCESS);
1271 default:
1272 errtryhelp(EXIT_FAILURE);
1273 }
1274 }
1275
1276 if (all && ncolumns == 0) {
1277 size_t maxsz = cxt->mode == LSCPU_OUTPUT_CACHES ?
1278 ARRAY_SIZE(coldescs_cache) :
1279 ARRAY_SIZE(coldescs_cpu);
1280
1281 for (i = 0; i < maxsz; i++)
1282 columns[ncolumns++] = i;
1283 }
1284
1285 if (cpu_modifier_specified && cxt->mode == LSCPU_OUTPUT_SUMMARY) {
1286 fprintf(stderr,
1287 _("%s: options --all, --online and --offline may only "
1288 "be used with options --extended or --parse.\n"),
1289 program_invocation_short_name);
1290 return EXIT_FAILURE;
1291 }
1292
1293 if (argc != optind) {
1294 warnx(_("bad usage"));
1295 errtryhelp(EXIT_FAILURE);
1296 }
1297
1298 /* set default cpu display mode if none was specified */
1299 if (!cxt->show_online && !cxt->show_offline) {
1300 cxt->show_online = 1;
1301 cxt->show_offline = cxt->mode == LSCPU_OUTPUT_READABLE ? 1 : 0;
1302 }
1303
1304 is_term = isatty(STDOUT_FILENO); /* global variable */
1305
1306 lscpu_init_debug();
1307
1308 lscpu_context_init_paths(cxt);
1309
1310 lscpu_read_cpulists(cxt);
1311 lscpu_read_cpuinfo(cxt);
1312 cxt->arch = lscpu_read_architecture(cxt);
1313
1314 lscpu_read_archext(cxt);
1315 lscpu_read_vulnerabilities(cxt);
1316 lscpu_read_numas(cxt);
1317 lscpu_read_topology(cxt);
1318
1319 lscpu_decode_arm(cxt);
1320
1321 cxt->virt = lscpu_read_virtualization(cxt);
1322
1323 switch(cxt->mode) {
1324 case LSCPU_OUTPUT_SUMMARY:
1325 print_summary(cxt);
1326 break;
1327 case LSCPU_OUTPUT_CACHES:
1328 if (!ncolumns) {
1329 columns[ncolumns++] = COL_CACHE_NAME;
1330 columns[ncolumns++] = COL_CACHE_ONESIZE;
1331 columns[ncolumns++] = COL_CACHE_ALLSIZE;
1332 columns[ncolumns++] = COL_CACHE_WAYS;
1333 columns[ncolumns++] = COL_CACHE_TYPE;
1334 columns[ncolumns++] = COL_CACHE_LEVEL;
1335 columns[ncolumns++] = COL_CACHE_SETS;
1336 columns[ncolumns++] = COL_CACHE_PHYLINE;
1337 columns[ncolumns++] = COL_CACHE_COHERENCYSIZE;
1338 }
1339 if (outarg && string_add_to_idarray(outarg, columns,
1340 ARRAY_SIZE(columns),
1341 &ncolumns, cache_column_name_to_id) < 0)
1342 return EXIT_FAILURE;
1343
1344 print_caches_readable(cxt, columns, ncolumns);
1345 break;
1346 case LSCPU_OUTPUT_READABLE:
1347 if (!ncolumns) {
1348 /* No list was given. Just print whatever is there. */
1349 struct lscpu_cputype *ct = lscpu_cputype_get_default(cxt);
1350
1351 columns[ncolumns++] = COL_CPU_CPU;
1352 if (cxt->nnodes)
1353 columns[ncolumns++] = COL_CPU_NODE;
1354 if (ct && ct->ndrawers)
1355 columns[ncolumns++] = COL_CPU_DRAWER;
1356 if (ct && ct->nbooks)
1357 columns[ncolumns++] = COL_CPU_BOOK;
1358 if (ct && ct->nsockets) {
1359 if (cxt->is_cluster)
1360 columns[ncolumns++] = COL_CPU_CLUSTER;
1361 else
1362 columns[ncolumns++] = COL_CPU_SOCKET;
1363 }
1364 if (ct && ct->ncores)
1365 columns[ncolumns++] = COL_CPU_CORE;
1366 if (cxt->ncaches)
1367 columns[ncolumns++] = COL_CPU_CACHE;
1368 if (cxt->online)
1369 columns[ncolumns++] = COL_CPU_ONLINE;
1370 if (ct && ct->has_configured)
1371 columns[ncolumns++] = COL_CPU_CONFIGURED;
1372 if (ct && ct->has_polarization)
1373 columns[ncolumns++] = COL_CPU_POLARIZATION;
1374 if (ct && ct->has_addresses)
1375 columns[ncolumns++] = COL_CPU_ADDRESS;
1376 if (ct && ct->has_freq) {
1377 columns[ncolumns++] = COL_CPU_MAXMHZ;
1378 columns[ncolumns++] = COL_CPU_MINMHZ;
1379 columns[ncolumns++] = COL_CPU_MHZ;
1380 }
1381 }
1382 if (outarg && string_add_to_idarray(outarg, columns,
1383 ARRAY_SIZE(columns),
1384 &ncolumns, cpu_column_name_to_id) < 0)
1385 return EXIT_FAILURE;
1386 print_cpus_readable(cxt, columns, ncolumns);
1387 break;
1388 case LSCPU_OUTPUT_PARSABLE:
1389 if (!ncolumns) {
1390 columns[ncolumns++] = COL_CPU_CPU;
1391 columns[ncolumns++] = COL_CPU_CORE;
1392 if (cxt->is_cluster)
1393 columns[ncolumns++] = COL_CPU_CLUSTER;
1394 else
1395 columns[ncolumns++] = COL_CPU_SOCKET;
1396 columns[ncolumns++] = COL_CPU_NODE;
1397 columns[ncolumns++] = COL_CPU_CACHE;
1398 cxt->show_compatible = 1;
1399 }
1400 if (outarg && string_add_to_idarray(outarg, columns,
1401 ARRAY_SIZE(columns),
1402 &ncolumns, cpu_column_name_to_id) < 0)
1403 return EXIT_FAILURE;
1404
1405 print_cpus_parsable(cxt, columns, ncolumns);
1406 break;
1407 }
1408
1409 lscpu_free_context(cxt);
1410
1411 return EXIT_SUCCESS;
1412 }