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