]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
ipcs: use unsigned type for uid/gid
[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 *
17 * You should have received a copy of the GNU General Public License
80dde62f
KZ
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5dd7507c
CQ
20 */
21
22#include <ctype.h>
23#include <dirent.h>
5dd7507c
CQ
24#include <errno.h>
25#include <fcntl.h>
26#include <getopt.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/utsname.h>
31#include <unistd.h>
32#include <stdarg.h>
39561c70
KZ
33#include <sys/types.h>
34#include <sys/stat.h>
5dd7507c 35
7e03f383 36#include "cpuset.h"
5dd7507c 37#include "nls.h"
4581647a 38#include "xalloc.h"
eb76ca98 39#include "c.h"
477251f8
KZ
40#include "strutils.h"
41#include "bitops.h"
42
5dd7507c
CQ
43
44#define CACHE_MAX 100
45
46/* /sys paths */
d50363cd 47#define _PATH_SYS_SYSTEM "/sys/devices/system"
7e03f383 48#define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu"
d50363cd 49#define _PATH_PROC_XEN "/proc/xen"
c8b64f6d 50#define _PATH_PROC_XENCAP _PATH_PROC_XEN "/capabilities"
d50363cd
KZ
51#define _PATH_PROC_CPUINFO "/proc/cpuinfo"
52#define _PATH_PROC_PCIDEVS "/proc/bus/pci/devices"
5dd7507c 53
c8b64f6d
KZ
54/* virtualization types */
55enum {
56 VIRT_NONE = 0,
57 VIRT_PARA,
58 VIRT_FULL
59};
60const char *virt_types[] = {
61 [VIRT_NONE] = N_("none"),
62 [VIRT_PARA] = N_("para"),
63 [VIRT_FULL] = N_("full")
64};
65
66/* hypervisor vendors */
67enum {
68 HYPER_NONE = 0,
69 HYPER_XEN,
70 HYPER_KVM,
99fbc877
SH
71 HYPER_MSHV,
72 HYPER_VMWARE
c8b64f6d
KZ
73};
74const char *hv_vendors[] = {
75 [HYPER_NONE] = NULL,
76 [HYPER_XEN] = "Xen",
77 [HYPER_KVM] = "KVM",
99fbc877
SH
78 [HYPER_MSHV] = "Microsoft",
79 [HYPER_VMWARE] = "VMware"
c8b64f6d
KZ
80};
81
f633ad4e 82/* CPU modes */
79e8b41a 83enum {
f633ad4e
KZ
84 MODE_32BIT = (1 << 1),
85 MODE_64BIT = (1 << 2)
79e8b41a 86};
c8b64f6d 87
e8aa16ee
KZ
88/* cache(s) description */
89struct cpu_cache {
7e03f383
KZ
90 char *name;
91 char *size;
92
93 int nsharedmaps;
94 cpu_set_t **sharedmaps;
e8aa16ee
KZ
95};
96
97/* global description */
98struct lscpu_desc {
5dd7507c
CQ
99 char *arch;
100 char *vendor;
101 char *family;
102 char *model;
c8b64f6d
KZ
103 char *virtflag; /* virtualization flag (vmx, svm) */
104 int hyper; /* hypervisor vendor ID */
105 int virtype; /* VIRT_PARA|FULL|NONE ? */
5dd7507c
CQ
106 char *mhz;
107 char *stepping;
9b8d4d5f 108 char *bogomips;
5dd7507c 109 char *flags;
79e8b41a
KZ
110 int mode; /* rm, lm or/and tm */
111
7e03f383 112 int ncpus; /* number of CPUs */
aac1e59e 113 cpu_set_t *online; /* mask with online CPUs */
7e03f383
KZ
114
115 int nnodes; /* number of NUMA modes */
116 cpu_set_t **nodemaps; /* array with NUMA nodes */
117
56baaa4e
HC
118 /* books -- based on book_siblings (internal kernel map of cpuX's
119 * hardware threads within the same book */
120 int nbooks; /* number of all online books */
121 cpu_set_t **bookmaps; /* unique book_siblings */
122
7e03f383
KZ
123 /* sockets -- based on core_siblings (internal kernel map of cpuX's
124 * hardware threads within the same physical_package_id (socket)) */
e282eec2 125 int nsockets; /* number of all online sockets */
7e03f383
KZ
126 cpu_set_t **socketmaps; /* unique core_siblings */
127
128 /* cores -- based on thread_siblings (internel kernel map of cpuX's
129 * hardware threads within the same core as cpuX) */
e282eec2 130 int ncores; /* number of all online cores */
7e03f383
KZ
131 cpu_set_t **coremaps; /* unique thread_siblings */
132
e282eec2 133 int nthreads; /* number of online threads */
7e03f383
KZ
134
135 int ncaches;
136 struct cpu_cache *caches;
5dd7507c
CQ
137};
138
d50363cd
KZ
139static size_t sysrootlen;
140static char pathbuf[PATH_MAX];
7e03f383 141static int maxcpus; /* size in bits of kernel cpu mask */
5dd7507c 142
aac1e59e
KZ
143#define is_cpu_online(_d, _cpu) \
144 ((_d) && (_d)->online ? \
145 CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0)
146
d50363cd
KZ
147static FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
148 __attribute__ ((__format__ (__printf__, 3, 4)));
ef173bde
KZ
149static void path_getstr(char *result, size_t len, const char *path, ...)
150 __attribute__ ((__format__ (__printf__, 3, 4)));
151static int path_getnum(const char *path, ...)
152 __attribute__ ((__format__ (__printf__, 1, 2)));
5dd7507c
CQ
153static int path_exist(const char *path, ...)
154 __attribute__ ((__format__ (__printf__, 1, 2)));
7e03f383 155static cpu_set_t *path_cpuset(const char *path, ...)
5dd7507c
CQ
156 __attribute__ ((__format__ (__printf__, 1, 2)));
157
477251f8
KZ
158/*
159 * Parsable output
160 */
161enum {
162 COL_CPU,
163 COL_CORE,
164 COL_SOCKET,
165 COL_NODE,
166 COL_BOOK,
167 COL_CACHE
168};
169
170static const char *colnames[] =
171{
172 [COL_CPU] = "CPU",
173 [COL_CORE] = "Core",
174 [COL_SOCKET] = "Socket",
175 [COL_NODE] = "Node",
176 [COL_BOOK] = "Book",
177 [COL_CACHE] = "Cache"
178};
179
180
181static int column_name_to_id(const char *name, size_t namesz)
182{
329fd1c3 183 size_t i;
477251f8
KZ
184
185 for (i = 0; i < ARRAY_SIZE(colnames); i++) {
186 const char *cn = colnames[i];
187
188 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
189 return i;
190 }
191 warnx(_("unknown column: %s"), name);
192 return -1;
193}
194
d50363cd
KZ
195static const char *
196path_vcreate(const char *path, va_list ap)
197{
198 if (sysrootlen)
199 vsnprintf(pathbuf + sysrootlen,
200 sizeof(pathbuf) - sysrootlen, path, ap);
201 else
202 vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
203 return pathbuf;
204}
205
5dd7507c 206static FILE *
d50363cd 207path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
5dd7507c 208{
d50363cd
KZ
209 FILE *f;
210 const char *p = path_vcreate(path, ap);
211
212 f = fopen(p, mode);
213 if (!f && exit_on_error)
214 err(EXIT_FAILURE, _("error: cannot open %s"), p);
215 return f;
5dd7507c
CQ
216}
217
218static FILE *
d50363cd 219path_fopen(const char *mode, int exit_on_error, const char *path, ...)
5dd7507c 220{
d50363cd
KZ
221 FILE *fd;
222 va_list ap;
223
224 va_start(ap, path);
fe81c7d1 225 fd = path_vfopen(mode, exit_on_error, path, ap);
d50363cd
KZ
226 va_end(ap);
227
228 return fd;
5dd7507c
CQ
229}
230
231static void
ef173bde
KZ
232path_getstr(char *result, size_t len, const char *path, ...)
233{
234 FILE *fd;
235 va_list ap;
236
237 va_start(ap, path);
d50363cd 238 fd = path_vfopen("r", 1, path, ap);
ef173bde
KZ
239 va_end(ap);
240
241 if (!fgets(result, len, fd))
242 err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
243 fclose(fd);
244
245 len = strlen(result);
246 if (result[len - 1] == '\n')
247 result[len - 1] = '\0';
248}
249
250static int
251path_getnum(const char *path, ...)
5dd7507c
CQ
252{
253 FILE *fd;
254 va_list ap;
ef173bde 255 int result;
5dd7507c
CQ
256
257 va_start(ap, path);
d50363cd 258 fd = path_vfopen("r", 1, path, ap);
5dd7507c
CQ
259 va_end(ap);
260
ef173bde 261 if (fscanf(fd, "%d", &result) != 1) {
5dd7507c 262 if (ferror(fd))
d50363cd 263 err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
5dd7507c 264 else
d50363cd 265 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
5dd7507c
CQ
266 }
267 fclose(fd);
ef173bde 268 return result;
5dd7507c
CQ
269}
270
271static int
272path_exist(const char *path, ...)
273{
274 va_list ap;
d50363cd 275 const char *p;
5dd7507c
CQ
276
277 va_start(ap, path);
d50363cd 278 p = path_vcreate(path, ap);
5dd7507c
CQ
279 va_end(ap);
280
d50363cd 281 return access(p, F_OK) == 0;
5dd7507c
CQ
282}
283
7e03f383 284static cpu_set_t *
aac1e59e 285path_cpuparse(int islist, const char *path, va_list ap)
5dd7507c 286{
7e03f383 287 FILE *fd;
7e03f383
KZ
288 cpu_set_t *set;
289 size_t setsize, len = maxcpus * 7;
290 char buf[len];
5dd7507c 291
7e03f383 292 fd = path_vfopen("r", 1, path, ap);
5dd7507c 293
7e03f383
KZ
294 if (!fgets(buf, len, fd))
295 err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
296 fclose(fd);
5dd7507c 297
7e03f383
KZ
298 len = strlen(buf);
299 if (buf[len - 1] == '\n')
300 buf[len - 1] = '\0';
301
302 set = cpuset_alloc(maxcpus, &setsize, NULL);
303 if (!set)
304 err(EXIT_FAILURE, _("failed to callocate cpu set"));
305
aac1e59e
KZ
306 if (islist) {
307 if (cpulist_parse(buf, set, setsize))
308 errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
309 } else {
310 if (cpumask_parse(buf, set, setsize))
311 errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
312 }
313 return set;
314}
315
316static cpu_set_t *
317path_cpuset(const char *path, ...)
318{
319 va_list ap;
320 cpu_set_t *set;
321
322 va_start(ap, path);
323 set = path_cpuparse(0, path, ap);
324 va_end(ap);
325
326 return set;
327}
328
329static cpu_set_t *
330path_cpulist(const char *path, ...)
331{
332 va_list ap;
333 cpu_set_t *set;
334
335 va_start(ap, path);
336 set = path_cpuparse(1, path, ap);
337 va_end(ap);
7e03f383
KZ
338
339 return set;
5dd7507c
CQ
340}
341
342/* Lookup a pattern and get the value from cpuinfo.
343 * Format is:
344 *
345 * "<pattern> : <key>"
346 */
347int lookup(char *line, char *pattern, char **value)
348{
349 char *p, *v;
350 int len = strlen(pattern);
351
352 if (!*line)
353 return 0;
354
355 /* pattern */
356 if (strncmp(line, pattern, len))
357 return 0;
358
359 /* white spaces */
360 for (p = line + len; isspace(*p); p++);
361
362 /* separator */
363 if (*p != ':')
364 return 0;
365
366 /* white spaces */
367 for (++p; isspace(*p); p++);
368
369 /* value */
370 if (!*p)
371 return 0;
372 v = p;
373
374 /* end of value */
375 len = strlen(line) - 1;
376 for (p = line + len; isspace(*(p-1)); p--);
377 *p = '\0';
378
379 *value = xstrdup(v);
380 return 1;
381}
382
f633ad4e
KZ
383/* Don't init the mode for platforms where we are not able to
384 * detect that CPU supports 64-bit mode.
385 */
386static int
387init_mode(void)
388{
389 int m = 0;
390
4e740fd8
KZ
391 if (sysrootlen)
392 /* reading info from any /{sys,proc} dump, don't mix it with
393 * information about our real CPU */
394 return 0;
395
f633ad4e
KZ
396#if defined(__alpha__) || defined(__ia64__)
397 m |= MODE_64BIT; /* 64bit platforms only */
398#endif
399 /* platforms with 64bit flag in /proc/cpuinfo, define
400 * 32bit default here */
401#if defined(__i386__) || defined(__x86_64__) || \
c487c90c 402 defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__)
f633ad4e
KZ
403 m |= MODE_32BIT;
404#endif
405 return m;
406}
407
5dd7507c 408static void
e8aa16ee 409read_basicinfo(struct lscpu_desc *desc)
5dd7507c 410{
d50363cd 411 FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO);
5dd7507c
CQ
412 char buf[BUFSIZ];
413 struct utsname utsbuf;
414
415 /* architecture */
416 if (uname(&utsbuf) == -1)
417 err(EXIT_FAILURE, _("error: uname failed"));
e8aa16ee 418 desc->arch = xstrdup(utsbuf.machine);
5dd7507c
CQ
419
420 /* count CPU(s) */
7e03f383
KZ
421 while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d", desc->ncpus))
422 desc->ncpus++;
5dd7507c
CQ
423
424 /* details */
425 while (fgets(buf, sizeof(buf), fp) != NULL) {
e8aa16ee
KZ
426 if (lookup(buf, "vendor", &desc->vendor)) ;
427 else if (lookup(buf, "vendor_id", &desc->vendor)) ;
e8aa16ee
KZ
428 else if (lookup(buf, "family", &desc->family)) ;
429 else if (lookup(buf, "cpu family", &desc->family)) ;
430 else if (lookup(buf, "model", &desc->model)) ;
431 else if (lookup(buf, "stepping", &desc->stepping)) ;
432 else if (lookup(buf, "cpu MHz", &desc->mhz)) ;
f633ad4e
KZ
433 else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */
434 else if (lookup(buf, "features", &desc->flags)) ; /* s390 */
c487c90c 435 else if (lookup(buf, "type", &desc->flags)) ; /* sparc64 */
9b8d4d5f 436 else if (lookup(buf, "bogomips", &desc->bogomips)) ;
1f5b7d4b
HC
437 /* S390 */
438 else if (lookup(buf, "bogomips per cpu", &desc->bogomips)) ;
5dd7507c
CQ
439 else
440 continue;
441 }
c8b64f6d 442
f633ad4e
KZ
443 desc->mode = init_mode();
444
e8aa16ee
KZ
445 if (desc->flags) {
446 snprintf(buf, sizeof(buf), " %s ", desc->flags);
c8b64f6d 447 if (strstr(buf, " svm "))
e8aa16ee 448 desc->virtflag = strdup("svm");
c8b64f6d 449 else if (strstr(buf, " vmx "))
e8aa16ee 450 desc->virtflag = strdup("vmx");
79e8b41a 451 if (strstr(buf, " lm "))
f633ad4e
KZ
452 desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */
453 if (strstr(buf, " zarch "))
454 desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */
c487c90c
KZ
455 if (strstr(buf, " sun4v ") || strstr(buf, " sun4u "))
456 desc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */
c8b64f6d
KZ
457 }
458
5dd7507c 459 fclose(fp);
7e03f383
KZ
460
461 if (path_exist(_PATH_SYS_SYSTEM "/cpu/kernel_max"))
3d6e5c35
GR
462 /* note that kernel_max is maximum index [NR_CPUS-1] */
463 maxcpus = path_getnum(_PATH_SYS_SYSTEM "/cpu/kernel_max") + 1;
7e03f383
KZ
464
465 else if (!sysrootlen)
466 /* the root is '/' so we are working with data from the current kernel */
467 maxcpus = get_max_number_of_cpus();
468 else
d0bb6987 469 /* we are reading some /sys snapshot instead of the real /sys,
7e03f383
KZ
470 * let's use any crazy number... */
471 maxcpus = desc->ncpus > 2048 ? desc->ncpus : 2048;
aac1e59e
KZ
472
473 /* get mask for online CPUs */
e282eec2
KZ
474 if (path_exist(_PATH_SYS_SYSTEM "/cpu/online")) {
475 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
5d4ba40d 476 desc->online = path_cpulist(_PATH_SYS_SYSTEM "/cpu/online");
e282eec2
KZ
477 desc->nthreads = CPU_COUNT_S(setsize, desc->online);
478 }
5dd7507c
CQ
479}
480
c8b64f6d
KZ
481static int
482has_pci_device(int vendor, int device)
483{
484 FILE *f;
485 int num, fn, ven, dev;
486 int res = 1;
487
d50363cd 488 f = path_fopen("r", 0, _PATH_PROC_PCIDEVS);
c8b64f6d
KZ
489 if (!f)
490 return 0;
491
492 /* for more details about bus/pci/devices format see
493 * drivers/pci/proc.c in linux kernel
494 */
495 while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]",
496 &num, &fn, &ven, &dev) == 4) {
497
498 if (ven == vendor && dev == device)
499 goto found;
500 }
501
502 res = 0;
503found:
504 fclose(f);
505 return res;
506}
507
508#if defined(__x86_64__) || defined(__i386__)
509
510/*
511 * This CPUID leaf returns the information about the hypervisor.
512 * EAX : maximum input value for CPUID supported by the hypervisor.
513 * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware.
514 */
515#define HYPERVISOR_INFO_LEAF 0x40000000
516
517static inline void
518cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
519 unsigned int *ecx, unsigned int *edx)
520{
c9239f23
MF
521 __asm__(
522#if defined(__PIC__) && defined(__i386__)
523 /* x86 PIC cannot clobber ebx -- gcc bitches */
524 "pushl %%ebx;"
525 "cpuid;"
526 "movl %%ebx, %%esi;"
527 "popl %%ebx;"
528 : "=S" (*ebx),
529#else
530 "cpuid;"
531 : "=b" (*ebx),
532#endif
533 "=a" (*eax),
c8b64f6d
KZ
534 "=c" (*ecx),
535 "=d" (*edx)
bc54770d 536 : "1" (op), "c"(0));
c8b64f6d
KZ
537}
538
539static void
e8aa16ee 540read_hypervisor_cpuid(struct lscpu_desc *desc)
c8b64f6d
KZ
541{
542 unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
543 char hyper_vendor_id[13];
544
545 memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id));
546
547 cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx);
548 memcpy(hyper_vendor_id + 0, &ebx, 4);
549 memcpy(hyper_vendor_id + 4, &ecx, 4);
550 memcpy(hyper_vendor_id + 8, &edx, 4);
551 hyper_vendor_id[12] = '\0';
552
553 if (!hyper_vendor_id[0])
554 return;
555
556 if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12))
e8aa16ee 557 desc->hyper = HYPER_XEN;
c8b64f6d 558 else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9))
e8aa16ee 559 desc->hyper = HYPER_KVM;
c8b64f6d 560 else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12))
e8aa16ee 561 desc->hyper = HYPER_MSHV;
99fbc877
SH
562 else if (!strncmp("VMwareVMware", hyper_vendor_id, 12))
563 desc->hyper = HYPER_VMWARE;
c8b64f6d
KZ
564}
565
566#else /* ! __x86_64__ */
567static void
e8aa16ee 568read_hypervisor_cpuid(struct lscpu_desc *desc)
c8b64f6d
KZ
569{
570}
571#endif
572
573static void
e8aa16ee 574read_hypervisor(struct lscpu_desc *desc)
c8b64f6d 575{
e8aa16ee 576 read_hypervisor_cpuid(desc);
c8b64f6d 577
e8aa16ee 578 if (desc->hyper)
c8b64f6d 579 /* hvm */
e8aa16ee 580 desc->virtype = VIRT_FULL;
c8b64f6d 581
d50363cd 582 else if (path_exist(_PATH_PROC_XEN)) {
c8b64f6d 583 /* Xen para-virt or dom0 */
d50363cd 584 FILE *fd = path_fopen("r", 0, _PATH_PROC_XENCAP);
c8b64f6d
KZ
585 int dom0 = 0;
586
587 if (fd) {
588 char buf[256];
589
590 if (fscanf(fd, "%s", buf) == 1 &&
591 !strcmp(buf, "control_d"))
592 dom0 = 1;
593 fclose(fd);
594 }
e8aa16ee
KZ
595 desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA;
596 desc->hyper = HYPER_XEN;
c8b64f6d
KZ
597
598 } else if (has_pci_device(0x5853, 0x0001)) {
599 /* Xen full-virt on non-x86_64 */
e8aa16ee
KZ
600 desc->hyper = HYPER_XEN;
601 desc->virtype = VIRT_FULL;
c8b64f6d
KZ
602 }
603}
604
7e03f383
KZ
605/* add @set to the @ary, unnecesary set is deallocated. */
606static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set)
607{
608 int i;
609 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
610
611 if (!ary)
612 return -1;
613
614 for (i = 0; i < *items; i++) {
615 if (CPU_EQUAL_S(setsize, set, ary[i]))
616 break;
617 }
618 if (i == *items) {
619 ary[*items] = set;
620 ++*items;
621 return 0;
622 }
623 CPU_FREE(set);
624 return 1;
625}
626
5dd7507c 627static void
7e03f383 628read_topology(struct lscpu_desc *desc, int num)
5dd7507c 629{
56baaa4e 630 cpu_set_t *thread_siblings, *core_siblings, *book_siblings;
7e03f383
KZ
631
632 if (!path_exist(_PATH_SYS_CPU "/cpu%d/topology/thread_siblings", num))
633 return;
5dd7507c 634
7e03f383
KZ
635 thread_siblings = path_cpuset(_PATH_SYS_CPU
636 "/cpu%d/topology/thread_siblings", num);
637 core_siblings = path_cpuset(_PATH_SYS_CPU
638 "/cpu%d/topology/core_siblings", num);
56baaa4e
HC
639 book_siblings = NULL;
640 if (path_exist(_PATH_SYS_CPU "/cpu%d/topology/book_siblings", num)) {
641 book_siblings = path_cpuset(_PATH_SYS_CPU
642 "/cpu%d/topology/book_siblings", num);
643 }
aac1e59e
KZ
644
645 if (!desc->coremaps) {
56baaa4e 646 int nbooks, nsockets, ncores, nthreads;
7e03f383
KZ
647 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
648
649 /* threads within one core */
650 nthreads = CPU_COUNT_S(setsize, thread_siblings);
651 /* cores within one socket */
652 ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads;
56baaa4e 653 /* number of sockets within one book */
7e03f383 654 nsockets = desc->ncpus / nthreads / ncores;
56baaa4e
HC
655 /* number of books */
656 nbooks = desc->ncpus / nthreads / ncores / nsockets;
e282eec2
KZ
657
658 /* all threads, see also read_basicinfo()
659 * -- this is fallback for kernels where is not
660 * /sys/devices/system/cpu/online.
661 */
662 if (!desc->nthreads)
663 desc->nthreads = nsockets * ncores * nthreads;
08de16d0
DB
664 if (book_siblings)
665 desc->bookmaps = xcalloc(nbooks, sizeof(cpu_set_t *));
666
667 desc->socketmaps = xcalloc(nsockets, sizeof(cpu_set_t *));
668 desc->coremaps = xcalloc(ncores * nsockets, sizeof(cpu_set_t *));
7e03f383 669 }
5dd7507c 670
7e03f383
KZ
671 add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings);
672 add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings);
56baaa4e
HC
673 if (book_siblings)
674 add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings);
7e03f383
KZ
675}
676
677static int
678cachecmp(const void *a, const void *b)
679{
680 struct cpu_cache *c1 = (struct cpu_cache *) a;
681 struct cpu_cache *c2 = (struct cpu_cache *) b;
682
683 return strcmp(c2->name, c1->name);
5dd7507c
CQ
684}
685
686static void
7e03f383 687read_cache(struct lscpu_desc *desc, int num)
5dd7507c
CQ
688{
689 char buf[256];
7e03f383 690 int i;
5dd7507c 691
aac1e59e 692 if (!desc->ncaches) {
7e03f383
KZ
693 while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d/cache/index%d",
694 num, desc->ncaches))
695 desc->ncaches++;
5dd7507c 696
7e03f383
KZ
697 if (!desc->ncaches)
698 return;
5dd7507c 699
08de16d0 700 desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches));
7e03f383
KZ
701 }
702 for (i = 0; i < desc->ncaches; i++) {
703 struct cpu_cache *ca = &desc->caches[i];
704 cpu_set_t *map;
705
706 if (!ca->name) {
707 int type, level;
708
709 /* cache type */
710 path_getstr(buf, sizeof(buf),
711 _PATH_SYS_CPU "/cpu%d/cache/index%d/type",
712 num, i);
713 if (!strcmp(buf, "Data"))
714 type = 'd';
715 else if (!strcmp(buf, "Instruction"))
716 type = 'i';
717 else
718 type = 0;
719
720 /* cache level */
721 level = path_getnum(_PATH_SYS_CPU "/cpu%d/cache/index%d/level",
722 num, i);
723 if (type)
724 snprintf(buf, sizeof(buf), "L%d%c", level, type);
725 else
726 snprintf(buf, sizeof(buf), "L%d", level);
727
728 ca->name = xstrdup(buf);
729
730 /* cache size */
731 path_getstr(buf, sizeof(buf),
732 _PATH_SYS_CPU "/cpu%d/cache/index%d/size",
733 num, i);
734 ca->size = xstrdup(buf);
735 }
5dd7507c 736
7e03f383
KZ
737 /* information about how CPUs share different caches */
738 map = path_cpuset(_PATH_SYS_CPU "/cpu%d/cache/index%d/shared_cpu_map",
739 num, i);
5dd7507c 740
08de16d0
DB
741 if (!ca->sharedmaps)
742 ca->sharedmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *));
7e03f383 743 add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map);
5dd7507c
CQ
744 }
745}
746
747static void
e8aa16ee 748read_nodes(struct lscpu_desc *desc)
5dd7507c
CQ
749{
750 int i;
751
752 /* number of NUMA node */
7e03f383
KZ
753 while (path_exist(_PATH_SYS_SYSTEM "/node/node%d", desc->nnodes))
754 desc->nnodes++;
755
756 if (!desc->nnodes)
757 return;
5dd7507c 758
08de16d0 759 desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *));
5dd7507c
CQ
760
761 /* information about how nodes share different CPUs */
7e03f383
KZ
762 for (i = 0; i < desc->nnodes; i++)
763 desc->nodemaps[i] = path_cpuset(
5dd7507c
CQ
764 _PATH_SYS_SYSTEM "/node/node%d/cpumap",
765 i);
766}
767
5dd7507c 768static void
477251f8 769print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible)
5dd7507c 770{
477251f8 771 int j;
7e03f383 772 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
5dd7507c 773
477251f8
KZ
774 switch (col) {
775 case COL_CPU:
5dd7507c 776 printf("%d", i);
477251f8
KZ
777 break;
778 case COL_CORE:
7e03f383
KZ
779 for (j = 0; j < desc->ncores; j++) {
780 if (CPU_ISSET_S(i, setsize, desc->coremaps[j])) {
477251f8 781 printf("%d", j);
7e03f383
KZ
782 break;
783 }
784 }
477251f8
KZ
785 break;
786 case COL_SOCKET:
7e03f383
KZ
787 for (j = 0; j < desc->nsockets; j++) {
788 if (CPU_ISSET_S(i, setsize, desc->socketmaps[j])) {
477251f8 789 printf("%d", j);
7e03f383
KZ
790 break;
791 }
792 }
477251f8
KZ
793 break;
794 case COL_NODE:
7e03f383
KZ
795 for (j = 0; j < desc->nnodes; j++) {
796 if (CPU_ISSET_S(i, setsize, desc->nodemaps[j])) {
477251f8 797 printf("%d", j);
7e03f383 798 break;
5dd7507c 799 }
7e03f383 800 }
477251f8
KZ
801 break;
802 case COL_BOOK:
56baaa4e
HC
803 for (j = 0; j < desc->nbooks; j++) {
804 if (CPU_ISSET_S(i, setsize, desc->bookmaps[j])) {
477251f8 805 printf("%d", j);
56baaa4e
HC
806 break;
807 }
808 }
477251f8
KZ
809 break;
810 case COL_CACHE:
7e03f383
KZ
811 for (j = desc->ncaches - 1; j >= 0; j--) {
812 struct cpu_cache *ca = &desc->caches[j];
813 int x;
5dd7507c 814
7e03f383
KZ
815 for (x = 0; x < ca->nsharedmaps; x++) {
816 if (CPU_ISSET_S(i, setsize, ca->sharedmaps[x])) {
477251f8
KZ
817 if (j != desc->ncaches - 1)
818 putchar(compatible ? ',' : ':');
819 printf("%d", x);
7e03f383
KZ
820 break;
821 }
5dd7507c 822 }
7e03f383
KZ
823 if (x == ca->nsharedmaps)
824 putchar(',');
5dd7507c 825 }
477251f8
KZ
826 break;
827 }
828}
829
830/*
831 * We support two formats:
832 *
833 * 1) "compatible" -- this format is compatible with the original lscpu(1)
834 * output and it contains fixed set of the columns. The CACHE columns are at
835 * the end of the line and the CACHE is not printed if the number of the caches
836 * is zero. The CACHE columns are separated by two commas, for example:
837 *
838 * $ lscpu --parse
839 * # CPU,Core,Socket,Node,,L1d,L1i,L2
840 * 0,0,0,0,,0,0,0
841 * 1,1,0,0,,1,1,0
842 *
843 * 2) "user defined output" -- this format prints always all columns without
844 * special prefix for CACHE column. If there are not CACHEs then the column is
845 * empty and the header "Cache" is printed rather than a real name of the cache.
846 * The CACHE columns are separated by ':'.
847 *
848 * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE
849 * # CPU,Core,Socket,Node,L1d:L1i:L2
850 * 0,0,0,0,0:0:0
851 * 1,1,0,0,1:1:0
852 */
853static void
854print_parsable(struct lscpu_desc *desc, int cols[], int ncols, int compatible)
855{
856 int i, c;
857
858 printf(_(
859 "# The following is the parsable format, which can be fed to other\n"
860 "# programs. Each different item in every column has an unique ID\n"
861 "# starting from zero.\n"));
862
863 fputs("# ", stdout);
864 for (i = 0; i < ncols; i++) {
865 if (cols[i] == COL_CACHE) {
866 if (compatible && !desc->ncaches)
867 continue;
868 if (i > 0)
869 putchar(',');
870 if (compatible && i != 0)
871 putchar(',');
872 for (c = desc->ncaches - 1; c >= 0; c--) {
873 printf("%s", desc->caches[c].name);
874 if (c > 0)
875 putchar(compatible ? ',' : ':');
876 }
877 if (!desc->ncaches)
878 fputs(colnames[cols[i]], stdout);
879 } else {
880 if (i > 0)
881 putchar(',');
882 fputs(colnames[cols[i]], stdout);
883 }
884 }
885 putchar('\n');
886
887 for (i = 0; i < desc->ncpus; i++) {
888 if (desc->online && !is_cpu_online(desc, i))
889 continue;
890 for (c = 0; c < ncols; c++) {
891 if (compatible && cols[c] == COL_CACHE) {
892 if (!desc->ncaches)
893 continue;
894 if (c > 0)
895 putchar(',');
896 }
897 if (c > 0)
898 putchar(',');
899 print_parsable_cell(desc, i, cols[c], compatible);
900 }
5dd7507c
CQ
901 putchar('\n');
902 }
903}
904
905
906/* output formats "<key> <value>"*/
907#define print_s(_key, _val) printf("%-23s%s\n", _key, _val)
908#define print_n(_key, _val) printf("%-23s%d\n", _key, _val)
909
910static void
4f912c6a
KZ
911print_cpuset(const char *key, cpu_set_t *set, int hex)
912{
913 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
914 size_t setbuflen = 7 * maxcpus;
915 char setbuf[setbuflen], *p;
916
917 if (hex) {
918 p = cpumask_create(setbuf, setbuflen, set, setsize);
919 printf("%-23s0x%s\n", key, p);
920 } else {
921 p = cpulist_create(setbuf, setbuflen, set, setsize);
922 print_s(key, p);
923 }
924
925}
926
927static void
928print_readable(struct lscpu_desc *desc, int hex)
5dd7507c 929{
7e03f383
KZ
930 char buf[512];
931 int i;
aac1e59e 932 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
7e03f383 933
e6b0611b 934 print_s(_("Architecture:"), desc->arch);
79e8b41a 935
f633ad4e 936 if (desc->mode) {
79e8b41a
KZ
937 char buf[64], *p = buf;
938
f633ad4e 939 if (desc->mode & MODE_32BIT) {
79e8b41a
KZ
940 strcpy(p, "32-bit, ");
941 p += 8;
942 }
f633ad4e 943 if (desc->mode & MODE_64BIT) {
79e8b41a
KZ
944 strcpy(p, "64-bit, ");
945 p += 8;
946 }
947 *(p - 2) = '\0';
948 print_s(_("CPU op-mode(s):"), buf);
949 }
aabe2441 950#if !defined(WORDS_BIGENDIAN)
9b8d4d5f
DB
951 print_s(_("Byte Order:"), "Little Endian");
952#else
953 print_s(_("Byte Order:"), "Big Endian");
9b8d4d5f 954#endif
4a939e04 955 print_n(_("CPU(s):"), desc->ncpus);
4f912c6a 956
5d4ba40d
KZ
957 if (desc->online)
958 print_cpuset(hex ? _("On-line CPU(s) mask:") :
959 _("On-line CPU(s) list:"),
960 desc->online, hex);
4f912c6a 961
5d4ba40d 962 if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) {
4f912c6a
KZ
963 cpu_set_t *set;
964
965 /* Linux kernel provides cpuset of off-line CPUs that contains
966 * all configured CPUs (see /sys/devices/system/cpu/offline),
967 * but want to print real (present in system) off-line CPUs only.
968 */
969 set = cpuset_alloc(maxcpus, NULL, NULL);
970 if (!set)
971 err(EXIT_FAILURE, _("failed to callocate cpu set"));
972 CPU_ZERO_S(setsize, set);
973 for (i = 0; i < desc->ncpus; i++) {
974 if (!is_cpu_online(desc, i))
975 CPU_SET_S(i, setsize, set);
976 }
977 print_cpuset(hex ? _("Off-line CPU(s) mask:") :
978 _("Off-line CPU(s) list:"),
979 set, hex);
980 cpuset_free(set);
981 }
5dd7507c 982
7e03f383
KZ
983 if (desc->nsockets) {
984 print_n(_("Thread(s) per core:"), desc->nthreads / desc->ncores);
985 print_n(_("Core(s) per socket:"), desc->ncores / desc->nsockets);
56baaa4e
HC
986 if (desc->nbooks) {
987 print_n(_("Socket(s) per book:"), desc->nsockets / desc->nbooks);
988 print_n(_("Book(s):"), desc->nbooks);
989 } else {
990 print_n(_("Socket(s):"), desc->nsockets);
991 }
5dd7507c 992 }
7e03f383
KZ
993 if (desc->nnodes)
994 print_n(_("NUMA node(s):"), desc->nnodes);
e8aa16ee
KZ
995 if (desc->vendor)
996 print_s(_("Vendor ID:"), desc->vendor);
997 if (desc->family)
998 print_s(_("CPU family:"), desc->family);
999 if (desc->model)
1000 print_s(_("Model:"), desc->model);
1001 if (desc->stepping)
1002 print_s(_("Stepping:"), desc->stepping);
1003 if (desc->mhz)
1004 print_s(_("CPU MHz:"), desc->mhz);
9b8d4d5f
DB
1005 if (desc->bogomips)
1006 print_s(_("BogoMIPS:"), desc->bogomips);
e8aa16ee
KZ
1007 if (desc->virtflag) {
1008 if (!strcmp(desc->virtflag, "svm"))
5dd7507c 1009 print_s(_("Virtualization:"), "AMD-V");
e8aa16ee 1010 else if (!strcmp(desc->virtflag, "vmx"))
5dd7507c
CQ
1011 print_s(_("Virtualization:"), "VT-x");
1012 }
e8aa16ee
KZ
1013 if (desc->hyper) {
1014 print_s(_("Hypervisor vendor:"), hv_vendors[desc->hyper]);
1015 print_s(_("Virtualization type:"), virt_types[desc->virtype]);
c8b64f6d 1016 }
7e03f383 1017 if (desc->ncaches) {
c8b64f6d 1018 char buf[512];
5dd7507c
CQ
1019 int i;
1020
7e03f383 1021 for (i = desc->ncaches - 1; i >= 0; i--) {
5dd7507c 1022 snprintf(buf, sizeof(buf),
7e03f383
KZ
1023 _("%s cache:"), desc->caches[i].name);
1024 print_s(buf, desc->caches[i].size);
1025 }
1026 }
1027
4f912c6a
KZ
1028 for (i = 0; i < desc->nnodes; i++) {
1029 snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), i);
1030 print_cpuset(buf, desc->nodemaps[i], hex);
5dd7507c
CQ
1031 }
1032}
1033
39561c70 1034static void __attribute__((__noreturn__)) usage(FILE *out)
5dd7507c 1035{
e8ab5ce3
BS
1036 fprintf(out, _(
1037 "\nUsage:\n"
1038 " %s [options]\n"), program_invocation_short_name);
1039
1040 puts(_( "\nOptions:\n"
1041 " -h, --help print this help\n"
477251f8 1042 " -p, --parse[=LIST] print out a parsable instead of a readable format\n"
e8ab5ce3
BS
1043 " -s, --sysroot DIR use directory DIR as system root\n"
1044 " -x, --hex print hexadecimal masks rather than lists of CPUs\n"));
4f912c6a 1045
39561c70 1046 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
5dd7507c
CQ
1047}
1048
1049int main(int argc, char *argv[])
1050{
e8aa16ee 1051 struct lscpu_desc _desc, *desc = &_desc;
4f912c6a 1052 int parsable = 0, c, i, hex = 0;
477251f8
KZ
1053 int columns[ARRAY_SIZE(colnames)], ncolumns = 0;
1054 int compatible = 0;
5dd7507c 1055
6c7d5ae9 1056 static const struct option longopts[] = {
47b6e8b6 1057 { "help", no_argument, 0, 'h' },
477251f8 1058 { "parse", optional_argument, 0, 'p' },
47b6e8b6 1059 { "sysroot", required_argument, 0, 's' },
4f912c6a 1060 { "hex", no_argument, 0, 'x' },
5dd7507c
CQ
1061 { NULL, 0, 0, 0 }
1062 };
1063
2f8f1388 1064 setlocale(LC_ALL, "");
5dd7507c
CQ
1065 bindtextdomain(PACKAGE, LOCALEDIR);
1066 textdomain(PACKAGE);
1067
477251f8 1068 while ((c = getopt_long(argc, argv, "hp::s:x", longopts, NULL)) != -1) {
5dd7507c
CQ
1069 switch (c) {
1070 case 'h':
39561c70 1071 usage(stdout);
5dd7507c
CQ
1072 case 'p':
1073 parsable = 1;
477251f8
KZ
1074 if (optarg) {
1075 if (*optarg == '=')
1076 optarg++;
1077 ncolumns = string_to_idarray(optarg,
1078 columns, ARRAY_SIZE(columns),
1079 column_name_to_id);
1080 if (ncolumns < 0)
1081 return EXIT_FAILURE;
1082 } else {
1083 columns[ncolumns++] = COL_CPU;
1084 columns[ncolumns++] = COL_CORE;
1085 columns[ncolumns++] = COL_SOCKET,
1086 columns[ncolumns++] = COL_NODE,
1087 columns[ncolumns++] = COL_CACHE;
1088 compatible = 1;
1089 }
5dd7507c 1090 break;
47b6e8b6 1091 case 's':
d50363cd 1092 sysrootlen = strlen(optarg);
47b6e8b6 1093 strncpy(pathbuf, optarg, sizeof(pathbuf));
d50363cd 1094 pathbuf[sizeof(pathbuf) - 1] = '\0';
47b6e8b6 1095 break;
4f912c6a
KZ
1096 case 'x':
1097 hex = 1;
1098 break;
5dd7507c 1099 default:
39561c70 1100 usage(stderr);
5dd7507c
CQ
1101 }
1102 }
1103
e8aa16ee 1104 memset(desc, 0, sizeof(*desc));
5dd7507c 1105
e8aa16ee 1106 read_basicinfo(desc);
5dd7507c 1107
7e03f383 1108 for (i = 0; i < desc->ncpus; i++) {
5d4ba40d 1109 if (desc->online && !is_cpu_online(desc, i))
aac1e59e 1110 continue;
7e03f383
KZ
1111 read_topology(desc, i);
1112 read_cache(desc, i);
47b6e8b6 1113 }
7e03f383
KZ
1114
1115 qsort(desc->caches, desc->ncaches, sizeof(struct cpu_cache), cachecmp);
1116
1117 read_nodes(desc);
5dd7507c 1118
e8aa16ee 1119 read_hypervisor(desc);
c8b64f6d 1120
5dd7507c
CQ
1121 /* Show time! */
1122 if (parsable)
477251f8 1123 print_parsable(desc, columns, ncolumns, compatible);
5dd7507c 1124 else
4f912c6a 1125 print_readable(desc, hex);
5dd7507c 1126
cf474aac 1127 return EXIT_SUCCESS;
5dd7507c 1128}