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