]>
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 | * | |
7cebf0bb SK |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
5dd7507c CQ |
20 | */ |
21 | ||
5bd31c6d | 22 | #include <assert.h> |
5dd7507c CQ |
23 | #include <ctype.h> |
24 | #include <dirent.h> | |
5dd7507c CQ |
25 | #include <errno.h> |
26 | #include <fcntl.h> | |
27 | #include <getopt.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/utsname.h> | |
32 | #include <unistd.h> | |
33 | #include <stdarg.h> | |
39561c70 KZ |
34 | #include <sys/types.h> |
35 | #include <sys/stat.h> | |
32865bd5 | 36 | #include <sys/personality.h> |
5dd7507c | 37 | |
6d9b1c19 KZ |
38 | #if (defined(__x86_64__) || defined(__i386__)) |
39 | # if !defined( __SANITIZE_ADDRESS__) | |
40 | # define INCLUDE_VMWARE_BDOOR | |
41 | # else | |
42 | # warning VMWARE detection disabled by __SANITIZE_ADDRESS__ | |
43 | # endif | |
b7744730 RM |
44 | #endif |
45 | ||
46 | #ifdef INCLUDE_VMWARE_BDOOR | |
47 | # include <stdint.h> | |
48 | # include <signal.h> | |
49 | # include <strings.h> | |
50 | # include <setjmp.h> | |
65322517 | 51 | # ifdef HAVE_SYS_IO_H |
b7744730 RM |
52 | # include <sys/io.h> |
53 | # endif | |
54 | #endif | |
55 | ||
bd9b94d1 SB |
56 | #if defined(HAVE_LIBRTAS) |
57 | #include <librtas.h> | |
58 | #endif | |
59 | ||
83db4eb2 OO |
60 | #include <libsmartcols.h> |
61 | ||
efb8854f | 62 | #include "closestream.h" |
41a8940d | 63 | #include "optutils.h" |
639eeb28 | 64 | #include "fileutils.h" |
71061694 | 65 | |
fb2627ce | 66 | #include "lscpu.h" |
477251f8 | 67 | |
5dd7507c CQ |
68 | #define CACHE_MAX 100 |
69 | ||
70 | /* /sys paths */ | |
d50363cd | 71 | #define _PATH_SYS_SYSTEM "/sys/devices/system" |
6e509042 | 72 | #define _PATH_SYS_HYP_FEATURES "/sys/hypervisor/properties/features" |
7e03f383 | 73 | #define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu" |
e9074a16 | 74 | #define _PATH_SYS_NODE _PATH_SYS_SYSTEM "/node" |
5dd7507c | 75 | |
0ebbe9f1 DZ |
76 | /* Xen Domain feature flag used for /sys/hypervisor/properties/features */ |
77 | #define XENFEAT_supervisor_mode_kernel 3 | |
78 | #define XENFEAT_mmu_pt_update_preserve_ad 5 | |
79 | #define XENFEAT_hvm_callback_vector 8 | |
80 | ||
81 | #define XEN_FEATURES_PV_MASK (1U << XENFEAT_mmu_pt_update_preserve_ad) | |
82 | #define XEN_FEATURES_PVH_MASK ( (1U << XENFEAT_supervisor_mode_kernel) \ | |
83 | | (1U << XENFEAT_hvm_callback_vector) ) | |
84 | ||
2ba641e5 | 85 | static const char *virt_types[] = { |
c8b64f6d KZ |
86 | [VIRT_NONE] = N_("none"), |
87 | [VIRT_PARA] = N_("para"), | |
857d9cdf KZ |
88 | [VIRT_FULL] = N_("full"), |
89 | [VIRT_CONT] = N_("container"), | |
c8b64f6d KZ |
90 | }; |
91 | ||
2ba641e5 | 92 | static const char *hv_vendors[] = { |
c8b64f6d KZ |
93 | [HYPER_NONE] = NULL, |
94 | [HYPER_XEN] = "Xen", | |
95 | [HYPER_KVM] = "KVM", | |
99fbc877 | 96 | [HYPER_MSHV] = "Microsoft", |
b8ec7bdf | 97 | [HYPER_VMWARE] = "VMware", |
fb2627ce OO |
98 | [HYPER_IBM] = "IBM", |
99 | [HYPER_VSERVER] = "Linux-VServer", | |
100 | [HYPER_UML] = "User-mode Linux", | |
101 | [HYPER_INNOTEK] = "Innotek GmbH", | |
102 | [HYPER_HITACHI] = "Hitachi", | |
96ce475f RM |
103 | [HYPER_PARALLELS] = "Parallels", |
104 | [HYPER_VBOX] = "Oracle", | |
5bd31c6d RM |
105 | [HYPER_OS400] = "OS/400", |
106 | [HYPER_PHYP] = "pHyp", | |
7572fb2b SB |
107 | [HYPER_SPAR] = "Unisys s-Par", |
108 | [HYPER_WSL] = "Windows Subsystem for Linux" | |
96ce475f RM |
109 | }; |
110 | ||
2ba641e5 | 111 | static const int hv_vendor_pci[] = { |
96ce475f RM |
112 | [HYPER_NONE] = 0x0000, |
113 | [HYPER_XEN] = 0x5853, | |
114 | [HYPER_KVM] = 0x0000, | |
115 | [HYPER_MSHV] = 0x1414, | |
116 | [HYPER_VMWARE] = 0x15ad, | |
117 | [HYPER_VBOX] = 0x80ee, | |
118 | }; | |
119 | ||
2ba641e5 | 120 | static const int hv_graphics_pci[] = { |
96ce475f RM |
121 | [HYPER_NONE] = 0x0000, |
122 | [HYPER_XEN] = 0x0001, | |
123 | [HYPER_KVM] = 0x0000, | |
124 | [HYPER_MSHV] = 0x5353, | |
125 | [HYPER_VMWARE] = 0x0710, | |
126 | [HYPER_VBOX] = 0xbeef, | |
c8b64f6d KZ |
127 | }; |
128 | ||
e8aa16ee | 129 | |
a0fff77e | 130 | /* dispatching modes */ |
2ba641e5 | 131 | static const char *disp_modes[] = { |
a0fff77e HC |
132 | [DISP_HORIZONTAL] = N_("horizontal"), |
133 | [DISP_VERTICAL] = N_("vertical") | |
134 | }; | |
135 | ||
2ba641e5 | 136 | static struct polarization_modes polar_modes[] = { |
8005924a KZ |
137 | [POLAR_UNKNOWN] = {"U", "-"}, |
138 | [POLAR_VLOW] = {"VL", "vert-low"}, | |
139 | [POLAR_VMEDIUM] = {"VM", "vert-medium"}, | |
140 | [POLAR_VHIGH] = {"VH", "vert-high"}, | |
141 | [POLAR_HORIZONTAL] = {"H", "horizontal"}, | |
2b8fcb81 HC |
142 | }; |
143 | ||
7e03f383 | 144 | static int maxcpus; /* size in bits of kernel cpu mask */ |
5dd7507c | 145 | |
aac1e59e | 146 | #define is_cpu_online(_d, _cpu) \ |
a5cfffff TK |
147 | ((_d) && (_d)->online ? \ |
148 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0) | |
149 | #define is_cpu_present(_d, _cpu) \ | |
150 | ((_d) && (_d)->present ? \ | |
151 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->present) : 0) | |
aac1e59e | 152 | |
4f642863 AT |
153 | #define real_cpu_num(_d, _i) ((_d)->idx2cpunum[(_i)]) |
154 | ||
477251f8 | 155 | /* |
3d27b76a | 156 | * IDs |
477251f8 KZ |
157 | */ |
158 | enum { | |
cc07239d KZ |
159 | COL_CPU_CPU, |
160 | COL_CPU_CORE, | |
161 | COL_CPU_SOCKET, | |
162 | COL_CPU_NODE, | |
163 | COL_CPU_BOOK, | |
164 | COL_CPU_DRAWER, | |
165 | COL_CPU_CACHE, | |
166 | COL_CPU_POLARIZATION, | |
167 | COL_CPU_ADDRESS, | |
168 | COL_CPU_CONFIGURED, | |
169 | COL_CPU_ONLINE, | |
170 | COL_CPU_MAXMHZ, | |
171 | COL_CPU_MINMHZ, | |
477251f8 KZ |
172 | }; |
173 | ||
0e86bc84 KZ |
174 | enum { |
175 | COL_CACHE_ALLSIZE, | |
176 | COL_CACHE_LEVEL, | |
177 | COL_CACHE_NAME, | |
178 | COL_CACHE_ONESIZE, | |
179 | COL_CACHE_TYPE, | |
180 | COL_CACHE_WAYS, | |
cf3b6b71 KZ |
181 | COL_CACHE_ALLOCPOL, |
182 | COL_CACHE_WRITEPOL, | |
183 | COL_CACHE_PHYLINE, | |
184 | COL_CACHE_SETS, | |
185 | COL_CACHE_COHERENCYSIZE | |
0e86bc84 KZ |
186 | }; |
187 | ||
188 | ||
3d27b76a KZ |
189 | /* column description |
190 | */ | |
191 | struct lscpu_coldesc { | |
192 | const char *name; | |
193 | const char *help; | |
194 | ||
0e86bc84 | 195 | int flags; |
3d27b76a | 196 | unsigned int is_abbr:1; /* name is abbreviation */ |
477251f8 KZ |
197 | }; |
198 | ||
cc07239d | 199 | static struct lscpu_coldesc coldescs_cpu[] = |
3d27b76a | 200 | { |
2ec00a10 KZ |
201 | [COL_CPU_CPU] = { "CPU", N_("logical CPU number"), SCOLS_FL_RIGHT, 1 }, |
202 | [COL_CPU_CORE] = { "CORE", N_("logical core number"), SCOLS_FL_RIGHT }, | |
203 | [COL_CPU_SOCKET] = { "SOCKET", N_("logical socket number"), SCOLS_FL_RIGHT }, | |
204 | [COL_CPU_NODE] = { "NODE", N_("logical NUMA node number"), SCOLS_FL_RIGHT }, | |
205 | [COL_CPU_BOOK] = { "BOOK", N_("logical book number"), SCOLS_FL_RIGHT }, | |
206 | [COL_CPU_DRAWER] = { "DRAWER", N_("logical drawer number"), SCOLS_FL_RIGHT }, | |
cc07239d KZ |
207 | [COL_CPU_CACHE] = { "CACHE", N_("shows how caches are shared between CPUs") }, |
208 | [COL_CPU_POLARIZATION] = { "POLARIZATION", N_("CPU dispatching mode on virtual hardware") }, | |
209 | [COL_CPU_ADDRESS] = { "ADDRESS", N_("physical address of a CPU") }, | |
210 | [COL_CPU_CONFIGURED] = { "CONFIGURED", N_("shows if the hypervisor has allocated the CPU") }, | |
2ec00a10 KZ |
211 | [COL_CPU_ONLINE] = { "ONLINE", N_("shows if Linux currently makes use of the CPU"), SCOLS_FL_RIGHT }, |
212 | [COL_CPU_MAXMHZ] = { "MAXMHZ", N_("shows the maximum MHz of the CPU"), SCOLS_FL_RIGHT }, | |
213 | [COL_CPU_MINMHZ] = { "MINMHZ", N_("shows the minimum MHz of the CPU"), SCOLS_FL_RIGHT } | |
0e86bc84 KZ |
214 | }; |
215 | ||
216 | static struct lscpu_coldesc coldescs_cache[] = | |
217 | { | |
218 | [COL_CACHE_ALLSIZE] = { "ALL-SIZE", N_("size of all system caches"), SCOLS_FL_RIGHT }, | |
219 | [COL_CACHE_LEVEL] = { "LEVEL", N_("cache level"), SCOLS_FL_RIGHT }, | |
220 | [COL_CACHE_NAME] = { "NAME", N_("cache name") }, | |
221 | [COL_CACHE_ONESIZE] = { "ONE-SIZE", N_("size of one cache"), SCOLS_FL_RIGHT }, | |
222 | [COL_CACHE_TYPE] = { "TYPE", N_("cache type") }, | |
cf3b6b71 KZ |
223 | [COL_CACHE_WAYS] = { "WAYS", N_("ways of associativity"), SCOLS_FL_RIGHT }, |
224 | [COL_CACHE_ALLOCPOL] = { "ALLOC-POLICY", N_("allocation policy") }, | |
225 | [COL_CACHE_WRITEPOL] = { "WRITE-POLICY", N_("write policy") }, | |
226 | [COL_CACHE_PHYLINE] = { "PHY-LINE", N_("number of physical cache line per cache t"), SCOLS_FL_RIGHT }, | |
227 | [COL_CACHE_SETS] = { "SETS", N_("number of sets in the cache; set lines has the same cache index"), SCOLS_FL_RIGHT }, | |
228 | [COL_CACHE_COHERENCYSIZE] = { "COHERENCY-SIZE", N_("minimum amount of data in bytes transferred from memory to cache"), SCOLS_FL_RIGHT } | |
3d27b76a | 229 | }; |
477251f8 | 230 | |
0e86bc84 KZ |
231 | |
232 | static int get_cache_full_size(struct lscpu_desc *desc, struct cpu_cache *ca, uint64_t *res); | |
233 | ||
583f14cc | 234 | static int |
cc07239d | 235 | cpu_column_name_to_id(const char *name, size_t namesz) |
477251f8 | 236 | { |
329fd1c3 | 237 | size_t i; |
477251f8 | 238 | |
cc07239d KZ |
239 | for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) { |
240 | const char *cn = coldescs_cpu[i].name; | |
477251f8 KZ |
241 | |
242 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
243 | return i; | |
244 | } | |
245 | warnx(_("unknown column: %s"), name); | |
246 | return -1; | |
247 | } | |
248 | ||
0e86bc84 KZ |
249 | static int |
250 | cache_column_name_to_id(const char *name, size_t namesz) | |
251 | { | |
252 | size_t i; | |
253 | ||
254 | for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++) { | |
255 | const char *cn = coldescs_cache[i].name; | |
256 | ||
257 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
258 | return i; | |
259 | } | |
260 | warnx(_("unknown column: %s"), name); | |
261 | return -1; | |
262 | } | |
263 | ||
5dd7507c CQ |
264 | /* Lookup a pattern and get the value from cpuinfo. |
265 | * Format is: | |
266 | * | |
267 | * "<pattern> : <key>" | |
268 | */ | |
583f14cc KZ |
269 | static int |
270 | lookup(char *line, char *pattern, char **value) | |
5dd7507c CQ |
271 | { |
272 | char *p, *v; | |
273 | int len = strlen(pattern); | |
274 | ||
e5fc6d6f RM |
275 | /* don't re-fill already found tags, first one wins */ |
276 | if (!*line || *value) | |
5dd7507c CQ |
277 | return 0; |
278 | ||
279 | /* pattern */ | |
280 | if (strncmp(line, pattern, len)) | |
281 | return 0; | |
282 | ||
283 | /* white spaces */ | |
284 | for (p = line + len; isspace(*p); p++); | |
285 | ||
286 | /* separator */ | |
287 | if (*p != ':') | |
288 | return 0; | |
289 | ||
290 | /* white spaces */ | |
291 | for (++p; isspace(*p); p++); | |
292 | ||
293 | /* value */ | |
294 | if (!*p) | |
295 | return 0; | |
296 | v = p; | |
297 | ||
298 | /* end of value */ | |
299 | len = strlen(line) - 1; | |
300 | for (p = line + len; isspace(*(p-1)); p--); | |
301 | *p = '\0'; | |
302 | ||
303 | *value = xstrdup(v); | |
304 | return 1; | |
305 | } | |
306 | ||
28b1658f HC |
307 | /* Parse extra cache lines contained within /proc/cpuinfo but which are not |
308 | * part of the cache topology information within the sysfs filesystem. | |
309 | * This is true for all shared caches on e.g. s390. When there are layers of | |
310 | * hypervisors in between it is not knows which CPUs share which caches. | |
311 | * Therefore information about shared caches is only available in | |
312 | * /proc/cpuinfo. | |
313 | * Format is: | |
314 | * "cache<nr> : level=<lvl> type=<type> scope=<scope> size=<size> line_size=<lsz> associativity=<as>" | |
315 | */ | |
316 | static int | |
317 | lookup_cache(char *line, struct lscpu_desc *desc) | |
318 | { | |
319 | struct cpu_cache *cache; | |
320 | long long size; | |
321 | char *p, type; | |
322 | int level; | |
323 | ||
324 | /* Make sure line starts with "cache<nr> :" */ | |
325 | if (strncmp(line, "cache", 5)) | |
326 | return 0; | |
327 | for (p = line + 5; isdigit(*p); p++); | |
328 | for (; isspace(*p); p++); | |
329 | if (*p != ':') | |
330 | return 0; | |
331 | ||
332 | p = strstr(line, "scope=") + 6; | |
333 | /* Skip private caches, also present in sysfs */ | |
334 | if (!p || strncmp(p, "Private", 7) == 0) | |
335 | return 0; | |
336 | p = strstr(line, "level="); | |
81a307bd KZ |
337 | if (!p || sscanf(p, "level=%d", &level) != 1) |
338 | return 0; | |
28b1658f | 339 | p = strstr(line, "type=") + 5; |
81a307bd KZ |
340 | if (!p || !*p) |
341 | return 0; | |
28b1658f HC |
342 | type = 0; |
343 | if (strncmp(p, "Data", 4) == 0) | |
344 | type = 'd'; | |
0e86bc84 | 345 | else if (strncmp(p, "Instruction", 11) == 0) |
28b1658f | 346 | type = 'i'; |
0e86bc84 KZ |
347 | else if (strncmp(p, "Unified", 7) == 0) |
348 | type = 'u'; | |
28b1658f | 349 | p = strstr(line, "size="); |
81a307bd KZ |
350 | if (!p || sscanf(p, "size=%lld", &size) != 1) |
351 | return 0; | |
352 | ||
28b1658f HC |
353 | desc->necaches++; |
354 | desc->ecaches = xrealloc(desc->ecaches, | |
355 | desc->necaches * sizeof(struct cpu_cache)); | |
356 | cache = &desc->ecaches[desc->necaches - 1]; | |
357 | memset(cache, 0 , sizeof(*cache)); | |
0e86bc84 KZ |
358 | |
359 | if (type == 'i' || type == 'd') | |
28b1658f HC |
360 | xasprintf(&cache->name, "L%d%c", level, type); |
361 | else | |
362 | xasprintf(&cache->name, "L%d", level); | |
0e86bc84 KZ |
363 | |
364 | cache->level = level; | |
365 | cache->size = size * 1024; | |
366 | ||
367 | cache->type = type == 'i' ? xstrdup("Instruction") : | |
368 | type == 'd' ? xstrdup("Data") : | |
369 | type == 'u' ? xstrdup("Unified") : NULL; | |
28b1658f HC |
370 | return 1; |
371 | } | |
372 | ||
f633ad4e KZ |
373 | /* Don't init the mode for platforms where we are not able to |
374 | * detect that CPU supports 64-bit mode. | |
375 | */ | |
376 | static int | |
8148217b | 377 | init_mode(struct lscpu_modifier *mod) |
f633ad4e KZ |
378 | { |
379 | int m = 0; | |
380 | ||
8148217b | 381 | if (mod->system == SYSTEM_SNAPSHOT) |
4e740fd8 KZ |
382 | /* reading info from any /{sys,proc} dump, don't mix it with |
383 | * information about our real CPU */ | |
384 | return 0; | |
385 | ||
f633ad4e KZ |
386 | #if defined(__alpha__) || defined(__ia64__) |
387 | m |= MODE_64BIT; /* 64bit platforms only */ | |
388 | #endif | |
389 | /* platforms with 64bit flag in /proc/cpuinfo, define | |
390 | * 32bit default here */ | |
391 | #if defined(__i386__) || defined(__x86_64__) || \ | |
c487c90c | 392 | defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) |
f633ad4e KZ |
393 | m |= MODE_32BIT; |
394 | #endif | |
32865bd5 JL |
395 | |
396 | #if defined(__aarch64__) | |
397 | { | |
398 | /* personality() is the most reliable way (since 4.7) | |
399 | * to determine aarch32 support */ | |
400 | int pers = personality(PER_LINUX32); | |
401 | if (pers != -1) { | |
402 | personality(pers); | |
403 | m |= MODE_32BIT; | |
404 | } | |
405 | m |= MODE_64BIT; | |
406 | } | |
407 | #endif | |
f633ad4e KZ |
408 | return m; |
409 | } | |
410 | ||
bd9b94d1 SB |
411 | #if defined(HAVE_LIBRTAS) |
412 | #define PROCESSOR_MODULE_INFO 43 | |
413 | static int strbe16toh(const char *buf, int offset) | |
414 | { | |
415 | return (buf[offset] << 8) + buf[offset+1]; | |
416 | } | |
417 | ||
418 | static void read_physical_info_powerpc(struct lscpu_desc *desc) | |
419 | { | |
420 | char buf[BUFSIZ]; | |
421 | int rc, len, ntypes; | |
422 | ||
423 | desc->physsockets = desc->physchips = desc->physcoresperchip = 0; | |
424 | ||
425 | rc = rtas_get_sysparm(PROCESSOR_MODULE_INFO, sizeof(buf), buf); | |
426 | if (rc < 0) | |
427 | return; | |
428 | ||
429 | len = strbe16toh(buf, 0); | |
430 | if (len < 8) | |
431 | return; | |
432 | ||
433 | ntypes = strbe16toh(buf, 2); | |
434 | ||
435 | assert(ntypes <= 1); | |
436 | if (!ntypes) | |
437 | return; | |
438 | ||
439 | desc->physsockets = strbe16toh(buf, 4); | |
440 | desc->physchips = strbe16toh(buf, 6); | |
441 | desc->physcoresperchip = strbe16toh(buf, 8); | |
442 | } | |
443 | #else | |
444 | static void read_physical_info_powerpc( | |
445 | struct lscpu_desc *desc __attribute__((__unused__))) | |
446 | { | |
447 | } | |
448 | #endif | |
449 | ||
639eeb28 KZ |
450 | static int cmp_vulnerability_name(const void *a0, const void *b0) |
451 | { | |
452 | const struct cpu_vulnerability *a = (const struct cpu_vulnerability *) a0, | |
453 | *b = (const struct cpu_vulnerability *) b0; | |
454 | return strcmp(a->name, b->name); | |
455 | } | |
456 | ||
457 | static void read_vulnerabilities(struct lscpu_desc *desc) | |
458 | { | |
459 | struct dirent *d; | |
460 | DIR *dir = ul_path_opendir(desc->syscpu, "vulnerabilities"); | |
461 | int n = 0; | |
462 | ||
463 | if (!dir) | |
464 | return; | |
465 | ||
466 | desc->nvuls = n = 0; | |
467 | ||
468 | while (xreaddir(dir)) | |
469 | n++; | |
470 | if (!n) | |
471 | return; | |
472 | ||
473 | rewinddir(dir); | |
474 | desc->vuls = xcalloc(n, sizeof(struct cpu_vulnerability)); | |
475 | ||
476 | while (desc->nvuls < n && (d = xreaddir(dir))) { | |
477 | char *str, *p; | |
478 | struct cpu_vulnerability *vu; | |
479 | ||
480 | #ifdef _DIRENT_HAVE_D_TYPE | |
481 | if (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) | |
482 | continue; | |
483 | #endif | |
484 | if (ul_path_readf_string(desc->syscpu, &str, | |
485 | "vulnerabilities/%s", d->d_name) <= 0) | |
486 | continue; | |
487 | ||
488 | vu = &desc->vuls[desc->nvuls++]; | |
489 | ||
490 | /* Name */ | |
491 | vu->name = xstrdup(d->d_name); | |
492 | *vu->name = toupper(*vu->name); | |
493 | strrep(vu->name, '_', ' '); | |
494 | ||
495 | /* Description */ | |
496 | vu->text = str; | |
497 | p = (char *) startswith(vu->text, "Mitigation"); | |
498 | if (p) { | |
499 | *p = ';'; | |
500 | strrem(vu->text, ':'); | |
501 | } | |
502 | } | |
503 | closedir(dir); | |
504 | ||
505 | qsort(desc->vuls, desc->nvuls, | |
506 | sizeof(struct cpu_vulnerability), cmp_vulnerability_name); | |
507 | } | |
508 | ||
509 | ||
510 | ||
744d62ee | 511 | |
5dd7507c | 512 | static void |
8148217b | 513 | read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
5dd7507c | 514 | { |
6e509042 | 515 | FILE *fp; |
5dd7507c CQ |
516 | char buf[BUFSIZ]; |
517 | struct utsname utsbuf; | |
a5cfffff | 518 | size_t setsize; |
6e509042 | 519 | cpu_set_t *cpuset = NULL; |
5dd7507c CQ |
520 | |
521 | /* architecture */ | |
522 | if (uname(&utsbuf) == -1) | |
523 | err(EXIT_FAILURE, _("error: uname failed")); | |
6e509042 KZ |
524 | |
525 | fp = ul_path_fopen(desc->procfs, "r", "cpuinfo"); | |
526 | if (!fp) | |
527 | err(EXIT_FAILURE, _("cannot open %s"), "/proc/cpuinfo"); | |
e8aa16ee | 528 | desc->arch = xstrdup(utsbuf.machine); |
5dd7507c | 529 | |
5dd7507c CQ |
530 | /* details */ |
531 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
e8aa16ee KZ |
532 | if (lookup(buf, "vendor", &desc->vendor)) ; |
533 | else if (lookup(buf, "vendor_id", &desc->vendor)) ; | |
744d62ee | 534 | else if (lookup(buf, "CPU implementer", &desc->vendor)) ; /* ARM and aarch64 */ |
e8aa16ee KZ |
535 | else if (lookup(buf, "family", &desc->family)) ; |
536 | else if (lookup(buf, "cpu family", &desc->family)) ; | |
86c4817e | 537 | else if (lookup(buf, "model", &desc->model)) ; |
744d62ee | 538 | else if (lookup(buf, "CPU part", &desc->model)) ; /* ARM and aarch64 */ |
86c4817e | 539 | else if (lookup(buf, "model name", &desc->modelname)) ; |
e8aa16ee | 540 | else if (lookup(buf, "stepping", &desc->stepping)) ; |
744d62ee | 541 | else if (lookup(buf, "CPU variant", &desc->stepping)) ; /* aarch64 */ |
e8aa16ee | 542 | else if (lookup(buf, "cpu MHz", &desc->mhz)) ; |
4632b288 HC |
543 | else if (lookup(buf, "cpu MHz dynamic", &desc->dynamic_mhz)) ; /* s390 */ |
544 | else if (lookup(buf, "cpu MHz static", &desc->static_mhz)) ; /* s390 */ | |
f633ad4e KZ |
545 | else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */ |
546 | else if (lookup(buf, "features", &desc->flags)) ; /* s390 */ | |
a3c455ac | 547 | else if (lookup(buf, "Features", &desc->flags)) ; /* aarch64 */ |
c487c90c | 548 | else if (lookup(buf, "type", &desc->flags)) ; /* sparc64 */ |
9b8d4d5f | 549 | else if (lookup(buf, "bogomips", &desc->bogomips)) ; |
a3c455ac | 550 | else if (lookup(buf, "BogoMIPS", &desc->bogomips)) ; /* aarch64 */ |
abcd6368 | 551 | else if (lookup(buf, "bogomips per cpu", &desc->bogomips)) ; /* s390 */ |
c95e3889 RM |
552 | else if (lookup(buf, "cpu", &desc->cpu)) ; |
553 | else if (lookup(buf, "revision", &desc->revision)) ; | |
a3c455ac | 554 | else if (lookup(buf, "CPU revision", &desc->revision)) ; /* aarch64 */ |
2c497d32 | 555 | else if (lookup(buf, "max thread id", &desc->mtid)) ; /* s390 */ |
3e48ef72 | 556 | else if (lookup(buf, "address sizes", &desc->addrsz)) ; /* x86 */ |
28b1658f | 557 | else if (lookup_cache(buf, desc)) ; |
5dd7507c CQ |
558 | else |
559 | continue; | |
560 | } | |
c8b64f6d | 561 | |
8148217b | 562 | desc->mode = init_mode(mod); |
f633ad4e | 563 | |
e8aa16ee KZ |
564 | if (desc->flags) { |
565 | snprintf(buf, sizeof(buf), " %s ", desc->flags); | |
c8b64f6d | 566 | if (strstr(buf, " svm ")) |
8290a249 | 567 | desc->virtflag = xstrdup("svm"); |
c8b64f6d | 568 | else if (strstr(buf, " vmx ")) |
8290a249 | 569 | desc->virtflag = xstrdup("vmx"); |
79e8b41a | 570 | if (strstr(buf, " lm ")) |
f633ad4e KZ |
571 | desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */ |
572 | if (strstr(buf, " zarch ")) | |
573 | desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */ | |
c487c90c KZ |
574 | if (strstr(buf, " sun4v ") || strstr(buf, " sun4u ")) |
575 | desc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */ | |
c8b64f6d KZ |
576 | } |
577 | ||
4581b716 KZ |
578 | if (desc->arch && mod->system != SYSTEM_SNAPSHOT) { |
579 | if (strcmp(desc->arch, "ppc64") == 0) | |
580 | desc->mode |= MODE_32BIT | MODE_64BIT; | |
581 | else if (strcmp(desc->arch, "ppc") == 0) | |
582 | desc->mode |= MODE_32BIT; | |
583 | } | |
584 | ||
5dd7507c | 585 | fclose(fp); |
7e03f383 | 586 | |
6e509042 | 587 | if (ul_path_read_s32(desc->syscpu, &maxcpus, "kernel_max") == 0) |
3d6e5c35 | 588 | /* note that kernel_max is maximum index [NR_CPUS-1] */ |
6e509042 | 589 | maxcpus += 1; |
7e03f383 | 590 | |
8148217b | 591 | else if (mod->system == SYSTEM_LIVE) |
7e03f383 KZ |
592 | /* the root is '/' so we are working with data from the current kernel */ |
593 | maxcpus = get_max_number_of_cpus(); | |
701e2b8e KZ |
594 | |
595 | if (maxcpus <= 0) | |
596 | /* error or we are reading some /sys snapshot instead of the | |
597 | * real /sys, let's use any crazy number... */ | |
a5cfffff TK |
598 | maxcpus = 2048; |
599 | ||
600 | setsize = CPU_ALLOC_SIZE(maxcpus); | |
601 | ||
6e509042 | 602 | if (ul_path_readf_cpulist(desc->syscpu, &cpuset, maxcpus, "possible") == 0) { |
4f642863 AT |
603 | int num, idx; |
604 | ||
6e509042 | 605 | desc->ncpuspos = CPU_COUNT_S(setsize, cpuset); |
4f642863 AT |
606 | desc->idx2cpunum = xcalloc(desc->ncpuspos, sizeof(int)); |
607 | ||
608 | for (num = 0, idx = 0; num < maxcpus; num++) { | |
6e509042 | 609 | if (CPU_ISSET_S(num, setsize, cpuset)) |
4f642863 AT |
610 | desc->idx2cpunum[idx++] = num; |
611 | } | |
6e509042 KZ |
612 | cpuset_free(cpuset); |
613 | cpuset = NULL; | |
a5cfffff TK |
614 | } else |
615 | err(EXIT_FAILURE, _("failed to determine number of CPUs: %s"), | |
1d56b55e | 616 | _PATH_SYS_CPU "/possible"); |
a5cfffff TK |
617 | |
618 | ||
619 | /* get mask for present CPUs */ | |
6e509042 | 620 | if (ul_path_readf_cpulist(desc->syscpu, &desc->present, maxcpus, "present") == 0) |
a5cfffff | 621 | desc->ncpus = CPU_COUNT_S(setsize, desc->present); |
aac1e59e KZ |
622 | |
623 | /* get mask for online CPUs */ | |
6e509042 | 624 | if (ul_path_readf_cpulist(desc->syscpu, &desc->online, maxcpus, "online") == 0) |
e282eec2 | 625 | desc->nthreads = CPU_COUNT_S(setsize, desc->online); |
a0fff77e HC |
626 | |
627 | /* get dispatching mode */ | |
6e509042 | 628 | if (ul_path_read_s32(desc->syscpu, &desc->dispatching, "dispatching") != 0) |
a0fff77e | 629 | desc->dispatching = -1; |
bd9b94d1 | 630 | |
16ca0551 KZ |
631 | /* get cpufreq boost mode */ |
632 | if (ul_path_read_s32(desc->syscpu, &desc->freqboost, "cpufreq/boost") != 0) | |
633 | desc->freqboost = -1; | |
634 | ||
bd9b94d1 SB |
635 | if (mod->system == SYSTEM_LIVE) |
636 | read_physical_info_powerpc(desc); | |
0c28f0c8 | 637 | |
6e509042 | 638 | if ((fp = ul_path_fopen(desc->procfs, "r", "sysinfo"))) { |
fe2e38f3 KZ |
639 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
640 | if (lookup(buf, "Type", &desc->machinetype)) | |
641 | break; | |
642 | } | |
904ffe1f | 643 | fclose(fp); |
0c28f0c8 | 644 | } |
639eeb28 KZ |
645 | |
646 | /* vulnerabilities */ | |
647 | if (ul_path_access(desc->syscpu, F_OK, "vulnerabilities") == 0) | |
648 | read_vulnerabilities(desc); | |
5dd7507c CQ |
649 | } |
650 | ||
c8b64f6d | 651 | static int |
6e509042 | 652 | has_pci_device(struct lscpu_desc *desc, unsigned int vendor, unsigned int device) |
c8b64f6d KZ |
653 | { |
654 | FILE *f; | |
13cf6fc8 | 655 | unsigned int num, fn, ven, dev; |
c8b64f6d KZ |
656 | int res = 1; |
657 | ||
6e509042 | 658 | f = ul_path_fopen(desc->procfs, "r", "bus/pci/devices"); |
c8b64f6d KZ |
659 | if (!f) |
660 | return 0; | |
661 | ||
662 | /* for more details about bus/pci/devices format see | |
663 | * drivers/pci/proc.c in linux kernel | |
664 | */ | |
665 | while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]", | |
666 | &num, &fn, &ven, &dev) == 4) { | |
667 | ||
668 | if (ven == vendor && dev == device) | |
669 | goto found; | |
670 | } | |
671 | ||
672 | res = 0; | |
673 | found: | |
674 | fclose(f); | |
675 | return res; | |
676 | } | |
677 | ||
678 | #if defined(__x86_64__) || defined(__i386__) | |
679 | ||
680 | /* | |
681 | * This CPUID leaf returns the information about the hypervisor. | |
682 | * EAX : maximum input value for CPUID supported by the hypervisor. | |
683 | * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware. | |
684 | */ | |
685 | #define HYPERVISOR_INFO_LEAF 0x40000000 | |
686 | ||
687 | static inline void | |
688 | cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, | |
689 | unsigned int *ecx, unsigned int *edx) | |
690 | { | |
c9239f23 MF |
691 | __asm__( |
692 | #if defined(__PIC__) && defined(__i386__) | |
693 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
7845b91d | 694 | "xchg %%ebx, %%esi;" |
c9239f23 | 695 | "cpuid;" |
7845b91d | 696 | "xchg %%esi, %%ebx;" |
c9239f23 MF |
697 | : "=S" (*ebx), |
698 | #else | |
699 | "cpuid;" | |
700 | : "=b" (*ebx), | |
701 | #endif | |
702 | "=a" (*eax), | |
c8b64f6d KZ |
703 | "=c" (*ecx), |
704 | "=d" (*edx) | |
bc54770d | 705 | : "1" (op), "c"(0)); |
c8b64f6d KZ |
706 | } |
707 | ||
708 | static void | |
e8aa16ee | 709 | read_hypervisor_cpuid(struct lscpu_desc *desc) |
c8b64f6d KZ |
710 | { |
711 | unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; | |
712 | char hyper_vendor_id[13]; | |
713 | ||
714 | memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id)); | |
715 | ||
716 | cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx); | |
717 | memcpy(hyper_vendor_id + 0, &ebx, 4); | |
718 | memcpy(hyper_vendor_id + 4, &ecx, 4); | |
719 | memcpy(hyper_vendor_id + 8, &edx, 4); | |
720 | hyper_vendor_id[12] = '\0'; | |
721 | ||
722 | if (!hyper_vendor_id[0]) | |
723 | return; | |
724 | ||
725 | if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12)) | |
e8aa16ee | 726 | desc->hyper = HYPER_XEN; |
c8b64f6d | 727 | else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9)) |
e8aa16ee | 728 | desc->hyper = HYPER_KVM; |
c8b64f6d | 729 | else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12)) |
e8aa16ee | 730 | desc->hyper = HYPER_MSHV; |
99fbc877 SH |
731 | else if (!strncmp("VMwareVMware", hyper_vendor_id, 12)) |
732 | desc->hyper = HYPER_VMWARE; | |
4597b692 BR |
733 | else if (!strncmp("UnisysSpar64", hyper_vendor_id, 12)) |
734 | desc->hyper = HYPER_SPAR; | |
c8b64f6d KZ |
735 | } |
736 | ||
b7744730 | 737 | #else /* ! (__x86_64__ || __i386__) */ |
c8b64f6d | 738 | static void |
6d791b4c | 739 | read_hypervisor_cpuid(struct lscpu_desc *desc __attribute__((__unused__))) |
c8b64f6d KZ |
740 | { |
741 | } | |
742 | #endif | |
743 | ||
6e509042 | 744 | static int is_devtree_compatible(struct lscpu_desc *desc, const char *str) |
3565bd7a | 745 | { |
6e509042 | 746 | FILE *fd = ul_path_fopen(desc->procfs, "r", "device-tree/compatible"); |
3565bd7a | 747 | |
3565bd7a KZ |
748 | if (fd) { |
749 | char buf[256]; | |
750 | size_t i, len; | |
751 | ||
752 | memset(buf, 0, sizeof(buf)); | |
753 | len = fread(buf, 1, sizeof(buf) - 1, fd); | |
754 | fclose(fd); | |
755 | ||
756 | for (i = 0; i < len;) { | |
757 | if (!strcmp(&buf[i], str)) | |
758 | return 1; | |
759 | i += strlen(&buf[i]); | |
760 | i++; | |
761 | } | |
762 | } | |
763 | ||
764 | return 0; | |
765 | } | |
766 | ||
5bd31c6d RM |
767 | static int |
768 | read_hypervisor_powerpc(struct lscpu_desc *desc) | |
769 | { | |
770 | assert(!desc->hyper); | |
771 | ||
79738105 | 772 | /* IBM iSeries: legacy, para-virtualized on top of OS/400 */ |
6e509042 | 773 | if (ul_path_access(desc->procfs, F_OK, "iSeries") == 0) { |
5bd31c6d RM |
774 | desc->hyper = HYPER_OS400; |
775 | desc->virtype = VIRT_PARA; | |
79738105 KZ |
776 | |
777 | /* PowerNV (POWER Non-Virtualized, bare-metal) */ | |
6e509042 | 778 | } else if (is_devtree_compatible(desc, "ibm,powernv")) { |
e03b613e KZ |
779 | desc->hyper = HYPER_NONE; |
780 | desc->virtype = VIRT_NONE; | |
79738105 | 781 | |
d45984be | 782 | /* PowerVM (IBM's proprietary hypervisor, aka pHyp) */ |
6e509042 KZ |
783 | } else if (ul_path_access(desc->procfs, F_OK, "device-tree/ibm,partition-name") == 0 |
784 | && ul_path_access(desc->procfs, F_OK, "device-tree/hmc-managed?") == 0 | |
785 | && ul_path_access(desc->procfs, F_OK, "device-tree/chosen/qemu,graphic-width") != 0) { | |
786 | ||
5bd31c6d RM |
787 | FILE *fd; |
788 | desc->hyper = HYPER_PHYP; | |
789 | desc->virtype = VIRT_PARA; | |
6e509042 KZ |
790 | |
791 | fd = ul_path_fopen(desc->procfs, "r", "device-tree/ibm,partition-name"); | |
5bd31c6d RM |
792 | if (fd) { |
793 | char buf[256]; | |
6cbf75e5 | 794 | if (fscanf(fd, "%255s", buf) == 1 && !strcmp(buf, "full")) |
5bd31c6d RM |
795 | desc->virtype = VIRT_NONE; |
796 | fclose(fd); | |
797 | } | |
79738105 KZ |
798 | |
799 | /* Qemu */ | |
6e509042 | 800 | } else if (is_devtree_compatible(desc, "qemu,pseries")) { |
e03b613e KZ |
801 | desc->hyper = HYPER_KVM; |
802 | desc->virtype = VIRT_PARA; | |
5bd31c6d | 803 | } |
5bd31c6d RM |
804 | return desc->hyper; |
805 | } | |
806 | ||
b7744730 RM |
807 | #ifdef INCLUDE_VMWARE_BDOOR |
808 | ||
809 | #define VMWARE_BDOOR_MAGIC 0x564D5868 | |
810 | #define VMWARE_BDOOR_PORT 0x5658 | |
811 | #define VMWARE_BDOOR_CMD_GETVERSION 10 | |
812 | ||
77242032 | 813 | static UL_ASAN_BLACKLIST |
7845b91d MF |
814 | void vmware_bdoor(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) |
815 | { | |
816 | __asm__( | |
817 | #if defined(__PIC__) && defined(__i386__) | |
818 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
819 | "xchg %%ebx, %%esi;" | |
820 | "inl (%%dx), %%eax;" | |
821 | "xchg %%esi, %%ebx;" | |
822 | : "=S" (*ebx), | |
823 | #else | |
824 | "inl (%%dx), %%eax;" | |
825 | : "=b" (*ebx), | |
826 | #endif | |
827 | "=a" (*eax), | |
828 | "=c" (*ecx), | |
829 | "=d" (*edx) | |
830 | : "0" (VMWARE_BDOOR_MAGIC), | |
831 | "1" (VMWARE_BDOOR_CMD_GETVERSION), | |
832 | "2" (VMWARE_BDOOR_PORT), | |
833 | "3" (0) | |
834 | : "memory"); | |
835 | } | |
b7744730 RM |
836 | |
837 | static jmp_buf segv_handler_env; | |
838 | ||
839 | static void | |
840 | segv_handler(__attribute__((__unused__)) int sig, | |
841 | __attribute__((__unused__)) siginfo_t *info, | |
842 | __attribute__((__unused__)) void *ignored) | |
843 | { | |
844 | siglongjmp(segv_handler_env, 1); | |
845 | } | |
846 | ||
847 | static int | |
848 | is_vmware_platform(void) | |
849 | { | |
850 | uint32_t eax, ebx, ecx, edx; | |
851 | struct sigaction act, oact; | |
852 | ||
b5d61108 KZ |
853 | /* |
854 | * FIXME: Not reliable for non-root users. Note it works as expected if | |
855 | * vmware_bdoor() is not optimized for PIE, but then it fails to build | |
856 | * on 32bit x86 systems. See lscpu git log for more details (commit | |
857 | * 7845b91dbc7690064a2be6df690e4aaba728fb04). kzak [3-Nov-2016] | |
858 | */ | |
859 | if (getuid() != 0) | |
860 | return 0; | |
861 | ||
b7744730 RM |
862 | /* |
863 | * The assembly routine for vmware detection works | |
864 | * fine under vmware, even if ran as regular user. But | |
865 | * on real HW or under other hypervisors, it segfaults (which is | |
866 | * expected). So we temporarily install SIGSEGV handler to catch | |
867 | * the signal. All this magic is needed because lscpu | |
868 | * isn't supposed to require root privileges. | |
869 | */ | |
870 | if (sigsetjmp(segv_handler_env, 1)) | |
871 | return 0; | |
872 | ||
9dd2d497 | 873 | memset(&act, 0, sizeof(act)); |
b7744730 RM |
874 | act.sa_sigaction = segv_handler; |
875 | act.sa_flags = SA_SIGINFO; | |
876 | ||
877 | if (sigaction(SIGSEGV, &act, &oact)) | |
223939d9 | 878 | err(EXIT_FAILURE, _("cannot set signal handler")); |
b7744730 | 879 | |
7845b91d | 880 | vmware_bdoor(&eax, &ebx, &ecx, &edx); |
b7744730 RM |
881 | |
882 | if (sigaction(SIGSEGV, &oact, NULL)) | |
223939d9 | 883 | err(EXIT_FAILURE, _("cannot restore signal handler")); |
b7744730 RM |
884 | |
885 | return eax != (uint32_t)-1 && ebx == VMWARE_BDOOR_MAGIC; | |
886 | } | |
887 | ||
888 | #else /* ! INCLUDE_VMWARE_BDOOR */ | |
889 | ||
890 | static int | |
891 | is_vmware_platform(void) | |
892 | { | |
893 | return 0; | |
894 | } | |
895 | ||
896 | #endif /* INCLUDE_VMWARE_BDOOR */ | |
897 | ||
c8b64f6d | 898 | static void |
eff79ceb | 899 | read_hypervisor(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
c8b64f6d | 900 | { |
fb2627ce OO |
901 | FILE *fd; |
902 | ||
7572fb2b SB |
903 | /* We have to detect WSL first. is_vmware_platform() crashes on Windows 10. */ |
904 | ||
6e509042 | 905 | if ((fd = ul_path_fopen(desc->procfs, "r", "sys/kernel/osrelease"))) { |
7572fb2b SB |
906 | char buf[256]; |
907 | ||
908 | if (fgets(buf, sizeof(buf), fd) != NULL) { | |
909 | if (strstr(buf, "Microsoft")) { | |
910 | desc->hyper = HYPER_WSL; | |
911 | desc->virtype = VIRT_CONT; | |
912 | } | |
913 | } | |
914 | fclose(fd); | |
915 | if (desc->virtype) | |
916 | return; | |
917 | } | |
918 | ||
fb2627ce | 919 | if (mod->system != SYSTEM_SNAPSHOT) { |
eff79ceb | 920 | read_hypervisor_cpuid(desc); |
fb2627ce OO |
921 | if (!desc->hyper) |
922 | desc->hyper = read_hypervisor_dmi(); | |
b7744730 RM |
923 | if (!desc->hyper && is_vmware_platform()) |
924 | desc->hyper = HYPER_VMWARE; | |
fb2627ce | 925 | } |
c8b64f6d | 926 | |
0ebbe9f1 | 927 | if (desc->hyper) { |
e8aa16ee | 928 | desc->virtype = VIRT_FULL; |
c8b64f6d | 929 | |
b4d1fbda | 930 | if (desc->hyper == HYPER_XEN) { |
0ebbe9f1 DZ |
931 | uint32_t features; |
932 | ||
6e509042 KZ |
933 | fd = ul_prefix_fopen(desc->prefix, "r", _PATH_SYS_HYP_FEATURES); |
934 | ||
0ebbe9f1 DZ |
935 | if (fd && fscanf(fd, "%x", &features) == 1) { |
936 | /* Xen PV domain */ | |
937 | if (features & XEN_FEATURES_PV_MASK) | |
938 | desc->virtype = VIRT_PARA; | |
939 | /* Xen PVH domain */ | |
940 | else if ((features & XEN_FEATURES_PVH_MASK) | |
941 | == XEN_FEATURES_PVH_MASK) | |
942 | desc->virtype = VIRT_PARA; | |
0ebbe9f1 | 943 | } |
d29b7ced KZ |
944 | if (fd) |
945 | fclose(fd); | |
0ebbe9f1 | 946 | } |
48f80ab3 KZ |
947 | } else if (read_hypervisor_powerpc(desc) > 0) { |
948 | /* read_hypervisor_powerpc() sets all necessary stuff to @desc */ | |
949 | ; | |
fb2627ce | 950 | /* Xen para-virt or dom0 */ |
48f80ab3 | 951 | } else if (ul_path_access(desc->procfs, F_OK, "xen") == 0) { |
c8b64f6d KZ |
952 | int dom0 = 0; |
953 | ||
6e509042 | 954 | fd = ul_path_fopen(desc->procfs, "r", "xen/capabilities"); |
c8b64f6d KZ |
955 | if (fd) { |
956 | char buf[256]; | |
957 | ||
6cbf75e5 | 958 | if (fscanf(fd, "%255s", buf) == 1 && |
c8b64f6d KZ |
959 | !strcmp(buf, "control_d")) |
960 | dom0 = 1; | |
961 | fclose(fd); | |
962 | } | |
e8aa16ee KZ |
963 | desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA; |
964 | desc->hyper = HYPER_XEN; | |
c8b64f6d | 965 | |
fb2627ce | 966 | /* Xen full-virt on non-x86_64 */ |
6e509042 | 967 | } else if (has_pci_device(desc, hv_vendor_pci[HYPER_XEN], hv_graphics_pci[HYPER_XEN])) { |
e8aa16ee KZ |
968 | desc->hyper = HYPER_XEN; |
969 | desc->virtype = VIRT_FULL; | |
6e509042 | 970 | } else if (has_pci_device(desc, hv_vendor_pci[HYPER_VMWARE], hv_graphics_pci[HYPER_VMWARE])) { |
96ce475f RM |
971 | desc->hyper = HYPER_VMWARE; |
972 | desc->virtype = VIRT_FULL; | |
6e509042 | 973 | } else if (has_pci_device(desc, hv_vendor_pci[HYPER_VBOX], hv_graphics_pci[HYPER_VBOX])) { |
96ce475f RM |
974 | desc->hyper = HYPER_VBOX; |
975 | desc->virtype = VIRT_FULL; | |
fb2627ce OO |
976 | |
977 | /* IBM PR/SM */ | |
6e509042 | 978 | } else if ((fd = ul_path_fopen(desc->procfs, "r", "sysinfo"))) { |
b8ec7bdf HC |
979 | char buf[BUFSIZ]; |
980 | ||
981 | desc->hyper = HYPER_IBM; | |
10829cd7 | 982 | desc->hypervisor = "PR/SM"; |
b8ec7bdf | 983 | desc->virtype = VIRT_FULL; |
904ffe1f | 984 | while (fgets(buf, sizeof(buf), fd) != NULL) { |
d0b75b03 | 985 | char *str, *p; |
10829cd7 | 986 | |
b8ec7bdf HC |
987 | if (!strstr(buf, "Control Program:")) |
988 | continue; | |
989 | if (!strstr(buf, "KVM")) | |
990 | desc->hyper = HYPER_IBM; | |
991 | else | |
992 | desc->hyper = HYPER_KVM; | |
d0b75b03 KZ |
993 | p = strchr(buf, ':'); |
994 | if (!p) | |
10829cd7 | 995 | continue; |
d0b75b03 | 996 | xasprintf(&str, "%s", p + 1); |
9d8537ff | 997 | |
10829cd7 HC |
998 | /* remove leading, trailing and repeating whitespace */ |
999 | while (*str == ' ') | |
1000 | str++; | |
1001 | desc->hypervisor = str; | |
1002 | str += strlen(str) - 1; | |
1003 | while ((*str == '\n') || (*str == ' ')) | |
1004 | *(str--) = '\0'; | |
1005 | while ((str = strstr(desc->hypervisor, " "))) | |
1006 | memmove(str, str + 1, strlen(str)); | |
350f5c8d | 1007 | break; |
b8ec7bdf | 1008 | } |
904ffe1f | 1009 | fclose(fd); |
c8b64f6d | 1010 | } |
fb2627ce OO |
1011 | |
1012 | /* OpenVZ/Virtuozzo - /proc/vz dir should exist | |
1013 | * /proc/bc should not */ | |
6e509042 KZ |
1014 | else if (ul_path_access(desc->procfs, F_OK, "vz") == 0 && |
1015 | ul_path_access(desc->procfs, F_OK, "bc") != 0) { | |
fb2627ce | 1016 | desc->hyper = HYPER_PARALLELS; |
857d9cdf | 1017 | desc->virtype = VIRT_CONT; |
fb2627ce OO |
1018 | |
1019 | /* IBM */ | |
857d9cdf | 1020 | } else if (desc->vendor && |
fb2627ce | 1021 | (strcmp(desc->vendor, "PowerVM Lx86") == 0 || |
857d9cdf | 1022 | strcmp(desc->vendor, "IBM/S390") == 0)) { |
fb2627ce | 1023 | desc->hyper = HYPER_IBM; |
857d9cdf | 1024 | desc->virtype = VIRT_FULL; |
fb2627ce OO |
1025 | |
1026 | /* User-mode-linux */ | |
857d9cdf | 1027 | } else if (desc->modelname && strstr(desc->modelname, "UML")) { |
fb2627ce | 1028 | desc->hyper = HYPER_UML; |
857d9cdf | 1029 | desc->virtype = VIRT_PARA; |
fb2627ce OO |
1030 | |
1031 | /* Linux-VServer */ | |
6e509042 | 1032 | } else if ((fd = ul_path_fopen(desc->procfs, "r", "self/status"))) { |
fb2627ce OO |
1033 | char buf[BUFSIZ]; |
1034 | char *val = NULL; | |
1035 | ||
fb2627ce OO |
1036 | while (fgets(buf, sizeof(buf), fd) != NULL) { |
1037 | if (lookup(buf, "VxID", &val)) | |
1038 | break; | |
1039 | } | |
1040 | fclose(fd); | |
1041 | ||
1042 | if (val) { | |
c6f6afc1 KZ |
1043 | char *org = val; |
1044 | ||
fb2627ce OO |
1045 | while (isdigit(*val)) |
1046 | ++val; | |
857d9cdf | 1047 | if (!*val) { |
fb2627ce | 1048 | desc->hyper = HYPER_VSERVER; |
857d9cdf KZ |
1049 | desc->virtype = VIRT_CONT; |
1050 | } | |
c6f6afc1 | 1051 | free(org); |
fb2627ce OO |
1052 | } |
1053 | } | |
c8b64f6d KZ |
1054 | } |
1055 | ||
455fe9a0 | 1056 | /* add @set to the @ary, unnecessary set is deallocated. */ |
7e03f383 KZ |
1057 | static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set) |
1058 | { | |
1059 | int i; | |
1060 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1061 | ||
1062 | if (!ary) | |
1063 | return -1; | |
1064 | ||
1065 | for (i = 0; i < *items; i++) { | |
1066 | if (CPU_EQUAL_S(setsize, set, ary[i])) | |
1067 | break; | |
1068 | } | |
1069 | if (i == *items) { | |
1070 | ary[*items] = set; | |
1071 | ++*items; | |
1072 | return 0; | |
1073 | } | |
1074 | CPU_FREE(set); | |
1075 | return 1; | |
1076 | } | |
1077 | ||
5dd7507c | 1078 | static void |
4f642863 | 1079 | read_topology(struct lscpu_desc *desc, int idx) |
5dd7507c | 1080 | { |
b3adf6ef HC |
1081 | cpu_set_t *thread_siblings, *core_siblings; |
1082 | cpu_set_t *book_siblings, *drawer_siblings; | |
0d2b5d2a HC |
1083 | int coreid, socketid, bookid, drawerid; |
1084 | int i, num = real_cpu_num(desc, idx); | |
7e03f383 | 1085 | |
6e509042 | 1086 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/topology/thread_siblings", num) != 0) |
7e03f383 | 1087 | return; |
5dd7507c | 1088 | |
6e509042 KZ |
1089 | ul_path_readf_cpuset(desc->syscpu, &thread_siblings, maxcpus, |
1090 | "cpu%d/topology/thread_siblings", num); | |
1091 | ul_path_readf_cpuset(desc->syscpu, &core_siblings, maxcpus, | |
1092 | "cpu%d/topology/core_siblings", num); | |
1093 | ul_path_readf_cpuset(desc->syscpu, &book_siblings, maxcpus, | |
1094 | "cpu%d/topology/book_siblings", num); | |
1095 | ul_path_readf_cpuset(desc->syscpu, &drawer_siblings, maxcpus, | |
1096 | "cpu%d/topology/drawer_siblings", num); | |
1097 | ||
1098 | if (ul_path_readf_s32(desc->syscpu, &coreid, "cpu%d/topology/core_id", num) != 0) | |
1099 | coreid = -1; | |
1100 | ||
1101 | if (ul_path_readf_s32(desc->syscpu, &socketid, "cpu%d/topology/physical_package_id", num) != 0) | |
1102 | socketid = -1; | |
1103 | ||
1104 | if (ul_path_readf_s32(desc->syscpu, &bookid, "cpu%d/topology/book_id", num) != 0) | |
1105 | bookid = -1; | |
1106 | ||
1107 | if (ul_path_readf_s32(desc->syscpu, &drawerid, "cpu%d/topology/drawer_id", num) != 0) | |
1108 | drawerid = -1; | |
aac1e59e KZ |
1109 | |
1110 | if (!desc->coremaps) { | |
b3adf6ef | 1111 | int ndrawers, nbooks, nsockets, ncores, nthreads; |
7e03f383 KZ |
1112 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
1113 | ||
1114 | /* threads within one core */ | |
1115 | nthreads = CPU_COUNT_S(setsize, thread_siblings); | |
d4bfa64d KZ |
1116 | if (!nthreads) |
1117 | nthreads = 1; | |
1118 | ||
7e03f383 KZ |
1119 | /* cores within one socket */ |
1120 | ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads; | |
d4bfa64d KZ |
1121 | if (!ncores) |
1122 | ncores = 1; | |
1123 | ||
1124 | /* number of sockets within one book. Because of odd / | |
1125 | * non-present cpu maps and to keep calculation easy we make | |
1126 | * sure that nsockets and nbooks is at least 1. | |
32a46618 | 1127 | */ |
d4bfa64d KZ |
1128 | nsockets = desc->ncpus / nthreads / ncores; |
1129 | if (!nsockets) | |
1130 | nsockets = 1; | |
1131 | ||
56baaa4e | 1132 | /* number of books */ |
d4bfa64d KZ |
1133 | nbooks = desc->ncpus / nthreads / ncores / nsockets; |
1134 | if (!nbooks) | |
1135 | nbooks = 1; | |
e282eec2 | 1136 | |
b3adf6ef HC |
1137 | /* number of drawers */ |
1138 | ndrawers = desc->ncpus / nbooks / nthreads / ncores / nsockets; | |
1139 | if (!ndrawers) | |
1140 | ndrawers = 1; | |
1141 | ||
e282eec2 | 1142 | /* all threads, see also read_basicinfo() |
32a46618 | 1143 | * -- fallback for kernels without |
e282eec2 KZ |
1144 | * /sys/devices/system/cpu/online. |
1145 | */ | |
1146 | if (!desc->nthreads) | |
b3adf6ef | 1147 | desc->nthreads = ndrawers * nbooks * nsockets * ncores * nthreads; |
d4bfa64d | 1148 | |
a5cfffff | 1149 | /* For each map we make sure that it can have up to ncpuspos |
9d1a3a18 HC |
1150 | * entries. This is because we cannot reliably calculate the |
1151 | * number of cores, sockets and books on all architectures. | |
1152 | * E.g. completely virtualized architectures like s390 may | |
1153 | * have multiple sockets of different sizes. | |
1154 | */ | |
a5cfffff TK |
1155 | desc->coremaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
1156 | desc->socketmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); | |
0d2b5d2a HC |
1157 | desc->coreids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); |
1158 | desc->socketids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); | |
1159 | for (i = 0; i < desc->ncpuspos; i++) | |
1160 | desc->coreids[i] = desc->socketids[i] = -1; | |
1161 | if (book_siblings) { | |
a5cfffff | 1162 | desc->bookmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
0d2b5d2a HC |
1163 | desc->bookids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); |
1164 | for (i = 0; i < desc->ncpuspos; i++) | |
1165 | desc->bookids[i] = -1; | |
1166 | } | |
1167 | if (drawer_siblings) { | |
b3adf6ef | 1168 | desc->drawermaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
0d2b5d2a HC |
1169 | desc->drawerids = xcalloc(desc->ncpuspos, sizeof(*desc->drawerids)); |
1170 | for (i = 0; i < desc->ncpuspos; i++) | |
1171 | desc->drawerids[i] = -1; | |
1172 | } | |
7e03f383 | 1173 | } |
5dd7507c | 1174 | |
7e03f383 | 1175 | add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings); |
0d2b5d2a | 1176 | desc->coreids[idx] = coreid; |
7e03f383 | 1177 | add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings); |
0d2b5d2a HC |
1178 | desc->socketids[idx] = socketid; |
1179 | if (book_siblings) { | |
56baaa4e | 1180 | add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings); |
0d2b5d2a HC |
1181 | desc->bookids[idx] = bookid; |
1182 | } | |
1183 | if (drawer_siblings) { | |
b3adf6ef | 1184 | add_cpuset_to_array(desc->drawermaps, &desc->ndrawers, drawer_siblings); |
0d2b5d2a HC |
1185 | desc->drawerids[idx] = drawerid; |
1186 | } | |
7e03f383 | 1187 | } |
4f642863 | 1188 | |
2b8fcb81 | 1189 | static void |
4f642863 | 1190 | read_polarization(struct lscpu_desc *desc, int idx) |
2b8fcb81 HC |
1191 | { |
1192 | char mode[64]; | |
4f642863 | 1193 | int num = real_cpu_num(desc, idx); |
2b8fcb81 HC |
1194 | |
1195 | if (desc->dispatching < 0) | |
1196 | return; | |
6e509042 | 1197 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/polarization", num) != 0) |
2b8fcb81 HC |
1198 | return; |
1199 | if (!desc->polarization) | |
a5cfffff | 1200 | desc->polarization = xcalloc(desc->ncpuspos, sizeof(int)); |
6e509042 KZ |
1201 | |
1202 | ul_path_readf_buffer(desc->syscpu, mode, sizeof(mode), "cpu%d/polarization", num); | |
1203 | ||
2b8fcb81 | 1204 | if (strncmp(mode, "vertical:low", sizeof(mode)) == 0) |
4f642863 | 1205 | desc->polarization[idx] = POLAR_VLOW; |
2b8fcb81 | 1206 | else if (strncmp(mode, "vertical:medium", sizeof(mode)) == 0) |
4f642863 | 1207 | desc->polarization[idx] = POLAR_VMEDIUM; |
2b8fcb81 | 1208 | else if (strncmp(mode, "vertical:high", sizeof(mode)) == 0) |
4f642863 | 1209 | desc->polarization[idx] = POLAR_VHIGH; |
2b8fcb81 | 1210 | else if (strncmp(mode, "horizontal", sizeof(mode)) == 0) |
4f642863 | 1211 | desc->polarization[idx] = POLAR_HORIZONTAL; |
2b8fcb81 | 1212 | else |
4f642863 | 1213 | desc->polarization[idx] = POLAR_UNKNOWN; |
2b8fcb81 | 1214 | } |
7e03f383 | 1215 | |
596b8845 | 1216 | static void |
4f642863 | 1217 | read_address(struct lscpu_desc *desc, int idx) |
596b8845 | 1218 | { |
4f642863 AT |
1219 | int num = real_cpu_num(desc, idx); |
1220 | ||
6e509042 | 1221 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/address", num) != 0) |
596b8845 HC |
1222 | return; |
1223 | if (!desc->addresses) | |
a5cfffff | 1224 | desc->addresses = xcalloc(desc->ncpuspos, sizeof(int)); |
6e509042 | 1225 | ul_path_readf_s32(desc->syscpu, &desc->addresses[idx], "cpu%d/address", num); |
596b8845 | 1226 | } |
7e03f383 | 1227 | |
d231eea1 | 1228 | static void |
4f642863 | 1229 | read_configured(struct lscpu_desc *desc, int idx) |
d231eea1 | 1230 | { |
4f642863 AT |
1231 | int num = real_cpu_num(desc, idx); |
1232 | ||
6e509042 | 1233 | if (ul_path_accessf(desc->syscpu, F_OK, "cpu%d/configure", num) != 0) |
d231eea1 HC |
1234 | return; |
1235 | if (!desc->configured) | |
a5cfffff | 1236 | desc->configured = xcalloc(desc->ncpuspos, sizeof(int)); |
6e509042 | 1237 | ul_path_readf_s32(desc->syscpu, &desc->configured[idx], "cpu%d/configure", num); |
d231eea1 HC |
1238 | } |
1239 | ||
fc07d9f5 | 1240 | /* Read overall maximum frequency of cpu */ |
89222b17 KZ |
1241 | static char * |
1242 | cpu_max_mhz(struct lscpu_desc *desc, char *buf, size_t bufsz) | |
fc07d9f5 MI |
1243 | { |
1244 | int i; | |
95f09bc6 | 1245 | float cpu_freq = 0.0; |
538b50cb | 1246 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
fc07d9f5 MI |
1247 | |
1248 | if (desc->present) { | |
95f09bc6 | 1249 | for (i = 0; i < desc->ncpuspos; i++) { |
538b50cb | 1250 | if (CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present) |
0145d84a | 1251 | && desc->maxmhz[i]) { |
fc07d9f5 MI |
1252 | float freq = atof(desc->maxmhz[i]); |
1253 | ||
1254 | if (freq > cpu_freq) | |
1255 | cpu_freq = freq; | |
1256 | } | |
1257 | } | |
1258 | } | |
89222b17 KZ |
1259 | snprintf(buf, bufsz, "%.4f", cpu_freq); |
1260 | return buf; | |
fc07d9f5 MI |
1261 | } |
1262 | ||
1263 | /* Read overall minimum frequency of cpu */ | |
89222b17 KZ |
1264 | static char * |
1265 | cpu_min_mhz(struct lscpu_desc *desc, char *buf, size_t bufsz) | |
fc07d9f5 | 1266 | { |
95f09bc6 DM |
1267 | int i; |
1268 | float cpu_freq = -1.0; | |
538b50cb | 1269 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
fc07d9f5 MI |
1270 | |
1271 | if (desc->present) { | |
95f09bc6 | 1272 | for (i = 0; i < desc->ncpuspos; i++) { |
538b50cb | 1273 | if (CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present) |
0145d84a | 1274 | && desc->minmhz[i]) { |
fc07d9f5 MI |
1275 | float freq = atof(desc->minmhz[i]); |
1276 | ||
95f09bc6 | 1277 | if (cpu_freq < 0.0 || freq < cpu_freq) |
fc07d9f5 MI |
1278 | cpu_freq = freq; |
1279 | } | |
1280 | } | |
1281 | } | |
89222b17 KZ |
1282 | snprintf(buf, bufsz, "%.4f", cpu_freq); |
1283 | return buf; | |
fc07d9f5 MI |
1284 | } |
1285 | ||
1286 | ||
44320710 | 1287 | static void |
4f642863 | 1288 | read_max_mhz(struct lscpu_desc *desc, int idx) |
44320710 | 1289 | { |
4f642863 | 1290 | int num = real_cpu_num(desc, idx); |
6e509042 | 1291 | int mhz; |
4f642863 | 1292 | |
6e509042 | 1293 | if (ul_path_readf_s32(desc->syscpu, &mhz, "cpu%d/cpufreq/cpuinfo_max_freq", num) != 0) |
44320710 | 1294 | return; |
e065a597 DB |
1295 | if (!desc->maxmhz) |
1296 | desc->maxmhz = xcalloc(desc->ncpuspos, sizeof(char *)); | |
6e509042 | 1297 | xasprintf(&desc->maxmhz[idx], "%.4f", (float) mhz / 1000); |
44320710 SK |
1298 | } |
1299 | ||
e065a597 | 1300 | static void |
4f642863 | 1301 | read_min_mhz(struct lscpu_desc *desc, int idx) |
e065a597 | 1302 | { |
4f642863 | 1303 | int num = real_cpu_num(desc, idx); |
6e509042 | 1304 | int mhz; |
4f642863 | 1305 | |
6e509042 | 1306 | if (ul_path_readf_s32(desc->syscpu, &mhz, "cpu%d/cpufreq/cpuinfo_min_freq", num) != 0) |
e065a597 DB |
1307 | return; |
1308 | if (!desc->minmhz) | |
1309 | desc->minmhz = xcalloc(desc->ncpuspos, sizeof(char *)); | |
6e509042 | 1310 | xasprintf(&desc->minmhz[idx], "%.4f", (float) mhz / 1000); |
e065a597 DB |
1311 | } |
1312 | ||
7e03f383 KZ |
1313 | static int |
1314 | cachecmp(const void *a, const void *b) | |
1315 | { | |
1316 | struct cpu_cache *c1 = (struct cpu_cache *) a; | |
1317 | struct cpu_cache *c2 = (struct cpu_cache *) b; | |
1318 | ||
1319 | return strcmp(c2->name, c1->name); | |
5dd7507c CQ |
1320 | } |
1321 | ||
1322 | static void | |
4f642863 | 1323 | read_cache(struct lscpu_desc *desc, int idx) |
5dd7507c CQ |
1324 | { |
1325 | char buf[256]; | |
7e03f383 | 1326 | int i; |
4f642863 | 1327 | int num = real_cpu_num(desc, idx); |
5dd7507c | 1328 | |
aac1e59e | 1329 | if (!desc->ncaches) { |
6e509042 KZ |
1330 | while (ul_path_accessf(desc->syscpu, F_OK, |
1331 | "cpu%d/cache/index%d", | |
1332 | num, desc->ncaches) == 0) | |
7e03f383 | 1333 | desc->ncaches++; |
5dd7507c | 1334 | |
7e03f383 KZ |
1335 | if (!desc->ncaches) |
1336 | return; | |
08de16d0 | 1337 | desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches)); |
7e03f383 KZ |
1338 | } |
1339 | for (i = 0; i < desc->ncaches; i++) { | |
1340 | struct cpu_cache *ca = &desc->caches[i]; | |
1341 | cpu_set_t *map; | |
1342 | ||
6e509042 KZ |
1343 | if (ul_path_accessf(desc->syscpu, F_OK, |
1344 | "cpu%d/cache/index%d", num, i) != 0) | |
dcdead42 | 1345 | continue; |
7e03f383 | 1346 | if (!ca->name) { |
0e86bc84 | 1347 | int type = 0; |
7e03f383 KZ |
1348 | |
1349 | /* cache type */ | |
0e86bc84 | 1350 | if (ul_path_readf_string(desc->syscpu, &ca->type, |
6e509042 | 1351 | "cpu%d/cache/index%d/type", num, i) > 0) { |
0e86bc84 | 1352 | if (!strcmp(ca->type, "Data")) |
6e509042 | 1353 | type = 'd'; |
0e86bc84 | 1354 | else if (!strcmp(ca->type, "Instruction")) |
6e509042 KZ |
1355 | type = 'i'; |
1356 | } | |
7e03f383 KZ |
1357 | |
1358 | /* cache level */ | |
0e86bc84 | 1359 | ul_path_readf_s32(desc->syscpu, &ca->level, |
6e509042 | 1360 | "cpu%d/cache/index%d/level", num, i); |
7e03f383 | 1361 | if (type) |
0e86bc84 | 1362 | snprintf(buf, sizeof(buf), "L%d%c", ca->level, type); |
7e03f383 | 1363 | else |
0e86bc84 | 1364 | snprintf(buf, sizeof(buf), "L%d", ca->level); |
7e03f383 KZ |
1365 | |
1366 | ca->name = xstrdup(buf); | |
1367 | ||
cf3b6b71 | 1368 | ul_path_readf_u32(desc->syscpu, &ca->ways_of_associativity, |
0e86bc84 | 1369 | "cpu%d/cache/index%d/ways_of_associativity", num, i); |
cf3b6b71 KZ |
1370 | ul_path_readf_u32(desc->syscpu, &ca->physical_line_partition, |
1371 | "cpu%d/cache/index%d/physical_line_partition", num, i); | |
1372 | ul_path_readf_u32(desc->syscpu, &ca->number_of_sets, | |
1373 | "cpu%d/cache/index%d/number_of_sets", num, i); | |
1374 | ul_path_readf_u32(desc->syscpu, &ca->coherency_line_size, | |
1375 | "cpu%d/cache/index%d/coherency_line_size", num, i); | |
1376 | ||
1377 | ul_path_readf_string(desc->syscpu, &ca->allocation_policy, | |
1378 | "cpu%d/cache/index%d/allocation_policy", num, i); | |
1379 | ul_path_readf_string(desc->syscpu, &ca->write_policy, | |
1380 | "cpu%d/cache/index%d/write_policy", num, i); | |
0e86bc84 | 1381 | |
7e03f383 | 1382 | /* cache size */ |
0e86bc84 KZ |
1383 | if (ul_path_readf_buffer(desc->syscpu, buf, sizeof(buf), |
1384 | "cpu%d/cache/index%d/size", num, i) > 0) | |
1385 | parse_size(buf, &ca->size, NULL); | |
1386 | else | |
1387 | ca->size = 0; | |
7e03f383 | 1388 | } |
5dd7507c | 1389 | |
7e03f383 | 1390 | /* information about how CPUs share different caches */ |
6e509042 KZ |
1391 | ul_path_readf_cpuset(desc->syscpu, &map, maxcpus, |
1392 | "cpu%d/cache/index%d/shared_cpu_map", num, i); | |
5dd7507c | 1393 | |
08de16d0 | 1394 | if (!ca->sharedmaps) |
a5cfffff | 1395 | ca->sharedmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
7e03f383 | 1396 | add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map); |
5dd7507c CQ |
1397 | } |
1398 | } | |
1399 | ||
e9074a16 KZ |
1400 | static inline int is_node_dirent(struct dirent *d) |
1401 | { | |
1402 | return | |
1403 | d && | |
1404 | #ifdef _DIRENT_HAVE_D_TYPE | |
c0cf4ae9 | 1405 | (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) && |
e9074a16 KZ |
1406 | #endif |
1407 | strncmp(d->d_name, "node", 4) == 0 && | |
1408 | isdigit_string(d->d_name + 4); | |
1409 | } | |
1410 | ||
39c758d1 KZ |
1411 | static int |
1412 | nodecmp(const void *ap, const void *bp) | |
1413 | { | |
1414 | int *a = (int *) ap, *b = (int *) bp; | |
1415 | return *a - *b; | |
1416 | } | |
1417 | ||
5dd7507c | 1418 | static void |
e8aa16ee | 1419 | read_nodes(struct lscpu_desc *desc) |
5dd7507c | 1420 | { |
e9074a16 KZ |
1421 | int i = 0; |
1422 | DIR *dir; | |
1423 | struct dirent *d; | |
6e509042 | 1424 | struct path_cxt *sysnode; |
5dd7507c | 1425 | |
a25fb9e8 RM |
1426 | desc->nnodes = 0; |
1427 | ||
6e509042 KZ |
1428 | sysnode = ul_new_path(_PATH_SYS_NODE); |
1429 | if (!sysnode) | |
1430 | err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_NODE); | |
1431 | ul_path_set_prefix(sysnode, desc->prefix); | |
1432 | ||
1433 | dir = ul_path_opendir(sysnode, NULL); | |
1434 | if (!dir) | |
1435 | goto done; | |
1436 | ||
a25fb9e8 | 1437 | while ((d = readdir(dir))) { |
e9074a16 KZ |
1438 | if (is_node_dirent(d)) |
1439 | desc->nnodes++; | |
1440 | } | |
1441 | ||
1442 | if (!desc->nnodes) { | |
a25fb9e8 | 1443 | closedir(dir); |
6e509042 | 1444 | goto done; |
e9074a16 | 1445 | } |
5dd7507c | 1446 | |
08de16d0 | 1447 | desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *)); |
e9074a16 KZ |
1448 | desc->idx2nodenum = xmalloc(desc->nnodes * sizeof(int)); |
1449 | ||
a25fb9e8 RM |
1450 | rewinddir(dir); |
1451 | while ((d = readdir(dir)) && i < desc->nnodes) { | |
1452 | if (is_node_dirent(d)) | |
1453 | desc->idx2nodenum[i++] = strtol_or_err(((d->d_name) + 4), | |
1454 | _("Failed to extract the node number")); | |
e9074a16 | 1455 | } |
a25fb9e8 RM |
1456 | closedir(dir); |
1457 | qsort(desc->idx2nodenum, desc->nnodes, sizeof(int), nodecmp); | |
5dd7507c CQ |
1458 | |
1459 | /* information about how nodes share different CPUs */ | |
7e03f383 | 1460 | for (i = 0; i < desc->nnodes; i++) |
6e509042 KZ |
1461 | ul_path_readf_cpuset(sysnode, &desc->nodemaps[i], maxcpus, |
1462 | "node%d/cpumap", desc->idx2nodenum[i]); | |
1463 | done: | |
1464 | ul_unref_path(sysnode); | |
5dd7507c CQ |
1465 | } |
1466 | ||
e3b3a2f3 | 1467 | static char * |
4f642863 | 1468 | get_cell_data(struct lscpu_desc *desc, int idx, int col, |
e3b3a2f3 KZ |
1469 | struct lscpu_modifier *mod, |
1470 | char *buf, size_t bufsz) | |
5dd7507c | 1471 | { |
7e03f383 | 1472 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
4f642863 AT |
1473 | size_t i; |
1474 | int cpu = real_cpu_num(desc, idx); | |
e3b3a2f3 KZ |
1475 | |
1476 | *buf = '\0'; | |
5dd7507c | 1477 | |
477251f8 | 1478 | switch (col) { |
cc07239d | 1479 | case COL_CPU_CPU: |
e3b3a2f3 | 1480 | snprintf(buf, bufsz, "%d", cpu); |
477251f8 | 1481 | break; |
cc07239d | 1482 | case COL_CPU_CORE: |
0d2b5d2a HC |
1483 | if (mod->physical) { |
1484 | if (desc->coreids[idx] == -1) | |
1485 | snprintf(buf, bufsz, "-"); | |
1486 | else | |
1487 | snprintf(buf, bufsz, "%d", desc->coreids[idx]); | |
1488 | } else { | |
1489 | if (cpuset_ary_isset(cpu, desc->coremaps, | |
1490 | desc->ncores, setsize, &i) == 0) | |
1491 | snprintf(buf, bufsz, "%zu", i); | |
1492 | } | |
477251f8 | 1493 | break; |
cc07239d | 1494 | case COL_CPU_SOCKET: |
0d2b5d2a HC |
1495 | if (mod->physical) { |
1496 | if (desc->socketids[idx] == -1) | |
1497 | snprintf(buf, bufsz, "-"); | |
1498 | else | |
1499 | snprintf(buf, bufsz, "%d", desc->socketids[idx]); | |
1500 | } else { | |
1501 | if (cpuset_ary_isset(cpu, desc->socketmaps, | |
1502 | desc->nsockets, setsize, &i) == 0) | |
1503 | snprintf(buf, bufsz, "%zu", i); | |
1504 | } | |
477251f8 | 1505 | break; |
cc07239d | 1506 | case COL_CPU_NODE: |
e9d659ea | 1507 | if (cpuset_ary_isset(cpu, desc->nodemaps, |
4f642863 | 1508 | desc->nnodes, setsize, &i) == 0) |
e9074a16 | 1509 | snprintf(buf, bufsz, "%d", desc->idx2nodenum[i]); |
477251f8 | 1510 | break; |
cc07239d | 1511 | case COL_CPU_DRAWER: |
0d2b5d2a HC |
1512 | if (mod->physical) { |
1513 | if (desc->drawerids[idx] == -1) | |
1514 | snprintf(buf, bufsz, "-"); | |
1515 | else | |
1516 | snprintf(buf, bufsz, "%d", desc->drawerids[idx]); | |
1517 | } else { | |
1518 | if (cpuset_ary_isset(cpu, desc->drawermaps, | |
1519 | desc->ndrawers, setsize, &i) == 0) | |
1520 | snprintf(buf, bufsz, "%zu", i); | |
1521 | } | |
b3adf6ef | 1522 | break; |
cc07239d | 1523 | case COL_CPU_BOOK: |
0d2b5d2a HC |
1524 | if (mod->physical) { |
1525 | if (desc->bookids[idx] == -1) | |
1526 | snprintf(buf, bufsz, "-"); | |
1527 | else | |
1528 | snprintf(buf, bufsz, "%d", desc->bookids[idx]); | |
1529 | } else { | |
1530 | if (cpuset_ary_isset(cpu, desc->bookmaps, | |
1531 | desc->nbooks, setsize, &i) == 0) | |
1532 | snprintf(buf, bufsz, "%zu", i); | |
1533 | } | |
477251f8 | 1534 | break; |
cc07239d | 1535 | case COL_CPU_CACHE: |
e3b3a2f3 KZ |
1536 | { |
1537 | char *p = buf; | |
1538 | size_t sz = bufsz; | |
1539 | int j; | |
1540 | ||
7e03f383 KZ |
1541 | for (j = desc->ncaches - 1; j >= 0; j--) { |
1542 | struct cpu_cache *ca = &desc->caches[j]; | |
e9d659ea KZ |
1543 | |
1544 | if (cpuset_ary_isset(cpu, ca->sharedmaps, | |
4f642863 | 1545 | ca->nsharedmaps, setsize, &i) == 0) { |
73f2bec5 | 1546 | int x = snprintf(p, sz, "%zu", i); |
06fa5817 | 1547 | if (x < 0 || (size_t) x >= sz) |
e3b3a2f3 KZ |
1548 | return NULL; |
1549 | p += x; | |
1550 | sz -= x; | |
1551 | } | |
1552 | if (j != 0) { | |
06fa5817 YK |
1553 | if (sz < 2) |
1554 | return NULL; | |
e3b3a2f3 KZ |
1555 | *p++ = mod->compat ? ',' : ':'; |
1556 | *p = '\0'; | |
0c5bbafa | 1557 | sz--; |
e3b3a2f3 | 1558 | } |
5dd7507c | 1559 | } |
477251f8 | 1560 | break; |
e3b3a2f3 | 1561 | } |
cc07239d | 1562 | case COL_CPU_POLARIZATION: |
ba45d8c1 | 1563 | if (desc->polarization) { |
4f642863 | 1564 | int x = desc->polarization[idx]; |
ba45d8c1 | 1565 | |
e3b3a2f3 | 1566 | snprintf(buf, bufsz, "%s", |
ba45d8c1 KZ |
1567 | mod->mode == OUTPUT_PARSABLE ? |
1568 | polar_modes[x].parsable : | |
1569 | polar_modes[x].readable); | |
1570 | } | |
2b8fcb81 | 1571 | break; |
cc07239d | 1572 | case COL_CPU_ADDRESS: |
596b8845 | 1573 | if (desc->addresses) |
4f642863 | 1574 | snprintf(buf, bufsz, "%d", desc->addresses[idx]); |
596b8845 | 1575 | break; |
cc07239d | 1576 | case COL_CPU_CONFIGURED: |
e43fc13e HC |
1577 | if (!desc->configured) |
1578 | break; | |
1579 | if (mod->mode == OUTPUT_PARSABLE) | |
f205c90a | 1580 | snprintf(buf, bufsz, "%s", |
4f642863 | 1581 | desc->configured[idx] ? _("Y") : _("N")); |
e43fc13e | 1582 | else |
f205c90a | 1583 | snprintf(buf, bufsz, "%s", |
4f642863 | 1584 | desc->configured[idx] ? _("yes") : _("no")); |
d231eea1 | 1585 | break; |
cc07239d | 1586 | case COL_CPU_ONLINE: |
e43fc13e HC |
1587 | if (!desc->online) |
1588 | break; | |
1589 | if (mod->mode == OUTPUT_PARSABLE) | |
f205c90a | 1590 | snprintf(buf, bufsz, "%s", |
e43fc13e HC |
1591 | is_cpu_online(desc, cpu) ? _("Y") : _("N")); |
1592 | else | |
f205c90a | 1593 | snprintf(buf, bufsz, "%s", |
847b982e | 1594 | is_cpu_online(desc, cpu) ? _("yes") : _("no")); |
a7e5300c | 1595 | break; |
cc07239d | 1596 | case COL_CPU_MAXMHZ: |
0145d84a | 1597 | if (desc->maxmhz && desc->maxmhz[idx]) |
4f642863 | 1598 | xstrncpy(buf, desc->maxmhz[idx], bufsz); |
e065a597 | 1599 | break; |
cc07239d | 1600 | case COL_CPU_MINMHZ: |
0145d84a | 1601 | if (desc->minmhz && desc->minmhz[idx]) |
4f642863 | 1602 | xstrncpy(buf, desc->minmhz[idx], bufsz); |
44320710 | 1603 | break; |
477251f8 | 1604 | } |
e3b3a2f3 KZ |
1605 | return buf; |
1606 | } | |
1607 | ||
1608 | static char * | |
1609 | get_cell_header(struct lscpu_desc *desc, int col, | |
1610 | struct lscpu_modifier *mod, | |
1611 | char *buf, size_t bufsz) | |
1612 | { | |
1613 | *buf = '\0'; | |
1614 | ||
cc07239d | 1615 | if (col == COL_CPU_CACHE) { |
e3b3a2f3 KZ |
1616 | char *p = buf; |
1617 | size_t sz = bufsz; | |
1618 | int i; | |
1619 | ||
1620 | for (i = desc->ncaches - 1; i >= 0; i--) { | |
1621 | int x = snprintf(p, sz, "%s", desc->caches[i].name); | |
06fa5817 | 1622 | if (x < 0 || (size_t) x >= sz) |
e3b3a2f3 KZ |
1623 | return NULL; |
1624 | sz -= x; | |
1625 | p += x; | |
1626 | if (i > 0) { | |
06fa5817 YK |
1627 | if (sz < 2) |
1628 | return NULL; | |
e3b3a2f3 KZ |
1629 | *p++ = mod->compat ? ',' : ':'; |
1630 | *p = '\0'; | |
0c5bbafa | 1631 | sz--; |
e3b3a2f3 KZ |
1632 | } |
1633 | } | |
1634 | if (desc->ncaches) | |
1635 | return buf; | |
1636 | } | |
cc07239d | 1637 | snprintf(buf, bufsz, "%s", coldescs_cpu[col].name); |
e3b3a2f3 | 1638 | return buf; |
477251f8 KZ |
1639 | } |
1640 | ||
0e86bc84 KZ |
1641 | /* |
1642 | * [-C] backend | |
1643 | */ | |
1644 | static void | |
1645 | print_caches_readable(struct lscpu_desc *desc, int cols[], int ncols, | |
1646 | struct lscpu_modifier *mod) | |
1647 | { | |
1648 | int i; | |
1649 | struct libscols_table *table; | |
1650 | ||
1651 | scols_init_debug(0); | |
1652 | ||
1653 | table = scols_new_table(); | |
1654 | if (!table) | |
1655 | err(EXIT_FAILURE, _("failed to allocate output table")); | |
1656 | if (mod->json) { | |
1657 | scols_table_enable_json(table, 1); | |
1658 | scols_table_set_name(table, "caches"); | |
1659 | } | |
1660 | ||
1661 | for (i = 0; i < ncols; i++) { | |
1662 | struct lscpu_coldesc *cd = &coldescs_cache[cols[i]]; | |
1663 | if (!scols_table_new_column(table, cd->name, 0, cd->flags)) | |
1664 | err(EXIT_FAILURE, _("failed to allocate output column")); | |
1665 | } | |
1666 | ||
4161da7b | 1667 | for (i = desc->ncaches - 1; i >= 0; i--) { |
0e86bc84 KZ |
1668 | struct cpu_cache *ca = &desc->caches[i]; |
1669 | struct libscols_line *line; | |
1670 | int c; | |
1671 | ||
1672 | line = scols_table_new_line(table, NULL); | |
1673 | if (!line) | |
1674 | err(EXIT_FAILURE, _("failed to allocate output line")); | |
1675 | ||
1676 | for (c = 0; c < ncols; c++) { | |
1677 | char *data = NULL; | |
1678 | int col = cols[c]; | |
1679 | ||
1680 | switch (col) { | |
1681 | case COL_CACHE_NAME: | |
1682 | if (ca->name) | |
1683 | data = xstrdup(ca->name); | |
1684 | break; | |
1685 | case COL_CACHE_ONESIZE: | |
1686 | if (!ca->size) | |
1687 | break; | |
1688 | if (mod->bytes) | |
1689 | xasprintf(&data, "%" PRIu64, ca->size); | |
1690 | else | |
1691 | data = size_to_human_string(SIZE_SUFFIX_1LETTER, ca->size); | |
1692 | break; | |
1693 | case COL_CACHE_ALLSIZE: | |
1694 | { | |
1695 | uint64_t sz = 0; | |
1696 | ||
1697 | if (get_cache_full_size(desc, ca, &sz) != 0) | |
1698 | break; | |
1699 | if (mod->bytes) | |
1700 | xasprintf(&data, "%" PRIu64, sz); | |
1701 | else | |
1702 | data = size_to_human_string(SIZE_SUFFIX_1LETTER, sz); | |
1703 | break; | |
1704 | } | |
1705 | case COL_CACHE_WAYS: | |
cf3b6b71 KZ |
1706 | if (ca->ways_of_associativity) |
1707 | xasprintf(&data, "%u", ca->ways_of_associativity); | |
0e86bc84 | 1708 | break; |
cf3b6b71 | 1709 | |
0e86bc84 KZ |
1710 | case COL_CACHE_TYPE: |
1711 | if (ca->type) | |
1712 | data = xstrdup(ca->type); | |
1713 | break; | |
1714 | case COL_CACHE_LEVEL: | |
1715 | if (ca->level) | |
1716 | xasprintf(&data, "%d", ca->level); | |
1717 | break; | |
cf3b6b71 KZ |
1718 | case COL_CACHE_ALLOCPOL: |
1719 | if (ca->allocation_policy) | |
1720 | data = xstrdup(ca->allocation_policy); | |
1721 | break; | |
1722 | case COL_CACHE_WRITEPOL: | |
1723 | if (ca->write_policy) | |
1724 | data = xstrdup(ca->write_policy); | |
1725 | break; | |
1726 | case COL_CACHE_PHYLINE: | |
1727 | if (ca->physical_line_partition) | |
1728 | xasprintf(&data, "%u", ca->physical_line_partition); | |
1729 | break; | |
1730 | case COL_CACHE_SETS: | |
1731 | if (ca->number_of_sets) | |
1732 | xasprintf(&data, "%u", ca->number_of_sets); | |
1733 | break; | |
1734 | case COL_CACHE_COHERENCYSIZE: | |
1735 | if (ca->coherency_line_size) | |
1736 | xasprintf(&data, "%u", ca->coherency_line_size); | |
1737 | break; | |
0e86bc84 KZ |
1738 | } |
1739 | ||
1740 | if (data && scols_line_refer_data(line, c, data)) | |
1741 | err(EXIT_FAILURE, _("failed to add output data")); | |
1742 | } | |
1743 | } | |
1744 | ||
1745 | scols_print_table(table); | |
1746 | scols_unref_table(table); | |
1747 | } | |
1748 | ||
477251f8 | 1749 | /* |
ba45d8c1 | 1750 | * [-p] backend, we support two parsable formats: |
477251f8 KZ |
1751 | * |
1752 | * 1) "compatible" -- this format is compatible with the original lscpu(1) | |
1753 | * output and it contains fixed set of the columns. The CACHE columns are at | |
1754 | * the end of the line and the CACHE is not printed if the number of the caches | |
1755 | * is zero. The CACHE columns are separated by two commas, for example: | |
1756 | * | |
1757 | * $ lscpu --parse | |
1758 | * # CPU,Core,Socket,Node,,L1d,L1i,L2 | |
1759 | * 0,0,0,0,,0,0,0 | |
1760 | * 1,1,0,0,,1,1,0 | |
1761 | * | |
1762 | * 2) "user defined output" -- this format prints always all columns without | |
1763 | * special prefix for CACHE column. If there are not CACHEs then the column is | |
1764 | * empty and the header "Cache" is printed rather than a real name of the cache. | |
1765 | * The CACHE columns are separated by ':'. | |
1766 | * | |
1767 | * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE | |
1768 | * # CPU,Core,Socket,Node,L1d:L1i:L2 | |
1769 | * 0,0,0,0,0:0:0 | |
1770 | * 1,1,0,0,1:1:0 | |
1771 | */ | |
1772 | static void | |
cc07239d | 1773 | print_cpus_parsable(struct lscpu_desc *desc, int cols[], int ncols, |
8005924a | 1774 | struct lscpu_modifier *mod) |
477251f8 | 1775 | { |
e3b3a2f3 KZ |
1776 | char buf[BUFSIZ], *data; |
1777 | int i; | |
477251f8 | 1778 | |
e3b3a2f3 KZ |
1779 | /* |
1780 | * Header | |
1781 | */ | |
477251f8 KZ |
1782 | printf(_( |
1783 | "# The following is the parsable format, which can be fed to other\n" | |
1784 | "# programs. Each different item in every column has an unique ID\n" | |
1785 | "# starting from zero.\n")); | |
1786 | ||
1787 | fputs("# ", stdout); | |
1788 | for (i = 0; i < ncols; i++) { | |
3d27b76a | 1789 | int col = cols[i]; |
b9d18bc3 | 1790 | |
cc07239d | 1791 | if (col == COL_CPU_CACHE) { |
8005924a | 1792 | if (mod->compat && !desc->ncaches) |
477251f8 | 1793 | continue; |
8005924a | 1794 | if (mod->compat && i != 0) |
477251f8 | 1795 | putchar(','); |
477251f8 | 1796 | } |
e3b3a2f3 KZ |
1797 | if (i > 0) |
1798 | putchar(','); | |
1799 | ||
3d27b76a | 1800 | data = get_cell_header(desc, col, mod, buf, sizeof(buf)); |
b9d18bc3 | 1801 | |
cc07239d KZ |
1802 | if (data && * data && col != COL_CPU_CACHE && |
1803 | !coldescs_cpu[col].is_abbr) { | |
3d27b76a KZ |
1804 | /* |
1805 | * For normal column names use mixed case (e.g. "Socket") | |
1806 | */ | |
1807 | char *p = data + 1; | |
1808 | ||
14e8be8a PU |
1809 | while (p && *p != '\0') { |
1810 | *p = tolower((unsigned int) *p); | |
1811 | p++; | |
1812 | } | |
3d27b76a | 1813 | } |
e3b3a2f3 | 1814 | fputs(data && *data ? data : "", stdout); |
477251f8 KZ |
1815 | } |
1816 | putchar('\n'); | |
1817 | ||
e3b3a2f3 KZ |
1818 | /* |
1819 | * Data | |
1820 | */ | |
a5cfffff | 1821 | for (i = 0; i < desc->ncpuspos; i++) { |
e3b3a2f3 | 1822 | int c; |
4f642863 | 1823 | int cpu = real_cpu_num(desc, i); |
e3b3a2f3 | 1824 | |
6dd7b74b SK |
1825 | if (desc->online) { |
1826 | if (!mod->offline && !is_cpu_online(desc, cpu)) | |
1827 | continue; | |
1828 | if (!mod->online && is_cpu_online(desc, cpu)) | |
1829 | continue; | |
1830 | } | |
4f642863 | 1831 | if (desc->present && !is_cpu_present(desc, cpu)) |
a5cfffff | 1832 | continue; |
477251f8 | 1833 | for (c = 0; c < ncols; c++) { |
cc07239d | 1834 | if (mod->compat && cols[c] == COL_CPU_CACHE) { |
477251f8 KZ |
1835 | if (!desc->ncaches) |
1836 | continue; | |
1837 | if (c > 0) | |
1838 | putchar(','); | |
1839 | } | |
1840 | if (c > 0) | |
1841 | putchar(','); | |
e3b3a2f3 KZ |
1842 | |
1843 | data = get_cell_data(desc, i, cols[c], mod, | |
1844 | buf, sizeof(buf)); | |
1845 | fputs(data && *data ? data : "", stdout); | |
477251f8 | 1846 | } |
5dd7507c CQ |
1847 | putchar('\n'); |
1848 | } | |
1849 | } | |
1850 | ||
ba45d8c1 KZ |
1851 | /* |
1852 | * [-e] backend | |
1853 | */ | |
1854 | static void | |
cc07239d | 1855 | print_cpus_readable(struct lscpu_desc *desc, int cols[], int ncols, |
ba45d8c1 KZ |
1856 | struct lscpu_modifier *mod) |
1857 | { | |
1858 | int i; | |
e7213e34 KZ |
1859 | char buf[BUFSIZ]; |
1860 | const char *data; | |
710ed55d | 1861 | struct libscols_table *table; |
ba45d8c1 | 1862 | |
710ed55d KZ |
1863 | scols_init_debug(0); |
1864 | ||
1865 | table = scols_new_table(); | |
83db4eb2 | 1866 | if (!table) |
780ce22c | 1867 | err(EXIT_FAILURE, _("failed to allocate output table")); |
19a5510b KZ |
1868 | if (mod->json) { |
1869 | scols_table_enable_json(table, 1); | |
1870 | scols_table_set_name(table, "cpus"); | |
1871 | } | |
ba45d8c1 KZ |
1872 | |
1873 | for (i = 0; i < ncols; i++) { | |
b9d18bc3 | 1874 | data = get_cell_header(desc, cols[i], mod, buf, sizeof(buf)); |
2ec00a10 | 1875 | if (!scols_table_new_column(table, data, 0, coldescs_cpu[cols[i]].flags)) |
780ce22c | 1876 | err(EXIT_FAILURE, _("failed to allocate output column")); |
ba45d8c1 KZ |
1877 | } |
1878 | ||
a5cfffff | 1879 | for (i = 0; i < desc->ncpuspos; i++) { |
ba45d8c1 | 1880 | int c; |
83db4eb2 | 1881 | struct libscols_line *line; |
4f642863 | 1882 | int cpu = real_cpu_num(desc, i); |
ba45d8c1 | 1883 | |
6dd7b74b SK |
1884 | if (desc->online) { |
1885 | if (!mod->offline && !is_cpu_online(desc, cpu)) | |
1886 | continue; | |
1887 | if (!mod->online && is_cpu_online(desc, cpu)) | |
1888 | continue; | |
1889 | } | |
4f642863 | 1890 | if (desc->present && !is_cpu_present(desc, cpu)) |
a5cfffff | 1891 | continue; |
ba45d8c1 | 1892 | |
83db4eb2 | 1893 | line = scols_table_new_line(table, NULL); |
e7213e34 | 1894 | if (!line) |
780ce22c | 1895 | err(EXIT_FAILURE, _("failed to allocate output line")); |
ba45d8c1 KZ |
1896 | |
1897 | for (c = 0; c < ncols; c++) { | |
1898 | data = get_cell_data(desc, i, cols[c], mod, | |
1899 | buf, sizeof(buf)); | |
e7213e34 KZ |
1900 | if (!data || !*data) |
1901 | data = "-"; | |
bf1eab07 | 1902 | if (scols_line_set_data(line, c, data)) |
780ce22c | 1903 | err(EXIT_FAILURE, _("failed to add output data")); |
ba45d8c1 KZ |
1904 | } |
1905 | } | |
1906 | ||
83db4eb2 OO |
1907 | scols_print_table(table); |
1908 | scols_unref_table(table); | |
ba45d8c1 | 1909 | } |
5dd7507c | 1910 | |
c82b12a0 KZ |
1911 | |
1912 | static void __attribute__ ((__format__(printf, 3, 4))) | |
1913 | add_summary_sprint(struct libscols_table *tb, | |
1914 | const char *txt, | |
1915 | const char *fmt, | |
1916 | ...) | |
1917 | { | |
1918 | struct libscols_line *ln = scols_table_new_line(tb, NULL); | |
1919 | char *data; | |
1920 | va_list args; | |
1921 | ||
1922 | if (!ln) | |
780ce22c | 1923 | err(EXIT_FAILURE, _("failed to allocate output line")); |
c82b12a0 KZ |
1924 | |
1925 | /* description column */ | |
a0661407 KZ |
1926 | if (txt && scols_line_set_data(ln, 0, txt)) |
1927 | err(EXIT_FAILURE, _("failed to add output data")); | |
c82b12a0 KZ |
1928 | |
1929 | /* data column */ | |
1930 | va_start(args, fmt); | |
1931 | xvasprintf(&data, fmt, args); | |
1932 | va_end(args); | |
1933 | ||
780ce22c KZ |
1934 | if (data && scols_line_refer_data(ln, 1, data)) |
1935 | err(EXIT_FAILURE, _("failed to add output data")); | |
c82b12a0 KZ |
1936 | } |
1937 | ||
1938 | #define add_summary_n(tb, txt, num) add_summary_sprint(tb, txt, "%d", num) | |
1939 | #define add_summary_s(tb, txt, str) add_summary_sprint(tb, txt, "%s", str) | |
5dd7507c CQ |
1940 | |
1941 | static void | |
c82b12a0 KZ |
1942 | print_cpuset(struct libscols_table *tb, |
1943 | const char *key, cpu_set_t *set, int hex) | |
4f912c6a KZ |
1944 | { |
1945 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1946 | size_t setbuflen = 7 * maxcpus; | |
1947 | char setbuf[setbuflen], *p; | |
1948 | ||
1949 | if (hex) { | |
1950 | p = cpumask_create(setbuf, setbuflen, set, setsize); | |
c82b12a0 | 1951 | add_summary_s(tb, key, p); |
4f912c6a KZ |
1952 | } else { |
1953 | p = cpulist_create(setbuf, setbuflen, set, setsize); | |
c82b12a0 | 1954 | add_summary_s(tb, key, p); |
4f912c6a | 1955 | } |
4f912c6a KZ |
1956 | } |
1957 | ||
0e86bc84 KZ |
1958 | static int get_cache_full_size(struct lscpu_desc *desc, |
1959 | struct cpu_cache *ca, uint64_t *res) | |
e5f72113 | 1960 | { |
e5f72113 | 1961 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
0e86bc84 | 1962 | int i, nshares = 0; |
e5f72113 KZ |
1963 | |
1964 | /* Count number of CPUs which shares the cache */ | |
1965 | for (i = 0; i < desc->ncpuspos; i++) { | |
1966 | int cpu = real_cpu_num(desc, i); | |
1967 | ||
1968 | if (desc->present && !is_cpu_present(desc, cpu)) | |
1969 | continue; | |
1970 | if (CPU_ISSET_S(cpu, setsize, ca->sharedmaps[0])) | |
1971 | nshares++; | |
1972 | } | |
1973 | ||
1974 | /* Correction for CPU threads */ | |
1975 | if (desc->nthreads > desc->ncores) | |
1976 | nshares /= (desc->nthreads / desc->ncores); | |
23df0510 SK |
1977 | if (nshares < 1) |
1978 | nshares = 1; | |
e5f72113 | 1979 | |
0e86bc84 | 1980 | *res = (desc->ncores / nshares) * ca->size; |
e5f72113 KZ |
1981 | return 0; |
1982 | } | |
1983 | ||
ba45d8c1 KZ |
1984 | /* |
1985 | * default output | |
1986 | */ | |
4f912c6a | 1987 | static void |
8005924a | 1988 | print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
5dd7507c | 1989 | { |
577d1a66 | 1990 | char buf[BUFSIZ]; |
fc07d9f5 | 1991 | int i = 0; |
aac1e59e | 1992 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
c82b12a0 KZ |
1993 | struct libscols_table *tb; |
1994 | ||
1995 | scols_init_debug(0); | |
1996 | ||
1997 | tb = scols_new_table(); | |
1998 | if (!tb) | |
780ce22c | 1999 | err(EXIT_FAILURE, _("failed to allocate output table")); |
7e03f383 | 2000 | |
c82b12a0 | 2001 | scols_table_enable_noheadings(tb, 1); |
19a5510b KZ |
2002 | if (mod->json) { |
2003 | scols_table_enable_json(tb, 1); | |
2004 | scols_table_set_name(tb, "lscpu"); | |
2005 | } | |
79e8b41a | 2006 | |
19a5510b | 2007 | if (scols_table_new_column(tb, "field", 0, 0) == NULL || |
b8910af5 | 2008 | scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES | SCOLS_FL_WRAP) == NULL) |
c82b12a0 KZ |
2009 | err(EXIT_FAILURE, _("failed to initialize output column")); |
2010 | ||
2011 | add_summary_s(tb, _("Architecture:"), desc->arch); | |
f633ad4e | 2012 | if (desc->mode) { |
577d1a66 | 2013 | char *p = buf; |
79e8b41a | 2014 | |
f633ad4e | 2015 | if (desc->mode & MODE_32BIT) { |
79e8b41a KZ |
2016 | strcpy(p, "32-bit, "); |
2017 | p += 8; | |
2018 | } | |
f633ad4e | 2019 | if (desc->mode & MODE_64BIT) { |
79e8b41a KZ |
2020 | strcpy(p, "64-bit, "); |
2021 | p += 8; | |
2022 | } | |
2023 | *(p - 2) = '\0'; | |
577d1a66 | 2024 | add_summary_s(tb, _("CPU op-mode(s):"), buf); |
79e8b41a | 2025 | } |
aabe2441 | 2026 | #if !defined(WORDS_BIGENDIAN) |
c82b12a0 | 2027 | add_summary_s(tb, _("Byte Order:"), "Little Endian"); |
9b8d4d5f | 2028 | #else |
c82b12a0 | 2029 | add_summary_s(tb, _("Byte Order:"), "Big Endian"); |
9b8d4d5f | 2030 | #endif |
3e48ef72 KZ |
2031 | |
2032 | if (desc->addrsz) | |
2033 | add_summary_s(tb, _("Address sizes:"), desc->addrsz); | |
2034 | ||
c82b12a0 | 2035 | add_summary_n(tb, _("CPU(s):"), desc->ncpus); |
4f912c6a | 2036 | |
5d4ba40d | 2037 | if (desc->online) |
c82b12a0 KZ |
2038 | print_cpuset(tb, mod->hex ? _("On-line CPU(s) mask:") : |
2039 | _("On-line CPU(s) list:"), | |
8005924a | 2040 | desc->online, mod->hex); |
4f912c6a | 2041 | |
5d4ba40d | 2042 | if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) { |
4f912c6a KZ |
2043 | cpu_set_t *set; |
2044 | ||
2045 | /* Linux kernel provides cpuset of off-line CPUs that contains | |
2046 | * all configured CPUs (see /sys/devices/system/cpu/offline), | |
2047 | * but want to print real (present in system) off-line CPUs only. | |
2048 | */ | |
2049 | set = cpuset_alloc(maxcpus, NULL, NULL); | |
2050 | if (!set) | |
2051 | err(EXIT_FAILURE, _("failed to callocate cpu set")); | |
2052 | CPU_ZERO_S(setsize, set); | |
a5cfffff | 2053 | for (i = 0; i < desc->ncpuspos; i++) { |
4f642863 AT |
2054 | int cpu = real_cpu_num(desc, i); |
2055 | if (!is_cpu_online(desc, cpu) && is_cpu_present(desc, cpu)) | |
2056 | CPU_SET_S(cpu, setsize, set); | |
4f912c6a | 2057 | } |
c82b12a0 KZ |
2058 | print_cpuset(tb, mod->hex ? _("Off-line CPU(s) mask:") : |
2059 | _("Off-line CPU(s) list:"), | |
8005924a | 2060 | set, mod->hex); |
4f912c6a KZ |
2061 | cpuset_free(set); |
2062 | } | |
5dd7507c | 2063 | |
7e03f383 | 2064 | if (desc->nsockets) { |
2c497d32 HC |
2065 | int threads_per_core, cores_per_socket, sockets_per_book; |
2066 | int books_per_drawer, drawers; | |
904ffe1f | 2067 | FILE *fd; |
8648ca96 | 2068 | |
2c497d32 HC |
2069 | threads_per_core = cores_per_socket = sockets_per_book = 0; |
2070 | books_per_drawer = drawers = 0; | |
8648ca96 HC |
2071 | /* s390 detects its cpu topology via /proc/sysinfo, if present. |
2072 | * Using simply the cpu topology masks in sysfs will not give | |
2073 | * usable results since everything is virtualized. E.g. | |
2074 | * virtual core 0 may have only 1 cpu, but virtual core 2 may | |
2075 | * five cpus. | |
2076 | * If the cpu topology is not exported (e.g. 2nd level guest) | |
2077 | * fall back to old calculation scheme. | |
2078 | */ | |
6e509042 | 2079 | if ((fd = ul_path_fopen(desc->procfs, "r", "sysinfo"))) { |
b3adf6ef | 2080 | int t0, t1; |
8648ca96 | 2081 | |
577d1a66 KZ |
2082 | while (fd && fgets(buf, sizeof(buf), fd) != NULL) { |
2083 | if (sscanf(buf, "CPU Topology SW:%d%d%d%d%d%d", | |
b3adf6ef HC |
2084 | &t0, &t1, &drawers, &books_per_drawer, |
2085 | &sockets_per_book, | |
8648ca96 HC |
2086 | &cores_per_socket) == 6) |
2087 | break; | |
2088 | } | |
ad655c88 KZ |
2089 | if (fd) |
2090 | fclose(fd); | |
8648ca96 | 2091 | } |
2c497d32 HC |
2092 | if (desc->mtid) |
2093 | threads_per_core = atoi(desc->mtid) + 1; | |
c82b12a0 | 2094 | add_summary_n(tb, _("Thread(s) per core:"), |
2c497d32 | 2095 | threads_per_core ?: desc->nthreads / desc->ncores); |
c82b12a0 | 2096 | add_summary_n(tb, _("Core(s) per socket:"), |
8648ca96 | 2097 | cores_per_socket ?: desc->ncores / desc->nsockets); |
56baaa4e | 2098 | if (desc->nbooks) { |
c82b12a0 | 2099 | add_summary_n(tb, _("Socket(s) per book:"), |
8648ca96 | 2100 | sockets_per_book ?: desc->nsockets / desc->nbooks); |
b3adf6ef | 2101 | if (desc->ndrawers) { |
c82b12a0 | 2102 | add_summary_n(tb, _("Book(s) per drawer:"), |
b3adf6ef | 2103 | books_per_drawer ?: desc->nbooks / desc->ndrawers); |
c82b12a0 | 2104 | add_summary_n(tb, _("Drawer(s):"), drawers ?: desc->ndrawers); |
b3adf6ef | 2105 | } else { |
c82b12a0 | 2106 | add_summary_n(tb, _("Book(s):"), books_per_drawer ?: desc->nbooks); |
b3adf6ef | 2107 | } |
56baaa4e | 2108 | } else { |
c82b12a0 | 2109 | add_summary_n(tb, _("Socket(s):"), sockets_per_book ?: desc->nsockets); |
56baaa4e | 2110 | } |
5dd7507c | 2111 | } |
7e03f383 | 2112 | if (desc->nnodes) |
c82b12a0 | 2113 | add_summary_n(tb, _("NUMA node(s):"), desc->nnodes); |
e8aa16ee | 2114 | if (desc->vendor) |
c82b12a0 | 2115 | add_summary_s(tb, _("Vendor ID:"), desc->vendor); |
0c28f0c8 | 2116 | if (desc->machinetype) |
c82b12a0 | 2117 | add_summary_s(tb, _("Machine type:"), desc->machinetype); |
e8aa16ee | 2118 | if (desc->family) |
c82b12a0 | 2119 | add_summary_s(tb, _("CPU family:"), desc->family); |
c95e3889 | 2120 | if (desc->model || desc->revision) |
c82b12a0 | 2121 | add_summary_s(tb, _("Model:"), desc->revision ? desc->revision : desc->model); |
c95e3889 | 2122 | if (desc->modelname || desc->cpu) |
c82b12a0 | 2123 | add_summary_s(tb, _("Model name:"), desc->cpu ? desc->cpu : desc->modelname); |
e8aa16ee | 2124 | if (desc->stepping) |
c82b12a0 | 2125 | add_summary_s(tb, _("Stepping:"), desc->stepping); |
16ca0551 KZ |
2126 | if (desc->freqboost >= 0) |
2127 | add_summary_s(tb, _("Frequency boost:"), desc->freqboost ? | |
2128 | _("enabled") : _("disabled")); | |
e8aa16ee | 2129 | if (desc->mhz) |
c82b12a0 | 2130 | add_summary_s(tb, _("CPU MHz:"), desc->mhz); |
4632b288 | 2131 | if (desc->dynamic_mhz) |
c82b12a0 | 2132 | add_summary_s(tb, _("CPU dynamic MHz:"), desc->dynamic_mhz); |
4632b288 | 2133 | if (desc->static_mhz) |
c82b12a0 | 2134 | add_summary_s(tb, _("CPU static MHz:"), desc->static_mhz); |
89222b17 KZ |
2135 | if (desc->maxmhz) |
2136 | add_summary_s(tb, _("CPU max MHz:"), cpu_max_mhz(desc, buf, sizeof(buf))); | |
2137 | if (desc->minmhz) | |
2138 | add_summary_s(tb, _("CPU min MHz:"), cpu_min_mhz(desc, buf, sizeof(buf))); | |
9b8d4d5f | 2139 | if (desc->bogomips) |
c82b12a0 | 2140 | add_summary_s(tb, _("BogoMIPS:"), desc->bogomips); |
e8aa16ee KZ |
2141 | if (desc->virtflag) { |
2142 | if (!strcmp(desc->virtflag, "svm")) | |
c82b12a0 | 2143 | add_summary_s(tb, _("Virtualization:"), "AMD-V"); |
e8aa16ee | 2144 | else if (!strcmp(desc->virtflag, "vmx")) |
c82b12a0 | 2145 | add_summary_s(tb, _("Virtualization:"), "VT-x"); |
5dd7507c | 2146 | } |
10829cd7 | 2147 | if (desc->hypervisor) |
c82b12a0 | 2148 | add_summary_s(tb, _("Hypervisor:"), desc->hypervisor); |
e8aa16ee | 2149 | if (desc->hyper) { |
c82b12a0 KZ |
2150 | add_summary_s(tb, _("Hypervisor vendor:"), hv_vendors[desc->hyper]); |
2151 | add_summary_s(tb, _("Virtualization type:"), _(virt_types[desc->virtype])); | |
c8b64f6d | 2152 | } |
a0fff77e | 2153 | if (desc->dispatching >= 0) |
c82b12a0 | 2154 | add_summary_s(tb, _("Dispatching mode:"), _(disp_modes[desc->dispatching])); |
7e03f383 | 2155 | if (desc->ncaches) { |
7e03f383 | 2156 | for (i = desc->ncaches - 1; i >= 0; i--) { |
e5f72113 KZ |
2157 | uint64_t sz = 0; |
2158 | char *tmp; | |
2011528b | 2159 | struct cpu_cache *ca = &desc->caches[i]; |
e5f72113 | 2160 | |
2011528b | 2161 | if (ca->size == 0) |
e5f72113 | 2162 | continue; |
2011528b KZ |
2163 | if (get_cache_full_size(desc, ca, &sz) != 0 || sz == 0) |
2164 | continue; | |
2165 | if (mod->bytes) | |
2166 | xasprintf(&tmp, "%" PRIu64, sz); | |
2167 | else | |
2168 | tmp = size_to_human_string( | |
e5f72113 KZ |
2169 | SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, |
2170 | sz); | |
30d5ceed | 2171 | snprintf(buf, sizeof(buf), _("%s cache:"), ca->name); |
e5f72113 KZ |
2172 | add_summary_s(tb, buf, tmp); |
2173 | free(tmp); | |
7e03f383 KZ |
2174 | } |
2175 | } | |
28b1658f | 2176 | if (desc->necaches) { |
28b1658f | 2177 | for (i = desc->necaches - 1; i >= 0; i--) { |
0e86bc84 | 2178 | char *tmp; |
2011528b | 2179 | struct cpu_cache *ca = &desc->ecaches[i]; |
0e86bc84 | 2180 | |
2011528b | 2181 | if (ca->size == 0) |
0e86bc84 | 2182 | continue; |
2011528b KZ |
2183 | if (mod->bytes) |
2184 | xasprintf(&tmp, "%" PRIu64, ca->size); | |
2185 | else | |
2186 | tmp = size_to_human_string( | |
0e86bc84 | 2187 | SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, |
2011528b | 2188 | ca->size); |
30d5ceed | 2189 | snprintf(buf, sizeof(buf), _("%s cache:"), ca->name); |
0e86bc84 KZ |
2190 | add_summary_s(tb, buf, tmp); |
2191 | free(tmp); | |
28b1658f HC |
2192 | } |
2193 | } | |
2194 | ||
4f912c6a | 2195 | for (i = 0; i < desc->nnodes; i++) { |
e9074a16 | 2196 | snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), desc->idx2nodenum[i]); |
c82b12a0 | 2197 | print_cpuset(tb, buf, desc->nodemaps[i], mod->hex); |
5dd7507c | 2198 | } |
ee1f1057 | 2199 | |
bd9b94d1 | 2200 | if (desc->physsockets) { |
c82b12a0 KZ |
2201 | add_summary_n(tb, _("Physical sockets:"), desc->physsockets); |
2202 | add_summary_n(tb, _("Physical chips:"), desc->physchips); | |
2203 | add_summary_n(tb, _("Physical cores/chip:"), desc->physcoresperchip); | |
bd9b94d1 | 2204 | } |
c82b12a0 | 2205 | |
639eeb28 KZ |
2206 | if (desc->vuls) { |
2207 | for (i = 0; i < desc->nvuls; i++) { | |
30d5ceed | 2208 | snprintf(buf, sizeof(buf), ("Vulnerability %s:"), desc->vuls[i].name); |
639eeb28 KZ |
2209 | add_summary_s(tb, buf, desc->vuls[i].text); |
2210 | } | |
2211 | } | |
2212 | ||
c82b12a0 KZ |
2213 | if (desc->flags) |
2214 | add_summary_s(tb, _("Flags:"), desc->flags); | |
2215 | ||
2216 | scols_print_table(tb); | |
2217 | scols_unref_table(tb); | |
5dd7507c CQ |
2218 | } |
2219 | ||
6e1eda6f | 2220 | static void __attribute__((__noreturn__)) usage(void) |
5dd7507c | 2221 | { |
6e1eda6f | 2222 | FILE *out = stdout; |
b9d18bc3 KZ |
2223 | size_t i; |
2224 | ||
2225 | fputs(USAGE_HEADER, out); | |
c6f095cf | 2226 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); |
7f1ec5e8 | 2227 | |
451dbcfa BS |
2228 | fputs(USAGE_SEPARATOR, out); |
2229 | fputs(_("Display information about the CPU architecture.\n"), out); | |
2230 | ||
b9d18bc3 | 2231 | fputs(USAGE_OPTIONS, out); |
c6f095cf BS |
2232 | fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out); |
2233 | fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out); | |
2011528b | 2234 | fputs(_(" -B, --bytes print sizes in bytes rather than in human readable format\n"), out); |
0e86bc84 | 2235 | fputs(_(" -C, --caches[=<list>] info about caches in extended readable format\n"), out); |
c6f095cf | 2236 | fputs(_(" -c, --offline print offline CPUs only\n"), out); |
19a5510b | 2237 | fputs(_(" -J, --json use JSON for default or extended format\n"), out); |
c6f095cf BS |
2238 | fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out); |
2239 | fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out); | |
2240 | fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out); | |
2241 | fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out); | |
0d2b5d2a | 2242 | fputs(_(" -y, --physical print physical instead of logical IDs\n"), out); |
0a31a242 | 2243 | fputs(_(" --output-all print all available columns for -e, -p or -C\n"), out); |
c6f095cf | 2244 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 | 2245 | printf(USAGE_HELP_OPTIONS(25)); |
b9d18bc3 | 2246 | |
cc07239d KZ |
2247 | fputs(_("\nAvailable output columns for -e or -p:\n"), out); |
2248 | for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) | |
2249 | fprintf(out, " %13s %s\n", coldescs_cpu[i].name, _(coldescs_cpu[i].help)); | |
3d27b76a | 2250 | |
0e86bc84 KZ |
2251 | fputs(_("\nAvailable output columns for -C:\n"), out); |
2252 | for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++) | |
2253 | fprintf(out, " %13s %s\n", coldescs_cache[i].name, _(coldescs_cache[i].help)); | |
2254 | ||
f45f3ec3 | 2255 | printf(USAGE_MAN_TAIL("lscpu(1)")); |
4f912c6a | 2256 | |
6e1eda6f | 2257 | exit(EXIT_SUCCESS); |
5dd7507c CQ |
2258 | } |
2259 | ||
2260 | int main(int argc, char *argv[]) | |
2261 | { | |
8005924a | 2262 | struct lscpu_modifier _mod = { .mode = OUTPUT_SUMMARY }, *mod = &_mod; |
87918040 | 2263 | struct lscpu_desc _desc = { .flags = NULL }, *desc = &_desc; |
0e86bc84 | 2264 | int c, i, all = 0; |
cc07239d | 2265 | int columns[ARRAY_SIZE(coldescs_cpu)], ncolumns = 0; |
7fc12cd2 | 2266 | int cpu_modifier_specified = 0; |
538b50cb | 2267 | size_t setsize; |
5dd7507c | 2268 | |
fbf0619b SK |
2269 | enum { |
2270 | OPT_OUTPUT_ALL = CHAR_MAX + 1, | |
2271 | }; | |
6c7d5ae9 | 2272 | static const struct option longopts[] = { |
87918040 SK |
2273 | { "all", no_argument, NULL, 'a' }, |
2274 | { "online", no_argument, NULL, 'b' }, | |
2011528b | 2275 | { "bytes", no_argument, NULL, 'B' }, |
0e86bc84 | 2276 | { "caches", optional_argument, NULL, 'C' }, |
87918040 SK |
2277 | { "offline", no_argument, NULL, 'c' }, |
2278 | { "help", no_argument, NULL, 'h' }, | |
2279 | { "extended", optional_argument, NULL, 'e' }, | |
19a5510b | 2280 | { "json", no_argument, NULL, 'J' }, |
87918040 SK |
2281 | { "parse", optional_argument, NULL, 'p' }, |
2282 | { "sysroot", required_argument, NULL, 's' }, | |
2283 | { "physical", no_argument, NULL, 'y' }, | |
2284 | { "hex", no_argument, NULL, 'x' }, | |
2285 | { "version", no_argument, NULL, 'V' }, | |
fbf0619b | 2286 | { "output-all", no_argument, NULL, OPT_OUTPUT_ALL }, |
87918040 | 2287 | { NULL, 0, NULL, 0 } |
5dd7507c CQ |
2288 | }; |
2289 | ||
8e97eb4b | 2290 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
0e86bc84 | 2291 | { 'C','e','p' }, |
a44cd891 | 2292 | { 'a','b','c' }, |
8e97eb4b KZ |
2293 | { 0 } |
2294 | }; | |
2295 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
2296 | ||
2f8f1388 | 2297 | setlocale(LC_ALL, ""); |
5dd7507c CQ |
2298 | bindtextdomain(PACKAGE, LOCALEDIR); |
2299 | textdomain(PACKAGE); | |
25b7045e | 2300 | close_stdout_atexit(); |
5dd7507c | 2301 | |
2011528b | 2302 | while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::s:xyV", longopts, NULL)) != -1) { |
8e97eb4b KZ |
2303 | |
2304 | err_exclusive_options(c, longopts, excl, excl_st); | |
2305 | ||
5dd7507c | 2306 | switch (c) { |
0ad29ff6 | 2307 | case 'a': |
7afc2387 | 2308 | mod->online = mod->offline = 1; |
7fc12cd2 | 2309 | cpu_modifier_specified = 1; |
0ad29ff6 | 2310 | break; |
2011528b KZ |
2311 | case 'B': |
2312 | mod->bytes = 1; | |
2313 | break; | |
23e9e95a KZ |
2314 | case 'b': |
2315 | mod->online = 1; | |
7fc12cd2 | 2316 | cpu_modifier_specified = 1; |
23e9e95a | 2317 | break; |
7afc2387 HC |
2318 | case 'c': |
2319 | mod->offline = 1; | |
7fc12cd2 | 2320 | cpu_modifier_specified = 1; |
7afc2387 | 2321 | break; |
0e86bc84 KZ |
2322 | case 'C': |
2323 | if (optarg) { | |
2324 | if (*optarg == '=') | |
2325 | optarg++; | |
2326 | ncolumns = string_to_idarray(optarg, | |
2327 | columns, ARRAY_SIZE(columns), | |
2328 | cache_column_name_to_id); | |
2329 | if (ncolumns < 0) | |
2330 | return EXIT_FAILURE; | |
2331 | } | |
2332 | mod->mode = OUTPUT_CACHES; | |
2333 | break; | |
19a5510b KZ |
2334 | case 'J': |
2335 | mod->json = 1; | |
2336 | break; | |
5dd7507c | 2337 | case 'p': |
ba45d8c1 | 2338 | case 'e': |
477251f8 KZ |
2339 | if (optarg) { |
2340 | if (*optarg == '=') | |
2341 | optarg++; | |
2342 | ncolumns = string_to_idarray(optarg, | |
2343 | columns, ARRAY_SIZE(columns), | |
cc07239d | 2344 | cpu_column_name_to_id); |
477251f8 KZ |
2345 | if (ncolumns < 0) |
2346 | return EXIT_FAILURE; | |
477251f8 | 2347 | } |
ba45d8c1 | 2348 | mod->mode = c == 'p' ? OUTPUT_PARSABLE : OUTPUT_READABLE; |
5dd7507c | 2349 | break; |
47b6e8b6 | 2350 | case 's': |
6e509042 | 2351 | desc->prefix = optarg; |
8148217b | 2352 | mod->system = SYSTEM_SNAPSHOT; |
47b6e8b6 | 2353 | break; |
4f912c6a | 2354 | case 'x': |
8005924a | 2355 | mod->hex = 1; |
4f912c6a | 2356 | break; |
0d2b5d2a HC |
2357 | case 'y': |
2358 | mod->physical = 1; | |
2359 | break; | |
fbf0619b | 2360 | case OPT_OUTPUT_ALL: |
0e86bc84 | 2361 | all = 1; |
fbf0619b | 2362 | break; |
2c308875 KZ |
2363 | |
2364 | case 'h': | |
2365 | usage(); | |
2366 | case 'V': | |
2367 | print_version(EXIT_SUCCESS); | |
5dd7507c | 2368 | default: |
677ec86c | 2369 | errtryhelp(EXIT_FAILURE); |
5dd7507c CQ |
2370 | } |
2371 | } | |
7bbb7829 | 2372 | |
0a31a242 | 2373 | if (all && ncolumns == 0) { |
0e86bc84 KZ |
2374 | size_t sz, maxsz = mod->mode == OUTPUT_CACHES ? |
2375 | ARRAY_SIZE(coldescs_cache) : | |
2376 | ARRAY_SIZE(coldescs_cpu); | |
2377 | ||
2378 | for (sz = 0; sz < maxsz; sz++) | |
0a31a242 | 2379 | columns[ncolumns++] = sz; |
0e86bc84 KZ |
2380 | } |
2381 | ||
7fc12cd2 HC |
2382 | if (cpu_modifier_specified && mod->mode == OUTPUT_SUMMARY) { |
2383 | fprintf(stderr, | |
2384 | _("%s: options --all, --online and --offline may only " | |
ac56e555 | 2385 | "be used with options --extended or --parse.\n"), |
7fc12cd2 HC |
2386 | program_invocation_short_name); |
2387 | return EXIT_FAILURE; | |
2388 | } | |
2389 | ||
6e1eda6f RM |
2390 | if (argc != optind) { |
2391 | warnx(_("bad usage")); | |
2392 | errtryhelp(EXIT_FAILURE); | |
2393 | } | |
7bbb7829 | 2394 | |
7afc2387 HC |
2395 | /* set default cpu display mode if none was specified */ |
2396 | if (!mod->online && !mod->offline) { | |
2397 | mod->online = 1; | |
2398 | mod->offline = mod->mode == OUTPUT_READABLE ? 1 : 0; | |
2399 | } | |
5dd7507c | 2400 | |
6e509042 KZ |
2401 | ul_path_init_debug(); |
2402 | ||
2403 | /* /sys/devices/system/cpu */ | |
2404 | desc->syscpu = ul_new_path(_PATH_SYS_CPU); | |
2405 | if (!desc->syscpu) | |
2406 | err(EXIT_FAILURE, _("failed to initialize CPUs sysfs handler")); | |
2407 | if (desc->prefix) | |
2408 | ul_path_set_prefix(desc->syscpu, desc->prefix); | |
2409 | ||
2410 | /* /proc */ | |
2411 | desc->procfs = ul_new_path("/proc"); | |
2412 | if (!desc->procfs) | |
2413 | err(EXIT_FAILURE, _("failed to initialize procfs handler")); | |
2414 | if (desc->prefix) | |
2415 | ul_path_set_prefix(desc->procfs, desc->prefix); | |
2416 | ||
8148217b | 2417 | read_basicinfo(desc, mod); |
5dd7507c | 2418 | |
538b50cb SB |
2419 | setsize = CPU_ALLOC_SIZE(maxcpus); |
2420 | ||
a5cfffff | 2421 | for (i = 0; i < desc->ncpuspos; i++) { |
0002704e HC |
2422 | /* only consider present CPUs */ |
2423 | if (desc->present && | |
538b50cb | 2424 | !CPU_ISSET_S(real_cpu_num(desc, i), setsize, desc->present)) |
0002704e | 2425 | continue; |
7e03f383 KZ |
2426 | read_topology(desc, i); |
2427 | read_cache(desc, i); | |
2b8fcb81 | 2428 | read_polarization(desc, i); |
596b8845 | 2429 | read_address(desc, i); |
d231eea1 | 2430 | read_configured(desc, i); |
44320710 | 2431 | read_max_mhz(desc, i); |
e065a597 | 2432 | read_min_mhz(desc, i); |
47b6e8b6 | 2433 | } |
7e03f383 | 2434 | |
960bf130 KZ |
2435 | if (desc->caches) |
2436 | qsort(desc->caches, desc->ncaches, | |
2437 | sizeof(struct cpu_cache), cachecmp); | |
7e03f383 | 2438 | |
28b1658f HC |
2439 | if (desc->ecaches) |
2440 | qsort(desc->ecaches, desc->necaches, | |
2441 | sizeof(struct cpu_cache), cachecmp); | |
2442 | ||
7e03f383 | 2443 | read_nodes(desc); |
eff79ceb | 2444 | read_hypervisor(desc, mod); |
744d62ee | 2445 | arm_cpu_decode(desc); |
c8b64f6d | 2446 | |
8005924a | 2447 | switch(mod->mode) { |
ba45d8c1 KZ |
2448 | case OUTPUT_SUMMARY: |
2449 | print_summary(desc, mod); | |
2450 | break; | |
0e86bc84 KZ |
2451 | case OUTPUT_CACHES: |
2452 | if (!ncolumns) { | |
2453 | columns[ncolumns++] = COL_CACHE_NAME; | |
2454 | columns[ncolumns++] = COL_CACHE_ONESIZE; | |
2455 | columns[ncolumns++] = COL_CACHE_ALLSIZE; | |
2456 | columns[ncolumns++] = COL_CACHE_WAYS; | |
2457 | columns[ncolumns++] = COL_CACHE_TYPE; | |
2458 | columns[ncolumns++] = COL_CACHE_LEVEL; | |
cf3b6b71 KZ |
2459 | columns[ncolumns++] = COL_CACHE_SETS; |
2460 | columns[ncolumns++] = COL_CACHE_PHYLINE; | |
2461 | columns[ncolumns++] = COL_CACHE_COHERENCYSIZE; | |
0e86bc84 KZ |
2462 | } |
2463 | print_caches_readable(desc, columns, ncolumns, mod); | |
2464 | break; | |
ba45d8c1 KZ |
2465 | case OUTPUT_PARSABLE: |
2466 | if (!ncolumns) { | |
cc07239d KZ |
2467 | columns[ncolumns++] = COL_CPU_CPU; |
2468 | columns[ncolumns++] = COL_CPU_CORE; | |
2469 | columns[ncolumns++] = COL_CPU_SOCKET; | |
2470 | columns[ncolumns++] = COL_CPU_NODE; | |
2471 | columns[ncolumns++] = COL_CPU_CACHE; | |
ba45d8c1 KZ |
2472 | mod->compat = 1; |
2473 | } | |
cc07239d | 2474 | print_cpus_parsable(desc, columns, ncolumns, mod); |
ba45d8c1 KZ |
2475 | break; |
2476 | case OUTPUT_READABLE: | |
2477 | if (!ncolumns) { | |
2478 | /* No list was given. Just print whatever is there. */ | |
cc07239d | 2479 | columns[ncolumns++] = COL_CPU_CPU; |
ba45d8c1 | 2480 | if (desc->nodemaps) |
cc07239d | 2481 | columns[ncolumns++] = COL_CPU_NODE; |
b3adf6ef | 2482 | if (desc->drawermaps) |
cc07239d | 2483 | columns[ncolumns++] = COL_CPU_DRAWER; |
ba45d8c1 | 2484 | if (desc->bookmaps) |
cc07239d | 2485 | columns[ncolumns++] = COL_CPU_BOOK; |
ba45d8c1 | 2486 | if (desc->socketmaps) |
cc07239d | 2487 | columns[ncolumns++] = COL_CPU_SOCKET; |
ba45d8c1 | 2488 | if (desc->coremaps) |
cc07239d | 2489 | columns[ncolumns++] = COL_CPU_CORE; |
ba45d8c1 | 2490 | if (desc->caches) |
cc07239d | 2491 | columns[ncolumns++] = COL_CPU_CACHE; |
a7e5300c | 2492 | if (desc->online) |
cc07239d | 2493 | columns[ncolumns++] = COL_CPU_ONLINE; |
d231eea1 | 2494 | if (desc->configured) |
cc07239d | 2495 | columns[ncolumns++] = COL_CPU_CONFIGURED; |
ba45d8c1 | 2496 | if (desc->polarization) |
cc07239d | 2497 | columns[ncolumns++] = COL_CPU_POLARIZATION; |
ba45d8c1 | 2498 | if (desc->addresses) |
cc07239d | 2499 | columns[ncolumns++] = COL_CPU_ADDRESS; |
e065a597 | 2500 | if (desc->maxmhz) |
cc07239d | 2501 | columns[ncolumns++] = COL_CPU_MAXMHZ; |
e065a597 | 2502 | if (desc->minmhz) |
cc07239d | 2503 | columns[ncolumns++] = COL_CPU_MINMHZ; |
ba45d8c1 | 2504 | } |
cc07239d | 2505 | print_cpus_readable(desc, columns, ncolumns, mod); |
ba45d8c1 | 2506 | break; |
8005924a | 2507 | } |
5dd7507c | 2508 | |
6e509042 KZ |
2509 | ul_unref_path(desc->syscpu); |
2510 | ul_unref_path(desc->procfs); | |
cf474aac | 2511 | return EXIT_SUCCESS; |
5dd7507c | 2512 | } |