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