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