]>
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> | |
5dd7507c | 36 | |
6d9b1c19 KZ |
37 | #if (defined(__x86_64__) || defined(__i386__)) |
38 | # if !defined( __SANITIZE_ADDRESS__) | |
39 | # define INCLUDE_VMWARE_BDOOR | |
40 | # else | |
41 | # warning VMWARE detection disabled by __SANITIZE_ADDRESS__ | |
42 | # endif | |
b7744730 RM |
43 | #endif |
44 | ||
45 | #ifdef INCLUDE_VMWARE_BDOOR | |
46 | # include <stdint.h> | |
47 | # include <signal.h> | |
48 | # include <strings.h> | |
49 | # include <setjmp.h> | |
65322517 | 50 | # ifdef HAVE_SYS_IO_H |
b7744730 RM |
51 | # include <sys/io.h> |
52 | # endif | |
53 | #endif | |
54 | ||
bd9b94d1 SB |
55 | #if defined(HAVE_LIBRTAS) |
56 | #include <librtas.h> | |
57 | #endif | |
58 | ||
83db4eb2 OO |
59 | #include <libsmartcols.h> |
60 | ||
7e03f383 | 61 | #include "cpuset.h" |
5dd7507c | 62 | #include "nls.h" |
4581647a | 63 | #include "xalloc.h" |
eb76ca98 | 64 | #include "c.h" |
477251f8 KZ |
65 | #include "strutils.h" |
66 | #include "bitops.h" | |
8148217b | 67 | #include "path.h" |
efb8854f | 68 | #include "closestream.h" |
41a8940d | 69 | #include "optutils.h" |
fb2627ce | 70 | #include "lscpu.h" |
477251f8 | 71 | |
5dd7507c CQ |
72 | #define CACHE_MAX 100 |
73 | ||
74 | /* /sys paths */ | |
d50363cd | 75 | #define _PATH_SYS_SYSTEM "/sys/devices/system" |
0ebbe9f1 | 76 | #define _PATH_SYS_HYP_FEATURES "/sys/hypervisor/properties/features" |
7e03f383 | 77 | #define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu" |
e9074a16 | 78 | #define _PATH_SYS_NODE _PATH_SYS_SYSTEM "/node" |
d50363cd | 79 | #define _PATH_PROC_XEN "/proc/xen" |
c8b64f6d | 80 | #define _PATH_PROC_XENCAP _PATH_PROC_XEN "/capabilities" |
d50363cd KZ |
81 | #define _PATH_PROC_CPUINFO "/proc/cpuinfo" |
82 | #define _PATH_PROC_PCIDEVS "/proc/bus/pci/devices" | |
b8ec7bdf | 83 | #define _PATH_PROC_SYSINFO "/proc/sysinfo" |
fb2627ce OO |
84 | #define _PATH_PROC_STATUS "/proc/self/status" |
85 | #define _PATH_PROC_VZ "/proc/vz" | |
86 | #define _PATH_PROC_BC "/proc/bc" | |
5bd31c6d | 87 | #define _PATH_PROC_DEVICETREE "/proc/device-tree" |
fb2627ce | 88 | #define _PATH_DEV_MEM "/dev/mem" |
5dd7507c | 89 | |
0ebbe9f1 DZ |
90 | /* Xen Domain feature flag used for /sys/hypervisor/properties/features */ |
91 | #define XENFEAT_supervisor_mode_kernel 3 | |
92 | #define XENFEAT_mmu_pt_update_preserve_ad 5 | |
93 | #define XENFEAT_hvm_callback_vector 8 | |
94 | ||
95 | #define XEN_FEATURES_PV_MASK (1U << XENFEAT_mmu_pt_update_preserve_ad) | |
96 | #define XEN_FEATURES_PVH_MASK ( (1U << XENFEAT_supervisor_mode_kernel) \ | |
97 | | (1U << XENFEAT_hvm_callback_vector) ) | |
98 | ||
c8b64f6d KZ |
99 | /* virtualization types */ |
100 | enum { | |
101 | VIRT_NONE = 0, | |
102 | VIRT_PARA, | |
857d9cdf KZ |
103 | VIRT_FULL, |
104 | VIRT_CONT | |
c8b64f6d KZ |
105 | }; |
106 | const char *virt_types[] = { | |
107 | [VIRT_NONE] = N_("none"), | |
108 | [VIRT_PARA] = N_("para"), | |
857d9cdf KZ |
109 | [VIRT_FULL] = N_("full"), |
110 | [VIRT_CONT] = N_("container"), | |
c8b64f6d KZ |
111 | }; |
112 | ||
c8b64f6d KZ |
113 | const char *hv_vendors[] = { |
114 | [HYPER_NONE] = NULL, | |
115 | [HYPER_XEN] = "Xen", | |
116 | [HYPER_KVM] = "KVM", | |
99fbc877 | 117 | [HYPER_MSHV] = "Microsoft", |
b8ec7bdf | 118 | [HYPER_VMWARE] = "VMware", |
fb2627ce OO |
119 | [HYPER_IBM] = "IBM", |
120 | [HYPER_VSERVER] = "Linux-VServer", | |
121 | [HYPER_UML] = "User-mode Linux", | |
122 | [HYPER_INNOTEK] = "Innotek GmbH", | |
123 | [HYPER_HITACHI] = "Hitachi", | |
96ce475f RM |
124 | [HYPER_PARALLELS] = "Parallels", |
125 | [HYPER_VBOX] = "Oracle", | |
5bd31c6d RM |
126 | [HYPER_OS400] = "OS/400", |
127 | [HYPER_PHYP] = "pHyp", | |
4597b692 | 128 | [HYPER_SPAR] = "Unisys s-Par" |
96ce475f RM |
129 | }; |
130 | ||
131 | const int hv_vendor_pci[] = { | |
132 | [HYPER_NONE] = 0x0000, | |
133 | [HYPER_XEN] = 0x5853, | |
134 | [HYPER_KVM] = 0x0000, | |
135 | [HYPER_MSHV] = 0x1414, | |
136 | [HYPER_VMWARE] = 0x15ad, | |
137 | [HYPER_VBOX] = 0x80ee, | |
138 | }; | |
139 | ||
140 | const int hv_graphics_pci[] = { | |
141 | [HYPER_NONE] = 0x0000, | |
142 | [HYPER_XEN] = 0x0001, | |
143 | [HYPER_KVM] = 0x0000, | |
144 | [HYPER_MSHV] = 0x5353, | |
145 | [HYPER_VMWARE] = 0x0710, | |
146 | [HYPER_VBOX] = 0xbeef, | |
c8b64f6d KZ |
147 | }; |
148 | ||
f633ad4e | 149 | /* CPU modes */ |
79e8b41a | 150 | enum { |
f633ad4e KZ |
151 | MODE_32BIT = (1 << 1), |
152 | MODE_64BIT = (1 << 2) | |
79e8b41a | 153 | }; |
c8b64f6d | 154 | |
e8aa16ee KZ |
155 | /* cache(s) description */ |
156 | struct cpu_cache { | |
7e03f383 KZ |
157 | char *name; |
158 | char *size; | |
159 | ||
160 | int nsharedmaps; | |
161 | cpu_set_t **sharedmaps; | |
e8aa16ee KZ |
162 | }; |
163 | ||
a0fff77e HC |
164 | /* dispatching modes */ |
165 | enum { | |
166 | DISP_HORIZONTAL = 0, | |
167 | DISP_VERTICAL = 1 | |
168 | }; | |
169 | ||
170 | const char *disp_modes[] = { | |
171 | [DISP_HORIZONTAL] = N_("horizontal"), | |
172 | [DISP_VERTICAL] = N_("vertical") | |
173 | }; | |
174 | ||
2b8fcb81 HC |
175 | /* cpu polarization */ |
176 | enum { | |
177 | POLAR_UNKNOWN = 0, | |
178 | POLAR_VLOW, | |
179 | POLAR_VMEDIUM, | |
180 | POLAR_VHIGH, | |
181 | POLAR_HORIZONTAL | |
182 | }; | |
183 | ||
8005924a KZ |
184 | struct polarization_modes { |
185 | char *parsable; | |
186 | char *readable; | |
187 | }; | |
188 | ||
189 | struct polarization_modes polar_modes[] = { | |
190 | [POLAR_UNKNOWN] = {"U", "-"}, | |
191 | [POLAR_VLOW] = {"VL", "vert-low"}, | |
192 | [POLAR_VMEDIUM] = {"VM", "vert-medium"}, | |
193 | [POLAR_VHIGH] = {"VH", "vert-high"}, | |
194 | [POLAR_HORIZONTAL] = {"H", "horizontal"}, | |
2b8fcb81 HC |
195 | }; |
196 | ||
e8aa16ee KZ |
197 | /* global description */ |
198 | struct lscpu_desc { | |
5dd7507c CQ |
199 | char *arch; |
200 | char *vendor; | |
0c28f0c8 | 201 | char *machinetype; /* s390 */ |
5dd7507c CQ |
202 | char *family; |
203 | char *model; | |
1461094c | 204 | char *modelname; |
c95e3889 RM |
205 | char *revision; /* alternative for model (ppc) */ |
206 | char *cpu; /* alternative for modelname (ppc, sparc) */ | |
c8b64f6d | 207 | char *virtflag; /* virtualization flag (vmx, svm) */ |
10829cd7 | 208 | char *hypervisor; /* hypervisor software */ |
c8b64f6d KZ |
209 | int hyper; /* hypervisor vendor ID */ |
210 | int virtype; /* VIRT_PARA|FULL|NONE ? */ | |
5dd7507c | 211 | char *mhz; |
4632b288 HC |
212 | char *dynamic_mhz; /* dynamic mega hertz (s390) */ |
213 | char *static_mhz; /* static mega hertz (s390) */ | |
e065a597 DB |
214 | char **maxmhz; /* maximum mega hertz */ |
215 | char **minmhz; /* minimum mega hertz */ | |
5dd7507c | 216 | char *stepping; |
9b8d4d5f | 217 | char *bogomips; |
5dd7507c | 218 | char *flags; |
a0fff77e | 219 | int dispatching; /* none, horizontal or vertical */ |
79e8b41a KZ |
220 | int mode; /* rm, lm or/and tm */ |
221 | ||
a5cfffff TK |
222 | int ncpuspos; /* maximal possible CPUs */ |
223 | int ncpus; /* number of present CPUs */ | |
224 | cpu_set_t *present; /* mask with present CPUs */ | |
aac1e59e | 225 | cpu_set_t *online; /* mask with online CPUs */ |
7e03f383 | 226 | |
4f642863 AT |
227 | int nthreads; /* number of online threads */ |
228 | ||
229 | int ncaches; | |
230 | struct cpu_cache *caches; | |
231 | ||
232 | /* | |
233 | * All maps are sequentially indexed (0..ncpuspos), the array index | |
234 | * does not have match with cpuX number as presented by kernel. You | |
235 | * have to use real_cpu_num() to get the real cpuX number. | |
236 | * | |
237 | * For example, the possible system CPUs are: 1,3,5, it means that | |
238 | * ncpuspos=3, so all arrays are in range 0..3. | |
239 | */ | |
240 | int *idx2cpunum; /* mapping index to CPU num */ | |
241 | ||
7e03f383 | 242 | int nnodes; /* number of NUMA modes */ |
e9074a16 | 243 | int *idx2nodenum; /* Support for discontinuous nodes */ |
7e03f383 KZ |
244 | cpu_set_t **nodemaps; /* array with NUMA nodes */ |
245 | ||
b3adf6ef HC |
246 | /* drawers -- based on drawer_siblings (internal kernel map of cpuX's |
247 | * hardware threads within the same drawer */ | |
248 | int ndrawers; /* number of all online drawers */ | |
249 | cpu_set_t **drawermaps; /* unique drawer_siblings */ | |
250 | ||
56baaa4e HC |
251 | /* books -- based on book_siblings (internal kernel map of cpuX's |
252 | * hardware threads within the same book */ | |
253 | int nbooks; /* number of all online books */ | |
254 | cpu_set_t **bookmaps; /* unique book_siblings */ | |
255 | ||
7e03f383 KZ |
256 | /* sockets -- based on core_siblings (internal kernel map of cpuX's |
257 | * hardware threads within the same physical_package_id (socket)) */ | |
e282eec2 | 258 | int nsockets; /* number of all online sockets */ |
7e03f383 KZ |
259 | cpu_set_t **socketmaps; /* unique core_siblings */ |
260 | ||
9e930041 | 261 | /* cores -- based on thread_siblings (internal kernel map of cpuX's |
7e03f383 | 262 | * hardware threads within the same core as cpuX) */ |
e282eec2 | 263 | int ncores; /* number of all online cores */ |
7e03f383 KZ |
264 | cpu_set_t **coremaps; /* unique thread_siblings */ |
265 | ||
2b8fcb81 | 266 | int *polarization; /* cpu polarization */ |
596b8845 | 267 | int *addresses; /* physical cpu addresses */ |
d231eea1 | 268 | int *configured; /* cpu configured */ |
bd9b94d1 SB |
269 | int physsockets; /* Physical sockets (modules) */ |
270 | int physchips; /* Physical chips */ | |
271 | int physcoresperchip; /* Physical cores per chip */ | |
5dd7507c CQ |
272 | }; |
273 | ||
8005924a KZ |
274 | enum { |
275 | OUTPUT_SUMMARY = 0, /* default */ | |
276 | OUTPUT_PARSABLE, /* -p */ | |
ba45d8c1 | 277 | OUTPUT_READABLE, /* -e */ |
8005924a KZ |
278 | }; |
279 | ||
8148217b HC |
280 | enum { |
281 | SYSTEM_LIVE = 0, /* analyzing a live system */ | |
282 | SYSTEM_SNAPSHOT, /* analyzing a snapshot of a different system */ | |
283 | }; | |
284 | ||
8005924a | 285 | struct lscpu_modifier { |
a7e5300c | 286 | int mode; /* OUTPUT_* */ |
8148217b | 287 | int system; /* SYSTEM_* */ |
a7e5300c HC |
288 | unsigned int hex:1, /* print CPU masks rather than CPU lists */ |
289 | compat:1, /* use backwardly compatible format */ | |
7afc2387 HC |
290 | online:1, /* print online CPUs */ |
291 | offline:1; /* print offline CPUs */ | |
8005924a KZ |
292 | }; |
293 | ||
7e03f383 | 294 | static int maxcpus; /* size in bits of kernel cpu mask */ |
5dd7507c | 295 | |
aac1e59e | 296 | #define is_cpu_online(_d, _cpu) \ |
a5cfffff TK |
297 | ((_d) && (_d)->online ? \ |
298 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0) | |
299 | #define is_cpu_present(_d, _cpu) \ | |
300 | ((_d) && (_d)->present ? \ | |
301 | CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->present) : 0) | |
aac1e59e | 302 | |
4f642863 AT |
303 | #define real_cpu_num(_d, _i) ((_d)->idx2cpunum[(_i)]) |
304 | ||
477251f8 | 305 | /* |
3d27b76a | 306 | * IDs |
477251f8 KZ |
307 | */ |
308 | enum { | |
309 | COL_CPU, | |
310 | COL_CORE, | |
311 | COL_SOCKET, | |
312 | COL_NODE, | |
313 | COL_BOOK, | |
b3adf6ef | 314 | COL_DRAWER, |
2b8fcb81 | 315 | COL_CACHE, |
596b8845 | 316 | COL_POLARIZATION, |
d231eea1 HC |
317 | COL_ADDRESS, |
318 | COL_CONFIGURED, | |
a7e5300c | 319 | COL_ONLINE, |
e065a597 DB |
320 | COL_MAXMHZ, |
321 | COL_MINMHZ, | |
477251f8 KZ |
322 | }; |
323 | ||
3d27b76a KZ |
324 | /* column description |
325 | */ | |
326 | struct lscpu_coldesc { | |
327 | const char *name; | |
328 | const char *help; | |
329 | ||
330 | unsigned int is_abbr:1; /* name is abbreviation */ | |
477251f8 KZ |
331 | }; |
332 | ||
3d27b76a KZ |
333 | static struct lscpu_coldesc coldescs[] = |
334 | { | |
335 | [COL_CPU] = { "CPU", N_("logical CPU number"), 1 }, | |
336 | [COL_CORE] = { "CORE", N_("logical core number") }, | |
337 | [COL_SOCKET] = { "SOCKET", N_("logical socket number") }, | |
338 | [COL_NODE] = { "NODE", N_("logical NUMA node number") }, | |
339 | [COL_BOOK] = { "BOOK", N_("logical book number") }, | |
b3adf6ef | 340 | [COL_DRAWER] = { "DRAWER", N_("logical drawer number") }, |
3d27b76a KZ |
341 | [COL_CACHE] = { "CACHE", N_("shows how caches are shared between CPUs") }, |
342 | [COL_POLARIZATION] = { "POLARIZATION", N_("CPU dispatching mode on virtual hardware") }, | |
343 | [COL_ADDRESS] = { "ADDRESS", N_("physical address of a CPU") }, | |
344 | [COL_CONFIGURED] = { "CONFIGURED", N_("shows if the hypervisor has allocated the CPU") }, | |
44320710 | 345 | [COL_ONLINE] = { "ONLINE", N_("shows if Linux currently makes use of the CPU") }, |
4df28845 BS |
346 | [COL_MAXMHZ] = { "MAXMHZ", N_("shows the maximum MHz of the CPU") }, |
347 | [COL_MINMHZ] = { "MINMHZ", N_("shows the minimum MHz of the CPU") } | |
3d27b76a | 348 | }; |
477251f8 | 349 | |
583f14cc KZ |
350 | static int |
351 | column_name_to_id(const char *name, size_t namesz) | |
477251f8 | 352 | { |
329fd1c3 | 353 | size_t i; |
477251f8 | 354 | |
3d27b76a KZ |
355 | for (i = 0; i < ARRAY_SIZE(coldescs); i++) { |
356 | const char *cn = coldescs[i].name; | |
477251f8 KZ |
357 | |
358 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
359 | return i; | |
360 | } | |
361 | warnx(_("unknown column: %s"), name); | |
362 | return -1; | |
363 | } | |
364 | ||
5dd7507c CQ |
365 | /* Lookup a pattern and get the value from cpuinfo. |
366 | * Format is: | |
367 | * | |
368 | * "<pattern> : <key>" | |
369 | */ | |
583f14cc KZ |
370 | static int |
371 | lookup(char *line, char *pattern, char **value) | |
5dd7507c CQ |
372 | { |
373 | char *p, *v; | |
374 | int len = strlen(pattern); | |
375 | ||
e5fc6d6f RM |
376 | /* don't re-fill already found tags, first one wins */ |
377 | if (!*line || *value) | |
5dd7507c CQ |
378 | return 0; |
379 | ||
380 | /* pattern */ | |
381 | if (strncmp(line, pattern, len)) | |
382 | return 0; | |
383 | ||
384 | /* white spaces */ | |
385 | for (p = line + len; isspace(*p); p++); | |
386 | ||
387 | /* separator */ | |
388 | if (*p != ':') | |
389 | return 0; | |
390 | ||
391 | /* white spaces */ | |
392 | for (++p; isspace(*p); p++); | |
393 | ||
394 | /* value */ | |
395 | if (!*p) | |
396 | return 0; | |
397 | v = p; | |
398 | ||
399 | /* end of value */ | |
400 | len = strlen(line) - 1; | |
401 | for (p = line + len; isspace(*(p-1)); p--); | |
402 | *p = '\0'; | |
403 | ||
404 | *value = xstrdup(v); | |
405 | return 1; | |
406 | } | |
407 | ||
f633ad4e KZ |
408 | /* Don't init the mode for platforms where we are not able to |
409 | * detect that CPU supports 64-bit mode. | |
410 | */ | |
411 | static int | |
8148217b | 412 | init_mode(struct lscpu_modifier *mod) |
f633ad4e KZ |
413 | { |
414 | int m = 0; | |
415 | ||
8148217b | 416 | if (mod->system == SYSTEM_SNAPSHOT) |
4e740fd8 KZ |
417 | /* reading info from any /{sys,proc} dump, don't mix it with |
418 | * information about our real CPU */ | |
419 | return 0; | |
420 | ||
f633ad4e KZ |
421 | #if defined(__alpha__) || defined(__ia64__) |
422 | m |= MODE_64BIT; /* 64bit platforms only */ | |
423 | #endif | |
424 | /* platforms with 64bit flag in /proc/cpuinfo, define | |
425 | * 32bit default here */ | |
426 | #if defined(__i386__) || defined(__x86_64__) || \ | |
c487c90c | 427 | defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) |
f633ad4e KZ |
428 | m |= MODE_32BIT; |
429 | #endif | |
430 | return m; | |
431 | } | |
432 | ||
bd9b94d1 SB |
433 | #if defined(HAVE_LIBRTAS) |
434 | #define PROCESSOR_MODULE_INFO 43 | |
435 | static int strbe16toh(const char *buf, int offset) | |
436 | { | |
437 | return (buf[offset] << 8) + buf[offset+1]; | |
438 | } | |
439 | ||
440 | static void read_physical_info_powerpc(struct lscpu_desc *desc) | |
441 | { | |
442 | char buf[BUFSIZ]; | |
443 | int rc, len, ntypes; | |
444 | ||
445 | desc->physsockets = desc->physchips = desc->physcoresperchip = 0; | |
446 | ||
447 | rc = rtas_get_sysparm(PROCESSOR_MODULE_INFO, sizeof(buf), buf); | |
448 | if (rc < 0) | |
449 | return; | |
450 | ||
451 | len = strbe16toh(buf, 0); | |
452 | if (len < 8) | |
453 | return; | |
454 | ||
455 | ntypes = strbe16toh(buf, 2); | |
456 | ||
457 | assert(ntypes <= 1); | |
458 | if (!ntypes) | |
459 | return; | |
460 | ||
461 | desc->physsockets = strbe16toh(buf, 4); | |
462 | desc->physchips = strbe16toh(buf, 6); | |
463 | desc->physcoresperchip = strbe16toh(buf, 8); | |
464 | } | |
465 | #else | |
466 | static void read_physical_info_powerpc( | |
467 | struct lscpu_desc *desc __attribute__((__unused__))) | |
468 | { | |
469 | } | |
470 | #endif | |
471 | ||
5dd7507c | 472 | static void |
8148217b | 473 | read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
5dd7507c | 474 | { |
d50363cd | 475 | FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO); |
5dd7507c CQ |
476 | char buf[BUFSIZ]; |
477 | struct utsname utsbuf; | |
a5cfffff | 478 | size_t setsize; |
5dd7507c CQ |
479 | |
480 | /* architecture */ | |
481 | if (uname(&utsbuf) == -1) | |
482 | err(EXIT_FAILURE, _("error: uname failed")); | |
e8aa16ee | 483 | desc->arch = xstrdup(utsbuf.machine); |
5dd7507c | 484 | |
5dd7507c CQ |
485 | /* details */ |
486 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
e8aa16ee KZ |
487 | if (lookup(buf, "vendor", &desc->vendor)) ; |
488 | else if (lookup(buf, "vendor_id", &desc->vendor)) ; | |
e8aa16ee KZ |
489 | else if (lookup(buf, "family", &desc->family)) ; |
490 | else if (lookup(buf, "cpu family", &desc->family)) ; | |
86c4817e RM |
491 | else if (lookup(buf, "model", &desc->model)) ; |
492 | else if (lookup(buf, "model name", &desc->modelname)) ; | |
e8aa16ee KZ |
493 | else if (lookup(buf, "stepping", &desc->stepping)) ; |
494 | else if (lookup(buf, "cpu MHz", &desc->mhz)) ; | |
4632b288 HC |
495 | else if (lookup(buf, "cpu MHz dynamic", &desc->dynamic_mhz)) ; /* s390 */ |
496 | else if (lookup(buf, "cpu MHz static", &desc->static_mhz)) ; /* s390 */ | |
f633ad4e KZ |
497 | else if (lookup(buf, "flags", &desc->flags)) ; /* x86 */ |
498 | else if (lookup(buf, "features", &desc->flags)) ; /* s390 */ | |
c487c90c | 499 | else if (lookup(buf, "type", &desc->flags)) ; /* sparc64 */ |
9b8d4d5f | 500 | else if (lookup(buf, "bogomips", &desc->bogomips)) ; |
abcd6368 | 501 | else if (lookup(buf, "bogomips per cpu", &desc->bogomips)) ; /* s390 */ |
c95e3889 RM |
502 | else if (lookup(buf, "cpu", &desc->cpu)) ; |
503 | else if (lookup(buf, "revision", &desc->revision)) ; | |
5dd7507c CQ |
504 | else |
505 | continue; | |
506 | } | |
c8b64f6d | 507 | |
8148217b | 508 | desc->mode = init_mode(mod); |
f633ad4e | 509 | |
e8aa16ee KZ |
510 | if (desc->flags) { |
511 | snprintf(buf, sizeof(buf), " %s ", desc->flags); | |
c8b64f6d | 512 | if (strstr(buf, " svm ")) |
8290a249 | 513 | desc->virtflag = xstrdup("svm"); |
c8b64f6d | 514 | else if (strstr(buf, " vmx ")) |
8290a249 | 515 | desc->virtflag = xstrdup("vmx"); |
79e8b41a | 516 | if (strstr(buf, " lm ")) |
f633ad4e KZ |
517 | desc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */ |
518 | if (strstr(buf, " zarch ")) | |
519 | desc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */ | |
c487c90c KZ |
520 | if (strstr(buf, " sun4v ") || strstr(buf, " sun4u ")) |
521 | desc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */ | |
c8b64f6d KZ |
522 | } |
523 | ||
4581b716 KZ |
524 | if (desc->arch && mod->system != SYSTEM_SNAPSHOT) { |
525 | if (strcmp(desc->arch, "ppc64") == 0) | |
526 | desc->mode |= MODE_32BIT | MODE_64BIT; | |
527 | else if (strcmp(desc->arch, "ppc") == 0) | |
528 | desc->mode |= MODE_32BIT; | |
529 | } | |
530 | ||
5dd7507c | 531 | fclose(fp); |
7e03f383 | 532 | |
1d56b55e | 533 | if (path_exist(_PATH_SYS_CPU "/kernel_max")) |
3d6e5c35 | 534 | /* note that kernel_max is maximum index [NR_CPUS-1] */ |
1d56b55e | 535 | maxcpus = path_read_s32(_PATH_SYS_CPU "/kernel_max") + 1; |
7e03f383 | 536 | |
8148217b | 537 | else if (mod->system == SYSTEM_LIVE) |
7e03f383 KZ |
538 | /* the root is '/' so we are working with data from the current kernel */ |
539 | maxcpus = get_max_number_of_cpus(); | |
701e2b8e KZ |
540 | |
541 | if (maxcpus <= 0) | |
542 | /* error or we are reading some /sys snapshot instead of the | |
543 | * real /sys, let's use any crazy number... */ | |
a5cfffff TK |
544 | maxcpus = 2048; |
545 | ||
546 | setsize = CPU_ALLOC_SIZE(maxcpus); | |
547 | ||
1d56b55e RM |
548 | if (path_exist(_PATH_SYS_CPU "/possible")) { |
549 | cpu_set_t *tmp = path_read_cpulist(maxcpus, _PATH_SYS_CPU "/possible"); | |
4f642863 AT |
550 | int num, idx; |
551 | ||
a5cfffff | 552 | desc->ncpuspos = CPU_COUNT_S(setsize, tmp); |
4f642863 AT |
553 | desc->idx2cpunum = xcalloc(desc->ncpuspos, sizeof(int)); |
554 | ||
555 | for (num = 0, idx = 0; num < maxcpus; num++) { | |
556 | if (CPU_ISSET(num, tmp)) | |
557 | desc->idx2cpunum[idx++] = num; | |
558 | } | |
a5cfffff TK |
559 | cpuset_free(tmp); |
560 | } else | |
561 | err(EXIT_FAILURE, _("failed to determine number of CPUs: %s"), | |
1d56b55e | 562 | _PATH_SYS_CPU "/possible"); |
a5cfffff TK |
563 | |
564 | ||
565 | /* get mask for present CPUs */ | |
1d56b55e RM |
566 | if (path_exist(_PATH_SYS_CPU "/present")) { |
567 | desc->present = path_read_cpulist(maxcpus, _PATH_SYS_CPU "/present"); | |
a5cfffff TK |
568 | desc->ncpus = CPU_COUNT_S(setsize, desc->present); |
569 | } | |
aac1e59e KZ |
570 | |
571 | /* get mask for online CPUs */ | |
1d56b55e RM |
572 | if (path_exist(_PATH_SYS_CPU "/online")) { |
573 | desc->online = path_read_cpulist(maxcpus, _PATH_SYS_CPU "/online"); | |
e282eec2 KZ |
574 | desc->nthreads = CPU_COUNT_S(setsize, desc->online); |
575 | } | |
a0fff77e HC |
576 | |
577 | /* get dispatching mode */ | |
1d56b55e RM |
578 | if (path_exist(_PATH_SYS_CPU "/dispatching")) |
579 | desc->dispatching = path_read_s32(_PATH_SYS_CPU "/dispatching"); | |
a0fff77e HC |
580 | else |
581 | desc->dispatching = -1; | |
bd9b94d1 SB |
582 | |
583 | if (mod->system == SYSTEM_LIVE) | |
584 | read_physical_info_powerpc(desc); | |
0c28f0c8 HC |
585 | |
586 | if (path_exist(_PATH_PROC_SYSINFO)) { | |
587 | FILE *fd = path_fopen("r", 0, _PATH_PROC_SYSINFO); | |
588 | ||
589 | while (fd && fgets(buf, sizeof(buf), fd) != NULL && !desc->machinetype) | |
590 | lookup(buf, "Type", &desc->machinetype); | |
591 | if (fd) | |
592 | fclose(fd); | |
593 | } | |
5dd7507c CQ |
594 | } |
595 | ||
c8b64f6d | 596 | static int |
140014a4 | 597 | has_pci_device(unsigned int vendor, unsigned int device) |
c8b64f6d KZ |
598 | { |
599 | FILE *f; | |
13cf6fc8 | 600 | unsigned int num, fn, ven, dev; |
c8b64f6d KZ |
601 | int res = 1; |
602 | ||
d50363cd | 603 | f = path_fopen("r", 0, _PATH_PROC_PCIDEVS); |
c8b64f6d KZ |
604 | if (!f) |
605 | return 0; | |
606 | ||
607 | /* for more details about bus/pci/devices format see | |
608 | * drivers/pci/proc.c in linux kernel | |
609 | */ | |
610 | while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]", | |
611 | &num, &fn, &ven, &dev) == 4) { | |
612 | ||
613 | if (ven == vendor && dev == device) | |
614 | goto found; | |
615 | } | |
616 | ||
617 | res = 0; | |
618 | found: | |
619 | fclose(f); | |
620 | return res; | |
621 | } | |
622 | ||
623 | #if defined(__x86_64__) || defined(__i386__) | |
624 | ||
625 | /* | |
626 | * This CPUID leaf returns the information about the hypervisor. | |
627 | * EAX : maximum input value for CPUID supported by the hypervisor. | |
628 | * EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware. | |
629 | */ | |
630 | #define HYPERVISOR_INFO_LEAF 0x40000000 | |
631 | ||
632 | static inline void | |
633 | cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, | |
634 | unsigned int *ecx, unsigned int *edx) | |
635 | { | |
c9239f23 MF |
636 | __asm__( |
637 | #if defined(__PIC__) && defined(__i386__) | |
638 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
7845b91d | 639 | "xchg %%ebx, %%esi;" |
c9239f23 | 640 | "cpuid;" |
7845b91d | 641 | "xchg %%esi, %%ebx;" |
c9239f23 MF |
642 | : "=S" (*ebx), |
643 | #else | |
644 | "cpuid;" | |
645 | : "=b" (*ebx), | |
646 | #endif | |
647 | "=a" (*eax), | |
c8b64f6d KZ |
648 | "=c" (*ecx), |
649 | "=d" (*edx) | |
bc54770d | 650 | : "1" (op), "c"(0)); |
c8b64f6d KZ |
651 | } |
652 | ||
653 | static void | |
e8aa16ee | 654 | read_hypervisor_cpuid(struct lscpu_desc *desc) |
c8b64f6d KZ |
655 | { |
656 | unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; | |
657 | char hyper_vendor_id[13]; | |
658 | ||
659 | memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id)); | |
660 | ||
661 | cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx); | |
662 | memcpy(hyper_vendor_id + 0, &ebx, 4); | |
663 | memcpy(hyper_vendor_id + 4, &ecx, 4); | |
664 | memcpy(hyper_vendor_id + 8, &edx, 4); | |
665 | hyper_vendor_id[12] = '\0'; | |
666 | ||
667 | if (!hyper_vendor_id[0]) | |
668 | return; | |
669 | ||
670 | if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12)) | |
e8aa16ee | 671 | desc->hyper = HYPER_XEN; |
c8b64f6d | 672 | else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9)) |
e8aa16ee | 673 | desc->hyper = HYPER_KVM; |
c8b64f6d | 674 | else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12)) |
e8aa16ee | 675 | desc->hyper = HYPER_MSHV; |
99fbc877 SH |
676 | else if (!strncmp("VMwareVMware", hyper_vendor_id, 12)) |
677 | desc->hyper = HYPER_VMWARE; | |
4597b692 BR |
678 | else if (!strncmp("UnisysSpar64", hyper_vendor_id, 12)) |
679 | desc->hyper = HYPER_SPAR; | |
c8b64f6d KZ |
680 | } |
681 | ||
b7744730 | 682 | #else /* ! (__x86_64__ || __i386__) */ |
c8b64f6d | 683 | static void |
6d791b4c | 684 | read_hypervisor_cpuid(struct lscpu_desc *desc __attribute__((__unused__))) |
c8b64f6d KZ |
685 | { |
686 | } | |
687 | #endif | |
688 | ||
3565bd7a KZ |
689 | static int is_compatible(const char *path, const char *str) |
690 | { | |
e03b613e | 691 | FILE *fd = path_fopen("r", 0, "%s", path); |
3565bd7a | 692 | |
3565bd7a KZ |
693 | if (fd) { |
694 | char buf[256]; | |
695 | size_t i, len; | |
696 | ||
697 | memset(buf, 0, sizeof(buf)); | |
698 | len = fread(buf, 1, sizeof(buf) - 1, fd); | |
699 | fclose(fd); | |
700 | ||
701 | for (i = 0; i < len;) { | |
702 | if (!strcmp(&buf[i], str)) | |
703 | return 1; | |
704 | i += strlen(&buf[i]); | |
705 | i++; | |
706 | } | |
707 | } | |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
5bd31c6d RM |
712 | static int |
713 | read_hypervisor_powerpc(struct lscpu_desc *desc) | |
714 | { | |
715 | assert(!desc->hyper); | |
716 | ||
79738105 | 717 | /* IBM iSeries: legacy, para-virtualized on top of OS/400 */ |
5bd31c6d RM |
718 | if (path_exist("/proc/iSeries")) { |
719 | desc->hyper = HYPER_OS400; | |
720 | desc->virtype = VIRT_PARA; | |
79738105 KZ |
721 | |
722 | /* PowerNV (POWER Non-Virtualized, bare-metal) */ | |
e03b613e KZ |
723 | } else if (is_compatible(_PATH_PROC_DEVICETREE "/compatible", "ibm,powernv")) { |
724 | desc->hyper = HYPER_NONE; | |
725 | desc->virtype = VIRT_NONE; | |
79738105 | 726 | |
d45984be | 727 | /* PowerVM (IBM's proprietary hypervisor, aka pHyp) */ |
5bd31c6d RM |
728 | } else if (path_exist(_PATH_PROC_DEVICETREE "/ibm,partition-name") |
729 | && path_exist(_PATH_PROC_DEVICETREE "/hmc-managed?") | |
730 | && !path_exist(_PATH_PROC_DEVICETREE "/chosen/qemu,graphic-width")) { | |
731 | FILE *fd; | |
732 | desc->hyper = HYPER_PHYP; | |
733 | desc->virtype = VIRT_PARA; | |
734 | fd = path_fopen("r", 0, _PATH_PROC_DEVICETREE "/ibm,partition-name"); | |
735 | if (fd) { | |
736 | char buf[256]; | |
6cbf75e5 | 737 | if (fscanf(fd, "%255s", buf) == 1 && !strcmp(buf, "full")) |
5bd31c6d RM |
738 | desc->virtype = VIRT_NONE; |
739 | fclose(fd); | |
740 | } | |
79738105 KZ |
741 | |
742 | /* Qemu */ | |
e03b613e KZ |
743 | } else if (is_compatible(_PATH_PROC_DEVICETREE "/compatible", "qemu,pseries")) { |
744 | desc->hyper = HYPER_KVM; | |
745 | desc->virtype = VIRT_PARA; | |
5bd31c6d | 746 | } |
5bd31c6d RM |
747 | return desc->hyper; |
748 | } | |
749 | ||
b7744730 RM |
750 | #ifdef INCLUDE_VMWARE_BDOOR |
751 | ||
752 | #define VMWARE_BDOOR_MAGIC 0x564D5868 | |
753 | #define VMWARE_BDOOR_PORT 0x5658 | |
754 | #define VMWARE_BDOOR_CMD_GETVERSION 10 | |
755 | ||
77242032 | 756 | static UL_ASAN_BLACKLIST |
7845b91d MF |
757 | void vmware_bdoor(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) |
758 | { | |
759 | __asm__( | |
760 | #if defined(__PIC__) && defined(__i386__) | |
761 | /* x86 PIC cannot clobber ebx -- gcc bitches */ | |
762 | "xchg %%ebx, %%esi;" | |
763 | "inl (%%dx), %%eax;" | |
764 | "xchg %%esi, %%ebx;" | |
765 | : "=S" (*ebx), | |
766 | #else | |
767 | "inl (%%dx), %%eax;" | |
768 | : "=b" (*ebx), | |
769 | #endif | |
770 | "=a" (*eax), | |
771 | "=c" (*ecx), | |
772 | "=d" (*edx) | |
773 | : "0" (VMWARE_BDOOR_MAGIC), | |
774 | "1" (VMWARE_BDOOR_CMD_GETVERSION), | |
775 | "2" (VMWARE_BDOOR_PORT), | |
776 | "3" (0) | |
777 | : "memory"); | |
778 | } | |
b7744730 RM |
779 | |
780 | static jmp_buf segv_handler_env; | |
781 | ||
782 | static void | |
783 | segv_handler(__attribute__((__unused__)) int sig, | |
784 | __attribute__((__unused__)) siginfo_t *info, | |
785 | __attribute__((__unused__)) void *ignored) | |
786 | { | |
787 | siglongjmp(segv_handler_env, 1); | |
788 | } | |
789 | ||
790 | static int | |
791 | is_vmware_platform(void) | |
792 | { | |
793 | uint32_t eax, ebx, ecx, edx; | |
794 | struct sigaction act, oact; | |
795 | ||
796 | /* | |
797 | * The assembly routine for vmware detection works | |
798 | * fine under vmware, even if ran as regular user. But | |
799 | * on real HW or under other hypervisors, it segfaults (which is | |
800 | * expected). So we temporarily install SIGSEGV handler to catch | |
801 | * the signal. All this magic is needed because lscpu | |
802 | * isn't supposed to require root privileges. | |
803 | */ | |
804 | if (sigsetjmp(segv_handler_env, 1)) | |
805 | return 0; | |
806 | ||
9dd2d497 | 807 | memset(&act, 0, sizeof(act)); |
b7744730 RM |
808 | act.sa_sigaction = segv_handler; |
809 | act.sa_flags = SA_SIGINFO; | |
810 | ||
811 | if (sigaction(SIGSEGV, &act, &oact)) | |
812 | err(EXIT_FAILURE, _("error: can not set signal handler")); | |
813 | ||
7845b91d | 814 | vmware_bdoor(&eax, &ebx, &ecx, &edx); |
b7744730 RM |
815 | |
816 | if (sigaction(SIGSEGV, &oact, NULL)) | |
817 | err(EXIT_FAILURE, _("error: can not restore signal handler")); | |
818 | ||
819 | return eax != (uint32_t)-1 && ebx == VMWARE_BDOOR_MAGIC; | |
820 | } | |
821 | ||
822 | #else /* ! INCLUDE_VMWARE_BDOOR */ | |
823 | ||
824 | static int | |
825 | is_vmware_platform(void) | |
826 | { | |
827 | return 0; | |
828 | } | |
829 | ||
830 | #endif /* INCLUDE_VMWARE_BDOOR */ | |
831 | ||
c8b64f6d | 832 | static void |
eff79ceb | 833 | read_hypervisor(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
c8b64f6d | 834 | { |
fb2627ce OO |
835 | FILE *fd; |
836 | ||
837 | if (mod->system != SYSTEM_SNAPSHOT) { | |
eff79ceb | 838 | read_hypervisor_cpuid(desc); |
fb2627ce OO |
839 | if (!desc->hyper) |
840 | desc->hyper = read_hypervisor_dmi(); | |
b7744730 RM |
841 | if (!desc->hyper && is_vmware_platform()) |
842 | desc->hyper = HYPER_VMWARE; | |
fb2627ce | 843 | } |
c8b64f6d | 844 | |
0ebbe9f1 | 845 | if (desc->hyper) { |
e8aa16ee | 846 | desc->virtype = VIRT_FULL; |
c8b64f6d | 847 | |
b4d1fbda | 848 | if (desc->hyper == HYPER_XEN) { |
0ebbe9f1 DZ |
849 | uint32_t features; |
850 | ||
851 | fd = path_fopen("r", 0, _PATH_SYS_HYP_FEATURES); | |
852 | if (fd && fscanf(fd, "%x", &features) == 1) { | |
853 | /* Xen PV domain */ | |
854 | if (features & XEN_FEATURES_PV_MASK) | |
855 | desc->virtype = VIRT_PARA; | |
856 | /* Xen PVH domain */ | |
857 | else if ((features & XEN_FEATURES_PVH_MASK) | |
858 | == XEN_FEATURES_PVH_MASK) | |
859 | desc->virtype = VIRT_PARA; | |
860 | fclose(fd); | |
861 | } else { | |
862 | err(EXIT_FAILURE, _("failed to read from: %s"), | |
863 | _PATH_SYS_HYP_FEATURES); | |
864 | } | |
865 | } | |
866 | } else if (read_hypervisor_powerpc(desc) > 0) {} | |
5bd31c6d | 867 | |
fb2627ce | 868 | /* Xen para-virt or dom0 */ |
d50363cd | 869 | else if (path_exist(_PATH_PROC_XEN)) { |
c8b64f6d | 870 | int dom0 = 0; |
fb2627ce | 871 | fd = path_fopen("r", 0, _PATH_PROC_XENCAP); |
c8b64f6d KZ |
872 | |
873 | if (fd) { | |
874 | char buf[256]; | |
875 | ||
6cbf75e5 | 876 | if (fscanf(fd, "%255s", buf) == 1 && |
c8b64f6d KZ |
877 | !strcmp(buf, "control_d")) |
878 | dom0 = 1; | |
879 | fclose(fd); | |
880 | } | |
e8aa16ee KZ |
881 | desc->virtype = dom0 ? VIRT_NONE : VIRT_PARA; |
882 | desc->hyper = HYPER_XEN; | |
c8b64f6d | 883 | |
fb2627ce | 884 | /* Xen full-virt on non-x86_64 */ |
96ce475f | 885 | } else if (has_pci_device( hv_vendor_pci[HYPER_XEN], hv_graphics_pci[HYPER_XEN])) { |
e8aa16ee KZ |
886 | desc->hyper = HYPER_XEN; |
887 | desc->virtype = VIRT_FULL; | |
96ce475f RM |
888 | } else if (has_pci_device( hv_vendor_pci[HYPER_VMWARE], hv_graphics_pci[HYPER_VMWARE])) { |
889 | desc->hyper = HYPER_VMWARE; | |
890 | desc->virtype = VIRT_FULL; | |
891 | } else if (has_pci_device( hv_vendor_pci[HYPER_VBOX], hv_graphics_pci[HYPER_VBOX])) { | |
892 | desc->hyper = HYPER_VBOX; | |
893 | desc->virtype = VIRT_FULL; | |
fb2627ce OO |
894 | |
895 | /* IBM PR/SM */ | |
b8ec7bdf | 896 | } else if (path_exist(_PATH_PROC_SYSINFO)) { |
145a3ca3 | 897 | FILE *sysinfo_fd = path_fopen("r", 0, _PATH_PROC_SYSINFO); |
b8ec7bdf HC |
898 | char buf[BUFSIZ]; |
899 | ||
145a3ca3 | 900 | if (!sysinfo_fd) |
b3eff5dd | 901 | return; |
b8ec7bdf | 902 | desc->hyper = HYPER_IBM; |
10829cd7 | 903 | desc->hypervisor = "PR/SM"; |
b8ec7bdf | 904 | desc->virtype = VIRT_FULL; |
145a3ca3 | 905 | while (fgets(buf, sizeof(buf), sysinfo_fd) != NULL) { |
10829cd7 HC |
906 | char *str; |
907 | ||
b8ec7bdf HC |
908 | if (!strstr(buf, "Control Program:")) |
909 | continue; | |
910 | if (!strstr(buf, "KVM")) | |
911 | desc->hyper = HYPER_IBM; | |
912 | else | |
913 | desc->hyper = HYPER_KVM; | |
10829cd7 HC |
914 | str = strchr(buf, ':'); |
915 | if (!str) | |
916 | continue; | |
9d8537ff KZ |
917 | xasprintf(&str, "%s", str + 1); |
918 | ||
10829cd7 HC |
919 | /* remove leading, trailing and repeating whitespace */ |
920 | while (*str == ' ') | |
921 | str++; | |
922 | desc->hypervisor = str; | |
923 | str += strlen(str) - 1; | |
924 | while ((*str == '\n') || (*str == ' ')) | |
925 | *(str--) = '\0'; | |
926 | while ((str = strstr(desc->hypervisor, " "))) | |
927 | memmove(str, str + 1, strlen(str)); | |
b8ec7bdf | 928 | } |
145a3ca3 | 929 | fclose(sysinfo_fd); |
c8b64f6d | 930 | } |
fb2627ce OO |
931 | |
932 | /* OpenVZ/Virtuozzo - /proc/vz dir should exist | |
933 | * /proc/bc should not */ | |
857d9cdf | 934 | else if (path_exist(_PATH_PROC_VZ) && !path_exist(_PATH_PROC_BC)) { |
fb2627ce | 935 | desc->hyper = HYPER_PARALLELS; |
857d9cdf | 936 | desc->virtype = VIRT_CONT; |
fb2627ce OO |
937 | |
938 | /* IBM */ | |
857d9cdf | 939 | } else if (desc->vendor && |
fb2627ce | 940 | (strcmp(desc->vendor, "PowerVM Lx86") == 0 || |
857d9cdf | 941 | strcmp(desc->vendor, "IBM/S390") == 0)) { |
fb2627ce | 942 | desc->hyper = HYPER_IBM; |
857d9cdf | 943 | desc->virtype = VIRT_FULL; |
fb2627ce OO |
944 | |
945 | /* User-mode-linux */ | |
857d9cdf | 946 | } else if (desc->modelname && strstr(desc->modelname, "UML")) { |
fb2627ce | 947 | desc->hyper = HYPER_UML; |
857d9cdf | 948 | desc->virtype = VIRT_PARA; |
fb2627ce OO |
949 | |
950 | /* Linux-VServer */ | |
857d9cdf | 951 | } else if (path_exist(_PATH_PROC_STATUS)) { |
fb2627ce OO |
952 | char buf[BUFSIZ]; |
953 | char *val = NULL; | |
954 | ||
dbbf8395 | 955 | fd = path_fopen("r", 1, _PATH_PROC_STATUS); |
fb2627ce OO |
956 | while (fgets(buf, sizeof(buf), fd) != NULL) { |
957 | if (lookup(buf, "VxID", &val)) | |
958 | break; | |
959 | } | |
960 | fclose(fd); | |
961 | ||
962 | if (val) { | |
963 | while (isdigit(*val)) | |
964 | ++val; | |
857d9cdf | 965 | if (!*val) { |
fb2627ce | 966 | desc->hyper = HYPER_VSERVER; |
857d9cdf KZ |
967 | desc->virtype = VIRT_CONT; |
968 | } | |
fb2627ce OO |
969 | } |
970 | } | |
c8b64f6d KZ |
971 | } |
972 | ||
455fe9a0 | 973 | /* add @set to the @ary, unnecessary set is deallocated. */ |
7e03f383 KZ |
974 | static int add_cpuset_to_array(cpu_set_t **ary, int *items, cpu_set_t *set) |
975 | { | |
976 | int i; | |
977 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
978 | ||
979 | if (!ary) | |
980 | return -1; | |
981 | ||
982 | for (i = 0; i < *items; i++) { | |
983 | if (CPU_EQUAL_S(setsize, set, ary[i])) | |
984 | break; | |
985 | } | |
986 | if (i == *items) { | |
987 | ary[*items] = set; | |
988 | ++*items; | |
989 | return 0; | |
990 | } | |
991 | CPU_FREE(set); | |
992 | return 1; | |
993 | } | |
994 | ||
5dd7507c | 995 | static void |
4f642863 | 996 | read_topology(struct lscpu_desc *desc, int idx) |
5dd7507c | 997 | { |
b3adf6ef HC |
998 | cpu_set_t *thread_siblings, *core_siblings; |
999 | cpu_set_t *book_siblings, *drawer_siblings; | |
4f642863 | 1000 | int num = real_cpu_num(desc, idx); |
7e03f383 KZ |
1001 | |
1002 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/topology/thread_siblings", num)) | |
1003 | return; | |
5dd7507c | 1004 | |
ca01695b | 1005 | thread_siblings = path_read_cpuset(maxcpus, _PATH_SYS_CPU |
7e03f383 | 1006 | "/cpu%d/topology/thread_siblings", num); |
ca01695b | 1007 | core_siblings = path_read_cpuset(maxcpus, _PATH_SYS_CPU |
7e03f383 | 1008 | "/cpu%d/topology/core_siblings", num); |
56baaa4e | 1009 | book_siblings = NULL; |
4f642863 | 1010 | if (path_exist(_PATH_SYS_CPU "/cpu%d/topology/book_siblings", num)) |
ca01695b | 1011 | book_siblings = path_read_cpuset(maxcpus, _PATH_SYS_CPU |
56baaa4e | 1012 | "/cpu%d/topology/book_siblings", num); |
b3adf6ef HC |
1013 | drawer_siblings = NULL; |
1014 | if (path_exist(_PATH_SYS_CPU "/cpu%d/topology/drawer_siblings", num)) | |
1015 | drawer_siblings = path_read_cpuset(maxcpus, _PATH_SYS_CPU | |
1016 | "/cpu%d/topology/drawer_siblings", num); | |
aac1e59e KZ |
1017 | |
1018 | if (!desc->coremaps) { | |
b3adf6ef | 1019 | int ndrawers, nbooks, nsockets, ncores, nthreads; |
7e03f383 KZ |
1020 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
1021 | ||
1022 | /* threads within one core */ | |
1023 | nthreads = CPU_COUNT_S(setsize, thread_siblings); | |
d4bfa64d KZ |
1024 | if (!nthreads) |
1025 | nthreads = 1; | |
1026 | ||
7e03f383 KZ |
1027 | /* cores within one socket */ |
1028 | ncores = CPU_COUNT_S(setsize, core_siblings) / nthreads; | |
d4bfa64d KZ |
1029 | if (!ncores) |
1030 | ncores = 1; | |
1031 | ||
1032 | /* number of sockets within one book. Because of odd / | |
1033 | * non-present cpu maps and to keep calculation easy we make | |
1034 | * sure that nsockets and nbooks is at least 1. | |
32a46618 | 1035 | */ |
d4bfa64d KZ |
1036 | nsockets = desc->ncpus / nthreads / ncores; |
1037 | if (!nsockets) | |
1038 | nsockets = 1; | |
1039 | ||
56baaa4e | 1040 | /* number of books */ |
d4bfa64d KZ |
1041 | nbooks = desc->ncpus / nthreads / ncores / nsockets; |
1042 | if (!nbooks) | |
1043 | nbooks = 1; | |
e282eec2 | 1044 | |
b3adf6ef HC |
1045 | /* number of drawers */ |
1046 | ndrawers = desc->ncpus / nbooks / nthreads / ncores / nsockets; | |
1047 | if (!ndrawers) | |
1048 | ndrawers = 1; | |
1049 | ||
e282eec2 | 1050 | /* all threads, see also read_basicinfo() |
32a46618 | 1051 | * -- fallback for kernels without |
e282eec2 KZ |
1052 | * /sys/devices/system/cpu/online. |
1053 | */ | |
1054 | if (!desc->nthreads) | |
b3adf6ef | 1055 | desc->nthreads = ndrawers * nbooks * nsockets * ncores * nthreads; |
d4bfa64d | 1056 | |
a5cfffff | 1057 | /* For each map we make sure that it can have up to ncpuspos |
9d1a3a18 HC |
1058 | * entries. This is because we cannot reliably calculate the |
1059 | * number of cores, sockets and books on all architectures. | |
1060 | * E.g. completely virtualized architectures like s390 may | |
1061 | * have multiple sockets of different sizes. | |
1062 | */ | |
a5cfffff TK |
1063 | desc->coremaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
1064 | desc->socketmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); | |
08de16d0 | 1065 | if (book_siblings) |
a5cfffff | 1066 | desc->bookmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
b3adf6ef HC |
1067 | if (drawer_siblings) |
1068 | desc->drawermaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); | |
7e03f383 | 1069 | } |
5dd7507c | 1070 | |
7e03f383 KZ |
1071 | add_cpuset_to_array(desc->socketmaps, &desc->nsockets, core_siblings); |
1072 | add_cpuset_to_array(desc->coremaps, &desc->ncores, thread_siblings); | |
56baaa4e HC |
1073 | if (book_siblings) |
1074 | add_cpuset_to_array(desc->bookmaps, &desc->nbooks, book_siblings); | |
b3adf6ef HC |
1075 | if (drawer_siblings) |
1076 | add_cpuset_to_array(desc->drawermaps, &desc->ndrawers, drawer_siblings); | |
7e03f383 | 1077 | } |
4f642863 | 1078 | |
2b8fcb81 | 1079 | static void |
4f642863 | 1080 | read_polarization(struct lscpu_desc *desc, int idx) |
2b8fcb81 HC |
1081 | { |
1082 | char mode[64]; | |
4f642863 | 1083 | int num = real_cpu_num(desc, idx); |
2b8fcb81 HC |
1084 | |
1085 | if (desc->dispatching < 0) | |
1086 | return; | |
1087 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/polarization", num)) | |
1088 | return; | |
1089 | if (!desc->polarization) | |
a5cfffff | 1090 | desc->polarization = xcalloc(desc->ncpuspos, sizeof(int)); |
ca01695b | 1091 | path_read_str(mode, sizeof(mode), _PATH_SYS_CPU "/cpu%d/polarization", num); |
2b8fcb81 | 1092 | if (strncmp(mode, "vertical:low", sizeof(mode)) == 0) |
4f642863 | 1093 | desc->polarization[idx] = POLAR_VLOW; |
2b8fcb81 | 1094 | else if (strncmp(mode, "vertical:medium", sizeof(mode)) == 0) |
4f642863 | 1095 | desc->polarization[idx] = POLAR_VMEDIUM; |
2b8fcb81 | 1096 | else if (strncmp(mode, "vertical:high", sizeof(mode)) == 0) |
4f642863 | 1097 | desc->polarization[idx] = POLAR_VHIGH; |
2b8fcb81 | 1098 | else if (strncmp(mode, "horizontal", sizeof(mode)) == 0) |
4f642863 | 1099 | desc->polarization[idx] = POLAR_HORIZONTAL; |
2b8fcb81 | 1100 | else |
4f642863 | 1101 | desc->polarization[idx] = POLAR_UNKNOWN; |
2b8fcb81 | 1102 | } |
7e03f383 | 1103 | |
596b8845 | 1104 | static void |
4f642863 | 1105 | read_address(struct lscpu_desc *desc, int idx) |
596b8845 | 1106 | { |
4f642863 AT |
1107 | int num = real_cpu_num(desc, idx); |
1108 | ||
596b8845 HC |
1109 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/address", num)) |
1110 | return; | |
1111 | if (!desc->addresses) | |
a5cfffff | 1112 | desc->addresses = xcalloc(desc->ncpuspos, sizeof(int)); |
4f642863 | 1113 | desc->addresses[idx] = path_read_s32(_PATH_SYS_CPU "/cpu%d/address", num); |
596b8845 | 1114 | } |
7e03f383 | 1115 | |
d231eea1 | 1116 | static void |
4f642863 | 1117 | read_configured(struct lscpu_desc *desc, int idx) |
d231eea1 | 1118 | { |
4f642863 AT |
1119 | int num = real_cpu_num(desc, idx); |
1120 | ||
d231eea1 HC |
1121 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", num)) |
1122 | return; | |
1123 | if (!desc->configured) | |
a5cfffff | 1124 | desc->configured = xcalloc(desc->ncpuspos, sizeof(int)); |
4f642863 | 1125 | desc->configured[idx] = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", num); |
d231eea1 HC |
1126 | } |
1127 | ||
44320710 | 1128 | static void |
4f642863 | 1129 | read_max_mhz(struct lscpu_desc *desc, int idx) |
44320710 | 1130 | { |
4f642863 AT |
1131 | int num = real_cpu_num(desc, idx); |
1132 | ||
44320710 SK |
1133 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/cpufreq/cpuinfo_max_freq", num)) |
1134 | return; | |
e065a597 DB |
1135 | if (!desc->maxmhz) |
1136 | desc->maxmhz = xcalloc(desc->ncpuspos, sizeof(char *)); | |
4f642863 | 1137 | xasprintf(&(desc->maxmhz[idx]), "%.4f", |
44320710 SK |
1138 | (float)path_read_s32(_PATH_SYS_CPU |
1139 | "/cpu%d/cpufreq/cpuinfo_max_freq", num) / 1000); | |
1140 | } | |
1141 | ||
e065a597 | 1142 | static void |
4f642863 | 1143 | read_min_mhz(struct lscpu_desc *desc, int idx) |
e065a597 | 1144 | { |
4f642863 AT |
1145 | int num = real_cpu_num(desc, idx); |
1146 | ||
e065a597 DB |
1147 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/cpufreq/cpuinfo_min_freq", num)) |
1148 | return; | |
1149 | if (!desc->minmhz) | |
1150 | desc->minmhz = xcalloc(desc->ncpuspos, sizeof(char *)); | |
4f642863 | 1151 | xasprintf(&(desc->minmhz[idx]), "%.4f", |
e065a597 DB |
1152 | (float)path_read_s32(_PATH_SYS_CPU |
1153 | "/cpu%d/cpufreq/cpuinfo_min_freq", num) / 1000); | |
1154 | } | |
1155 | ||
7e03f383 KZ |
1156 | static int |
1157 | cachecmp(const void *a, const void *b) | |
1158 | { | |
1159 | struct cpu_cache *c1 = (struct cpu_cache *) a; | |
1160 | struct cpu_cache *c2 = (struct cpu_cache *) b; | |
1161 | ||
1162 | return strcmp(c2->name, c1->name); | |
5dd7507c CQ |
1163 | } |
1164 | ||
1165 | static void | |
4f642863 | 1166 | read_cache(struct lscpu_desc *desc, int idx) |
5dd7507c CQ |
1167 | { |
1168 | char buf[256]; | |
7e03f383 | 1169 | int i; |
4f642863 | 1170 | int num = real_cpu_num(desc, idx); |
5dd7507c | 1171 | |
aac1e59e | 1172 | if (!desc->ncaches) { |
1d56b55e | 1173 | while(path_exist(_PATH_SYS_CPU "/cpu%d/cache/index%d", |
7e03f383 KZ |
1174 | num, desc->ncaches)) |
1175 | desc->ncaches++; | |
5dd7507c | 1176 | |
7e03f383 KZ |
1177 | if (!desc->ncaches) |
1178 | return; | |
5dd7507c | 1179 | |
08de16d0 | 1180 | desc->caches = xcalloc(desc->ncaches, sizeof(*desc->caches)); |
7e03f383 KZ |
1181 | } |
1182 | for (i = 0; i < desc->ncaches; i++) { | |
1183 | struct cpu_cache *ca = &desc->caches[i]; | |
1184 | cpu_set_t *map; | |
1185 | ||
1d56b55e | 1186 | if (!path_exist(_PATH_SYS_CPU "/cpu%d/cache/index%d", |
dcdead42 HC |
1187 | num, i)) |
1188 | continue; | |
7e03f383 KZ |
1189 | if (!ca->name) { |
1190 | int type, level; | |
1191 | ||
1192 | /* cache type */ | |
ca01695b | 1193 | path_read_str(buf, sizeof(buf), |
7e03f383 KZ |
1194 | _PATH_SYS_CPU "/cpu%d/cache/index%d/type", |
1195 | num, i); | |
1196 | if (!strcmp(buf, "Data")) | |
1197 | type = 'd'; | |
1198 | else if (!strcmp(buf, "Instruction")) | |
1199 | type = 'i'; | |
1200 | else | |
1201 | type = 0; | |
1202 | ||
1203 | /* cache level */ | |
ca01695b | 1204 | level = path_read_s32(_PATH_SYS_CPU "/cpu%d/cache/index%d/level", |
7e03f383 KZ |
1205 | num, i); |
1206 | if (type) | |
1207 | snprintf(buf, sizeof(buf), "L%d%c", level, type); | |
1208 | else | |
1209 | snprintf(buf, sizeof(buf), "L%d", level); | |
1210 | ||
1211 | ca->name = xstrdup(buf); | |
1212 | ||
1213 | /* cache size */ | |
10d927ab RM |
1214 | if (path_exist(_PATH_SYS_CPU "/cpu%d/cache/index%d/size",num, i)) { |
1215 | path_read_str(buf, sizeof(buf), | |
1216 | _PATH_SYS_CPU "/cpu%d/cache/index%d/size", num, i); | |
1217 | ca->size = xstrdup(buf); | |
1218 | } else { | |
1219 | ca->size = xstrdup("unknown size"); | |
1220 | } | |
7e03f383 | 1221 | } |
5dd7507c | 1222 | |
7e03f383 | 1223 | /* information about how CPUs share different caches */ |
ca01695b | 1224 | map = path_read_cpuset(maxcpus, |
8148217b HC |
1225 | _PATH_SYS_CPU "/cpu%d/cache/index%d/shared_cpu_map", |
1226 | num, i); | |
5dd7507c | 1227 | |
08de16d0 | 1228 | if (!ca->sharedmaps) |
a5cfffff | 1229 | ca->sharedmaps = xcalloc(desc->ncpuspos, sizeof(cpu_set_t *)); |
7e03f383 | 1230 | add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map); |
5dd7507c CQ |
1231 | } |
1232 | } | |
1233 | ||
e9074a16 KZ |
1234 | static inline int is_node_dirent(struct dirent *d) |
1235 | { | |
1236 | return | |
1237 | d && | |
1238 | #ifdef _DIRENT_HAVE_D_TYPE | |
c0cf4ae9 | 1239 | (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) && |
e9074a16 KZ |
1240 | #endif |
1241 | strncmp(d->d_name, "node", 4) == 0 && | |
1242 | isdigit_string(d->d_name + 4); | |
1243 | } | |
1244 | ||
39c758d1 KZ |
1245 | static int |
1246 | nodecmp(const void *ap, const void *bp) | |
1247 | { | |
1248 | int *a = (int *) ap, *b = (int *) bp; | |
1249 | return *a - *b; | |
1250 | } | |
1251 | ||
5dd7507c | 1252 | static void |
e8aa16ee | 1253 | read_nodes(struct lscpu_desc *desc) |
5dd7507c | 1254 | { |
e9074a16 KZ |
1255 | int i = 0; |
1256 | DIR *dir; | |
1257 | struct dirent *d; | |
1258 | char *path; | |
5dd7507c CQ |
1259 | |
1260 | /* number of NUMA node */ | |
e9074a16 KZ |
1261 | path = path_strdup(_PATH_SYS_NODE); |
1262 | dir = opendir(path); | |
1263 | free(path); | |
7e03f383 | 1264 | |
e9074a16 KZ |
1265 | while (dir && (d = readdir(dir))) { |
1266 | if (is_node_dirent(d)) | |
1267 | desc->nnodes++; | |
1268 | } | |
1269 | ||
1270 | if (!desc->nnodes) { | |
1271 | if (dir) | |
1272 | closedir(dir); | |
7e03f383 | 1273 | return; |
e9074a16 | 1274 | } |
5dd7507c | 1275 | |
08de16d0 | 1276 | desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *)); |
e9074a16 KZ |
1277 | desc->idx2nodenum = xmalloc(desc->nnodes * sizeof(int)); |
1278 | ||
1279 | if (dir) { | |
1280 | rewinddir(dir); | |
1281 | while ((d = readdir(dir)) && i < desc->nnodes) { | |
1282 | if (is_node_dirent(d)) | |
1283 | desc->idx2nodenum[i++] = strtol_or_err(((d->d_name) + 4), | |
1284 | _("Failed to extract the node number")); | |
1285 | } | |
1286 | closedir(dir); | |
39c758d1 | 1287 | qsort(desc->idx2nodenum, desc->nnodes, sizeof(int), nodecmp); |
e9074a16 | 1288 | } |
5dd7507c CQ |
1289 | |
1290 | /* information about how nodes share different CPUs */ | |
7e03f383 | 1291 | for (i = 0; i < desc->nnodes; i++) |
ca01695b | 1292 | desc->nodemaps[i] = path_read_cpuset(maxcpus, |
1d56b55e | 1293 | _PATH_SYS_NODE "/node%d/cpumap", |
e9074a16 | 1294 | desc->idx2nodenum[i]); |
5dd7507c CQ |
1295 | } |
1296 | ||
e3b3a2f3 | 1297 | static char * |
4f642863 | 1298 | get_cell_data(struct lscpu_desc *desc, int idx, int col, |
e3b3a2f3 KZ |
1299 | struct lscpu_modifier *mod, |
1300 | char *buf, size_t bufsz) | |
5dd7507c | 1301 | { |
7e03f383 | 1302 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
4f642863 AT |
1303 | size_t i; |
1304 | int cpu = real_cpu_num(desc, idx); | |
e3b3a2f3 KZ |
1305 | |
1306 | *buf = '\0'; | |
5dd7507c | 1307 | |
477251f8 KZ |
1308 | switch (col) { |
1309 | case COL_CPU: | |
e3b3a2f3 | 1310 | snprintf(buf, bufsz, "%d", cpu); |
477251f8 KZ |
1311 | break; |
1312 | case COL_CORE: | |
e9d659ea | 1313 | if (cpuset_ary_isset(cpu, desc->coremaps, |
4f642863 | 1314 | desc->ncores, setsize, &i) == 0) |
73f2bec5 | 1315 | snprintf(buf, bufsz, "%zu", i); |
477251f8 KZ |
1316 | break; |
1317 | case COL_SOCKET: | |
e9d659ea | 1318 | if (cpuset_ary_isset(cpu, desc->socketmaps, |
4f642863 | 1319 | desc->nsockets, setsize, &i) == 0) |
73f2bec5 | 1320 | snprintf(buf, bufsz, "%zu", i); |
477251f8 KZ |
1321 | break; |
1322 | case COL_NODE: | |
e9d659ea | 1323 | if (cpuset_ary_isset(cpu, desc->nodemaps, |
4f642863 | 1324 | desc->nnodes, setsize, &i) == 0) |
e9074a16 | 1325 | snprintf(buf, bufsz, "%d", desc->idx2nodenum[i]); |
477251f8 | 1326 | break; |
b3adf6ef HC |
1327 | case COL_DRAWER: |
1328 | if (cpuset_ary_isset(cpu, desc->drawermaps, | |
1329 | desc->ndrawers, setsize, &i) == 0) | |
1330 | snprintf(buf, bufsz, "%zu", i); | |
1331 | break; | |
477251f8 | 1332 | case COL_BOOK: |
e9d659ea | 1333 | if (cpuset_ary_isset(cpu, desc->bookmaps, |
4f642863 | 1334 | desc->nbooks, setsize, &i) == 0) |
73f2bec5 | 1335 | snprintf(buf, bufsz, "%zu", i); |
477251f8 KZ |
1336 | break; |
1337 | case COL_CACHE: | |
e3b3a2f3 KZ |
1338 | { |
1339 | char *p = buf; | |
1340 | size_t sz = bufsz; | |
1341 | int j; | |
1342 | ||
7e03f383 KZ |
1343 | for (j = desc->ncaches - 1; j >= 0; j--) { |
1344 | struct cpu_cache *ca = &desc->caches[j]; | |
e9d659ea KZ |
1345 | |
1346 | if (cpuset_ary_isset(cpu, ca->sharedmaps, | |
4f642863 | 1347 | ca->nsharedmaps, setsize, &i) == 0) { |
73f2bec5 | 1348 | int x = snprintf(p, sz, "%zu", i); |
06fa5817 | 1349 | if (x < 0 || (size_t) x >= sz) |
e3b3a2f3 KZ |
1350 | return NULL; |
1351 | p += x; | |
1352 | sz -= x; | |
1353 | } | |
1354 | if (j != 0) { | |
06fa5817 YK |
1355 | if (sz < 2) |
1356 | return NULL; | |
e3b3a2f3 KZ |
1357 | *p++ = mod->compat ? ',' : ':'; |
1358 | *p = '\0'; | |
0c5bbafa | 1359 | sz--; |
e3b3a2f3 | 1360 | } |
5dd7507c | 1361 | } |
477251f8 | 1362 | break; |
e3b3a2f3 | 1363 | } |
2b8fcb81 | 1364 | case COL_POLARIZATION: |
ba45d8c1 | 1365 | if (desc->polarization) { |
4f642863 | 1366 | int x = desc->polarization[idx]; |
ba45d8c1 | 1367 | |
e3b3a2f3 | 1368 | snprintf(buf, bufsz, "%s", |
ba45d8c1 KZ |
1369 | mod->mode == OUTPUT_PARSABLE ? |
1370 | polar_modes[x].parsable : | |
1371 | polar_modes[x].readable); | |
1372 | } | |
2b8fcb81 | 1373 | break; |
596b8845 HC |
1374 | case COL_ADDRESS: |
1375 | if (desc->addresses) | |
4f642863 | 1376 | snprintf(buf, bufsz, "%d", desc->addresses[idx]); |
596b8845 | 1377 | break; |
d231eea1 | 1378 | case COL_CONFIGURED: |
e43fc13e HC |
1379 | if (!desc->configured) |
1380 | break; | |
1381 | if (mod->mode == OUTPUT_PARSABLE) | |
f205c90a | 1382 | snprintf(buf, bufsz, "%s", |
4f642863 | 1383 | desc->configured[idx] ? _("Y") : _("N")); |
e43fc13e | 1384 | else |
f205c90a | 1385 | snprintf(buf, bufsz, "%s", |
4f642863 | 1386 | desc->configured[idx] ? _("yes") : _("no")); |
d231eea1 | 1387 | break; |
a7e5300c | 1388 | case COL_ONLINE: |
e43fc13e HC |
1389 | if (!desc->online) |
1390 | break; | |
1391 | if (mod->mode == OUTPUT_PARSABLE) | |
f205c90a | 1392 | snprintf(buf, bufsz, "%s", |
e43fc13e HC |
1393 | is_cpu_online(desc, cpu) ? _("Y") : _("N")); |
1394 | else | |
f205c90a | 1395 | snprintf(buf, bufsz, "%s", |
847b982e | 1396 | is_cpu_online(desc, cpu) ? _("yes") : _("no")); |
a7e5300c | 1397 | break; |
e065a597 DB |
1398 | case COL_MAXMHZ: |
1399 | if (desc->maxmhz) | |
4f642863 | 1400 | xstrncpy(buf, desc->maxmhz[idx], bufsz); |
e065a597 DB |
1401 | break; |
1402 | case COL_MINMHZ: | |
1403 | if (desc->minmhz) | |
4f642863 | 1404 | xstrncpy(buf, desc->minmhz[idx], bufsz); |
44320710 | 1405 | break; |
477251f8 | 1406 | } |
e3b3a2f3 KZ |
1407 | return buf; |
1408 | } | |
1409 | ||
1410 | static char * | |
1411 | get_cell_header(struct lscpu_desc *desc, int col, | |
1412 | struct lscpu_modifier *mod, | |
1413 | char *buf, size_t bufsz) | |
1414 | { | |
1415 | *buf = '\0'; | |
1416 | ||
1417 | if (col == COL_CACHE) { | |
1418 | char *p = buf; | |
1419 | size_t sz = bufsz; | |
1420 | int i; | |
1421 | ||
1422 | for (i = desc->ncaches - 1; i >= 0; i--) { | |
1423 | int x = snprintf(p, sz, "%s", desc->caches[i].name); | |
06fa5817 | 1424 | if (x < 0 || (size_t) x >= sz) |
e3b3a2f3 KZ |
1425 | return NULL; |
1426 | sz -= x; | |
1427 | p += x; | |
1428 | if (i > 0) { | |
06fa5817 YK |
1429 | if (sz < 2) |
1430 | return NULL; | |
e3b3a2f3 KZ |
1431 | *p++ = mod->compat ? ',' : ':'; |
1432 | *p = '\0'; | |
0c5bbafa | 1433 | sz--; |
e3b3a2f3 KZ |
1434 | } |
1435 | } | |
1436 | if (desc->ncaches) | |
1437 | return buf; | |
1438 | } | |
756d79cd | 1439 | snprintf(buf, bufsz, "%s", coldescs[col].name); |
e3b3a2f3 | 1440 | return buf; |
477251f8 KZ |
1441 | } |
1442 | ||
1443 | /* | |
ba45d8c1 | 1444 | * [-p] backend, we support two parsable formats: |
477251f8 KZ |
1445 | * |
1446 | * 1) "compatible" -- this format is compatible with the original lscpu(1) | |
1447 | * output and it contains fixed set of the columns. The CACHE columns are at | |
1448 | * the end of the line and the CACHE is not printed if the number of the caches | |
1449 | * is zero. The CACHE columns are separated by two commas, for example: | |
1450 | * | |
1451 | * $ lscpu --parse | |
1452 | * # CPU,Core,Socket,Node,,L1d,L1i,L2 | |
1453 | * 0,0,0,0,,0,0,0 | |
1454 | * 1,1,0,0,,1,1,0 | |
1455 | * | |
1456 | * 2) "user defined output" -- this format prints always all columns without | |
1457 | * special prefix for CACHE column. If there are not CACHEs then the column is | |
1458 | * empty and the header "Cache" is printed rather than a real name of the cache. | |
1459 | * The CACHE columns are separated by ':'. | |
1460 | * | |
1461 | * $ lscpu --parse=CPU,CORE,SOCKET,NODE,CACHE | |
1462 | * # CPU,Core,Socket,Node,L1d:L1i:L2 | |
1463 | * 0,0,0,0,0:0:0 | |
1464 | * 1,1,0,0,1:1:0 | |
1465 | */ | |
1466 | static void | |
8005924a KZ |
1467 | print_parsable(struct lscpu_desc *desc, int cols[], int ncols, |
1468 | struct lscpu_modifier *mod) | |
477251f8 | 1469 | { |
e3b3a2f3 KZ |
1470 | char buf[BUFSIZ], *data; |
1471 | int i; | |
477251f8 | 1472 | |
e3b3a2f3 KZ |
1473 | /* |
1474 | * Header | |
1475 | */ | |
477251f8 KZ |
1476 | printf(_( |
1477 | "# The following is the parsable format, which can be fed to other\n" | |
1478 | "# programs. Each different item in every column has an unique ID\n" | |
1479 | "# starting from zero.\n")); | |
1480 | ||
1481 | fputs("# ", stdout); | |
1482 | for (i = 0; i < ncols; i++) { | |
3d27b76a | 1483 | int col = cols[i]; |
b9d18bc3 | 1484 | |
3d27b76a | 1485 | if (col == COL_CACHE) { |
8005924a | 1486 | if (mod->compat && !desc->ncaches) |
477251f8 | 1487 | continue; |
8005924a | 1488 | if (mod->compat && i != 0) |
477251f8 | 1489 | putchar(','); |
477251f8 | 1490 | } |
e3b3a2f3 KZ |
1491 | if (i > 0) |
1492 | putchar(','); | |
1493 | ||
3d27b76a | 1494 | data = get_cell_header(desc, col, mod, buf, sizeof(buf)); |
b9d18bc3 | 1495 | |
3d27b76a KZ |
1496 | if (data && * data && col != COL_CACHE && |
1497 | !coldescs[col].is_abbr) { | |
1498 | /* | |
1499 | * For normal column names use mixed case (e.g. "Socket") | |
1500 | */ | |
1501 | char *p = data + 1; | |
1502 | ||
14e8be8a PU |
1503 | while (p && *p != '\0') { |
1504 | *p = tolower((unsigned int) *p); | |
1505 | p++; | |
1506 | } | |
3d27b76a | 1507 | } |
e3b3a2f3 | 1508 | fputs(data && *data ? data : "", stdout); |
477251f8 KZ |
1509 | } |
1510 | putchar('\n'); | |
1511 | ||
e3b3a2f3 KZ |
1512 | /* |
1513 | * Data | |
1514 | */ | |
a5cfffff | 1515 | for (i = 0; i < desc->ncpuspos; i++) { |
e3b3a2f3 | 1516 | int c; |
4f642863 | 1517 | int cpu = real_cpu_num(desc, i); |
e3b3a2f3 | 1518 | |
4f642863 | 1519 | if (!mod->offline && desc->online && !is_cpu_online(desc, cpu)) |
7afc2387 | 1520 | continue; |
4f642863 | 1521 | if (!mod->online && desc->online && is_cpu_online(desc, cpu)) |
477251f8 | 1522 | continue; |
4f642863 | 1523 | if (desc->present && !is_cpu_present(desc, cpu)) |
a5cfffff | 1524 | continue; |
477251f8 | 1525 | for (c = 0; c < ncols; c++) { |
8005924a | 1526 | if (mod->compat && cols[c] == COL_CACHE) { |
477251f8 KZ |
1527 | if (!desc->ncaches) |
1528 | continue; | |
1529 | if (c > 0) | |
1530 | putchar(','); | |
1531 | } | |
1532 | if (c > 0) | |
1533 | putchar(','); | |
e3b3a2f3 KZ |
1534 | |
1535 | data = get_cell_data(desc, i, cols[c], mod, | |
1536 | buf, sizeof(buf)); | |
1537 | fputs(data && *data ? data : "", stdout); | |
477251f8 | 1538 | } |
5dd7507c CQ |
1539 | putchar('\n'); |
1540 | } | |
1541 | } | |
1542 | ||
ba45d8c1 KZ |
1543 | /* |
1544 | * [-e] backend | |
1545 | */ | |
1546 | static void | |
1547 | print_readable(struct lscpu_desc *desc, int cols[], int ncols, | |
1548 | struct lscpu_modifier *mod) | |
1549 | { | |
1550 | int i; | |
e7213e34 KZ |
1551 | char buf[BUFSIZ]; |
1552 | const char *data; | |
710ed55d | 1553 | struct libscols_table *table; |
ba45d8c1 | 1554 | |
710ed55d KZ |
1555 | scols_init_debug(0); |
1556 | ||
1557 | table = scols_new_table(); | |
83db4eb2 | 1558 | if (!table) |
ba45d8c1 KZ |
1559 | err(EXIT_FAILURE, _("failed to initialize output table")); |
1560 | ||
1561 | for (i = 0; i < ncols; i++) { | |
b9d18bc3 | 1562 | data = get_cell_header(desc, cols[i], mod, buf, sizeof(buf)); |
83db4eb2 | 1563 | if (!scols_table_new_column(table, xstrdup(data), 0, 0)) |
e7213e34 | 1564 | err(EXIT_FAILURE, _("failed to initialize output column")); |
ba45d8c1 KZ |
1565 | } |
1566 | ||
a5cfffff | 1567 | for (i = 0; i < desc->ncpuspos; i++) { |
ba45d8c1 | 1568 | int c; |
83db4eb2 | 1569 | struct libscols_line *line; |
4f642863 | 1570 | int cpu = real_cpu_num(desc, i); |
ba45d8c1 | 1571 | |
4f642863 | 1572 | if (!mod->offline && desc->online && !is_cpu_online(desc, cpu)) |
7afc2387 | 1573 | continue; |
4f642863 | 1574 | if (!mod->online && desc->online && is_cpu_online(desc, cpu)) |
ba45d8c1 | 1575 | continue; |
4f642863 | 1576 | if (desc->present && !is_cpu_present(desc, cpu)) |
a5cfffff | 1577 | continue; |
ba45d8c1 | 1578 | |
83db4eb2 | 1579 | line = scols_table_new_line(table, NULL); |
e7213e34 KZ |
1580 | if (!line) |
1581 | err(EXIT_FAILURE, _("failed to initialize output line")); | |
ba45d8c1 KZ |
1582 | |
1583 | for (c = 0; c < ncols; c++) { | |
1584 | data = get_cell_data(desc, i, cols[c], mod, | |
1585 | buf, sizeof(buf)); | |
e7213e34 KZ |
1586 | if (!data || !*data) |
1587 | data = "-"; | |
1588 | scols_line_set_data(line, c, data); | |
ba45d8c1 KZ |
1589 | } |
1590 | } | |
1591 | ||
83db4eb2 OO |
1592 | scols_print_table(table); |
1593 | scols_unref_table(table); | |
ba45d8c1 | 1594 | } |
5dd7507c CQ |
1595 | |
1596 | /* output formats "<key> <value>"*/ | |
1597 | #define print_s(_key, _val) printf("%-23s%s\n", _key, _val) | |
1598 | #define print_n(_key, _val) printf("%-23s%d\n", _key, _val) | |
1599 | ||
1600 | static void | |
4f912c6a KZ |
1601 | print_cpuset(const char *key, cpu_set_t *set, int hex) |
1602 | { | |
1603 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); | |
1604 | size_t setbuflen = 7 * maxcpus; | |
1605 | char setbuf[setbuflen], *p; | |
1606 | ||
1607 | if (hex) { | |
1608 | p = cpumask_create(setbuf, setbuflen, set, setsize); | |
1609 | printf("%-23s0x%s\n", key, p); | |
1610 | } else { | |
1611 | p = cpulist_create(setbuf, setbuflen, set, setsize); | |
1612 | print_s(key, p); | |
1613 | } | |
1614 | ||
1615 | } | |
1616 | ||
ba45d8c1 KZ |
1617 | /* |
1618 | * default output | |
1619 | */ | |
4f912c6a | 1620 | static void |
8005924a | 1621 | print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod) |
5dd7507c | 1622 | { |
7e03f383 KZ |
1623 | char buf[512]; |
1624 | int i; | |
aac1e59e | 1625 | size_t setsize = CPU_ALLOC_SIZE(maxcpus); |
7e03f383 | 1626 | |
e6b0611b | 1627 | print_s(_("Architecture:"), desc->arch); |
79e8b41a | 1628 | |
f633ad4e | 1629 | if (desc->mode) { |
2d7bee25 | 1630 | char mbuf[64], *p = mbuf; |
79e8b41a | 1631 | |
f633ad4e | 1632 | if (desc->mode & MODE_32BIT) { |
79e8b41a KZ |
1633 | strcpy(p, "32-bit, "); |
1634 | p += 8; | |
1635 | } | |
f633ad4e | 1636 | if (desc->mode & MODE_64BIT) { |
79e8b41a KZ |
1637 | strcpy(p, "64-bit, "); |
1638 | p += 8; | |
1639 | } | |
1640 | *(p - 2) = '\0'; | |
2d7bee25 | 1641 | print_s(_("CPU op-mode(s):"), mbuf); |
79e8b41a | 1642 | } |
aabe2441 | 1643 | #if !defined(WORDS_BIGENDIAN) |
9b8d4d5f DB |
1644 | print_s(_("Byte Order:"), "Little Endian"); |
1645 | #else | |
1646 | print_s(_("Byte Order:"), "Big Endian"); | |
9b8d4d5f | 1647 | #endif |
4a939e04 | 1648 | print_n(_("CPU(s):"), desc->ncpus); |
4f912c6a | 1649 | |
5d4ba40d | 1650 | if (desc->online) |
8005924a KZ |
1651 | print_cpuset(mod->hex ? _("On-line CPU(s) mask:") : |
1652 | _("On-line CPU(s) list:"), | |
1653 | desc->online, mod->hex); | |
4f912c6a | 1654 | |
5d4ba40d | 1655 | if (desc->online && CPU_COUNT_S(setsize, desc->online) != desc->ncpus) { |
4f912c6a KZ |
1656 | cpu_set_t *set; |
1657 | ||
1658 | /* Linux kernel provides cpuset of off-line CPUs that contains | |
1659 | * all configured CPUs (see /sys/devices/system/cpu/offline), | |
1660 | * but want to print real (present in system) off-line CPUs only. | |
1661 | */ | |
1662 | set = cpuset_alloc(maxcpus, NULL, NULL); | |
1663 | if (!set) | |
1664 | err(EXIT_FAILURE, _("failed to callocate cpu set")); | |
1665 | CPU_ZERO_S(setsize, set); | |
a5cfffff | 1666 | for (i = 0; i < desc->ncpuspos; i++) { |
4f642863 AT |
1667 | int cpu = real_cpu_num(desc, i); |
1668 | if (!is_cpu_online(desc, cpu) && is_cpu_present(desc, cpu)) | |
1669 | CPU_SET_S(cpu, setsize, set); | |
4f912c6a | 1670 | } |
8005924a KZ |
1671 | print_cpuset(mod->hex ? _("Off-line CPU(s) mask:") : |
1672 | _("Off-line CPU(s) list:"), | |
1673 | set, mod->hex); | |
4f912c6a KZ |
1674 | cpuset_free(set); |
1675 | } | |
5dd7507c | 1676 | |
7e03f383 | 1677 | if (desc->nsockets) { |
b3adf6ef | 1678 | int cores_per_socket, sockets_per_book, books_per_drawer, drawers; |
8648ca96 | 1679 | |
b3adf6ef | 1680 | cores_per_socket = sockets_per_book = books_per_drawer = drawers = 0; |
8648ca96 HC |
1681 | /* s390 detects its cpu topology via /proc/sysinfo, if present. |
1682 | * Using simply the cpu topology masks in sysfs will not give | |
1683 | * usable results since everything is virtualized. E.g. | |
1684 | * virtual core 0 may have only 1 cpu, but virtual core 2 may | |
1685 | * five cpus. | |
1686 | * If the cpu topology is not exported (e.g. 2nd level guest) | |
1687 | * fall back to old calculation scheme. | |
1688 | */ | |
1689 | if (path_exist(_PATH_PROC_SYSINFO)) { | |
1690 | FILE *fd = path_fopen("r", 0, _PATH_PROC_SYSINFO); | |
2d7bee25 | 1691 | char pbuf[BUFSIZ]; |
b3adf6ef | 1692 | int t0, t1; |
8648ca96 | 1693 | |
2d7bee25 SK |
1694 | while (fd && fgets(pbuf, sizeof(pbuf), fd) != NULL) { |
1695 | if (sscanf(pbuf, "CPU Topology SW:%d%d%d%d%d%d", | |
b3adf6ef HC |
1696 | &t0, &t1, &drawers, &books_per_drawer, |
1697 | &sockets_per_book, | |
8648ca96 HC |
1698 | &cores_per_socket) == 6) |
1699 | break; | |
1700 | } | |
ad655c88 KZ |
1701 | if (fd) |
1702 | fclose(fd); | |
8648ca96 | 1703 | } |
7e03f383 | 1704 | print_n(_("Thread(s) per core:"), desc->nthreads / desc->ncores); |
8648ca96 HC |
1705 | print_n(_("Core(s) per socket:"), |
1706 | cores_per_socket ?: desc->ncores / desc->nsockets); | |
56baaa4e | 1707 | if (desc->nbooks) { |
8648ca96 HC |
1708 | print_n(_("Socket(s) per book:"), |
1709 | sockets_per_book ?: desc->nsockets / desc->nbooks); | |
b3adf6ef HC |
1710 | if (desc->ndrawers) { |
1711 | print_n(_("Book(s) per drawer:"), | |
1712 | books_per_drawer ?: desc->nbooks / desc->ndrawers); | |
156740ca | 1713 | print_n(_("Drawer(s):"), drawers ?: desc->ndrawers); |
b3adf6ef HC |
1714 | } else { |
1715 | print_n(_("Book(s):"), books_per_drawer ?: desc->nbooks); | |
1716 | } | |
56baaa4e | 1717 | } else { |
8648ca96 | 1718 | print_n(_("Socket(s):"), sockets_per_book ?: desc->nsockets); |
56baaa4e | 1719 | } |
5dd7507c | 1720 | } |
7e03f383 KZ |
1721 | if (desc->nnodes) |
1722 | print_n(_("NUMA node(s):"), desc->nnodes); | |
e8aa16ee KZ |
1723 | if (desc->vendor) |
1724 | print_s(_("Vendor ID:"), desc->vendor); | |
0c28f0c8 HC |
1725 | if (desc->machinetype) |
1726 | print_s(_("Machine type:"), desc->machinetype); | |
e8aa16ee KZ |
1727 | if (desc->family) |
1728 | print_s(_("CPU family:"), desc->family); | |
c95e3889 RM |
1729 | if (desc->model || desc->revision) |
1730 | print_s(_("Model:"), desc->revision ? desc->revision : desc->model); | |
1731 | if (desc->modelname || desc->cpu) | |
1732 | print_s(_("Model name:"), desc->cpu ? desc->cpu : desc->modelname); | |
e8aa16ee KZ |
1733 | if (desc->stepping) |
1734 | print_s(_("Stepping:"), desc->stepping); | |
1735 | if (desc->mhz) | |
1736 | print_s(_("CPU MHz:"), desc->mhz); | |
4632b288 HC |
1737 | if (desc->dynamic_mhz) |
1738 | print_s(_("CPU dynamic MHz:"), desc->dynamic_mhz); | |
1739 | if (desc->static_mhz) | |
1740 | print_s(_("CPU static MHz:"), desc->static_mhz); | |
e065a597 DB |
1741 | if (desc->maxmhz) |
1742 | print_s(_("CPU max MHz:"), desc->maxmhz[0]); | |
1743 | if (desc->minmhz) | |
1744 | print_s(_("CPU min MHz:"), desc->minmhz[0]); | |
9b8d4d5f DB |
1745 | if (desc->bogomips) |
1746 | print_s(_("BogoMIPS:"), desc->bogomips); | |
e8aa16ee KZ |
1747 | if (desc->virtflag) { |
1748 | if (!strcmp(desc->virtflag, "svm")) | |
5dd7507c | 1749 | print_s(_("Virtualization:"), "AMD-V"); |
e8aa16ee | 1750 | else if (!strcmp(desc->virtflag, "vmx")) |
5dd7507c CQ |
1751 | print_s(_("Virtualization:"), "VT-x"); |
1752 | } | |
10829cd7 HC |
1753 | if (desc->hypervisor) |
1754 | print_s(_("Hypervisor:"), desc->hypervisor); | |
e8aa16ee KZ |
1755 | if (desc->hyper) { |
1756 | print_s(_("Hypervisor vendor:"), hv_vendors[desc->hyper]); | |
50c6ee4c | 1757 | print_s(_("Virtualization type:"), _(virt_types[desc->virtype])); |
c8b64f6d | 1758 | } |
a0fff77e | 1759 | if (desc->dispatching >= 0) |
50c6ee4c | 1760 | print_s(_("Dispatching mode:"), _(disp_modes[desc->dispatching])); |
7e03f383 | 1761 | if (desc->ncaches) { |
2d7bee25 | 1762 | char cbuf[512]; |
5dd7507c | 1763 | |
7e03f383 | 1764 | for (i = desc->ncaches - 1; i >= 0; i--) { |
2d7bee25 | 1765 | snprintf(cbuf, sizeof(cbuf), |
7e03f383 | 1766 | _("%s cache:"), desc->caches[i].name); |
2d7bee25 | 1767 | print_s(cbuf, desc->caches[i].size); |
7e03f383 KZ |
1768 | } |
1769 | } | |
1770 | ||
4f912c6a | 1771 | for (i = 0; i < desc->nnodes; i++) { |
e9074a16 | 1772 | snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), desc->idx2nodenum[i]); |
8005924a | 1773 | print_cpuset(buf, desc->nodemaps[i], mod->hex); |
5dd7507c | 1774 | } |
ee1f1057 WC |
1775 | |
1776 | if (desc->flags) | |
1777 | print_s(_("Flags:"), desc->flags); | |
bd9b94d1 SB |
1778 | |
1779 | if (desc->physsockets) { | |
1780 | print_n(_("Physical sockets:"), desc->physsockets); | |
1781 | print_n(_("Physical chips:"), desc->physchips); | |
1782 | print_n(_("Physical cores/chip:"), desc->physcoresperchip); | |
1783 | } | |
5dd7507c CQ |
1784 | } |
1785 | ||
39561c70 | 1786 | static void __attribute__((__noreturn__)) usage(FILE *out) |
5dd7507c | 1787 | { |
b9d18bc3 KZ |
1788 | size_t i; |
1789 | ||
1790 | fputs(USAGE_HEADER, out); | |
c6f095cf | 1791 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); |
7f1ec5e8 | 1792 | |
451dbcfa BS |
1793 | fputs(USAGE_SEPARATOR, out); |
1794 | fputs(_("Display information about the CPU architecture.\n"), out); | |
1795 | ||
b9d18bc3 | 1796 | fputs(USAGE_OPTIONS, out); |
c6f095cf BS |
1797 | fputs(_(" -a, --all print both online and offline CPUs (default for -e)\n"), out); |
1798 | fputs(_(" -b, --online print online CPUs only (default for -p)\n"), out); | |
1799 | fputs(_(" -c, --offline print offline CPUs only\n"), out); | |
1800 | fputs(_(" -e, --extended[=<list>] print out an extended readable format\n"), out); | |
1801 | fputs(_(" -p, --parse[=<list>] print out a parsable format\n"), out); | |
1802 | fputs(_(" -s, --sysroot <dir> use specified directory as system root\n"), out); | |
1803 | fputs(_(" -x, --hex print hexadecimal masks rather than lists of CPUs\n"), out); | |
1804 | fputs(USAGE_SEPARATOR, out); | |
1805 | fputs(USAGE_HELP, out); | |
1806 | fputs(USAGE_VERSION, out); | |
b9d18bc3 KZ |
1807 | |
1808 | fprintf(out, _("\nAvailable columns:\n")); | |
1809 | ||
3d27b76a KZ |
1810 | for (i = 0; i < ARRAY_SIZE(coldescs); i++) |
1811 | fprintf(out, " %13s %s\n", coldescs[i].name, _(coldescs[i].help)); | |
1812 | ||
a587cc55 | 1813 | fprintf(out, USAGE_MAN_TAIL("lscpu(1)")); |
4f912c6a | 1814 | |
39561c70 | 1815 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); |
5dd7507c CQ |
1816 | } |
1817 | ||
1818 | int main(int argc, char *argv[]) | |
1819 | { | |
8005924a KZ |
1820 | struct lscpu_modifier _mod = { .mode = OUTPUT_SUMMARY }, *mod = &_mod; |
1821 | struct lscpu_desc _desc = { .flags = 0 }, *desc = &_desc; | |
1822 | int c, i; | |
3d27b76a | 1823 | int columns[ARRAY_SIZE(coldescs)], ncolumns = 0; |
7fc12cd2 | 1824 | int cpu_modifier_specified = 0; |
5dd7507c | 1825 | |
6c7d5ae9 | 1826 | static const struct option longopts[] = { |
0ad29ff6 | 1827 | { "all", no_argument, 0, 'a' }, |
23e9e95a | 1828 | { "online", no_argument, 0, 'b' }, |
7afc2387 | 1829 | { "offline", no_argument, 0, 'c' }, |
47b6e8b6 | 1830 | { "help", no_argument, 0, 'h' }, |
ba45d8c1 | 1831 | { "extended", optional_argument, 0, 'e' }, |
477251f8 | 1832 | { "parse", optional_argument, 0, 'p' }, |
47b6e8b6 | 1833 | { "sysroot", required_argument, 0, 's' }, |
4f912c6a | 1834 | { "hex", no_argument, 0, 'x' }, |
44de912c | 1835 | { "version", no_argument, 0, 'V' }, |
5dd7507c CQ |
1836 | { NULL, 0, 0, 0 } |
1837 | }; | |
1838 | ||
8e97eb4b KZ |
1839 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
1840 | { 'a','b','c' }, | |
1841 | { 'e','p' }, | |
1842 | { 0 } | |
1843 | }; | |
1844 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
1845 | ||
2f8f1388 | 1846 | setlocale(LC_ALL, ""); |
5dd7507c CQ |
1847 | bindtextdomain(PACKAGE, LOCALEDIR); |
1848 | textdomain(PACKAGE); | |
efb8854f | 1849 | atexit(close_stdout); |
5dd7507c | 1850 | |
7afc2387 | 1851 | while ((c = getopt_long(argc, argv, "abce::hp::s:xV", longopts, NULL)) != -1) { |
8e97eb4b KZ |
1852 | |
1853 | err_exclusive_options(c, longopts, excl, excl_st); | |
1854 | ||
5dd7507c | 1855 | switch (c) { |
0ad29ff6 | 1856 | case 'a': |
7afc2387 | 1857 | mod->online = mod->offline = 1; |
7fc12cd2 | 1858 | cpu_modifier_specified = 1; |
0ad29ff6 | 1859 | break; |
23e9e95a KZ |
1860 | case 'b': |
1861 | mod->online = 1; | |
7fc12cd2 | 1862 | cpu_modifier_specified = 1; |
23e9e95a | 1863 | break; |
7afc2387 HC |
1864 | case 'c': |
1865 | mod->offline = 1; | |
7fc12cd2 | 1866 | cpu_modifier_specified = 1; |
7afc2387 | 1867 | break; |
5dd7507c | 1868 | case 'h': |
39561c70 | 1869 | usage(stdout); |
5dd7507c | 1870 | case 'p': |
ba45d8c1 | 1871 | case 'e': |
477251f8 KZ |
1872 | if (optarg) { |
1873 | if (*optarg == '=') | |
1874 | optarg++; | |
1875 | ncolumns = string_to_idarray(optarg, | |
1876 | columns, ARRAY_SIZE(columns), | |
1877 | column_name_to_id); | |
1878 | if (ncolumns < 0) | |
1879 | return EXIT_FAILURE; | |
477251f8 | 1880 | } |
ba45d8c1 | 1881 | mod->mode = c == 'p' ? OUTPUT_PARSABLE : OUTPUT_READABLE; |
5dd7507c | 1882 | break; |
47b6e8b6 | 1883 | case 's': |
ca01695b | 1884 | path_set_prefix(optarg); |
8148217b | 1885 | mod->system = SYSTEM_SNAPSHOT; |
47b6e8b6 | 1886 | break; |
4f912c6a | 1887 | case 'x': |
8005924a | 1888 | mod->hex = 1; |
4f912c6a | 1889 | break; |
44de912c | 1890 | case 'V': |
f6277500 | 1891 | printf(UTIL_LINUX_VERSION); |
44de912c | 1892 | return EXIT_SUCCESS; |
5dd7507c | 1893 | default: |
39561c70 | 1894 | usage(stderr); |
5dd7507c CQ |
1895 | } |
1896 | } | |
7bbb7829 | 1897 | |
7fc12cd2 HC |
1898 | if (cpu_modifier_specified && mod->mode == OUTPUT_SUMMARY) { |
1899 | fprintf(stderr, | |
1900 | _("%s: options --all, --online and --offline may only " | |
ac56e555 | 1901 | "be used with options --extended or --parse.\n"), |
7fc12cd2 HC |
1902 | program_invocation_short_name); |
1903 | return EXIT_FAILURE; | |
1904 | } | |
1905 | ||
7bbb7829 HC |
1906 | if (argc != optind) |
1907 | usage(stderr); | |
1908 | ||
7afc2387 HC |
1909 | /* set default cpu display mode if none was specified */ |
1910 | if (!mod->online && !mod->offline) { | |
1911 | mod->online = 1; | |
1912 | mod->offline = mod->mode == OUTPUT_READABLE ? 1 : 0; | |
1913 | } | |
5dd7507c | 1914 | |
8148217b | 1915 | read_basicinfo(desc, mod); |
5dd7507c | 1916 | |
a5cfffff | 1917 | for (i = 0; i < desc->ncpuspos; i++) { |
7e03f383 KZ |
1918 | read_topology(desc, i); |
1919 | read_cache(desc, i); | |
2b8fcb81 | 1920 | read_polarization(desc, i); |
596b8845 | 1921 | read_address(desc, i); |
d231eea1 | 1922 | read_configured(desc, i); |
44320710 | 1923 | read_max_mhz(desc, i); |
e065a597 | 1924 | read_min_mhz(desc, i); |
47b6e8b6 | 1925 | } |
7e03f383 | 1926 | |
960bf130 KZ |
1927 | if (desc->caches) |
1928 | qsort(desc->caches, desc->ncaches, | |
1929 | sizeof(struct cpu_cache), cachecmp); | |
7e03f383 KZ |
1930 | |
1931 | read_nodes(desc); | |
eff79ceb | 1932 | read_hypervisor(desc, mod); |
c8b64f6d | 1933 | |
8005924a | 1934 | switch(mod->mode) { |
ba45d8c1 KZ |
1935 | case OUTPUT_SUMMARY: |
1936 | print_summary(desc, mod); | |
1937 | break; | |
1938 | case OUTPUT_PARSABLE: | |
1939 | if (!ncolumns) { | |
1940 | columns[ncolumns++] = COL_CPU; | |
1941 | columns[ncolumns++] = COL_CORE; | |
1942 | columns[ncolumns++] = COL_SOCKET; | |
1943 | columns[ncolumns++] = COL_NODE; | |
1944 | columns[ncolumns++] = COL_CACHE; | |
1945 | mod->compat = 1; | |
1946 | } | |
1947 | print_parsable(desc, columns, ncolumns, mod); | |
1948 | break; | |
1949 | case OUTPUT_READABLE: | |
1950 | if (!ncolumns) { | |
1951 | /* No list was given. Just print whatever is there. */ | |
1952 | columns[ncolumns++] = COL_CPU; | |
1953 | if (desc->nodemaps) | |
8005924a | 1954 | columns[ncolumns++] = COL_NODE; |
b3adf6ef HC |
1955 | if (desc->drawermaps) |
1956 | columns[ncolumns++] = COL_DRAWER; | |
ba45d8c1 KZ |
1957 | if (desc->bookmaps) |
1958 | columns[ncolumns++] = COL_BOOK; | |
1959 | if (desc->socketmaps) | |
1960 | columns[ncolumns++] = COL_SOCKET; | |
1961 | if (desc->coremaps) | |
1962 | columns[ncolumns++] = COL_CORE; | |
1963 | if (desc->caches) | |
8005924a | 1964 | columns[ncolumns++] = COL_CACHE; |
a7e5300c HC |
1965 | if (desc->online) |
1966 | columns[ncolumns++] = COL_ONLINE; | |
d231eea1 HC |
1967 | if (desc->configured) |
1968 | columns[ncolumns++] = COL_CONFIGURED; | |
ba45d8c1 KZ |
1969 | if (desc->polarization) |
1970 | columns[ncolumns++] = COL_POLARIZATION; | |
1971 | if (desc->addresses) | |
1972 | columns[ncolumns++] = COL_ADDRESS; | |
e065a597 DB |
1973 | if (desc->maxmhz) |
1974 | columns[ncolumns++] = COL_MAXMHZ; | |
1975 | if (desc->minmhz) | |
1976 | columns[ncolumns++] = COL_MINMHZ; | |
ba45d8c1 KZ |
1977 | } |
1978 | print_readable(desc, columns, ncolumns, mod); | |
1979 | break; | |
8005924a | 1980 | } |
5dd7507c | 1981 | |
cf474aac | 1982 | return EXIT_SUCCESS; |
5dd7507c | 1983 | } |