]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lscpu.h
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / lscpu.h
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Copyright (C) 2013 Ondrej Oprala <ooprala@redhat.com>
10 * Copyright (C) 2013-2023 Karel Zak <kzak@redhat.com>
11 */
12 #ifndef LSCPU_H
13 #define LSCPU_H
14
15 #include "c.h"
16 #include "nls.h"
17 #include "cpuset.h"
18 #include "xalloc.h"
19 #include "strutils.h"
20 #include "bitops.h"
21 #include "path.h"
22 #include "pathnames.h"
23 #include "all-io.h"
24 #include "debug.h"
25
26 #define LSCPU_DEBUG_INIT (1 << 1)
27 #define LSCPU_DEBUG_MISC (1 << 2)
28 #define LSCPU_DEBUG_GATHER (1 << 3)
29 #define LSCPU_DEBUG_TYPE (1 << 4)
30 #define LSCPU_DEBUG_CPU (1 << 5)
31 #define LSCPU_DEBUG_VIRT (1 << 6)
32 #define LSBLK_DEBUG_ALL 0xFFFF
33
34 UL_DEBUG_DECLARE_MASK(lscpu);
35 #define DBG(m, x) __UL_DBG(lscpu, LSCPU_DEBUG_, m, x)
36 #define ON_DBG(m, x) __UL_DBG_CALL(lscpu, LSCPU_DEBUG_, m, x)
37
38 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lscpu)
39 #include "debugobj.h"
40
41 #define _PATH_SYS_SYSTEM "/sys/devices/system"
42 #define _PATH_SYS_HYP_FEATURES "/sys/hypervisor/properties/features"
43 #define _PATH_SYS_CPU _PATH_SYS_SYSTEM "/cpu"
44 #define _PATH_SYS_NODE _PATH_SYS_SYSTEM "/node"
45 #define _PATH_SYS_DMI "/sys/firmware/dmi/tables/DMI"
46 #define _PATH_ACPI_PPTT "/sys/firmware/acpi/tables/PPTT"
47
48 struct lscpu_cache {
49 int id; /* unique identifier */
50 int nth; /* cache<number> from cpuinfo */
51 char *name;
52 char *type;
53 char *allocation_policy;
54 char *write_policy;
55
56 int level;
57 uint64_t size;
58
59 unsigned int ways_of_associativity;
60 unsigned int physical_line_partition;
61 unsigned int number_of_sets;
62 unsigned int coherency_line_size;
63
64 cpu_set_t *sharedmap;
65 };
66
67 struct lscpu_cputype {
68 int refcount;
69
70 char *vendor;
71 int vendor_id; /* created by lscpu_decode_arm() */
72 char *bios_vendor; /* aarch64 */
73 char *machinetype; /* s390 */
74 char *family;
75 char *model;
76 char *modelname;
77 char *bios_modelname; /* aarch64 */
78 char *bios_family; /* aarch64 */
79 char *revision; /* alternative for model (ppc) */
80 char *stepping;
81 char *bogomips;
82 char *flags;
83 char *mtid; /* maximum thread id (s390) */
84 char *addrsz; /* address sizes */
85 int dispatching; /* -1 if not evailable, DIST_* */
86 int freqboost; /* -1 if not evailable */
87
88 size_t physsockets; /* Physical sockets (modules) */
89 size_t physchips; /* Physical chips */
90 size_t physcoresperchip; /* Physical cores per chip */
91
92 size_t nthreads_per_core;
93 size_t ncores_per_socket;
94 size_t nsockets_per_book;
95 size_t nbooks_per_drawer;
96 size_t ndrawers_per_system;
97
98 char *dynamic_mhz; /* s390; copy from the first CPU */
99 char *static_mhz; /* s390; copy from the first CPU */
100
101 /* siblings maps */
102 size_t ncores;
103 cpu_set_t **coremaps;
104 size_t nsockets;
105 cpu_set_t **socketmaps;
106 size_t nbooks;
107 cpu_set_t **bookmaps;
108 size_t ndrawers;
109 cpu_set_t **drawermaps;
110
111 unsigned int has_freq : 1,
112 has_configured : 1,
113 has_polarization : 1,
114 has_addresses : 1;
115
116 size_t nr_socket_on_cluster; /* the number of sockets if the is_cluster is 1 */
117
118 char *isa; /* loongarch */
119 };
120
121 /* dispatching modes */
122 enum {
123 DISP_HORIZONTAL = 0,
124 DISP_VERTICAL = 1
125 };
126
127 /* cpu polarization */
128 enum {
129 POLAR_UNKNOWN = 0,
130 POLAR_VLOW,
131 POLAR_VMEDIUM,
132 POLAR_VHIGH,
133 POLAR_HORIZONTAL
134 };
135
136 struct lscpu_cpu {
137 int refcount;
138 struct lscpu_cputype *type;
139
140 int logical_id;
141
142 char *bogomips; /* per-CPU bogomips */
143 char *mhz; /* freq from cpuinfo */
144 char *dynamic_mhz; /* from cpuinf for s390 */
145 char *static_mhz; /* from cpuinf for s390 */
146 float mhz_max_freq; /* realtime freq from /sys/.../cpuinfo_max_freq */
147 float mhz_min_freq; /* realtime freq from /sys/.../cpuinfo_min_freq */
148 float mhz_cur_freq;
149
150 int coreid;
151 int socketid;
152 int bookid;
153 int drawerid;
154
155 int polarization; /* POLAR_* */
156 int address; /* physical cpu address */
157 int configured; /* cpu configured */
158 };
159
160 struct lscpu_arch {
161 char *name; /* uname() .machine */
162
163 unsigned int bit32:1,
164 bit64:1;
165 };
166
167 struct lscpu_vulnerability {
168 char *name;
169 char *text;
170 };
171
172 /* virtualization types */
173 enum {
174 VIRT_TYPE_NONE = 0,
175 VIRT_TYPE_PARA,
176 VIRT_TYPE_FULL,
177 VIRT_TYPE_CONTAINER
178 };
179
180 /* hypervisor vendors */
181 enum {
182 VIRT_VENDOR_NONE = 0,
183 VIRT_VENDOR_XEN,
184 VIRT_VENDOR_KVM,
185 VIRT_VENDOR_MSHV,
186 VIRT_VENDOR_VMWARE,
187 VIRT_VENDOR_IBM, /* sys-z powervm */
188 VIRT_VENDOR_VSERVER,
189 VIRT_VENDOR_UML,
190 VIRT_VENDOR_INNOTEK, /* VBOX */
191 VIRT_VENDOR_HITACHI,
192 VIRT_VENDOR_PARALLELS, /* OpenVZ/VIrtuozzo */
193 VIRT_VENDOR_VBOX,
194 VIRT_VENDOR_OS400,
195 VIRT_VENDOR_PHYP,
196 VIRT_VENDOR_SPAR,
197 VIRT_VENDOR_WSL,
198 };
199
200 struct lscpu_virt {
201 char *cpuflag; /* virtualization flag (vmx, svm) */
202 char *hypervisor; /* hypervisor software */
203 int vendor; /* VIRT_VENDOR_* */
204 int type; /* VIRT_TYPE_* ? */
205
206 };
207
208 enum {
209 LSCPU_OUTPUT_SUMMARY = 0, /* default */
210 LSCPU_OUTPUT_CACHES,
211 LSCPU_OUTPUT_PARSABLE,
212 LSCPU_OUTPUT_READABLE
213 };
214
215 struct lscpu_cxt {
216 int maxcpus; /* size in bits of kernel cpu mask */
217 size_t setsize;
218 const char *prefix; /* path to /sys and /proc snapshot or NULL */
219
220 struct path_cxt *syscpu; /* _PATH_SYS_CPU path handler */
221 struct path_cxt *procfs; /* /proc path handler */
222 struct path_cxt *rootfs; /* / path handler */
223
224 size_t ncputypes;
225 struct lscpu_cputype **cputypes;
226
227 size_t npossibles; /* number of possible CPUs */
228 struct lscpu_cpu **cpus; /* possible CPUs, contains gaps (cups[n]=NULL) */
229
230 size_t npresents;
231 cpu_set_t *present; /* mask with present CPUs */
232
233 size_t nonlines; /* aka number of trhreads */
234 cpu_set_t *online; /* mask with online CPUs */
235
236 struct lscpu_arch *arch;
237 struct lscpu_virt *virt;
238
239 struct lscpu_vulnerability *vuls; /* array of CPU vulnerabilities */
240 size_t nvuls; /* number of CPU vulnerabilities */
241
242 struct lscpu_cache *caches; /* all instances of the all caches from /sys */
243 size_t ncaches;
244
245 struct lscpu_cache *ecaches;
246 size_t necaches; /* extra caches (s390) from /proc/cpuinfo */
247
248 size_t nnodes; /* number of NUMA modes */
249 int *idx2nodenum; /* Support for discontinuous nodes */
250 cpu_set_t **nodemaps; /* array with NUMA nodes */
251
252 int mode; /* LSCPU_OUTPUT_* */
253
254 unsigned int noalive : 1,
255 show_online : 1,
256 show_offline : 1,
257 show_physical : 1,
258 show_compatible : 1,
259 hex : 1,
260 json : 1,
261 bytes : 1;
262
263 int is_cluster; /* For aarch64 if the machine doesn't have ACPI PPTT */
264 };
265
266 #define is_cpu_online(_cxt, _cpu) \
267 ((_cxt) && (_cpu) && (_cxt)->online && \
268 CPU_ISSET_S((_cpu)->logical_id, (_cxt)->setsize, (_cxt)->online))
269
270 #define is_cpu_present(_cxt, _cpu) \
271 ((_cxt) && (_cpu) && (_cxt)->present && \
272 CPU_ISSET_S((_cpu)->logical_id, (_cxt)->setsize, (_cxt)->present))
273
274 struct lscpu_cputype *lscpu_new_cputype(void);
275 void lscpu_ref_cputype(struct lscpu_cputype *ct);
276 void lscpu_unref_cputype(struct lscpu_cputype *ct);
277 struct lscpu_cputype *lscpu_add_cputype(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
278 struct lscpu_cputype *lscpu_cputype_get_default(struct lscpu_cxt *cxt);
279
280 int lscpu_read_cpuinfo(struct lscpu_cxt *cxt);
281 int lscpu_read_cpulists(struct lscpu_cxt *cxt);
282 int lscpu_read_archext(struct lscpu_cxt *cxt);
283 int lscpu_read_vulnerabilities(struct lscpu_cxt *cxt);
284 int lscpu_read_numas(struct lscpu_cxt *cxt);
285
286 void lscpu_free_caches(struct lscpu_cache *caches, size_t n);
287 void lscpu_sort_caches(struct lscpu_cache *caches, size_t n);
288
289 size_t lscpu_get_cache_full_size(struct lscpu_cxt *cxt, const char *name, int *instances);
290 struct lscpu_cache *lscpu_cpu_get_cache(struct lscpu_cxt *cxt,
291 struct lscpu_cpu *cpu, const char *name);
292
293 int lscpu_read_topology(struct lscpu_cxt *cxt);
294 void lscpu_cputype_free_topology(struct lscpu_cputype *ct);
295
296 float lsblk_cputype_get_maxmhz(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
297 float lsblk_cputype_get_minmhz(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
298 float lsblk_cputype_get_scalmhz(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
299
300 struct lscpu_arch *lscpu_read_architecture(struct lscpu_cxt *cxt);
301 void lscpu_free_architecture(struct lscpu_arch *ar);
302
303 struct lscpu_virt *lscpu_read_virtualization(struct lscpu_cxt *cxt);
304 void lscpu_free_virtualization(struct lscpu_virt *virt);
305
306 struct lscpu_cpu *lscpu_new_cpu(int id);
307 void lscpu_ref_cpu(struct lscpu_cpu *cpu);
308 void lscpu_unref_cpu(struct lscpu_cpu *cpu);
309 struct lscpu_cpu *lscpu_get_cpu(struct lscpu_cxt *cxt, int logical_id);
310 int lscpu_cpu_set_type(struct lscpu_cpu *cpu, struct lscpu_cputype *type);
311 int lscpu_create_cpus(struct lscpu_cxt *cxt, cpu_set_t *cpuset, size_t setsize);
312 struct lscpu_cpu *lscpu_cpus_loopup_by_type(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
313
314 void lscpu_decode_arm(struct lscpu_cxt *cxt);
315
316 int lookup(char *line, char *pattern, char **value);
317
318 void *get_mem_chunk(size_t base, size_t len, const char *devmem);
319
320 struct lscpu_dmi_header
321 {
322 uint8_t type;
323 uint8_t length;
324 uint16_t handle;
325 uint8_t *data;
326 };
327
328 struct dmi_info {
329 char *vendor;
330 char *product;
331 char *manufacturer;
332 int sockets;
333
334 /* Processor Information */
335 uint16_t processor_family;
336 char *processor_manufacturer;
337 char *processor_version;
338 uint16_t current_speed;
339 char *part_num;
340 };
341
342
343 void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data);
344 char *dmi_string(const struct lscpu_dmi_header *dm, uint8_t s);
345 int parse_dmi_table(uint16_t len, uint16_t num, uint8_t *data, struct dmi_info *di);
346 size_t get_number_of_physical_sockets_from_dmi(void);
347 int dmi_decode_cputype(struct lscpu_cputype *);
348 #endif /* LSCPU_H */