]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu.c
tests: add dump from UltraSparc T1 to lscpu tests
[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
4e740fd8
KZ
345 if (sysrootlen)
346 /* reading info from any /{sys,proc} dump, don't mix it with
347 * information about our real CPU */
348 return 0;
349
f633ad4e
KZ
350#if defined(__alpha__) || defined(__ia64__)
351 m |= MODE_64BIT; /* 64bit platforms only */
352#endif
353 /* platforms with 64bit flag in /proc/cpuinfo, define
354 * 32bit default here */
355#if defined(__i386__) || defined(__x86_64__) || \
356 defined(__s390x__) || defined(__s390__)
357 m |= MODE_32BIT;
358#endif
359 return m;
360}
361
5dd7507c 362static void
e8aa16ee 363read_basicinfo(struct lscpu_desc *desc)
5dd7507c 364{
d50363cd 365 FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO);
5dd7507c
CQ
366 char buf[BUFSIZ];
367 struct utsname utsbuf;
368
369 /* architecture */
370 if (uname(&utsbuf) == -1)
371 err(EXIT_FAILURE, _("error: uname failed"));
e8aa16ee 372 desc->arch = xstrdup(utsbuf.machine);
5dd7507c
CQ
373
374 /* count CPU(s) */
7e03f383
KZ
375 while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d", desc->ncpus))
376 desc->ncpus++;
5dd7507c
CQ
377
378 /* details */
379 while (fgets(buf, sizeof(buf), fp) != NULL) {
e8aa16ee
KZ
380 if (lookup(buf, "vendor", &desc->vendor)) ;
381 else if (lookup(buf, "vendor_id", &desc->vendor)) ;
e8aa16ee
KZ
382 else if (lookup(buf, "family", &desc->family)) ;
383 else if (lookup(buf, "cpu family", &desc->family)) ;
384 else if (lookup(buf, "model", &desc->model)) ;
385 else if (lookup(buf, "stepping", &desc->stepping)) ;
386 else if (lookup(buf, "cpu MHz", &desc->mhz)) ;
f633ad4e
KZ
387 else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */
388 else if (lookup(buf, "features", &desc->flags)) ; /* s390 */
9b8d4d5f 389 else if (lookup(buf, "bogomips", &desc->bogomips)) ;
5dd7507c
CQ
390 else
391 continue;
392 }
c8b64f6d 393
f633ad4e
KZ
394 desc->mode = init_mode();
395
e8aa16ee
KZ
396 if (desc->flags) {
397 snprintf(buf, sizeof(buf), " %s ", desc->flags);
c8b64f6d 398 if (strstr(buf, " svm "))
e8aa16ee 399 desc->virtflag = strdup("svm");
c8b64f6d 400 else if (strstr(buf, " vmx "))
e8aa16ee 401 desc->virtflag = strdup("vmx");
79e8b41a 402 if (strstr(buf, " lm "))
f633ad4e
KZ
403 desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */
404 if (strstr(buf, " zarch "))
405 desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */
c8b64f6d
KZ
406 }
407
5dd7507c 408 fclose(fp);
7e03f383
KZ
409
410 if (path_exist(_PATH_SYS_SYSTEM "/cpu/kernel_max"))
3d6e5c35
GR
411 /* note that kernel_max is maximum index [NR_CPUS-1] */
412 maxcpus = path_getnum(_PATH_SYS_SYSTEM "/cpu/kernel_max") + 1;
7e03f383
KZ
413
414 else if (!sysrootlen)
415 /* the root is '/' so we are working with data from the current kernel */
416 maxcpus = get_max_number_of_cpus();
417 else
d0bb6987 418 /* we are reading some /sys snapshot instead of the real /sys,
7e03f383
KZ
419 * let's use any crazy number... */
420 maxcpus = desc->ncpus > 2048 ? desc->ncpus : 2048;
aac1e59e
KZ
421
422 /* get mask for online CPUs */
e282eec2
KZ
423 if (path_exist(_PATH_SYS_SYSTEM "/cpu/online")) {
424 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
5d4ba40d 425 desc->online = path_cpulist(_PATH_SYS_SYSTEM "/cpu/online");
e282eec2
KZ
426 desc->nthreads = CPU_COUNT_S(setsize, desc->online);
427 }
5dd7507c
CQ
428}
429
c8b64f6d
KZ
430static int
431has_pci_device(int vendor, int device)
432{
433 FILE *f;
434 int num, fn, ven, dev;
435 int res = 1;
436
d50363cd 437 f = path_fopen("r", 0, _PATH_PROC_PCIDEVS);
c8b64f6d
KZ
438 if (!f)
439 return 0;
440
441 /* for more details about bus/pci/devices format see
442 * drivers/pci/proc.c in linux kernel
443 */
444 while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]",
445 &num, &fn, &ven, &dev) == 4) {
446
447 if (ven == vendor && dev == device)
448 goto found;
449 }
450
451 res = 0;
452found:
453 fclose(f);
454 return res;
455}
456
457#if defined(__x86_64__) || defined(__i386__)
458
459/*
460 * This CPUID leaf returns the information about the hypervisor.
461 * EAX : maximum input value for CPUID supported by the hypervisor.
462 * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware.
463 */
464#define HYPERVISOR_INFO_LEAF 0x40000000
465
466static inline void
467cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
468 unsigned int *ecx, unsigned int *edx)
469{
c9239f23
MF
470 __asm__(
471#if defined(__PIC__) && defined(__i386__)
472 /* x86 PIC cannot clobber ebx -- gcc bitches */
473 "pushl %%ebx;"
474 "cpuid;"
475 "movl %%ebx, %%esi;"
476 "popl %%ebx;"
477 : "=S" (*ebx),
478#else
479 "cpuid;"
480 : "=b" (*ebx),
481#endif
482 "=a" (*eax),
c8b64f6d
KZ
483 "=c" (*ecx),
484 "=d" (*edx)
bc54770d 485 : "1" (op), "c"(0));
c8b64f6d
KZ
486}
487
488static void
e8aa16ee 489read_hypervisor_cpuid(struct lscpu_desc *desc)
c8b64f6d
KZ
490{
491 unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
492 char hyper_vendor_id[13];
493
494 memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id));
495
496 cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx);
497 memcpy(hyper_vendor_id + 0, &ebx, 4);
498 memcpy(hyper_vendor_id + 4, &ecx, 4);
499 memcpy(hyper_vendor_id + 8, &edx, 4);
500 hyper_vendor_id[12] = '\0';
501
502 if (!hyper_vendor_id[0])
503 return;
504
505 if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12))
e8aa16ee 506 desc->hyper = HYPER_XEN;
c8b64f6d 507 else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9))
e8aa16ee 508 desc->hyper = HYPER_KVM;
c8b64f6d 509 else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12))
e8aa16ee 510 desc->hyper = HYPER_MSHV;
c8b64f6d
KZ
511}
512
513#else /* ! __x86_64__ */
514static void
e8aa16ee 515read_hypervisor_cpuid(struct lscpu_desc *desc)
c8b64f6d
KZ
516{
517}
518#endif
519
520static void
e8aa16ee 521read_hypervisor(struct lscpu_desc *desc)
c8b64f6d 522{
e8aa16ee 523 read_hypervisor_cpuid(desc);
c8b64f6d 524
e8aa16ee 525 if (desc->hyper)
c8b64f6d 526 /* hvm */
e8aa16ee 527 desc->virtype = VIRT_FULL;
c8b64f6d 528
d50363cd 529 else if (path_exist(_PATH_PROC_XEN)) {
c8b64f6d 530 /* Xen para-virt or dom0 */
d50363cd 531 FILE *fd = path_fopen("r", 0, _PATH_PROC_XENCAP);
c8b64f6d
KZ
532 int dom0 = 0;
533
534 if (fd) {
535 char buf[256];
536
537 if (fscanf(fd, "%s", buf) == 1 &&
538 !strcmp(buf, "control_d"))
539 dom0 = 1;
540 fclose(fd);
541 }
e8aa16ee
KZ
542 desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA;
543 desc->hyper = HYPER_XEN;
c8b64f6d
KZ
544
545 } else if (has_pci_device(0x5853, 0x0001)) {
546 /* Xen full-virt on non-x86_64 */
e8aa16ee
KZ
547 desc->hyper = HYPER_XEN;
548 desc->virtype = VIRT_FULL;
c8b64f6d
KZ
549 }
550}
551
7e03f383
KZ
552/* add @set to the @ary, unnecesary set is deallocated. */
553static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set)
554{
555 int i;
556 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
557
558 if (!ary)
559 return -1;
560
561 for (i = 0; i < *items; i++) {
562 if (CPU_EQUAL_S(setsize, set, ary[i]))
563 break;
564 }
565 if (i == *items) {
566 ary[*items] = set;
567 ++*items;
568 return 0;
569 }
570 CPU_FREE(set);
571 return 1;
572}
573
5dd7507c 574static void
7e03f383 575read_topology(struct lscpu_desc *desc, int num)
5dd7507c 576{
7e03f383
KZ
577 cpu_set_t *thread_siblings, *core_siblings;
578
579 if (!path_exist(_PATH_SYS_CPU "/cpu%d/topology/thread_siblings", num))
580 return;
5dd7507c 581
7e03f383
KZ
582 thread_siblings = path_cpuset(_PATH_SYS_CPU
583 "/cpu%d/topology/thread_siblings", num);
584 core_siblings = path_cpuset(_PATH_SYS_CPU
585 "/cpu%d/topology/core_siblings", num);
aac1e59e
KZ
586
587 if (!desc->coremaps) {
7e03f383
KZ
588 int ncores, nsockets, nthreads;
589 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
590
591 /* threads within one core */
592 nthreads = CPU_COUNT_S(setsize, thread_siblings);
593 /* cores within one socket */
594 ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads;
595 /* number of sockets */
596 nsockets = desc->ncpus / nthreads / ncores;
e282eec2
KZ
597
598 /* all threads, see also read_basicinfo()
599 * -- this is fallback for kernels where is not
600 * /sys/devices/system/cpu/online.
601 */
602 if (!desc->nthreads)
603 desc->nthreads = nsockets * ncores * nthreads;
7e03f383
KZ
604
605 desc->socketmaps = calloc(nsockets, sizeof(cpu_set_t *));
606 if (!desc->socketmaps)
607 err(EXIT_FAILURE, _("error: calloc failed"));
608 desc->coremaps = calloc(ncores * nsockets, sizeof(cpu_set_t *));
609 if (!desc->coremaps)
610 err(EXIT_FAILURE, _("error: calloc failed"));
611 }
5dd7507c 612
7e03f383
KZ
613 add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings);
614 add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings);
615}
616
617static int
618cachecmp(const void *a, const void *b)
619{
620 struct cpu_cache *c1 = (struct cpu_cache *) a;
621 struct cpu_cache *c2 = (struct cpu_cache *) b;
622
623 return strcmp(c2->name, c1->name);
5dd7507c
CQ
624}
625
626static void
7e03f383 627read_cache(struct lscpu_desc *desc, int num)
5dd7507c
CQ
628{
629 char buf[256];
7e03f383 630 int i;
5dd7507c 631
aac1e59e 632 if (!desc->ncaches) {
7e03f383
KZ
633 while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d/cache/index%d",
634 num, desc->ncaches))
635 desc->ncaches++;
5dd7507c 636
7e03f383
KZ
637 if (!desc->ncaches)
638 return;
5dd7507c 639
7e03f383
KZ
640 desc->caches = calloc(desc->ncaches, sizeof(*desc->caches));
641 if (!desc->caches)
642 err(EXIT_FAILURE, _("calloc failed"));
643 }
644 for (i = 0; i < desc->ncaches; i++) {
645 struct cpu_cache *ca = &desc->caches[i];
646 cpu_set_t *map;
647
648 if (!ca->name) {
649 int type, level;
650
651 /* cache type */
652 path_getstr(buf, sizeof(buf),
653 _PATH_SYS_CPU "/cpu%d/cache/index%d/type",
654 num, i);
655 if (!strcmp(buf, "Data"))
656 type = 'd';
657 else if (!strcmp(buf, "Instruction"))
658 type = 'i';
659 else
660 type = 0;
661
662 /* cache level */
663 level = path_getnum(_PATH_SYS_CPU "/cpu%d/cache/index%d/level",
664 num, i);
665 if (type)
666 snprintf(buf, sizeof(buf), "L%d%c", level, type);
667 else
668 snprintf(buf, sizeof(buf), "L%d", level);
669
670 ca->name = xstrdup(buf);
671
672 /* cache size */
673 path_getstr(buf, sizeof(buf),
674 _PATH_SYS_CPU "/cpu%d/cache/index%d/size",
675 num, i);
676 ca->size = xstrdup(buf);
677 }
5dd7507c 678
7e03f383
KZ
679 /* information about how CPUs share different caches */
680 map = path_cpuset(_PATH_SYS_CPU "/cpu%d/cache/index%d/shared_cpu_map",
681 num, i);
5dd7507c 682
7e03f383
KZ
683 if (!ca->sharedmaps) {
684 ca->sharedmaps = calloc(desc->ncpus, sizeof(cpu_set_t *));
685 if (!ca->sharedmaps)
686 err(EXIT_FAILURE, _("error: calloc failed"));
687 }
5dd7507c 688
7e03f383 689 add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map);
5dd7507c
CQ
690 }
691}
692
693static void
e8aa16ee 694read_nodes(struct lscpu_desc *desc)
5dd7507c
CQ
695{
696 int i;
697
698 /* number of NUMA node */
7e03f383
KZ
699 while (path_exist(_PATH_SYS_SYSTEM "/node/node%d", desc->nnodes))
700 desc->nnodes++;
701
702 if (!desc->nnodes)
703 return;
5dd7507c 704
7e03f383
KZ
705 desc->nodemaps = calloc(desc->nnodes, sizeof(cpu_set_t *));
706 if (!desc->nodemaps)
707 err(EXIT_FAILURE, _("error: calloc failed"));
5dd7507c
CQ
708
709 /* information about how nodes share different CPUs */
7e03f383
KZ
710 for (i = 0; i < desc->nnodes; i++)
711 desc->nodemaps[i] = path_cpuset(
5dd7507c
CQ
712 _PATH_SYS_SYSTEM "/node/node%d/cpumap",
713 i);
714}
715
5dd7507c 716static void
e8aa16ee 717print_parsable(struct lscpu_desc *desc)
5dd7507c
CQ
718{
719 int i, j;
7e03f383 720 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
5dd7507c 721
47b6e8b6 722 printf(_(
5dd7507c 723 "# The following is the parsable format, which can be fed to other\n"
47b6e8b6 724 "# programs. Each different item in every column has an unique ID\n"
5dd7507c 725 "# starting from zero.\n"
47b6e8b6 726 "# CPU,Core,Socket,Node"));
5dd7507c 727
7e03f383 728 if (desc->ncaches) {
5dd7507c
CQ
729 /* separator between CPU topology and cache information */
730 putchar(',');
731
7e03f383
KZ
732 for (i = desc->ncaches - 1; i >= 0; i--)
733 printf(",%s", desc->caches[i].name);
5dd7507c
CQ
734 }
735 putchar('\n');
736
7e03f383
KZ
737 for (i = 0; i < desc->ncpus; i++) {
738
5d4ba40d 739 if (desc->online && !is_cpu_online(desc, i))
aac1e59e
KZ
740 continue;
741
7e03f383 742 /* #CPU */
5dd7507c
CQ
743 printf("%d", i);
744
7e03f383
KZ
745 /* Core */
746 for (j = 0; j < desc->ncores; j++) {
747 if (CPU_ISSET_S(i, setsize, desc->coremaps[j])) {
748 printf(",%d", j);
749 break;
750 }
751 }
752 if (j == desc->ncores)
753 putchar(',');
5dd7507c 754
7e03f383
KZ
755 /* Socket */
756 for (j = 0; j < desc->nsockets; j++) {
757 if (CPU_ISSET_S(i, setsize, desc->socketmaps[j])) {
758 printf(",%d", j);
759 break;
760 }
761 }
762 if (j == desc->nsockets)
763 putchar(',');
5dd7507c 764
7e03f383
KZ
765 /* Nodes */
766 for (j = 0; j < desc->nnodes; j++) {
767 if (CPU_ISSET_S(i, setsize, desc->nodemaps[j])) {
768 printf(",%d", j);
769 break;
5dd7507c 770 }
7e03f383
KZ
771 }
772 if (j == desc->nnodes)
5dd7507c
CQ
773 putchar(',');
774
7e03f383 775 if (desc->ncaches)
5dd7507c
CQ
776 putchar(',');
777
7e03f383
KZ
778 /* Caches */
779 for (j = desc->ncaches - 1; j >= 0; j--) {
780 struct cpu_cache *ca = &desc->caches[j];
781 int x;
5dd7507c 782
7e03f383
KZ
783 for (x = 0; x < ca->nsharedmaps; x++) {
784 if (CPU_ISSET_S(i, setsize, ca->sharedmaps[x])) {
785 printf(",%d", x);
786 break;
787 }
5dd7507c 788 }
7e03f383
KZ
789 if (x == ca->nsharedmaps)
790 putchar(',');
5dd7507c
CQ
791 }
792 putchar('\n');
793 }
794}
795
796
797/* output formats "<key> <value>"*/
798#define print_s(_key, _val) printf("%-23s%s\n", _key, _val)
799#define print_n(_key, _val) printf("%-23s%d\n", _key, _val)
800
801static void
4f912c6a
KZ
802print_cpuset(const char *key, cpu_set_t *set, int hex)
803{
804 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
805 size_t setbuflen = 7 * maxcpus;
806 char setbuf[setbuflen], *p;
807
808 if (hex) {
809 p = cpumask_create(setbuf, setbuflen, set, setsize);
810 printf("%-23s0x%s\n", key, p);
811 } else {
812 p = cpulist_create(setbuf, setbuflen, set, setsize);
813 print_s(key, p);
814 }
815
816}
817
818static void
819print_readable(struct lscpu_desc *desc, int hex)
5dd7507c 820{
7e03f383
KZ
821 char buf[512];
822 int i;
aac1e59e 823 size_t setsize = CPU_ALLOC_SIZE(maxcpus);
7e03f383 824
e6b0611b 825 print_s(_("Architecture:"), desc->arch);
79e8b41a 826
f633ad4e 827 if (desc->mode) {
79e8b41a
KZ
828 char buf[64], *p = buf;
829
f633ad4e 830 if (desc->mode & MODE_32BIT) {
79e8b41a
KZ
831 strcpy(p, "32-bit, ");
832 p += 8;
833 }
f633ad4e 834 if (desc->mode & MODE_64BIT) {
79e8b41a
KZ
835 strcpy(p, "64-bit, ");
836 p += 8;
837 }
838 *(p - 2) = '\0';
839 print_s(_("CPU op-mode(s):"), buf);
840 }
aabe2441 841#if !defined(WORDS_BIGENDIAN)
9b8d4d5f
DB
842 print_s(_("Byte Order:"), "Little Endian");
843#else
844 print_s(_("Byte Order:"), "Big Endian");
9b8d4d5f 845#endif
4a939e04 846 print_n(_("CPU(s):"), desc->ncpus);
4f912c6a 847
5d4ba40d
KZ
848 if (desc->online)
849 print_cpuset(hex ? _("On-line CPU(s) mask:") :
850 _("On-line CPU(s) list:"),
851 desc->online, hex);
4f912c6a 852
5d4ba40d 853 if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) {
4f912c6a
KZ
854 cpu_set_t *set;
855
856 /* Linux kernel provides cpuset of off-line CPUs that contains
857 * all configured CPUs (see /sys/devices/system/cpu/offline),
858 * but want to print real (present in system) off-line CPUs only.
859 */
860 set = cpuset_alloc(maxcpus, NULL, NULL);
861 if (!set)
862 err(EXIT_FAILURE, _("failed to callocate cpu set"));
863 CPU_ZERO_S(setsize, set);
864 for (i = 0; i < desc->ncpus; i++) {
865 if (!is_cpu_online(desc, i))
866 CPU_SET_S(i, setsize, set);
867 }
868 print_cpuset(hex ? _("Off-line CPU(s) mask:") :
869 _("Off-line CPU(s) list:"),
870 set, hex);
871 cpuset_free(set);
872 }
5dd7507c 873
7e03f383
KZ
874 if (desc->nsockets) {
875 print_n(_("Thread(s) per core:"), desc->nthreads / desc->ncores);
876 print_n(_("Core(s) per socket:"), desc->ncores / desc->nsockets);
877 print_n(_("CPU socket(s):"), desc->nsockets);
5dd7507c
CQ
878 }
879
7e03f383
KZ
880 if (desc->nnodes)
881 print_n(_("NUMA node(s):"), desc->nnodes);
e8aa16ee
KZ
882 if (desc->vendor)
883 print_s(_("Vendor ID:"), desc->vendor);
884 if (desc->family)
885 print_s(_("CPU family:"), desc->family);
886 if (desc->model)
887 print_s(_("Model:"), desc->model);
888 if (desc->stepping)
889 print_s(_("Stepping:"), desc->stepping);
890 if (desc->mhz)
891 print_s(_("CPU MHz:"), desc->mhz);
9b8d4d5f
DB
892 if (desc->bogomips)
893 print_s(_("BogoMIPS:"), desc->bogomips);
e8aa16ee
KZ
894 if (desc->virtflag) {
895 if (!strcmp(desc->virtflag, "svm"))
5dd7507c 896 print_s(_("Virtualization:"), "AMD-V");
e8aa16ee 897 else if (!strcmp(desc->virtflag, "vmx"))
5dd7507c
CQ
898 print_s(_("Virtualization:"), "VT-x");
899 }
e8aa16ee
KZ
900 if (desc->hyper) {
901 print_s(_("Hypervisor vendor:"), hv_vendors[desc->hyper]);
902 print_s(_("Virtualization type:"), virt_types[desc->virtype]);
c8b64f6d 903 }
7e03f383 904 if (desc->ncaches) {
c8b64f6d 905 char buf[512];
5dd7507c
CQ
906 int i;
907
7e03f383 908 for (i = desc->ncaches - 1; i >= 0; i--) {
5dd7507c 909 snprintf(buf, sizeof(buf),
7e03f383
KZ
910 _("%s cache:"), desc->caches[i].name);
911 print_s(buf, desc->caches[i].size);
912 }
913 }
914
4f912c6a
KZ
915 for (i = 0; i < desc->nnodes; i++) {
916 snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), i);
917 print_cpuset(buf, desc->nodemaps[i], hex);
5dd7507c
CQ
918 }
919}
920
39561c70 921static void __attribute__((__noreturn__)) usage(FILE *out)
5dd7507c 922{
e8ab5ce3
BS
923 fprintf(out, _(
924 "\nUsage:\n"
925 " %s [options]\n"), program_invocation_short_name);
926
927 puts(_( "\nOptions:\n"
928 " -h, --help print this help\n"
929 " -p, --parse print out a parsable instead of a readable format\n"
930 " -s, --sysroot DIR use directory DIR as system root\n"
931 " -x, --hex print hexadecimal masks rather than lists of CPUs\n"));
4f912c6a 932
39561c70 933 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
5dd7507c
CQ
934}
935
936int main(int argc, char *argv[])
937{
e8aa16ee 938 struct lscpu_desc _desc, *desc = &_desc;
4f912c6a 939 int parsable = 0, c, i, hex = 0;
5dd7507c 940
6c7d5ae9 941 static const struct option longopts[] = {
47b6e8b6
CQ
942 { "help", no_argument, 0, 'h' },
943 { "parse", no_argument, 0, 'p' },
944 { "sysroot", required_argument, 0, 's' },
4f912c6a 945 { "hex", no_argument, 0, 'x' },
5dd7507c
CQ
946 { NULL, 0, 0, 0 }
947 };
948
2f8f1388 949 setlocale(LC_ALL, "");
5dd7507c
CQ
950 bindtextdomain(PACKAGE, LOCALEDIR);
951 textdomain(PACKAGE);
952
4f912c6a 953 while((c = getopt_long(argc, argv, "hps:x", longopts, NULL)) != -1) {
5dd7507c
CQ
954 switch (c) {
955 case 'h':
39561c70 956 usage(stdout);
5dd7507c
CQ
957 case 'p':
958 parsable = 1;
959 break;
47b6e8b6 960 case 's':
d50363cd 961 sysrootlen = strlen(optarg);
47b6e8b6 962 strncpy(pathbuf, optarg, sizeof(pathbuf));
d50363cd 963 pathbuf[sizeof(pathbuf) - 1] = '\0';
47b6e8b6 964 break;
4f912c6a
KZ
965 case 'x':
966 hex = 1;
967 break;
5dd7507c 968 default:
39561c70 969 usage(stderr);
5dd7507c
CQ
970 }
971 }
972
e8aa16ee 973 memset(desc, 0, sizeof(*desc));
5dd7507c 974
e8aa16ee 975 read_basicinfo(desc);
5dd7507c 976
7e03f383 977 for (i = 0; i < desc->ncpus; i++) {
5d4ba40d 978 if (desc->online && !is_cpu_online(desc, i))
aac1e59e 979 continue;
7e03f383
KZ
980 read_topology(desc, i);
981 read_cache(desc, i);
47b6e8b6 982 }
7e03f383
KZ
983
984 qsort(desc->caches, desc->ncaches, sizeof(struct cpu_cache), cachecmp);
985
986 read_nodes(desc);
5dd7507c 987
e8aa16ee 988 read_hypervisor(desc);
c8b64f6d 989
5dd7507c
CQ
990 /* Show time! */
991 if (parsable)
e8aa16ee 992 print_parsable(desc);
5dd7507c 993 else
4f912c6a 994 print_readable(desc, hex);
5dd7507c 995
cf474aac 996 return EXIT_SUCCESS;
5dd7507c 997}