]>
Commit | Line | Data |
---|---|---|
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 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it would 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, write to the Free Software Foundation, | |
19 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include <ctype.h> | |
23 | #include <dirent.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> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | ||
36 | #include "cpuset.h" | |
37 | #include "nls.h" | |
38 | #include "xalloc.h" | |
39 | #include "c.h" | |
40 | #include "strutils.h" | |
41 | #include "bitops.h" | |
42 | ||
43 | ||
44 | #define CACHE_MAX 100 | |
45 | ||
46 | /* /sys paths */ | |
47 | #define _PATH_SYS_SYSTEM "/sys/devices/system" | |
48 | #define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu" | |
49 | #define _PATH_PROC_XEN "/proc/xen" | |
50 | #define _PATH_PROC_XENCAP _PATH_PROC_XEN "/capabilities" | |
51 | #define _PATH_PROC_CPUINFO "/proc/cpuinfo" | |
52 | #define _PATH_PROC_PCIDEVS "/proc/bus/pci/devices" | |
53 | #define _PATH_PROC_SYSINFO "/proc/sysinfo" | |
54 | ||
55 | /* virtualization types */ | |
56 | enum { | |
57 | VIRT_NONE = 0, | |
58 | VIRT_PARA, | |
59 | VIRT_FULL | |
60 | }; | |
61 | const char *virt_types[] = { | |
62 | [VIRT_NONE] = N_("none"), | |
63 | [VIRT_PARA] = N_("para"), | |
64 | [VIRT_FULL] = N_("full") | |
65 | }; | |
66 | ||
67 | /* hypervisor vendors */ | |
68 | enum { | |
69 | HYPER_NONE = 0, | |
70 | HYPER_XEN, | |
71 | HYPER_KVM, | |
72 | HYPER_MSHV, | |
73 | HYPER_VMWARE, | |
74 | HYPER_IBM | |
75 | }; | |
76 | const char *hv_vendors[] = { | |
77 | [HYPER_NONE] = NULL, | |
78 | [HYPER_XEN] = "Xen", | |
79 | [HYPER_KVM] = "KVM", | |
80 | [HYPER_MSHV] = "Microsoft", | |
81 | [HYPER_VMWARE] = "VMware", | |
82 | [HYPER_IBM] = "IBM" | |
83 | }; | |
84 | ||
85 | /* CPU modes */ | |
86 | enum { | |
87 | MODE_32BIT = (1 << 1), | |
88 | MODE_64BIT = (1 << 2) | |
89 | }; | |
90 | ||
91 | /* cache(s) description */ | |
92 | struct cpu_cache { | |
93 | char *name; | |
94 | char *size; | |
95 | ||
96 | int nsharedmaps; | |
97 | cpu_set_t **sharedmaps; | |
98 | }; | |
99 | ||
100 | /* dispatching modes */ | |
101 | enum { | |
102 | DISP_HORIZONTAL = 0, | |
103 | DISP_VERTICAL = 1 | |
104 | }; | |
105 | ||
106 | const char *disp_modes[] = { | |
107 | [DISP_HORIZONTAL] = N_("horizontal"), | |
108 | [DISP_VERTICAL] = N_("vertical") | |
109 | }; | |
110 | ||
111 | /* cpu polarization */ | |
112 | enum { | |
113 | POLAR_UNKNOWN = 0, | |
114 | POLAR_VLOW, | |
115 | POLAR_VMEDIUM, | |
116 | POLAR_VHIGH, | |
117 | POLAR_HORIZONTAL | |
118 | }; | |
119 | ||
120 | const char *polar_modes[] = { | |
121 | [POLAR_UNKNOWN] = "U", | |
122 | [POLAR_VLOW] = "VL", | |
123 | [POLAR_VMEDIUM] = "VM", | |
124 | [POLAR_VHIGH] = "VH", | |
125 | [POLAR_HORIZONTAL] = "H" | |
126 | }; | |
127 | ||
128 | /* global description */ | |
129 | struct lscpu_desc { | |
130 | char *arch; | |
131 | char *vendor; | |
132 | char *family; | |
133 | char *model; | |
134 | char *virtflag; /* virtualization flag (vmx, svm) */ | |
135 | int hyper; /* hypervisor vendor ID */ | |
136 | int virtype; /* VIRT_PARA|FULL|NONE ? */ | |
137 | char *mhz; | |
138 | char *stepping; | |
139 | char *bogomips; | |
140 | char *flags; | |
141 | int dispatching; /* none, horizontal or vertical */ | |
142 | int mode; /* rm, lm or/and tm */ | |
143 | ||
144 | int ncpus; /* number of CPUs */ | |
145 | cpu_set_t *online; /* mask with online CPUs */ | |
146 | ||
147 | int nnodes; /* number of NUMA modes */ | |
148 | cpu_set_t **nodemaps; /* array with NUMA nodes */ | |
149 | ||
150 | /* books -- based on book_siblings (internal kernel map of cpuX's | |
151 | * hardware threads within the same book */ | |
152 | int nbooks; /* number of all online books */ | |
153 | cpu_set_t **bookmaps; /* unique book_siblings */ | |
154 | ||
155 | /* sockets -- based on core_siblings (internal kernel map of cpuX's | |
156 | * hardware threads within the same physical_package_id (socket)) */ | |
157 | int nsockets; /* number of all online sockets */ | |
158 | cpu_set_t **socketmaps; /* unique core_siblings */ | |
159 | ||
160 | /* cores -- based on thread_siblings (internel kernel map of cpuX's | |
161 | * hardware threads within the same core as cpuX) */ | |
162 | int ncores; /* number of all online cores */ | |
163 | cpu_set_t **coremaps; /* unique thread_siblings */ | |
164 | ||
165 | int nthreads; /* number of online threads */ | |
166 | ||
167 | int ncaches; | |
168 | struct cpu_cache *caches; | |
169 | ||
170 | int *polarization; /* cpu polarization */ | |
171 | }; | |
172 | ||
173 | static size_t sysrootlen; | |
174 | static char pathbuf[PATH_MAX]; | |
175 | static int maxcpus; /* size in bits of kernel cpu mask */ | |
176 | ||
177 | #define is_cpu_online(_d, _cpu) \ | |
178 | ((_d) && (_d)->online ? \ | |
179 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0) | |
180 | ||
181 | static FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...) | |
182 | __attribute__ ((__format__ (__printf__, 3, 4))); | |
183 | static void path_getstr(char *result, size_t len, const char *path, ...) | |
184 | __attribute__ ((__format__ (__printf__, 3, 4))); | |
185 | static int path_getnum(const char *path, ...) | |
186 | __attribute__ ((__format__ (__printf__, 1, 2))); | |
187 | static int path_exist(const char *path, ...) | |
188 | __attribute__ ((__format__ (__printf__, 1, 2))); | |
189 | static cpu_set_t *path_cpuset(const char *path, ...) | |
190 | __attribute__ ((__format__ (__printf__, 1, 2))); | |
191 | ||
192 | /* | |
193 | * Parsable output | |
194 | */ | |
195 | enum { | |
196 | COL_CPU, | |
197 | COL_CORE, | |
198 | COL_SOCKET, | |
199 | COL_NODE, | |
200 | COL_BOOK, | |
201 | COL_CACHE, | |
202 | COL_POLARIZATION | |
203 | }; | |
204 | ||
205 | static const char *colnames[] = | |
206 | { | |
207 | [COL_CPU] = "CPU", | |
208 | [COL_CORE] = "Core", | |
209 | [COL_SOCKET] = "Socket", | |
210 | [COL_NODE] = "Node", | |
211 | [COL_BOOK] = "Book", | |
212 | [COL_CACHE] = "Cache", | |
213 | [COL_POLARIZATION] = "Polarization" | |
214 | }; | |
215 | ||
216 | ||
217 | static int column_name_to_id(const char *name, size_t namesz) | |
218 | { | |
219 | size_t i; | |
220 | ||
221 | for (i = 0; i < ARRAY_SIZE(colnames); i++) { | |
222 | const char *cn = colnames[i]; | |
223 | ||
224 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
225 | return i; | |
226 | } | |
227 | warnx(_("unknown column: %s"), name); | |
228 | return -1; | |
229 | } | |
230 | ||
231 | static const char * | |
232 | path_vcreate(const char *path, va_list ap) | |
233 | { | |
234 | if (sysrootlen) | |
235 | vsnprintf(pathbuf + sysrootlen, | |
236 | sizeof(pathbuf) - sysrootlen, path, ap); | |
237 | else | |
238 | vsnprintf(pathbuf, sizeof(pathbuf), path, ap); | |
239 | return pathbuf; | |
240 | } | |
241 | ||
242 | static FILE * | |
243 | path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap) | |
244 | { | |
245 | FILE *f; | |
246 | const char *p = path_vcreate(path, ap); | |
247 | ||
248 | f = fopen(p, mode); | |
249 | if (!f && exit_on_error) | |
250 | err(EXIT_FAILURE, _("error: cannot open %s"), p); | |
251 | return f; | |
252 | } | |
253 | ||
254 | static FILE * | |
255 | path_fopen(const char *mode, int exit_on_error, const char *path, ...) | |
256 | { | |
257 | FILE *fd; | |
258 | va_list ap; | |
259 | ||
260 | va_start(ap, path); | |
261 | fd = path_vfopen(mode, exit_on_error, path, ap); | |
262 | va_end(ap); | |
263 | ||
264 | return fd; | |
265 | } | |
266 | ||
267 | static void | |
268 | path_getstr(char *result, size_t len, const char *path, ...) | |
269 | { | |
270 | FILE *fd; | |
271 | va_list ap; | |
272 | ||
273 | va_start(ap, path); | |
274 | fd = path_vfopen("r", 1, path, ap); | |
275 | va_end(ap); | |
276 | ||
277 | if (!fgets(result, len, fd)) | |
278 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); | |
279 | fclose(fd); | |
280 | ||
281 | len = strlen(result); | |
282 | if (result[len - 1] == '\n') | |
283 | result[len - 1] = '\0'; | |
284 | } | |
285 | ||
286 | static int | |
287 | path_getnum(const char *path, ...) | |
288 | { | |
289 | FILE *fd; | |
290 | va_list ap; | |
291 | int result; | |
292 | ||
293 | va_start(ap, path); | |
294 | fd = path_vfopen("r", 1, path, ap); | |
295 | va_end(ap); | |
296 | ||
297 | if (fscanf(fd, "%d", &result) != 1) { | |
298 | if (ferror(fd)) | |
299 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); | |
300 | else | |
301 | errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); | |
302 | } | |
303 | fclose(fd); | |
304 | return result; | |
305 | } | |
306 | ||
307 | static int | |
308 | path_exist(const char *path, ...) | |
309 | { | |
310 | va_list ap; | |
311 | const char *p; | |
312 | ||
313 | va_start(ap, path); | |
314 | p = path_vcreate(path, ap); | |
315 | va_end(ap); | |
316 | ||
317 | return access(p, F_OK) == 0; | |
318 | } | |
319 | ||
320 | static cpu_set_t * | |
321 | path_cpuparse(int islist, const char *path, va_list ap) | |
322 | { | |
323 | FILE *fd; | |
324 | cpu_set_t *set; | |
325 | size_t setsize, len = maxcpus * 7; | |
326 | char buf[len]; | |
327 | ||
328 | fd = path_vfopen("r", 1, path, ap); | |
329 | ||
330 | if (!fgets(buf, len, fd)) | |
331 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); | |
332 | fclose(fd); | |
333 | ||
334 | len = strlen(buf); | |
335 | if (buf[len - 1] == '\n') | |
336 | buf[len - 1] = '\0'; | |
337 | ||
338 | set = cpuset_alloc(maxcpus, &setsize, NULL); | |
339 | if (!set) | |
340 | err(EXIT_FAILURE, _("failed to callocate cpu set")); | |
341 | ||
342 | if (islist) { | |
343 | if (cpulist_parse(buf, set, setsize)) | |
344 | errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf); | |
345 | } else { | |
346 | if (cpumask_parse(buf, set, setsize)) | |
347 | errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf); | |
348 | } | |
349 | return set; | |
350 | } | |
351 | ||
352 | static cpu_set_t * | |
353 | path_cpuset(const char *path, ...) | |
354 | { | |
355 | va_list ap; | |
356 | cpu_set_t *set; | |
357 | ||
358 | va_start(ap, path); | |
359 | set = path_cpuparse(0, path, ap); | |
360 | va_end(ap); | |
361 | ||
362 | return set; | |
363 | } | |
364 | ||
365 | static cpu_set_t * | |
366 | path_cpulist(const char *path, ...) | |
367 | { | |
368 | va_list ap; | |
369 | cpu_set_t *set; | |
370 | ||
371 | va_start(ap, path); | |
372 | set = path_cpuparse(1, path, ap); | |
373 | va_end(ap); | |
374 | ||
375 | return set; | |
376 | } | |
377 | ||
378 | /* Lookup a pattern and get the value from cpuinfo. | |
379 | * Format is: | |
380 | * | |
381 | * "<pattern> : <key>" | |
382 | */ | |
383 | int lookup(char *line, char *pattern, char **value) | |
384 | { | |
385 | char *p, *v; | |
386 | int len = strlen(pattern); | |
387 | ||
388 | if (!*line) | |
389 | return 0; | |
390 | ||
391 | /* pattern */ | |
392 | if (strncmp(line, pattern, len)) | |
393 | return 0; | |
394 | ||
395 | /* white spaces */ | |
396 | for (p = line + len; isspace(*p); p++); | |
397 | ||
398 | /* separator */ | |
399 | if (*p != ':') | |
400 | return 0; | |
401 | ||
402 | /* white spaces */ | |
403 | for (++p; isspace(*p); p++); | |
404 | ||
405 | /* value */ | |
406 | if (!*p) | |
407 | return 0; | |
408 | v = p; | |
409 | ||
410 | /* end of value */ | |
411 | len = strlen(line) - 1; | |
412 | for (p = line + len; isspace(*(p-1)); p--); | |
413 | *p = '\0'; | |
414 | ||
415 | *value = xstrdup(v); | |
416 | return 1; | |
417 | } | |
418 | ||
419 | /* Don't init the mode for platforms where we are not able to | |
420 | * detect that CPU supports 64-bit mode. | |
421 | */ | |
422 | static int | |
423 | init_mode(void) | |
424 | { | |
425 | int m = 0; | |
426 | ||
427 | if (sysrootlen) | |
428 | /* reading info from any /{sys,proc} dump, don't mix it with | |
429 | * information about our real CPU */ | |
430 | return 0; | |
431 | ||
432 | #if defined(__alpha__) || defined(__ia64__) | |
433 | m |= MODE_64BIT; /* 64bit platforms only */ | |
434 | #endif | |
435 | /* platforms with 64bit flag in /proc/cpuinfo, define | |
436 | * 32bit default here */ | |
437 | #if defined(__i386__) || defined(__x86_64__) || \ | |
438 | defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) | |
439 | m |= MODE_32BIT; | |
440 | #endif | |
441 | return m; | |
442 | } | |
443 | ||
444 | static void | |
445 | read_basicinfo(struct lscpu_desc *desc) | |
446 | { | |
447 | FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO); | |
448 | char buf[BUFSIZ]; | |
449 | struct utsname utsbuf; | |
450 | ||
451 | /* architecture */ | |
452 | if (uname(&utsbuf) == -1) | |
453 | err(EXIT_FAILURE, _("error: uname failed")); | |
454 | desc->arch = xstrdup(utsbuf.machine); | |
455 | ||
456 | /* count CPU(s) */ | |
457 | while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d", desc->ncpus)) | |
458 | desc->ncpus++; | |
459 | ||
460 | /* details */ | |
461 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
462 | if (lookup(buf, "vendor", &desc->vendor)) ; | |
463 | else if (lookup(buf, "vendor_id", &desc->vendor)) ; | |
464 | else if (lookup(buf, "family", &desc->family)) ; | |
465 | else if (lookup(buf, "cpu family", &desc->family)) ; | |
466 | else if (lookup(buf, "model", &desc->model)) ; | |
467 | else if (lookup(buf, "stepping", &desc->stepping)) ; | |
468 | else if (lookup(buf, "cpu MHz", &desc->mhz)) ; | |
469 | else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */ | |
470 | else if (lookup(buf, "features", &desc->flags)) ; /* s390 */ | |
471 | else if (lookup(buf, "type", &desc->flags)) ; /* sparc64 */ | |
472 | else if (lookup(buf, "bogomips", &desc->bogomips)) ; | |
473 | else if (lookup(buf, "bogomips per cpu", &desc->bogomips)) ; /* s390 */ | |
474 | else | |
475 | continue; | |
476 | } | |
477 | ||
478 | desc->mode = init_mode(); | |
479 | ||
480 | if (desc->flags) { | |
481 | snprintf(buf, sizeof(buf), " %s ", desc->flags); | |
482 | if (strstr(buf, " svm ")) | |
483 | desc->virtflag = strdup("svm"); | |
484 | else if (strstr(buf, " vmx ")) | |
485 | desc->virtflag = strdup("vmx"); | |
486 | if (strstr(buf, " lm ")) | |
487 | desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */ | |
488 | if (strstr(buf, " zarch ")) | |
489 | desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */ | |
490 | if (strstr(buf, " sun4v ") || strstr(buf, " sun4u ")) | |
491 | desc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */ | |
492 | } | |
493 | ||
494 | fclose(fp); | |
495 | ||
496 | if (path_exist(_PATH_SYS_SYSTEM "/cpu/kernel_max")) | |
497 | /* note that kernel_max is maximum index [NR_CPUS-1] */ | |
498 | maxcpus = path_getnum(_PATH_SYS_SYSTEM "/cpu/kernel_max") + 1; | |
499 | ||
500 | else if (!sysrootlen) | |
501 | /* the root is '/' so we are working with data from the current kernel */ | |
502 | maxcpus = get_max_number_of_cpus(); | |
503 | else | |
504 | /* we are reading some /sys snapshot instead of the real /sys, | |
505 | * let's use any crazy number... */ | |
506 | maxcpus = desc->ncpus > 2048 ? desc->ncpus : 2048; | |
507 | ||
508 | /* get mask for online CPUs */ | |
509 | if (path_exist(_PATH_SYS_SYSTEM "/cpu/online")) { | |
510 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
511 | desc->online = path_cpulist(_PATH_SYS_SYSTEM "/cpu/online"); | |
512 | desc->nthreads = CPU_COUNT_S(setsize, desc->online); | |
513 | } | |
514 | ||
515 | /* get dispatching mode */ | |
516 | if (path_exist(_PATH_SYS_SYSTEM "/cpu/dispatching")) | |
517 | desc->dispatching = path_getnum(_PATH_SYS_SYSTEM "/cpu/dispatching"); | |
518 | else | |
519 | desc->dispatching = -1; | |
520 | } | |
521 | ||
522 | static int | |
523 | has_pci_device(int vendor, int device) | |
524 | { | |
525 | FILE *f; | |
526 | int num, fn, ven, dev; | |
527 | int res = 1; | |
528 | ||
529 | f = path_fopen("r", 0, _PATH_PROC_PCIDEVS); | |
530 | if (!f) | |
531 | return 0; | |
532 | ||
533 | /* for more details about bus/pci/devices format see | |
534 | * drivers/pci/proc.c in linux kernel | |
535 | */ | |
536 | while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]", | |
537 | &num, &fn, &ven, &dev) == 4) { | |
538 | ||
539 | if (ven == vendor && dev == device) | |
540 | goto found; | |
541 | } | |
542 | ||
543 | res = 0; | |
544 | found: | |
545 | fclose(f); | |
546 | return res; | |
547 | } | |
548 | ||
549 | #if defined(__x86_64__) || defined(__i386__) | |
550 | ||
551 | /* | |
552 | * This CPUID leaf returns the information about the hypervisor. | |
553 | * EAX : maximum input value for CPUID supported by the hypervisor. | |
554 | * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware. | |
555 | */ | |
556 | #define HYPERVISOR_INFO_LEAF 0x40000000 | |
557 | ||
558 | static inline void | |
559 | cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, | |
560 | unsigned int *ecx, unsigned int *edx) | |
561 | { | |
562 | __asm__( | |
563 | #if defined(__PIC__) && defined(__i386__) | |
564 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
565 | "pushl %%ebx;" | |
566 | "cpuid;" | |
567 | "movl %%ebx, %%esi;" | |
568 | "popl %%ebx;" | |
569 | : "=S" (*ebx), | |
570 | #else | |
571 | "cpuid;" | |
572 | : "=b" (*ebx), | |
573 | #endif | |
574 | "=a" (*eax), | |
575 | "=c" (*ecx), | |
576 | "=d" (*edx) | |
577 | : "1" (op), "c"(0)); | |
578 | } | |
579 | ||
580 | static void | |
581 | read_hypervisor_cpuid(struct lscpu_desc *desc) | |
582 | { | |
583 | unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; | |
584 | char hyper_vendor_id[13]; | |
585 | ||
586 | memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id)); | |
587 | ||
588 | cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx); | |
589 | memcpy(hyper_vendor_id + 0, &ebx, 4); | |
590 | memcpy(hyper_vendor_id + 4, &ecx, 4); | |
591 | memcpy(hyper_vendor_id + 8, &edx, 4); | |
592 | hyper_vendor_id[12] = '\0'; | |
593 | ||
594 | if (!hyper_vendor_id[0]) | |
595 | return; | |
596 | ||
597 | if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12)) | |
598 | desc->hyper = HYPER_XEN; | |
599 | else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9)) | |
600 | desc->hyper = HYPER_KVM; | |
601 | else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12)) | |
602 | desc->hyper = HYPER_MSHV; | |
603 | else if (!strncmp("VMwareVMware", hyper_vendor_id, 12)) | |
604 | desc->hyper = HYPER_VMWARE; | |
605 | } | |
606 | ||
607 | #else /* ! __x86_64__ */ | |
608 | static void | |
609 | read_hypervisor_cpuid(struct lscpu_desc *desc) | |
610 | { | |
611 | } | |
612 | #endif | |
613 | ||
614 | static void | |
615 | read_hypervisor(struct lscpu_desc *desc) | |
616 | { | |
617 | read_hypervisor_cpuid(desc); | |
618 | ||
619 | if (desc->hyper) | |
620 | /* hvm */ | |
621 | desc->virtype = VIRT_FULL; | |
622 | ||
623 | else if (path_exist(_PATH_PROC_XEN)) { | |
624 | /* Xen para-virt or dom0 */ | |
625 | FILE *fd = path_fopen("r", 0, _PATH_PROC_XENCAP); | |
626 | int dom0 = 0; | |
627 | ||
628 | if (fd) { | |
629 | char buf[256]; | |
630 | ||
631 | if (fscanf(fd, "%s", buf) == 1 && | |
632 | !strcmp(buf, "control_d")) | |
633 | dom0 = 1; | |
634 | fclose(fd); | |
635 | } | |
636 | desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA; | |
637 | desc->hyper = HYPER_XEN; | |
638 | ||
639 | } else if (has_pci_device(0x5853, 0x0001)) { | |
640 | /* Xen full-virt on non-x86_64 */ | |
641 | desc->hyper = HYPER_XEN; | |
642 | desc->virtype = VIRT_FULL; | |
643 | } else if (path_exist(_PATH_PROC_SYSINFO)) { | |
644 | FILE *fd = path_fopen("r", 0, _PATH_PROC_SYSINFO); | |
645 | char buf[BUFSIZ]; | |
646 | ||
647 | desc->hyper = HYPER_IBM; | |
648 | desc->virtype = VIRT_FULL; | |
649 | while (fgets(buf, sizeof(buf), fd) != NULL) { | |
650 | if (!strstr(buf, "Control Program:")) | |
651 | continue; | |
652 | if (!strstr(buf, "KVM")) | |
653 | desc->hyper = HYPER_IBM; | |
654 | else | |
655 | desc->hyper = HYPER_KVM; | |
656 | } | |
657 | fclose(fd); | |
658 | } | |
659 | } | |
660 | ||
661 | /* add @set to the @ary, unnecesary set is deallocated. */ | |
662 | static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set) | |
663 | { | |
664 | int i; | |
665 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
666 | ||
667 | if (!ary) | |
668 | return -1; | |
669 | ||
670 | for (i = 0; i < *items; i++) { | |
671 | if (CPU_EQUAL_S(setsize, set, ary[i])) | |
672 | break; | |
673 | } | |
674 | if (i == *items) { | |
675 | ary[*items] = set; | |
676 | ++*items; | |
677 | return 0; | |
678 | } | |
679 | CPU_FREE(set); | |
680 | return 1; | |
681 | } | |
682 | ||
683 | static void | |
684 | read_topology(struct lscpu_desc *desc, int num) | |
685 | { | |
686 | cpu_set_t *thread_siblings, *core_siblings, *book_siblings; | |
687 | ||
688 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/topology/thread_siblings", num)) | |
689 | return; | |
690 | ||
691 | thread_siblings = path_cpuset(_PATH_SYS_CPU | |
692 | "/cpu%d/topology/thread_siblings", num); | |
693 | core_siblings = path_cpuset(_PATH_SYS_CPU | |
694 | "/cpu%d/topology/core_siblings", num); | |
695 | book_siblings = NULL; | |
696 | if (path_exist(_PATH_SYS_CPU "/cpu%d/topology/book_siblings", num)) { | |
697 | book_siblings = path_cpuset(_PATH_SYS_CPU | |
698 | "/cpu%d/topology/book_siblings", num); | |
699 | } | |
700 | ||
701 | if (!desc->coremaps) { | |
702 | int nbooks, nsockets, ncores, nthreads; | |
703 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
704 | ||
705 | /* threads within one core */ | |
706 | nthreads = CPU_COUNT_S(setsize, thread_siblings); | |
707 | /* cores within one socket */ | |
708 | ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads; | |
709 | /* number of sockets within one book. | |
710 | * Because of odd / non-present cpu maps and to keep | |
711 | * calculation easy we make sure that nsockets and | |
712 | * nbooks is at least 1. | |
713 | */ | |
714 | nsockets = desc->ncpus / nthreads / ncores ?: 1; | |
715 | /* number of books */ | |
716 | nbooks = desc->ncpus / nthreads / ncores / nsockets ?: 1; | |
717 | ||
718 | /* all threads, see also read_basicinfo() | |
719 | * -- fallback for kernels without | |
720 | * /sys/devices/system/cpu/online. | |
721 | */ | |
722 | if (!desc->nthreads) | |
723 | desc->nthreads = nbooks * nsockets * ncores * nthreads; | |
724 | /* For each map we make sure that it can have up to ncpus | |
725 | * entries. This is because we cannot reliably calculate the | |
726 | * number of cores, sockets and books on all architectures. | |
727 | * E.g. completely virtualized architectures like s390 may | |
728 | * have multiple sockets of different sizes. | |
729 | */ | |
730 | desc->coremaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *)); | |
731 | desc->socketmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *)); | |
732 | if (book_siblings) | |
733 | desc->bookmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *)); | |
734 | } | |
735 | ||
736 | add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings); | |
737 | add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings); | |
738 | if (book_siblings) | |
739 | add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings); | |
740 | } | |
741 | static void | |
742 | read_polarization(struct lscpu_desc *desc, int num) | |
743 | { | |
744 | char mode[64]; | |
745 | ||
746 | if (desc->dispatching < 0) | |
747 | return; | |
748 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/polarization", num)) | |
749 | return; | |
750 | if (!desc->polarization) | |
751 | desc->polarization = xcalloc(desc->ncpus, sizeof(int)); | |
752 | path_getstr(mode, sizeof(mode), _PATH_SYS_CPU "/cpu%d/polarization", num); | |
753 | if (strncmp(mode, "vertical:low", sizeof(mode)) == 0) | |
754 | desc->polarization[num] = POLAR_VLOW; | |
755 | else if (strncmp(mode, "vertical:medium", sizeof(mode)) == 0) | |
756 | desc->polarization[num] = POLAR_VMEDIUM; | |
757 | else if (strncmp(mode, "vertical:high", sizeof(mode)) == 0) | |
758 | desc->polarization[num] = POLAR_VHIGH; | |
759 | else if (strncmp(mode, "horizontal", sizeof(mode)) == 0) | |
760 | desc->polarization[num] = POLAR_HORIZONTAL; | |
761 | else | |
762 | desc->polarization[num] = POLAR_UNKNOWN; | |
763 | } | |
764 | ||
765 | static int | |
766 | cachecmp(const void *a, const void *b) | |
767 | { | |
768 | struct cpu_cache *c1 = (struct cpu_cache *) a; | |
769 | struct cpu_cache *c2 = (struct cpu_cache *) b; | |
770 | ||
771 | return strcmp(c2->name, c1->name); | |
772 | } | |
773 | ||
774 | static void | |
775 | read_cache(struct lscpu_desc *desc, int num) | |
776 | { | |
777 | char buf[256]; | |
778 | int i; | |
779 | ||
780 | if (!desc->ncaches) { | |
781 | while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d/cache/index%d", | |
782 | num, desc->ncaches)) | |
783 | desc->ncaches++; | |
784 | ||
785 | if (!desc->ncaches) | |
786 | return; | |
787 | ||
788 | desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches)); | |
789 | } | |
790 | for (i = 0; i < desc->ncaches; i++) { | |
791 | struct cpu_cache *ca = &desc->caches[i]; | |
792 | cpu_set_t *map; | |
793 | ||
794 | if (!ca->name) { | |
795 | int type, level; | |
796 | ||
797 | /* cache type */ | |
798 | path_getstr(buf, sizeof(buf), | |
799 | _PATH_SYS_CPU "/cpu%d/cache/index%d/type", | |
800 | num, i); | |
801 | if (!strcmp(buf, "Data")) | |
802 | type = 'd'; | |
803 | else if (!strcmp(buf, "Instruction")) | |
804 | type = 'i'; | |
805 | else | |
806 | type = 0; | |
807 | ||
808 | /* cache level */ | |
809 | level = path_getnum(_PATH_SYS_CPU "/cpu%d/cache/index%d/level", | |
810 | num, i); | |
811 | if (type) | |
812 | snprintf(buf, sizeof(buf), "L%d%c", level, type); | |
813 | else | |
814 | snprintf(buf, sizeof(buf), "L%d", level); | |
815 | ||
816 | ca->name = xstrdup(buf); | |
817 | ||
818 | /* cache size */ | |
819 | path_getstr(buf, sizeof(buf), | |
820 | _PATH_SYS_CPU "/cpu%d/cache/index%d/size", | |
821 | num, i); | |
822 | ca->size = xstrdup(buf); | |
823 | } | |
824 | ||
825 | /* information about how CPUs share different caches */ | |
826 | map = path_cpuset(_PATH_SYS_CPU "/cpu%d/cache/index%d/shared_cpu_map", | |
827 | num, i); | |
828 | ||
829 | if (!ca->sharedmaps) | |
830 | ca->sharedmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *)); | |
831 | add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map); | |
832 | } | |
833 | } | |
834 | ||
835 | static void | |
836 | read_nodes(struct lscpu_desc *desc) | |
837 | { | |
838 | int i; | |
839 | ||
840 | /* number of NUMA node */ | |
841 | while (path_exist(_PATH_SYS_SYSTEM "/node/node%d", desc->nnodes)) | |
842 | desc->nnodes++; | |
843 | ||
844 | if (!desc->nnodes) | |
845 | return; | |
846 | ||
847 | desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *)); | |
848 | ||
849 | /* information about how nodes share different CPUs */ | |
850 | for (i = 0; i < desc->nnodes; i++) | |
851 | desc->nodemaps[i] = path_cpuset( | |
852 | _PATH_SYS_SYSTEM "/node/node%d/cpumap", | |
853 | i); | |
854 | } | |
855 | ||
856 | static void | |
857 | print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible) | |
858 | { | |
859 | int j; | |
860 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
861 | ||
862 | switch (col) { | |
863 | case COL_CPU: | |
864 | printf("%d", i); | |
865 | break; | |
866 | case COL_CORE: | |
867 | for (j = 0; j < desc->ncores; j++) { | |
868 | if (CPU_ISSET_S(i, setsize, desc->coremaps[j])) { | |
869 | printf("%d", j); | |
870 | break; | |
871 | } | |
872 | } | |
873 | break; | |
874 | case COL_SOCKET: | |
875 | for (j = 0; j < desc->nsockets; j++) { | |
876 | if (CPU_ISSET_S(i, setsize, desc->socketmaps[j])) { | |
877 | printf("%d", j); | |
878 | break; | |
879 | } | |
880 | } | |
881 | break; | |
882 | case COL_NODE: | |
883 | for (j = 0; j < desc->nnodes; j++) { | |
884 | if (CPU_ISSET_S(i, setsize, desc->nodemaps[j])) { | |
885 | printf("%d", j); | |
886 | break; | |
887 | } | |
888 | } | |
889 | break; | |
890 | case COL_BOOK: | |
891 | for (j = 0; j < desc->nbooks; j++) { | |
892 | if (CPU_ISSET_S(i, setsize, desc->bookmaps[j])) { | |
893 | printf("%d", j); | |
894 | break; | |
895 | } | |
896 | } | |
897 | break; | |
898 | case COL_CACHE: | |
899 | for (j = desc->ncaches - 1; j >= 0; j--) { | |
900 | struct cpu_cache *ca = &desc->caches[j]; | |
901 | int x; | |
902 | ||
903 | for (x = 0; x < ca->nsharedmaps; x++) { | |
904 | if (CPU_ISSET_S(i, setsize, ca->sharedmaps[x])) { | |
905 | if (j != desc->ncaches - 1) | |
906 | putchar(compatible ? ',' : ':'); | |
907 | printf("%d", x); | |
908 | break; | |
909 | } | |
910 | } | |
911 | if (x == ca->nsharedmaps) | |
912 | putchar(','); | |
913 | } | |
914 | break; | |
915 | case COL_POLARIZATION: | |
916 | if (desc->polarization) | |
917 | printf("%s", polar_modes[desc->polarization[i]]); | |
918 | break; | |
919 | } | |
920 | } | |
921 | ||
922 | /* | |
923 | * We support two formats: | |
924 | * | |
925 | * 1) "compatible" -- this format is compatible with the original lscpu(1) | |
926 | * output and it contains fixed set of the columns. The CACHE columns are at | |
927 | * the end of the line and the CACHE is not printed if the number of the caches | |
928 | * is zero. The CACHE columns are separated by two commas, for example: | |
929 | * | |
930 | * $ lscpu --parse | |
931 | * # CPU,Core,Socket,Node,,L1d,L1i,L2 | |
932 | * 0,0,0,0,,0,0,0 | |
933 | * 1,1,0,0,,1,1,0 | |
934 | * | |
935 | * 2) "user defined output" -- this format prints always all columns without | |
936 | * special prefix for CACHE column. If there are not CACHEs then the column is | |
937 | * empty and the header "Cache" is printed rather than a real name of the cache. | |
938 | * The CACHE columns are separated by ':'. | |
939 | * | |
940 | * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE | |
941 | * # CPU,Core,Socket,Node,L1d:L1i:L2 | |
942 | * 0,0,0,0,0:0:0 | |
943 | * 1,1,0,0,1:1:0 | |
944 | */ | |
945 | static void | |
946 | print_parsable(struct lscpu_desc *desc, int cols[], int ncols, int compatible) | |
947 | { | |
948 | int i, c; | |
949 | ||
950 | printf(_( | |
951 | "# The following is the parsable format, which can be fed to other\n" | |
952 | "# programs. Each different item in every column has an unique ID\n" | |
953 | "# starting from zero.\n")); | |
954 | ||
955 | fputs("# ", stdout); | |
956 | for (i = 0; i < ncols; i++) { | |
957 | if (cols[i] == COL_CACHE) { | |
958 | if (compatible && !desc->ncaches) | |
959 | continue; | |
960 | if (i > 0) | |
961 | putchar(','); | |
962 | if (compatible && i != 0) | |
963 | putchar(','); | |
964 | for (c = desc->ncaches - 1; c >= 0; c--) { | |
965 | printf("%s", desc->caches[c].name); | |
966 | if (c > 0) | |
967 | putchar(compatible ? ',' : ':'); | |
968 | } | |
969 | if (!desc->ncaches) | |
970 | fputs(colnames[cols[i]], stdout); | |
971 | } else { | |
972 | if (i > 0) | |
973 | putchar(','); | |
974 | fputs(colnames[cols[i]], stdout); | |
975 | } | |
976 | } | |
977 | putchar('\n'); | |
978 | ||
979 | for (i = 0; i < desc->ncpus; i++) { | |
980 | if (desc->online && !is_cpu_online(desc, i)) | |
981 | continue; | |
982 | for (c = 0; c < ncols; c++) { | |
983 | if (compatible && cols[c] == COL_CACHE) { | |
984 | if (!desc->ncaches) | |
985 | continue; | |
986 | if (c > 0) | |
987 | putchar(','); | |
988 | } | |
989 | if (c > 0) | |
990 | putchar(','); | |
991 | print_parsable_cell(desc, i, cols[c], compatible); | |
992 | } | |
993 | putchar('\n'); | |
994 | } | |
995 | } | |
996 | ||
997 | ||
998 | /* output formats "<key> <value>"*/ | |
999 | #define print_s(_key, _val) printf("%-23s%s\n", _key, _val) | |
1000 | #define print_n(_key, _val) printf("%-23s%d\n", _key, _val) | |
1001 | ||
1002 | static void | |
1003 | print_cpuset(const char *key, cpu_set_t *set, int hex) | |
1004 | { | |
1005 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1006 | size_t setbuflen = 7 * maxcpus; | |
1007 | char setbuf[setbuflen], *p; | |
1008 | ||
1009 | if (hex) { | |
1010 | p = cpumask_create(setbuf, setbuflen, set, setsize); | |
1011 | printf("%-23s0x%s\n", key, p); | |
1012 | } else { | |
1013 | p = cpulist_create(setbuf, setbuflen, set, setsize); | |
1014 | print_s(key, p); | |
1015 | } | |
1016 | ||
1017 | } | |
1018 | ||
1019 | static void | |
1020 | print_readable(struct lscpu_desc *desc, int hex) | |
1021 | { | |
1022 | char buf[512]; | |
1023 | int i; | |
1024 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1025 | ||
1026 | print_s(_("Architecture:"), desc->arch); | |
1027 | ||
1028 | if (desc->mode) { | |
1029 | char buf[64], *p = buf; | |
1030 | ||
1031 | if (desc->mode & MODE_32BIT) { | |
1032 | strcpy(p, "32-bit, "); | |
1033 | p += 8; | |
1034 | } | |
1035 | if (desc->mode & MODE_64BIT) { | |
1036 | strcpy(p, "64-bit, "); | |
1037 | p += 8; | |
1038 | } | |
1039 | *(p - 2) = '\0'; | |
1040 | print_s(_("CPU op-mode(s):"), buf); | |
1041 | } | |
1042 | #if !defined(WORDS_BIGENDIAN) | |
1043 | print_s(_("Byte Order:"), "Little Endian"); | |
1044 | #else | |
1045 | print_s(_("Byte Order:"), "Big Endian"); | |
1046 | #endif | |
1047 | print_n(_("CPU(s):"), desc->ncpus); | |
1048 | ||
1049 | if (desc->online) | |
1050 | print_cpuset(hex ? _("On-line CPU(s) mask:") : | |
1051 | _("On-line CPU(s) list:"), | |
1052 | desc->online, hex); | |
1053 | ||
1054 | if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) { | |
1055 | cpu_set_t *set; | |
1056 | ||
1057 | /* Linux kernel provides cpuset of off-line CPUs that contains | |
1058 | * all configured CPUs (see /sys/devices/system/cpu/offline), | |
1059 | * but want to print real (present in system) off-line CPUs only. | |
1060 | */ | |
1061 | set = cpuset_alloc(maxcpus, NULL, NULL); | |
1062 | if (!set) | |
1063 | err(EXIT_FAILURE, _("failed to callocate cpu set")); | |
1064 | CPU_ZERO_S(setsize, set); | |
1065 | for (i = 0; i < desc->ncpus; i++) { | |
1066 | if (!is_cpu_online(desc, i)) | |
1067 | CPU_SET_S(i, setsize, set); | |
1068 | } | |
1069 | print_cpuset(hex ? _("Off-line CPU(s) mask:") : | |
1070 | _("Off-line CPU(s) list:"), | |
1071 | set, hex); | |
1072 | cpuset_free(set); | |
1073 | } | |
1074 | ||
1075 | if (desc->nsockets) { | |
1076 | int cores_per_socket, sockets_per_book, books; | |
1077 | ||
1078 | cores_per_socket = sockets_per_book = books = 0; | |
1079 | /* s390 detects its cpu topology via /proc/sysinfo, if present. | |
1080 | * Using simply the cpu topology masks in sysfs will not give | |
1081 | * usable results since everything is virtualized. E.g. | |
1082 | * virtual core 0 may have only 1 cpu, but virtual core 2 may | |
1083 | * five cpus. | |
1084 | * If the cpu topology is not exported (e.g. 2nd level guest) | |
1085 | * fall back to old calculation scheme. | |
1086 | */ | |
1087 | if (path_exist(_PATH_PROC_SYSINFO)) { | |
1088 | FILE *fd = path_fopen("r", 0, _PATH_PROC_SYSINFO); | |
1089 | char buf[BUFSIZ]; | |
1090 | int t0, t1, t2; | |
1091 | ||
1092 | while (fgets(buf, sizeof(buf), fd) != NULL) { | |
1093 | if (sscanf(buf, "CPU Topology SW:%d%d%d%d%d%d", | |
1094 | &t0, &t1, &t2, &books, &sockets_per_book, | |
1095 | &cores_per_socket) == 6) | |
1096 | break; | |
1097 | } | |
1098 | } | |
1099 | print_n(_("Thread(s) per core:"), desc->nthreads / desc->ncores); | |
1100 | print_n(_("Core(s) per socket:"), | |
1101 | cores_per_socket ?: desc->ncores / desc->nsockets); | |
1102 | if (desc->nbooks) { | |
1103 | print_n(_("Socket(s) per book:"), | |
1104 | sockets_per_book ?: desc->nsockets / desc->nbooks); | |
1105 | print_n(_("Book(s):"), books ?: desc->nbooks); | |
1106 | } else { | |
1107 | print_n(_("Socket(s):"), sockets_per_book ?: desc->nsockets); | |
1108 | } | |
1109 | } | |
1110 | if (desc->nnodes) | |
1111 | print_n(_("NUMA node(s):"), desc->nnodes); | |
1112 | if (desc->vendor) | |
1113 | print_s(_("Vendor ID:"), desc->vendor); | |
1114 | if (desc->family) | |
1115 | print_s(_("CPU family:"), desc->family); | |
1116 | if (desc->model) | |
1117 | print_s(_("Model:"), desc->model); | |
1118 | if (desc->stepping) | |
1119 | print_s(_("Stepping:"), desc->stepping); | |
1120 | if (desc->mhz) | |
1121 | print_s(_("CPU MHz:"), desc->mhz); | |
1122 | if (desc->bogomips) | |
1123 | print_s(_("BogoMIPS:"), desc->bogomips); | |
1124 | if (desc->virtflag) { | |
1125 | if (!strcmp(desc->virtflag, "svm")) | |
1126 | print_s(_("Virtualization:"), "AMD-V"); | |
1127 | else if (!strcmp(desc->virtflag, "vmx")) | |
1128 | print_s(_("Virtualization:"), "VT-x"); | |
1129 | } | |
1130 | if (desc->hyper) { | |
1131 | print_s(_("Hypervisor vendor:"), hv_vendors[desc->hyper]); | |
1132 | print_s(_("Virtualization type:"), virt_types[desc->virtype]); | |
1133 | } | |
1134 | if (desc->dispatching >= 0) | |
1135 | print_s(_("Dispatching mode:"), disp_modes[desc->dispatching]); | |
1136 | if (desc->ncaches) { | |
1137 | char buf[512]; | |
1138 | int i; | |
1139 | ||
1140 | for (i = desc->ncaches - 1; i >= 0; i--) { | |
1141 | snprintf(buf, sizeof(buf), | |
1142 | _("%s cache:"), desc->caches[i].name); | |
1143 | print_s(buf, desc->caches[i].size); | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | for (i = 0; i < desc->nnodes; i++) { | |
1148 | snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), i); | |
1149 | print_cpuset(buf, desc->nodemaps[i], hex); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | static void __attribute__((__noreturn__)) usage(FILE *out) | |
1154 | { | |
1155 | fprintf(out, _( | |
1156 | "\nUsage:\n" | |
1157 | " %s [options]\n"), program_invocation_short_name); | |
1158 | ||
1159 | puts(_( "\nOptions:\n" | |
1160 | " -h, --help print this help\n" | |
1161 | " -p, --parse[=LIST] print out a parsable instead of a readable format\n" | |
1162 | " -s, --sysroot DIR use directory DIR as system root\n" | |
1163 | " -x, --hex print hexadecimal masks rather than lists of CPUs\n")); | |
1164 | ||
1165 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
1166 | } | |
1167 | ||
1168 | int main(int argc, char *argv[]) | |
1169 | { | |
1170 | struct lscpu_desc _desc, *desc = &_desc; | |
1171 | int parsable = 0, c, i, hex = 0; | |
1172 | int columns[ARRAY_SIZE(colnames)], ncolumns = 0; | |
1173 | int compatible = 0; | |
1174 | ||
1175 | static const struct option longopts[] = { | |
1176 | { "help", no_argument, 0, 'h' }, | |
1177 | { "parse", optional_argument, 0, 'p' }, | |
1178 | { "sysroot", required_argument, 0, 's' }, | |
1179 | { "hex", no_argument, 0, 'x' }, | |
1180 | { NULL, 0, 0, 0 } | |
1181 | }; | |
1182 | ||
1183 | setlocale(LC_ALL, ""); | |
1184 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1185 | textdomain(PACKAGE); | |
1186 | ||
1187 | while ((c = getopt_long(argc, argv, "hp::s:x", longopts, NULL)) != -1) { | |
1188 | switch (c) { | |
1189 | case 'h': | |
1190 | usage(stdout); | |
1191 | case 'p': | |
1192 | parsable = 1; | |
1193 | if (optarg) { | |
1194 | if (*optarg == '=') | |
1195 | optarg++; | |
1196 | ncolumns = string_to_idarray(optarg, | |
1197 | columns, ARRAY_SIZE(columns), | |
1198 | column_name_to_id); | |
1199 | if (ncolumns < 0) | |
1200 | return EXIT_FAILURE; | |
1201 | } else { | |
1202 | columns[ncolumns++] = COL_CPU; | |
1203 | columns[ncolumns++] = COL_CORE; | |
1204 | columns[ncolumns++] = COL_SOCKET, | |
1205 | columns[ncolumns++] = COL_NODE, | |
1206 | columns[ncolumns++] = COL_CACHE; | |
1207 | compatible = 1; | |
1208 | } | |
1209 | break; | |
1210 | case 's': | |
1211 | sysrootlen = strlen(optarg); | |
1212 | strncpy(pathbuf, optarg, sizeof(pathbuf)); | |
1213 | pathbuf[sizeof(pathbuf) - 1] = '\0'; | |
1214 | break; | |
1215 | case 'x': | |
1216 | hex = 1; | |
1217 | break; | |
1218 | default: | |
1219 | usage(stderr); | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | memset(desc, 0, sizeof(*desc)); | |
1224 | ||
1225 | read_basicinfo(desc); | |
1226 | ||
1227 | for (i = 0; i < desc->ncpus; i++) { | |
1228 | if (desc->online && !is_cpu_online(desc, i)) | |
1229 | continue; | |
1230 | read_topology(desc, i); | |
1231 | read_cache(desc, i); | |
1232 | read_polarization(desc, i); | |
1233 | } | |
1234 | ||
1235 | qsort(desc->caches, desc->ncaches, sizeof(struct cpu_cache), cachecmp); | |
1236 | ||
1237 | read_nodes(desc); | |
1238 | ||
1239 | read_hypervisor(desc); | |
1240 | ||
1241 | /* Show time! */ | |
1242 | if (parsable) | |
1243 | print_parsable(desc, columns, ncolumns, compatible); | |
1244 | else | |
1245 | print_readable(desc, hex); | |
1246 | ||
1247 | return EXIT_SUCCESS; | |
1248 | } |