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