]>
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> | |
39561c70 KZ |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
5dd7507c | 35 | |
7e03f383 | 36 | #include "cpuset.h" |
5dd7507c | 37 | #include "nls.h" |
4581647a | 38 | #include "xalloc.h" |
eb76ca98 | 39 | #include "c.h" |
477251f8 KZ |
40 | #include "strutils.h" |
41 | #include "bitops.h" | |
42 | ||
5dd7507c CQ |
43 | |
44 | #define CACHE_MAX 100 | |
45 | ||
46 | /* /sys paths */ | |
d50363cd | 47 | #define _PATH_SYS_SYSTEM "/sys/devices/system" |
7e03f383 | 48 | #define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu" |
d50363cd | 49 | #define _PATH_PROC_XEN "/proc/xen" |
c8b64f6d | 50 | #define _PATH_PROC_XENCAP _PATH_PROC_XEN "/capabilities" |
d50363cd KZ |
51 | #define _PATH_PROC_CPUINFO "/proc/cpuinfo" |
52 | #define _PATH_PROC_PCIDEVS "/proc/bus/pci/devices" | |
b8ec7bdf | 53 | #define _PATH_PROC_SYSINFO "/proc/sysinfo" |
5dd7507c | 54 | |
c8b64f6d KZ |
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, | |
99fbc877 | 72 | HYPER_MSHV, |
b8ec7bdf HC |
73 | HYPER_VMWARE, |
74 | HYPER_IBM | |
c8b64f6d KZ |
75 | }; |
76 | const char *hv_vendors[] = { | |
77 | [HYPER_NONE] = NULL, | |
78 | [HYPER_XEN] = "Xen", | |
79 | [HYPER_KVM] = "KVM", | |
99fbc877 | 80 | [HYPER_MSHV] = "Microsoft", |
b8ec7bdf HC |
81 | [HYPER_VMWARE] = "VMware", |
82 | [HYPER_IBM] = "IBM" | |
c8b64f6d KZ |
83 | }; |
84 | ||
f633ad4e | 85 | /* CPU modes */ |
79e8b41a | 86 | enum { |
f633ad4e KZ |
87 | MODE_32BIT = (1 << 1), |
88 | MODE_64BIT = (1 << 2) | |
79e8b41a | 89 | }; |
c8b64f6d | 90 | |
e8aa16ee KZ |
91 | /* cache(s) description */ |
92 | struct cpu_cache { | |
7e03f383 KZ |
93 | char *name; |
94 | char *size; | |
95 | ||
96 | int nsharedmaps; | |
97 | cpu_set_t **sharedmaps; | |
e8aa16ee KZ |
98 | }; |
99 | ||
a0fff77e HC |
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 | ||
2b8fcb81 HC |
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 | ||
e8aa16ee KZ |
128 | /* global description */ |
129 | struct lscpu_desc { | |
5dd7507c CQ |
130 | char *arch; |
131 | char *vendor; | |
132 | char *family; | |
133 | char *model; | |
c8b64f6d KZ |
134 | char *virtflag; /* virtualization flag (vmx, svm) */ |
135 | int hyper; /* hypervisor vendor ID */ | |
136 | int virtype; /* VIRT_PARA|FULL|NONE ? */ | |
5dd7507c CQ |
137 | char *mhz; |
138 | char *stepping; | |
9b8d4d5f | 139 | char *bogomips; |
5dd7507c | 140 | char *flags; |
a0fff77e | 141 | int dispatching; /* none, horizontal or vertical */ |
79e8b41a KZ |
142 | int mode; /* rm, lm or/and tm */ |
143 | ||
7e03f383 | 144 | int ncpus; /* number of CPUs */ |
aac1e59e | 145 | cpu_set_t *online; /* mask with online CPUs */ |
7e03f383 KZ |
146 | |
147 | int nnodes; /* number of NUMA modes */ | |
148 | cpu_set_t **nodemaps; /* array with NUMA nodes */ | |
149 | ||
56baaa4e HC |
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 | ||
7e03f383 KZ |
155 | /* sockets -- based on core_siblings (internal kernel map of cpuX's |
156 | * hardware threads within the same physical_package_id (socket)) */ | |
e282eec2 | 157 | int nsockets; /* number of all online sockets */ |
7e03f383 KZ |
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) */ | |
e282eec2 | 162 | int ncores; /* number of all online cores */ |
7e03f383 KZ |
163 | cpu_set_t **coremaps; /* unique thread_siblings */ |
164 | ||
e282eec2 | 165 | int nthreads; /* number of online threads */ |
7e03f383 KZ |
166 | |
167 | int ncaches; | |
168 | struct cpu_cache *caches; | |
2b8fcb81 HC |
169 | |
170 | int *polarization; /* cpu polarization */ | |
5dd7507c CQ |
171 | }; |
172 | ||
d50363cd KZ |
173 | static size_t sysrootlen; |
174 | static char pathbuf[PATH_MAX]; | |
7e03f383 | 175 | static int maxcpus; /* size in bits of kernel cpu mask */ |
5dd7507c | 176 | |
aac1e59e KZ |
177 | #define is_cpu_online(_d, _cpu) \ |
178 | ((_d) && (_d)->online ? \ | |
179 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0) | |
180 | ||
d50363cd KZ |
181 | static FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...) |
182 | __attribute__ ((__format__ (__printf__, 3, 4))); | |
ef173bde KZ |
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))); | |
5dd7507c CQ |
187 | static int path_exist(const char *path, ...) |
188 | __attribute__ ((__format__ (__printf__, 1, 2))); | |
7e03f383 | 189 | static cpu_set_t *path_cpuset(const char *path, ...) |
5dd7507c CQ |
190 | __attribute__ ((__format__ (__printf__, 1, 2))); |
191 | ||
477251f8 KZ |
192 | /* |
193 | * Parsable output | |
194 | */ | |
195 | enum { | |
196 | COL_CPU, | |
197 | COL_CORE, | |
198 | COL_SOCKET, | |
199 | COL_NODE, | |
200 | COL_BOOK, | |
2b8fcb81 HC |
201 | COL_CACHE, |
202 | COL_POLARIZATION | |
477251f8 KZ |
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", | |
2b8fcb81 HC |
212 | [COL_CACHE] = "Cache", |
213 | [COL_POLARIZATION] = "Polarization" | |
477251f8 KZ |
214 | }; |
215 | ||
216 | ||
217 | static int column_name_to_id(const char *name, size_t namesz) | |
218 | { | |
329fd1c3 | 219 | size_t i; |
477251f8 KZ |
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 | ||
d50363cd KZ |
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 | ||
5dd7507c | 242 | static FILE * |
d50363cd | 243 | path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap) |
5dd7507c | 244 | { |
d50363cd KZ |
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; | |
5dd7507c CQ |
252 | } |
253 | ||
254 | static FILE * | |
d50363cd | 255 | path_fopen(const char *mode, int exit_on_error, const char *path, ...) |
5dd7507c | 256 | { |
d50363cd KZ |
257 | FILE *fd; |
258 | va_list ap; | |
259 | ||
260 | va_start(ap, path); | |
fe81c7d1 | 261 | fd = path_vfopen(mode, exit_on_error, path, ap); |
d50363cd KZ |
262 | va_end(ap); |
263 | ||
264 | return fd; | |
5dd7507c CQ |
265 | } |
266 | ||
267 | static void | |
ef173bde KZ |
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); | |
d50363cd | 274 | fd = path_vfopen("r", 1, path, ap); |
ef173bde KZ |
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, ...) | |
5dd7507c CQ |
288 | { |
289 | FILE *fd; | |
290 | va_list ap; | |
ef173bde | 291 | int result; |
5dd7507c CQ |
292 | |
293 | va_start(ap, path); | |
d50363cd | 294 | fd = path_vfopen("r", 1, path, ap); |
5dd7507c CQ |
295 | va_end(ap); |
296 | ||
ef173bde | 297 | if (fscanf(fd, "%d", &result) != 1) { |
5dd7507c | 298 | if (ferror(fd)) |
d50363cd | 299 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); |
5dd7507c | 300 | else |
d50363cd | 301 | errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); |
5dd7507c CQ |
302 | } |
303 | fclose(fd); | |
ef173bde | 304 | return result; |
5dd7507c CQ |
305 | } |
306 | ||
307 | static int | |
308 | path_exist(const char *path, ...) | |
309 | { | |
310 | va_list ap; | |
d50363cd | 311 | const char *p; |
5dd7507c CQ |
312 | |
313 | va_start(ap, path); | |
d50363cd | 314 | p = path_vcreate(path, ap); |
5dd7507c CQ |
315 | va_end(ap); |
316 | ||
d50363cd | 317 | return access(p, F_OK) == 0; |
5dd7507c CQ |
318 | } |
319 | ||
7e03f383 | 320 | static cpu_set_t * |
aac1e59e | 321 | path_cpuparse(int islist, const char *path, va_list ap) |
5dd7507c | 322 | { |
7e03f383 | 323 | FILE *fd; |
7e03f383 KZ |
324 | cpu_set_t *set; |
325 | size_t setsize, len = maxcpus * 7; | |
326 | char buf[len]; | |
5dd7507c | 327 | |
7e03f383 | 328 | fd = path_vfopen("r", 1, path, ap); |
5dd7507c | 329 | |
7e03f383 KZ |
330 | if (!fgets(buf, len, fd)) |
331 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); | |
332 | fclose(fd); | |
5dd7507c | 333 | |
7e03f383 KZ |
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 | ||
aac1e59e KZ |
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); | |
7e03f383 KZ |
374 | |
375 | return set; | |
5dd7507c CQ |
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 | ||
f633ad4e KZ |
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 | ||
4e740fd8 KZ |
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 | ||
f633ad4e KZ |
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__) || \ | |
c487c90c | 438 | defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) |
f633ad4e KZ |
439 | m |= MODE_32BIT; |
440 | #endif | |
441 | return m; | |
442 | } | |
443 | ||
5dd7507c | 444 | static void |
e8aa16ee | 445 | read_basicinfo(struct lscpu_desc *desc) |
5dd7507c | 446 | { |
d50363cd | 447 | FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO); |
5dd7507c CQ |
448 | char buf[BUFSIZ]; |
449 | struct utsname utsbuf; | |
450 | ||
451 | /* architecture */ | |
452 | if (uname(&utsbuf) == -1) | |
453 | err(EXIT_FAILURE, _("error: uname failed")); | |
e8aa16ee | 454 | desc->arch = xstrdup(utsbuf.machine); |
5dd7507c CQ |
455 | |
456 | /* count CPU(s) */ | |
7e03f383 KZ |
457 | while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d", desc->ncpus)) |
458 | desc->ncpus++; | |
5dd7507c CQ |
459 | |
460 | /* details */ | |
461 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
e8aa16ee KZ |
462 | if (lookup(buf, "vendor", &desc->vendor)) ; |
463 | else if (lookup(buf, "vendor_id", &desc->vendor)) ; | |
e8aa16ee KZ |
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)) ; | |
f633ad4e KZ |
469 | else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */ |
470 | else if (lookup(buf, "features", &desc->flags)) ; /* s390 */ | |
c487c90c | 471 | else if (lookup(buf, "type", &desc->flags)) ; /* sparc64 */ |
9b8d4d5f | 472 | else if (lookup(buf, "bogomips", &desc->bogomips)) ; |
abcd6368 | 473 | else if (lookup(buf, "bogomips per cpu", &desc->bogomips)) ; /* s390 */ |
5dd7507c CQ |
474 | else |
475 | continue; | |
476 | } | |
c8b64f6d | 477 | |
f633ad4e KZ |
478 | desc->mode = init_mode(); |
479 | ||
e8aa16ee KZ |
480 | if (desc->flags) { |
481 | snprintf(buf, sizeof(buf), " %s ", desc->flags); | |
c8b64f6d | 482 | if (strstr(buf, " svm ")) |
e8aa16ee | 483 | desc->virtflag = strdup("svm"); |
c8b64f6d | 484 | else if (strstr(buf, " vmx ")) |
e8aa16ee | 485 | desc->virtflag = strdup("vmx"); |
79e8b41a | 486 | if (strstr(buf, " lm ")) |
f633ad4e KZ |
487 | desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */ |
488 | if (strstr(buf, " zarch ")) | |
489 | desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */ | |
c487c90c KZ |
490 | if (strstr(buf, " sun4v ") || strstr(buf, " sun4u ")) |
491 | desc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */ | |
c8b64f6d KZ |
492 | } |
493 | ||
5dd7507c | 494 | fclose(fp); |
7e03f383 KZ |
495 | |
496 | if (path_exist(_PATH_SYS_SYSTEM "/cpu/kernel_max")) | |
3d6e5c35 GR |
497 | /* note that kernel_max is maximum index [NR_CPUS-1] */ |
498 | maxcpus = path_getnum(_PATH_SYS_SYSTEM "/cpu/kernel_max") + 1; | |
7e03f383 KZ |
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 | |
d0bb6987 | 504 | /* we are reading some /sys snapshot instead of the real /sys, |
7e03f383 KZ |
505 | * let's use any crazy number... */ |
506 | maxcpus = desc->ncpus > 2048 ? desc->ncpus : 2048; | |
aac1e59e KZ |
507 | |
508 | /* get mask for online CPUs */ | |
e282eec2 KZ |
509 | if (path_exist(_PATH_SYS_SYSTEM "/cpu/online")) { |
510 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
5d4ba40d | 511 | desc->online = path_cpulist(_PATH_SYS_SYSTEM "/cpu/online"); |
e282eec2 KZ |
512 | desc->nthreads = CPU_COUNT_S(setsize, desc->online); |
513 | } | |
a0fff77e HC |
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; | |
5dd7507c CQ |
520 | } |
521 | ||
c8b64f6d KZ |
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 | ||
d50363cd | 529 | f = path_fopen("r", 0, _PATH_PROC_PCIDEVS); |
c8b64f6d KZ |
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 | { | |
c9239f23 MF |
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), | |
c8b64f6d KZ |
575 | "=c" (*ecx), |
576 | "=d" (*edx) | |
bc54770d | 577 | : "1" (op), "c"(0)); |
c8b64f6d KZ |
578 | } |
579 | ||
580 | static void | |
e8aa16ee | 581 | read_hypervisor_cpuid(struct lscpu_desc *desc) |
c8b64f6d KZ |
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)) | |
e8aa16ee | 598 | desc->hyper = HYPER_XEN; |
c8b64f6d | 599 | else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9)) |
e8aa16ee | 600 | desc->hyper = HYPER_KVM; |
c8b64f6d | 601 | else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12)) |
e8aa16ee | 602 | desc->hyper = HYPER_MSHV; |
99fbc877 SH |
603 | else if (!strncmp("VMwareVMware", hyper_vendor_id, 12)) |
604 | desc->hyper = HYPER_VMWARE; | |
c8b64f6d KZ |
605 | } |
606 | ||
607 | #else /* ! __x86_64__ */ | |
608 | static void | |
e8aa16ee | 609 | read_hypervisor_cpuid(struct lscpu_desc *desc) |
c8b64f6d KZ |
610 | { |
611 | } | |
612 | #endif | |
613 | ||
614 | static void | |
e8aa16ee | 615 | read_hypervisor(struct lscpu_desc *desc) |
c8b64f6d | 616 | { |
e8aa16ee | 617 | read_hypervisor_cpuid(desc); |
c8b64f6d | 618 | |
e8aa16ee | 619 | if (desc->hyper) |
c8b64f6d | 620 | /* hvm */ |
e8aa16ee | 621 | desc->virtype = VIRT_FULL; |
c8b64f6d | 622 | |
d50363cd | 623 | else if (path_exist(_PATH_PROC_XEN)) { |
c8b64f6d | 624 | /* Xen para-virt or dom0 */ |
d50363cd | 625 | FILE *fd = path_fopen("r", 0, _PATH_PROC_XENCAP); |
c8b64f6d KZ |
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 | } | |
e8aa16ee KZ |
636 | desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA; |
637 | desc->hyper = HYPER_XEN; | |
c8b64f6d KZ |
638 | |
639 | } else if (has_pci_device(0x5853, 0x0001)) { | |
640 | /* Xen full-virt on non-x86_64 */ | |
e8aa16ee KZ |
641 | desc->hyper = HYPER_XEN; |
642 | desc->virtype = VIRT_FULL; | |
b8ec7bdf HC |
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); | |
c8b64f6d KZ |
658 | } |
659 | } | |
660 | ||
7e03f383 KZ |
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 | ||
5dd7507c | 683 | static void |
7e03f383 | 684 | read_topology(struct lscpu_desc *desc, int num) |
5dd7507c | 685 | { |
56baaa4e | 686 | cpu_set_t *thread_siblings, *core_siblings, *book_siblings; |
7e03f383 KZ |
687 | |
688 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/topology/thread_siblings", num)) | |
689 | return; | |
5dd7507c | 690 | |
7e03f383 KZ |
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); | |
56baaa4e HC |
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 | } | |
aac1e59e KZ |
700 | |
701 | if (!desc->coremaps) { | |
56baaa4e | 702 | int nbooks, nsockets, ncores, nthreads; |
7e03f383 KZ |
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; | |
32a46618 HC |
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; | |
56baaa4e | 715 | /* number of books */ |
32a46618 | 716 | nbooks = desc->ncpus / nthreads / ncores / nsockets ?: 1; |
e282eec2 KZ |
717 | |
718 | /* all threads, see also read_basicinfo() | |
32a46618 | 719 | * -- fallback for kernels without |
e282eec2 KZ |
720 | * /sys/devices/system/cpu/online. |
721 | */ | |
722 | if (!desc->nthreads) | |
32a46618 | 723 | desc->nthreads = nbooks * nsockets * ncores * nthreads; |
9d1a3a18 HC |
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 *)); | |
08de16d0 | 732 | if (book_siblings) |
9d1a3a18 | 733 | desc->bookmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *)); |
7e03f383 | 734 | } |
5dd7507c | 735 | |
7e03f383 KZ |
736 | add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings); |
737 | add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings); | |
56baaa4e HC |
738 | if (book_siblings) |
739 | add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings); | |
7e03f383 | 740 | } |
2b8fcb81 HC |
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 | } | |
7e03f383 KZ |
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); | |
5dd7507c CQ |
772 | } |
773 | ||
774 | static void | |
7e03f383 | 775 | read_cache(struct lscpu_desc *desc, int num) |
5dd7507c CQ |
776 | { |
777 | char buf[256]; | |
7e03f383 | 778 | int i; |
5dd7507c | 779 | |
aac1e59e | 780 | if (!desc->ncaches) { |
7e03f383 KZ |
781 | while(path_exist(_PATH_SYS_SYSTEM "/cpu/cpu%d/cache/index%d", |
782 | num, desc->ncaches)) | |
783 | desc->ncaches++; | |
5dd7507c | 784 | |
7e03f383 KZ |
785 | if (!desc->ncaches) |
786 | return; | |
5dd7507c | 787 | |
08de16d0 | 788 | desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches)); |
7e03f383 KZ |
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 | } | |
5dd7507c | 824 | |
7e03f383 KZ |
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); | |
5dd7507c | 828 | |
08de16d0 DB |
829 | if (!ca->sharedmaps) |
830 | ca->sharedmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *)); | |
7e03f383 | 831 | add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map); |
5dd7507c CQ |
832 | } |
833 | } | |
834 | ||
835 | static void | |
e8aa16ee | 836 | read_nodes(struct lscpu_desc *desc) |
5dd7507c CQ |
837 | { |
838 | int i; | |
839 | ||
840 | /* number of NUMA node */ | |
7e03f383 KZ |
841 | while (path_exist(_PATH_SYS_SYSTEM "/node/node%d", desc->nnodes)) |
842 | desc->nnodes++; | |
843 | ||
844 | if (!desc->nnodes) | |
845 | return; | |
5dd7507c | 846 | |
08de16d0 | 847 | desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *)); |
5dd7507c CQ |
848 | |
849 | /* information about how nodes share different CPUs */ | |
7e03f383 KZ |
850 | for (i = 0; i < desc->nnodes; i++) |
851 | desc->nodemaps[i] = path_cpuset( | |
5dd7507c CQ |
852 | _PATH_SYS_SYSTEM "/node/node%d/cpumap", |
853 | i); | |
854 | } | |
855 | ||
5dd7507c | 856 | static void |
477251f8 | 857 | print_parsable_cell(struct lscpu_desc *desc, int i, int col, int compatible) |
5dd7507c | 858 | { |
477251f8 | 859 | int j; |
7e03f383 | 860 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
5dd7507c | 861 | |
477251f8 KZ |
862 | switch (col) { |
863 | case COL_CPU: | |
5dd7507c | 864 | printf("%d", i); |
477251f8 KZ |
865 | break; |
866 | case COL_CORE: | |
7e03f383 KZ |
867 | for (j = 0; j < desc->ncores; j++) { |
868 | if (CPU_ISSET_S(i, setsize, desc->coremaps[j])) { | |
477251f8 | 869 | printf("%d", j); |
7e03f383 KZ |
870 | break; |
871 | } | |
872 | } | |
477251f8 KZ |
873 | break; |
874 | case COL_SOCKET: | |
7e03f383 KZ |
875 | for (j = 0; j < desc->nsockets; j++) { |
876 | if (CPU_ISSET_S(i, setsize, desc->socketmaps[j])) { | |
477251f8 | 877 | printf("%d", j); |
7e03f383 KZ |
878 | break; |
879 | } | |
880 | } | |
477251f8 KZ |
881 | break; |
882 | case COL_NODE: | |
7e03f383 KZ |
883 | for (j = 0; j < desc->nnodes; j++) { |
884 | if (CPU_ISSET_S(i, setsize, desc->nodemaps[j])) { | |
477251f8 | 885 | printf("%d", j); |
7e03f383 | 886 | break; |
5dd7507c | 887 | } |
7e03f383 | 888 | } |
477251f8 KZ |
889 | break; |
890 | case COL_BOOK: | |
56baaa4e HC |
891 | for (j = 0; j < desc->nbooks; j++) { |
892 | if (CPU_ISSET_S(i, setsize, desc->bookmaps[j])) { | |
477251f8 | 893 | printf("%d", j); |
56baaa4e HC |
894 | break; |
895 | } | |
896 | } | |
477251f8 KZ |
897 | break; |
898 | case COL_CACHE: | |
7e03f383 KZ |
899 | for (j = desc->ncaches - 1; j >= 0; j--) { |
900 | struct cpu_cache *ca = &desc->caches[j]; | |
901 | int x; | |
5dd7507c | 902 | |
7e03f383 KZ |
903 | for (x = 0; x < ca->nsharedmaps; x++) { |
904 | if (CPU_ISSET_S(i, setsize, ca->sharedmaps[x])) { | |
477251f8 KZ |
905 | if (j != desc->ncaches - 1) |
906 | putchar(compatible ? ',' : ':'); | |
907 | printf("%d", x); | |
7e03f383 KZ |
908 | break; |
909 | } | |
5dd7507c | 910 | } |
7e03f383 KZ |
911 | if (x == ca->nsharedmaps) |
912 | putchar(','); | |
5dd7507c | 913 | } |
477251f8 | 914 | break; |
2b8fcb81 HC |
915 | case COL_POLARIZATION: |
916 | if (desc->polarization) | |
917 | printf("%s", polar_modes[desc->polarization[i]]); | |
918 | break; | |
477251f8 KZ |
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 | } | |
5dd7507c CQ |
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 | |
4f912c6a KZ |
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) | |
5dd7507c | 1021 | { |
7e03f383 KZ |
1022 | char buf[512]; |
1023 | int i; | |
aac1e59e | 1024 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
7e03f383 | 1025 | |
e6b0611b | 1026 | print_s(_("Architecture:"), desc->arch); |
79e8b41a | 1027 | |
f633ad4e | 1028 | if (desc->mode) { |
79e8b41a KZ |
1029 | char buf[64], *p = buf; |
1030 | ||
f633ad4e | 1031 | if (desc->mode & MODE_32BIT) { |
79e8b41a KZ |
1032 | strcpy(p, "32-bit, "); |
1033 | p += 8; | |
1034 | } | |
f633ad4e | 1035 | if (desc->mode & MODE_64BIT) { |
79e8b41a KZ |
1036 | strcpy(p, "64-bit, "); |
1037 | p += 8; | |
1038 | } | |
1039 | *(p - 2) = '\0'; | |
1040 | print_s(_("CPU op-mode(s):"), buf); | |
1041 | } | |
aabe2441 | 1042 | #if !defined(WORDS_BIGENDIAN) |
9b8d4d5f DB |
1043 | print_s(_("Byte Order:"), "Little Endian"); |
1044 | #else | |
1045 | print_s(_("Byte Order:"), "Big Endian"); | |
9b8d4d5f | 1046 | #endif |
4a939e04 | 1047 | print_n(_("CPU(s):"), desc->ncpus); |
4f912c6a | 1048 | |
5d4ba40d KZ |
1049 | if (desc->online) |
1050 | print_cpuset(hex ? _("On-line CPU(s) mask:") : | |
1051 | _("On-line CPU(s) list:"), | |
1052 | desc->online, hex); | |
4f912c6a | 1053 | |
5d4ba40d | 1054 | if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) { |
4f912c6a KZ |
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 | } | |
5dd7507c | 1074 | |
7e03f383 | 1075 | if (desc->nsockets) { |
8648ca96 HC |
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 | } | |
7e03f383 | 1099 | print_n(_("Thread(s) per core:"), desc->nthreads / desc->ncores); |
8648ca96 HC |
1100 | print_n(_("Core(s) per socket:"), |
1101 | cores_per_socket ?: desc->ncores / desc->nsockets); | |
56baaa4e | 1102 | if (desc->nbooks) { |
8648ca96 HC |
1103 | print_n(_("Socket(s) per book:"), |
1104 | sockets_per_book ?: desc->nsockets / desc->nbooks); | |
1105 | print_n(_("Book(s):"), books ?: desc->nbooks); | |
56baaa4e | 1106 | } else { |
8648ca96 | 1107 | print_n(_("Socket(s):"), sockets_per_book ?: desc->nsockets); |
56baaa4e | 1108 | } |
5dd7507c | 1109 | } |
7e03f383 KZ |
1110 | if (desc->nnodes) |
1111 | print_n(_("NUMA node(s):"), desc->nnodes); | |
e8aa16ee KZ |
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); | |
9b8d4d5f DB |
1122 | if (desc->bogomips) |
1123 | print_s(_("BogoMIPS:"), desc->bogomips); | |
e8aa16ee KZ |
1124 | if (desc->virtflag) { |
1125 | if (!strcmp(desc->virtflag, "svm")) | |
5dd7507c | 1126 | print_s(_("Virtualization:"), "AMD-V"); |
e8aa16ee | 1127 | else if (!strcmp(desc->virtflag, "vmx")) |
5dd7507c CQ |
1128 | print_s(_("Virtualization:"), "VT-x"); |
1129 | } | |
e8aa16ee KZ |
1130 | if (desc->hyper) { |
1131 | print_s(_("Hypervisor vendor:"), hv_vendors[desc->hyper]); | |
1132 | print_s(_("Virtualization type:"), virt_types[desc->virtype]); | |
c8b64f6d | 1133 | } |
a0fff77e HC |
1134 | if (desc->dispatching >= 0) |
1135 | print_s(_("Dispatching mode:"), disp_modes[desc->dispatching]); | |
7e03f383 | 1136 | if (desc->ncaches) { |
c8b64f6d | 1137 | char buf[512]; |
5dd7507c CQ |
1138 | int i; |
1139 | ||
7e03f383 | 1140 | for (i = desc->ncaches - 1; i >= 0; i--) { |
5dd7507c | 1141 | snprintf(buf, sizeof(buf), |
7e03f383 KZ |
1142 | _("%s cache:"), desc->caches[i].name); |
1143 | print_s(buf, desc->caches[i].size); | |
1144 | } | |
1145 | } | |
1146 | ||
4f912c6a KZ |
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); | |
5dd7507c CQ |
1150 | } |
1151 | } | |
1152 | ||
39561c70 | 1153 | static void __attribute__((__noreturn__)) usage(FILE *out) |
5dd7507c | 1154 | { |
e8ab5ce3 BS |
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" | |
477251f8 | 1161 | " -p, --parse[=LIST] print out a parsable instead of a readable format\n" |
e8ab5ce3 BS |
1162 | " -s, --sysroot DIR use directory DIR as system root\n" |
1163 | " -x, --hex print hexadecimal masks rather than lists of CPUs\n")); | |
4f912c6a | 1164 | |
39561c70 | 1165 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); |
5dd7507c CQ |
1166 | } |
1167 | ||
1168 | int main(int argc, char *argv[]) | |
1169 | { | |
e8aa16ee | 1170 | struct lscpu_desc _desc, *desc = &_desc; |
4f912c6a | 1171 | int parsable = 0, c, i, hex = 0; |
477251f8 KZ |
1172 | int columns[ARRAY_SIZE(colnames)], ncolumns = 0; |
1173 | int compatible = 0; | |
5dd7507c | 1174 | |
6c7d5ae9 | 1175 | static const struct option longopts[] = { |
47b6e8b6 | 1176 | { "help", no_argument, 0, 'h' }, |
477251f8 | 1177 | { "parse", optional_argument, 0, 'p' }, |
47b6e8b6 | 1178 | { "sysroot", required_argument, 0, 's' }, |
4f912c6a | 1179 | { "hex", no_argument, 0, 'x' }, |
5dd7507c CQ |
1180 | { NULL, 0, 0, 0 } |
1181 | }; | |
1182 | ||
2f8f1388 | 1183 | setlocale(LC_ALL, ""); |
5dd7507c CQ |
1184 | bindtextdomain(PACKAGE, LOCALEDIR); |
1185 | textdomain(PACKAGE); | |
1186 | ||
477251f8 | 1187 | while ((c = getopt_long(argc, argv, "hp::s:x", longopts, NULL)) != -1) { |
5dd7507c CQ |
1188 | switch (c) { |
1189 | case 'h': | |
39561c70 | 1190 | usage(stdout); |
5dd7507c CQ |
1191 | case 'p': |
1192 | parsable = 1; | |
477251f8 KZ |
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 | } | |
5dd7507c | 1209 | break; |
47b6e8b6 | 1210 | case 's': |
d50363cd | 1211 | sysrootlen = strlen(optarg); |
47b6e8b6 | 1212 | strncpy(pathbuf, optarg, sizeof(pathbuf)); |
d50363cd | 1213 | pathbuf[sizeof(pathbuf) - 1] = '\0'; |
47b6e8b6 | 1214 | break; |
4f912c6a KZ |
1215 | case 'x': |
1216 | hex = 1; | |
1217 | break; | |
5dd7507c | 1218 | default: |
39561c70 | 1219 | usage(stderr); |
5dd7507c CQ |
1220 | } |
1221 | } | |
1222 | ||
e8aa16ee | 1223 | memset(desc, 0, sizeof(*desc)); |
5dd7507c | 1224 | |
e8aa16ee | 1225 | read_basicinfo(desc); |
5dd7507c | 1226 | |
7e03f383 | 1227 | for (i = 0; i < desc->ncpus; i++) { |
5d4ba40d | 1228 | if (desc->online && !is_cpu_online(desc, i)) |
aac1e59e | 1229 | continue; |
7e03f383 KZ |
1230 | read_topology(desc, i); |
1231 | read_cache(desc, i); | |
2b8fcb81 | 1232 | read_polarization(desc, i); |
47b6e8b6 | 1233 | } |
7e03f383 KZ |
1234 | |
1235 | qsort(desc->caches, desc->ncaches, sizeof(struct cpu_cache), cachecmp); | |
1236 | ||
1237 | read_nodes(desc); | |
5dd7507c | 1238 | |
e8aa16ee | 1239 | read_hypervisor(desc); |
c8b64f6d | 1240 | |
5dd7507c CQ |
1241 | /* Show time! */ |
1242 | if (parsable) | |
477251f8 | 1243 | print_parsable(desc, columns, ncolumns, compatible); |
5dd7507c | 1244 | else |
4f912c6a | 1245 | print_readable(desc, hex); |
5dd7507c | 1246 | |
cf474aac | 1247 | return EXIT_SUCCESS; |
5dd7507c | 1248 | } |