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