]>
Commit | Line | Data |
---|---|---|
405d51ed | 1 | |
d34b7f1b KZ |
2 | #include <sys/utsname.h> |
3 | #include <sys/personality.h> | |
4 | ||
095be2c2 | 5 | #include "lscpu.h" |
450cb0b7 | 6 | |
0796923a KZ |
7 | #include "fileutils.h" |
8 | ||
5a6da72c KZ |
9 | /* Lookup a pattern and get the value for format "<pattern> : <key>" |
10 | */ | |
909d3575 | 11 | int lookup(char *line, char *pattern, char **value) |
5a6da72c KZ |
12 | { |
13 | char *p, *v; | |
14 | int len = strlen(pattern); | |
15 | ||
16 | /* don't re-fill already found tags, first one wins */ | |
17 | if (!*line || *value) | |
18 | return 0; | |
19 | /* pattern */ | |
20 | if (strncmp(line, pattern, len)) | |
21 | return 0; | |
22 | /* white spaces */ | |
23 | for (p = line + len; isspace(*p); p++); | |
24 | ||
25 | /* separator */ | |
26 | if (*p != ':') | |
27 | return 0; | |
28 | /* white spaces */ | |
29 | for (++p; isspace(*p); p++); | |
30 | ||
31 | /* value */ | |
32 | if (!*p) | |
33 | return 0; | |
34 | v = p; | |
35 | ||
36 | /* end of value */ | |
37 | len = strlen(line) - 1; | |
38 | for (p = line + len; isspace(*(p-1)); p--); | |
39 | *p = '\0'; | |
40 | ||
41 | *value = xstrdup(v); | |
42 | return 1; | |
43 | } | |
44 | ||
71464ded | 45 | struct lscpu_cputype *lscpu_new_cputype(void) |
405d51ed KZ |
46 | { |
47 | struct lscpu_cputype *ct; | |
48 | ||
49 | ct = xcalloc(1, sizeof(struct lscpu_cputype)); | |
50 | ct->refcount = 1; | |
91eef60c KZ |
51 | ct->dispatching = -1; |
52 | ct->freqboost = -1; | |
405d51ed | 53 | |
450cb0b7 | 54 | DBG(TYPE, ul_debugobj(ct, "alloc")); |
405d51ed KZ |
55 | return ct; |
56 | } | |
57 | ||
71464ded KZ |
58 | void lscpu_ref_cputype(struct lscpu_cputype *ct) |
59 | { | |
f5d13532 | 60 | if (ct) { |
71464ded | 61 | ct->refcount++; |
ee0fabda | 62 | DBG(TYPE, ul_debugobj(ct, ">>> ref %d", ct->refcount)); |
f5d13532 | 63 | } |
71464ded KZ |
64 | } |
65 | ||
66 | void lscpu_unref_cputype(struct lscpu_cputype *ct) | |
67 | { | |
68 | if (!ct) | |
69 | return; | |
70 | ||
ff7449d2 | 71 | /*DBG(TYPE, ul_debugobj(ct, ">>> unref %d", ct->refcount - 1));*/ |
f5d13532 | 72 | |
71464ded | 73 | if (--ct->refcount <= 0) { |
ff7449d2 | 74 | DBG(TYPE, ul_debugobj(ct, " freeing %s/%s", ct->vendor, ct->model)); |
f669523b | 75 | lscpu_cputype_free_topology(ct); |
b152ec54 KZ |
76 | free(ct->vendor); |
77 | free(ct->machinetype); /* s390 */ | |
78 | free(ct->family); | |
79 | free(ct->model); | |
80 | free(ct->modelname); | |
81 | free(ct->revision); /* alternative for model (ppc) */ | |
b152ec54 KZ |
82 | free(ct->stepping); |
83 | free(ct->bogomips); | |
84 | free(ct->flags); | |
85 | free(ct->mtid); /* maximum thread id (s390) */ | |
86 | free(ct->addrsz); /* address sizes */ | |
778254d9 KZ |
87 | free(ct->static_mhz); |
88 | free(ct->dynamic_mhz); | |
71464ded KZ |
89 | free(ct); |
90 | } | |
91 | } | |
92 | ||
d234a381 KZ |
93 | struct lscpu_cputype *lscpu_cputype_get_default(struct lscpu_cxt *cxt) |
94 | { | |
95 | return cxt->cputypes ? cxt->cputypes[0] : NULL; | |
96 | } | |
855bab2a KZ |
97 | |
98 | #define match(astr, bstr) \ | |
99 | ((!astr && !bstr) || (astr && bstr && strcmp(astr, bstr) == 0)) | |
100 | ||
f5d13532 KZ |
101 | struct lscpu_cputype *lscpu_add_cputype(struct lscpu_cxt *cxt, struct lscpu_cputype *ct) |
102 | { | |
d234a381 | 103 | DBG(TYPE, ul_debugobj(ct, "add new")); |
855bab2a KZ |
104 | cxt->cputypes = xrealloc(cxt->cputypes, (cxt->ncputypes + 1) |
105 | * sizeof(struct lscpu_cputype *)); | |
106 | cxt->cputypes[cxt->ncputypes] = ct; | |
107 | cxt->ncputypes++; | |
108 | lscpu_ref_cputype(ct); | |
109 | return ct; | |
110 | } | |
111 | ||
f495abb7 KZ |
112 | static void fprintf_cputypes(FILE *f, struct lscpu_cxt *cxt) |
113 | { | |
114 | size_t i; | |
115 | ||
116 | for (i = 0; i < cxt->ncputypes; i++) { | |
117 | struct lscpu_cputype *ct = cxt->cputypes[i]; | |
118 | ||
119 | fprintf(f, "\n vendor: %s\n", ct->vendor); | |
120 | fprintf(f, " machinetype: %s\n", ct->machinetype); | |
121 | fprintf(f, " family: %s\n", ct->family); | |
122 | fprintf(f, " model: %s\n", ct->model); | |
123 | fprintf(f, " modelname: %s\n", ct->modelname); | |
124 | fprintf(f, " revision: %s\n", ct->revision); | |
125 | fprintf(f, " stepping: %s\n", ct->stepping); | |
126 | fprintf(f, " mtid: %s\n", ct->mtid); | |
127 | fprintf(f, " addrsz: %s\n", ct->addrsz); | |
128 | } | |
129 | } | |
f5d13532 KZ |
130 | |
131 | enum { | |
132 | CPUINFO_LINE_UNKNOWN, /* unknown line */ | |
133 | CPUINFO_LINE_CPUTYPE, /* line found in type_patterns[] */ | |
134 | CPUINFO_LINE_CPU, /* line found in cpu_patterns[] */ | |
135 | CPUINFO_LINE_CACHE /* line found in cache_pattern[] */ | |
136 | }; | |
137 | ||
cbf8f129 KZ |
138 | /* Describes /proc/cpuinfo fields */ |
139 | struct cpuinfo_pattern { | |
140 | int id; /* field ID */ | |
f5d13532 | 141 | int domain; /* CPUINFO_LINE_* */ |
cbf8f129 KZ |
142 | const char *pattern; /* field name as used in /proc/cpuinfo */ |
143 | size_t offset; /* offset in lscpu_cputype or lscpu_cpu struct */ | |
144 | }; | |
145 | ||
146 | /* field identifiers (field name may be different on different archs) */ | |
147 | enum { | |
148 | PAT_ADDRESS_SIZES, | |
cc94324e KZ |
149 | PAT_BOGOMIPS, /* global */ |
150 | PAT_BOGOMIPS_CPU, /* per-cpu */ | |
cbf8f129 KZ |
151 | PAT_CPU, |
152 | PAT_FAMILY, | |
153 | PAT_FEATURES, | |
154 | PAT_FLAGS, | |
155 | PAT_IMPLEMENTER, | |
156 | PAT_MAX_THREAD_ID, | |
157 | PAT_MHZ, | |
158 | PAT_MHZ_DYNAMIC, | |
159 | PAT_MHZ_STATIC, | |
160 | PAT_MODEL, | |
161 | PAT_MODEL_NAME, | |
162 | PAT_PART, | |
163 | PAT_PROCESSOR, | |
164 | PAT_REVISION, | |
165 | PAT_STEPPING, | |
166 | PAT_TYPE, | |
167 | PAT_VARIANT, | |
168 | PAT_VENDOR, | |
25c2a72c | 169 | PAT_CACHE |
329ec35f KZ |
170 | }; |
171 | ||
cbf8f129 KZ |
172 | /* |
173 | * /proc/cpuinfo to lscpu_cputype conversion | |
174 | */ | |
175 | #define DEF_PAT_CPUTYPE(_str, _id, _member) \ | |
329ec35f | 176 | { \ |
cbf8f129 | 177 | .id = (_id), \ |
f5d13532 | 178 | .domain = CPUINFO_LINE_CPUTYPE, \ |
329ec35f | 179 | .pattern = (_str), \ |
cbf8f129 | 180 | .offset = offsetof(struct lscpu_cputype, _member), \ |
329ec35f KZ |
181 | } |
182 | ||
cbf8f129 | 183 | static const struct cpuinfo_pattern type_patterns[] = |
329ec35f | 184 | { |
cbf8f129 | 185 | /* Sort by fields name! */ |
60e7ccb8 | 186 | DEF_PAT_CPUTYPE( "ASEs implemented", PAT_FLAGS, flags), /* mips */ |
cbf8f129 KZ |
187 | DEF_PAT_CPUTYPE( "BogoMIPS", PAT_BOGOMIPS, bogomips), /* aarch64 */ |
188 | DEF_PAT_CPUTYPE( "CPU implementer", PAT_IMPLEMENTER,vendor), /* ARM and aarch64 */ | |
189 | DEF_PAT_CPUTYPE( "CPU part", PAT_PART, model), /* ARM and aarch64 */ | |
190 | DEF_PAT_CPUTYPE( "CPU revision", PAT_REVISION, revision), /* aarch64 */ | |
191 | DEF_PAT_CPUTYPE( "CPU variant", PAT_VARIANT, stepping), /* aarch64 */ | |
192 | DEF_PAT_CPUTYPE( "Features", PAT_FEATURES, flags), /* aarch64 */ | |
193 | DEF_PAT_CPUTYPE( "address sizes", PAT_ADDRESS_SIZES, addrsz),/* x86 */ | |
cbf8f129 | 194 | DEF_PAT_CPUTYPE( "bogomips per cpu", PAT_BOGOMIPS, bogomips), /* s390 */ |
d234a381 | 195 | DEF_PAT_CPUTYPE( "cpu", PAT_CPU, modelname), /* ppc, sparc */ |
ee0fabda | 196 | DEF_PAT_CPUTYPE( "cpu family", PAT_FAMILY, family), |
60e7ccb8 | 197 | DEF_PAT_CPUTYPE( "cpu model", PAT_MODEL, model), /* mips */ |
cbf8f129 KZ |
198 | DEF_PAT_CPUTYPE( "family", PAT_FAMILY, family), |
199 | DEF_PAT_CPUTYPE( "features", PAT_FEATURES, flags), /* s390 */ | |
200 | DEF_PAT_CPUTYPE( "flags", PAT_FLAGS, flags), /* x86 */ | |
201 | DEF_PAT_CPUTYPE( "max thread id", PAT_MAX_THREAD_ID, mtid), /* s390 */ | |
cbf8f129 | 202 | DEF_PAT_CPUTYPE( "model", PAT_MODEL, model), |
ee0fabda | 203 | DEF_PAT_CPUTYPE( "model name", PAT_MODEL_NAME, modelname), |
cbf8f129 KZ |
204 | DEF_PAT_CPUTYPE( "revision", PAT_REVISION, revision), |
205 | DEF_PAT_CPUTYPE( "stepping", PAT_STEPPING, stepping), | |
206 | DEF_PAT_CPUTYPE( "type", PAT_TYPE, flags), /* sparc64 */ | |
207 | DEF_PAT_CPUTYPE( "vendor", PAT_VENDOR, vendor), | |
855bab2a | 208 | DEF_PAT_CPUTYPE( "vendor_id", PAT_VENDOR, vendor), /* s390 */ |
cbf8f129 KZ |
209 | }; |
210 | ||
211 | /* | |
212 | * /proc/cpuinfo to lscpu_cpu conversion | |
213 | */ | |
214 | #define DEF_PAT_CPU(_str, _id, _member) \ | |
215 | { \ | |
216 | .id = (_id), \ | |
f5d13532 | 217 | .domain = CPUINFO_LINE_CPU, \ |
cbf8f129 KZ |
218 | .pattern = (_str), \ |
219 | .offset = offsetof(struct lscpu_cpu, _member), \ | |
220 | } | |
221 | ||
222 | static const struct cpuinfo_pattern cpu_patterns[] = | |
223 | { | |
224 | /* Sort by fields name! */ | |
cc94324e | 225 | DEF_PAT_CPU( "bogomips", PAT_BOGOMIPS_CPU, bogomips), |
05b9c556 | 226 | DEF_PAT_CPU( "cpu MHz", PAT_MHZ, mhz), |
cc94324e KZ |
227 | DEF_PAT_CPU( "cpu MHz dynamic", PAT_MHZ_DYNAMIC, dynamic_mhz), /* s390 */ |
228 | DEF_PAT_CPU( "cpu MHz static", PAT_MHZ_STATIC, static_mhz), /* s390 */ | |
cc94324e KZ |
229 | DEF_PAT_CPU( "cpu number", PAT_PROCESSOR, logical_id), /* s390 */ |
230 | DEF_PAT_CPU( "processor", PAT_PROCESSOR, logical_id), | |
f5d13532 | 231 | |
329ec35f KZ |
232 | }; |
233 | ||
25c2a72c KZ |
234 | /* |
235 | * /proc/cpuinfo to lscpu_cache conversion | |
236 | */ | |
237 | #define DEF_PAT_CACHE(_str, _id) \ | |
238 | { \ | |
239 | .id = (_id), \ | |
240 | .domain = CPUINFO_LINE_CACHE, \ | |
241 | .pattern = (_str) \ | |
242 | } | |
243 | ||
244 | static const struct cpuinfo_pattern cache_patterns[] = | |
245 | { | |
246 | /* Sort by fields name! */ | |
247 | DEF_PAT_CACHE("cache", PAT_CACHE), | |
248 | }; | |
249 | ||
329ec35f KZ |
250 | #define CPUTYPE_PATTERN_BUFSZ 32 |
251 | ||
252 | static int cmp_pattern(const void *a0, const void *b0) | |
253 | { | |
cbf8f129 KZ |
254 | const struct cpuinfo_pattern |
255 | *a = (const struct cpuinfo_pattern *) a0, | |
256 | *b = (const struct cpuinfo_pattern *) b0; | |
329ec35f KZ |
257 | return strcmp(a->pattern, b->pattern); |
258 | } | |
259 | ||
f5d13532 KZ |
260 | struct cpuinfo_parser { |
261 | struct lscpu_cxt *cxt; | |
262 | struct lscpu_cpu *curr_cpu; | |
263 | struct lscpu_cputype *curr_type; | |
f495abb7 | 264 | unsigned int curr_type_added : 1; |
f5d13532 KZ |
265 | }; |
266 | ||
ee0fabda KZ |
267 | static int is_different_cputype(struct lscpu_cputype *ct, size_t offset, const char *value) |
268 | { | |
269 | switch (offset) { | |
270 | case offsetof(struct lscpu_cputype, vendor): | |
271 | return ct->vendor && value && strcmp(ct->vendor, value) != 0; | |
272 | case offsetof(struct lscpu_cputype, model): | |
273 | return ct->model && value && strcmp(ct->model, value) != 0; | |
274 | case offsetof(struct lscpu_cputype, modelname): | |
275 | return ct->modelname && value && strcmp(ct->modelname, value) != 0; | |
276 | case offsetof(struct lscpu_cputype, stepping): | |
277 | return ct->stepping && value && strcmp(ct->stepping, value) != 0; | |
278 | } | |
279 | return 0; | |
280 | } | |
281 | ||
25c2a72c | 282 | /* cannonicalize @str -- remove number at the end return the |
f5d13532 KZ |
283 | * number by @keynum. This is usable for example for "processor 5" or "cache1" |
284 | * cpuinfo lines */ | |
285 | static char *key_cleanup(char *str, int *keynum) | |
286 | { | |
287 | size_t sz = rtrim_whitespace((unsigned char *)str); | |
288 | size_t i; | |
289 | ||
290 | if (!sz) | |
291 | return str; | |
f5d13532 | 292 | |
25c2a72c KZ |
293 | for (i = sz; i > 0; i--) { |
294 | if (!isdigit(str[i - 1])) | |
295 | break; | |
296 | } | |
297 | ||
298 | if (i < sz) { | |
f5d13532 KZ |
299 | *keynum = atoi(str + i); |
300 | str[i] = '\0'; | |
25c2a72c | 301 | rtrim_whitespace((unsigned char *)str); |
f5d13532 KZ |
302 | } |
303 | return str; | |
304 | } | |
305 | ||
306 | static const struct cpuinfo_pattern *cpuinfo_parse_line(char *str, char **value, int *keynum) | |
329ec35f | 307 | { |
cbf8f129 | 308 | struct cpuinfo_pattern key, *pat; |
f5d13532 KZ |
309 | char *p, *v; |
310 | char buf[CPUTYPE_PATTERN_BUFSZ] = { 0 }; | |
329ec35f | 311 | |
d234a381 | 312 | DBG(GATHER, ul_debug("parse \"%s\"", str)); |
329ec35f KZ |
313 | |
314 | if (!str || !*str) | |
f5d13532 KZ |
315 | return NULL; |
316 | p = (char *) skip_blank(str); | |
329ec35f | 317 | if (!p || !*p) |
f5d13532 | 318 | return NULL; |
329ec35f KZ |
319 | |
320 | v = strchr(p, ':'); | |
321 | if (!v || !*v) | |
f5d13532 | 322 | return NULL; |
329ec35f | 323 | |
cbf8f129 | 324 | /* prepare name of the field */ |
329ec35f KZ |
325 | xstrncpy(buf, p, sizeof(buf)); |
326 | buf[v - p] = '\0'; | |
327 | v++; | |
328 | ||
801b125f | 329 | /* prepare value */ |
f5d13532 | 330 | v = (char *) skip_space(v); |
801b125f | 331 | if (!v || !*v) |
f5d13532 | 332 | return NULL; |
801b125f | 333 | |
f5d13532 | 334 | key.pattern = key_cleanup(buf, keynum); |
f5d13532 KZ |
335 | /* CPU-type */ |
336 | if ((pat = bsearch(&key, type_patterns, | |
cbf8f129 KZ |
337 | ARRAY_SIZE(type_patterns), |
338 | sizeof(struct cpuinfo_pattern), | |
f5d13532 KZ |
339 | cmp_pattern))) |
340 | goto found; | |
341 | ||
342 | /* CPU */ | |
343 | if ((pat = bsearch(&key, cpu_patterns, | |
cbf8f129 KZ |
344 | ARRAY_SIZE(cpu_patterns), |
345 | sizeof(struct cpuinfo_pattern), | |
f5d13532 KZ |
346 | cmp_pattern))) |
347 | goto found; | |
348 | ||
25c2a72c KZ |
349 | /* CACHE */ |
350 | if ((pat = bsearch(&key, cache_patterns, | |
351 | ARRAY_SIZE(cache_patterns), | |
352 | sizeof(struct cpuinfo_pattern), | |
353 | cmp_pattern))) | |
354 | goto found; | |
355 | ||
f5d13532 KZ |
356 | return NULL; |
357 | found: | |
358 | rtrim_whitespace((unsigned char *) v); | |
359 | *value = v; | |
360 | return pat; | |
329ec35f KZ |
361 | } |
362 | ||
25c2a72c KZ |
363 | /* Parse extra cache lines contained within /proc/cpuinfo but which are not |
364 | * part of the cache topology information within the sysfs filesystem. This is | |
365 | * true for all shared caches on e.g. s390. When there are layers of | |
366 | * hypervisors in between it is not knows which CPUs share which caches. | |
367 | * Therefore information about shared caches is only available in | |
368 | * /proc/cpuinfo. Format is: | |
369 | * | |
370 | * cache<nr> : level=<lvl> type=<type> scope=<scope> size=<size> line_size=<lsz> associativity=<as> | |
371 | * | |
372 | * the cache<nr> part is parsed in cpuinfo_parse_line, in this function parses part after ":". | |
373 | */ | |
374 | static int cpuinfo_parse_cache(struct lscpu_cxt *cxt, int keynum, char *data) | |
375 | { | |
376 | struct lscpu_cache *cache; | |
377 | long long size; | |
378 | char *p, type; | |
379 | int level; | |
ddc92e39 | 380 | unsigned int line_size, associativity; |
25c2a72c KZ |
381 | |
382 | DBG(GATHER, ul_debugobj(cxt, " parse cpuinfo cache '%s'", data)); | |
383 | ||
384 | p = strstr(data, "scope=") + 6; | |
385 | /* Skip private caches, also present in sysfs */ | |
386 | if (!p || strncmp(p, "Private", 7) == 0) | |
387 | return 0; | |
388 | p = strstr(data, "level="); | |
389 | if (!p || sscanf(p, "level=%d", &level) != 1) | |
390 | return 0; | |
391 | p = strstr(data, "type=") + 5; | |
392 | if (!p || !*p) | |
393 | return 0; | |
394 | type = 0; | |
395 | if (strncmp(p, "Data", 4) == 0) | |
396 | type = 'd'; | |
397 | else if (strncmp(p, "Instruction", 11) == 0) | |
398 | type = 'i'; | |
399 | else if (strncmp(p, "Unified", 7) == 0) | |
400 | type = 'u'; | |
401 | p = strstr(data, "size="); | |
402 | if (!p || sscanf(p, "size=%lld", &size) != 1) | |
ddc92e39 KZ |
403 | return 0; |
404 | ||
405 | p = strstr(data, "line_size="); | |
406 | if (!p || sscanf(p, "line_size=%u", &line_size) != 1) | |
407 | return 0; | |
408 | ||
409 | p = strstr(data, "associativity="); | |
410 | if (!p || sscanf(p, "associativity=%u", &associativity) != 1) | |
411 | return 0; | |
25c2a72c KZ |
412 | |
413 | cxt->necaches++; | |
414 | cxt->ecaches = xrealloc(cxt->ecaches, | |
415 | cxt->necaches * sizeof(struct lscpu_cache)); | |
416 | cache = &cxt->ecaches[cxt->necaches - 1]; | |
417 | memset(cache, 0 , sizeof(*cache)); | |
418 | ||
419 | if (type == 'i' || type == 'd') | |
420 | xasprintf(&cache->name, "L%d%c", level, type); | |
421 | else | |
422 | xasprintf(&cache->name, "L%d", level); | |
423 | ||
424 | cache->nth = keynum; | |
425 | cache->level = level; | |
426 | cache->size = size * 1024; | |
ddc92e39 KZ |
427 | cache->ways_of_associativity = associativity; |
428 | cache->coherency_line_size = line_size; | |
429 | /* Number of sets for s390. For safety, just check divide by zero */ | |
430 | cache->number_of_sets = line_size ? (cache->size / line_size): 0; | |
431 | cache->number_of_sets = associativity ? (cache->number_of_sets / associativity) : 0; | |
25c2a72c KZ |
432 | |
433 | cache->type = type == 'i' ? xstrdup("Instruction") : | |
434 | type == 'd' ? xstrdup("Data") : | |
435 | type == 'u' ? xstrdup("Unified") : NULL; | |
436 | return 1; | |
437 | } | |
438 | ||
cbf8f129 | 439 | int lscpu_read_cpuinfo(struct lscpu_cxt *cxt) |
405d51ed | 440 | { |
329ec35f KZ |
441 | FILE *fp; |
442 | char buf[BUFSIZ]; | |
d4228e9d KZ |
443 | size_t i; |
444 | struct lscpu_cputype *ct; | |
f5d13532 | 445 | struct cpuinfo_parser _pr = { .cxt = cxt }, *pr = &_pr; |
329ec35f | 446 | |
801b125f KZ |
447 | assert(cxt->npossibles); /* lscpu_create_cpus() required */ |
448 | assert(cxt->cpus); | |
449 | ||
cbf8f129 | 450 | DBG(GATHER, ul_debugobj(cxt, "reading cpuinfo")); |
329ec35f KZ |
451 | |
452 | fp = ul_path_fopen(cxt->procfs, "r", "cpuinfo"); | |
453 | if (!fp) | |
454 | err(EXIT_FAILURE, _("cannot open %s"), "/proc/cpuinfo"); | |
455 | ||
d234a381 | 456 | do { |
f5d13532 KZ |
457 | int keynum = -1; |
458 | char *p = NULL, *value = NULL; | |
459 | const struct cpuinfo_pattern *pattern; | |
d234a381 KZ |
460 | |
461 | if (fgets(buf, sizeof(buf), fp) != NULL) | |
f5d13532 | 462 | p = (char *) skip_space(buf); |
d234a381 | 463 | |
fd5999d9 | 464 | if (p == NULL || (*buf && !*p)) { |
f5d13532 | 465 | /* Blank line separates information */ |
d234a381 KZ |
466 | if (p == NULL) |
467 | break; /* fgets() returns nothing; EOF */ | |
f5d13532 KZ |
468 | continue; |
469 | } | |
470 | ||
471 | rtrim_whitespace((unsigned char *) buf); | |
472 | ||
473 | /* parse */ | |
474 | pattern = cpuinfo_parse_line(p, &value, &keynum); | |
475 | if (!pattern) { | |
476 | DBG(GATHER, ul_debug("'%s' not found", buf)); | |
477 | continue; | |
478 | } | |
479 | ||
480 | /* set data */ | |
481 | switch (pattern->domain) { | |
482 | case CPUINFO_LINE_CPU: | |
483 | if (pattern->id == PAT_PROCESSOR) { | |
ee0fabda | 484 | /* switch CPU */ |
f5d13532 | 485 | int id = keynum >= 0 ? keynum : atoi(value); |
ee0fabda KZ |
486 | |
487 | if (pr->curr_cpu && pr->curr_type) | |
488 | lscpu_cpu_set_type(pr->curr_cpu, pr->curr_type); | |
489 | ||
f5d13532 KZ |
490 | lscpu_unref_cpu(pr->curr_cpu); |
491 | pr->curr_cpu = lscpu_get_cpu(cxt, id); | |
ee0fabda | 492 | |
f5d13532 KZ |
493 | if (!pr->curr_cpu) |
494 | DBG(GATHER, ul_debug("*** cpu ID '%d' undefined", id)); | |
495 | else | |
496 | DBG(GATHER, ul_debug(" switch to CPU %d", id)); | |
497 | lscpu_ref_cpu(pr->curr_cpu); | |
498 | break; | |
499 | } | |
500 | if (!pr->curr_cpu) | |
501 | DBG(GATHER, ul_debug("*** cpu data before cpu ID")); | |
502 | else | |
503 | strdup_to_offset(pr->curr_cpu, pattern->offset, value); | |
778254d9 KZ |
504 | |
505 | if (pattern->id == PAT_MHZ_DYNAMIC && pr->curr_type && !pr->curr_type->dynamic_mhz) | |
506 | pr->curr_type->dynamic_mhz = xstrdup(value); | |
507 | if (pattern->id == PAT_MHZ_STATIC && pr->curr_type && !pr->curr_type->static_mhz) | |
508 | pr->curr_type->static_mhz = xstrdup(value); | |
cc94324e KZ |
509 | if (pattern->id == PAT_BOGOMIPS_CPU && pr->curr_type && !pr->curr_type->bogomips) |
510 | pr->curr_type->bogomips = xstrdup(value); | |
f5d13532 KZ |
511 | break; |
512 | case CPUINFO_LINE_CPUTYPE: | |
ee0fabda KZ |
513 | if (pr->curr_type && is_different_cputype(pr->curr_type, pattern->offset, value)) { |
514 | lscpu_unref_cputype(pr->curr_type); | |
515 | pr->curr_type = NULL; | |
516 | } | |
517 | if (!pr->curr_type) { | |
f5d13532 | 518 | pr->curr_type = lscpu_new_cputype(); |
ee0fabda KZ |
519 | lscpu_add_cputype(cxt, pr->curr_type); |
520 | } | |
521 | ||
f5d13532 KZ |
522 | strdup_to_offset(pr->curr_type, pattern->offset, value); |
523 | break; | |
524 | case CPUINFO_LINE_CACHE: | |
25c2a72c KZ |
525 | if (pattern->id != PAT_CACHE) |
526 | break; | |
527 | cpuinfo_parse_cache(cxt, keynum, value); | |
f5d13532 | 528 | break; |
329ec35f | 529 | } |
d234a381 | 530 | } while (1); |
329ec35f | 531 | |
f495abb7 KZ |
532 | DBG(GATHER, fprintf_cputypes(stderr, cxt)); |
533 | ||
63c5e7f8 KZ |
534 | if (pr->curr_cpu && !pr->curr_cpu->type) |
535 | lscpu_cpu_set_type(pr->curr_cpu, pr->curr_type); | |
536 | ||
ee0fabda KZ |
537 | lscpu_unref_cputype(pr->curr_type); |
538 | lscpu_unref_cpu(pr->curr_cpu); | |
539 | ||
329ec35f | 540 | fclose(fp); |
eadaf6d7 | 541 | lscpu_sort_caches(cxt->ecaches, cxt->necaches); |
e36e01f9 | 542 | |
d4228e9d KZ |
543 | /* Set the default type to CPUs which are missing (or not parsed) |
544 | * in cpuinfo */ | |
545 | ct = lscpu_cputype_get_default(cxt); | |
546 | for (i = 0; i < cxt->npossibles; i++) { | |
547 | struct lscpu_cpu *cpu = cxt->cpus[i]; | |
548 | ||
549 | if (cpu && !cpu->type) | |
550 | lscpu_cpu_set_type(cpu, ct); | |
551 | } | |
552 | ||
405d51ed KZ |
553 | return 0; |
554 | } | |
555 | ||
a94bb435 | 556 | struct lscpu_arch *lscpu_read_architecture(struct lscpu_cxt *cxt) |
d34b7f1b KZ |
557 | { |
558 | struct utsname utsbuf; | |
559 | struct lscpu_arch *ar; | |
560 | struct lscpu_cputype *ct; | |
561 | ||
562 | assert(cxt); | |
d34b7f1b | 563 | |
a94bb435 | 564 | DBG(GATHER, ul_debug("reading architecture")); |
d34b7f1b KZ |
565 | |
566 | if (uname(&utsbuf) == -1) | |
567 | err(EXIT_FAILURE, _("error: uname failed")); | |
568 | ||
a94bb435 | 569 | ar = xcalloc(1, sizeof(*cxt->arch)); |
d34b7f1b KZ |
570 | ar->name = xstrdup(utsbuf.machine); |
571 | ||
572 | if (cxt->noalive) | |
573 | /* reading info from any /{sys,proc} dump, don't mix it with | |
574 | * information about our real CPU */ | |
575 | ; | |
576 | else { | |
577 | #if defined(__alpha__) || defined(__ia64__) | |
578 | ar->bit64 = 1; /* 64bit platforms only */ | |
579 | #endif | |
580 | /* platforms with 64bit flag in /proc/cpuinfo, define | |
581 | * 32bit default here */ | |
582 | #if defined(__i386__) || defined(__x86_64__) || \ | |
583 | defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) | |
584 | ar->bit32 = 1; | |
585 | #endif | |
586 | ||
587 | #if defined(__aarch64__) | |
588 | { | |
589 | /* personality() is the most reliable way (since 4.7) | |
590 | * to determine aarch32 support */ | |
591 | int pers = personality(PER_LINUX32); | |
592 | if (pers != -1) { | |
593 | personality(pers); | |
594 | ar->bit32 = 1; | |
595 | } | |
596 | ar->bit64 = 1; | |
597 | } | |
598 | #endif | |
599 | } | |
600 | ||
601 | ct = lscpu_cputype_get_default(cxt); | |
602 | if (ct && ct->flags) { | |
603 | char buf[BUFSIZ]; | |
604 | ||
605 | snprintf(buf, sizeof(buf), " %s ", ct->flags); | |
606 | if (strstr(buf, " lm ")) | |
607 | ar->bit32 = 1, ar->bit64 = 1; /* x86_64 */ | |
608 | if (strstr(buf, " zarch ")) | |
609 | ar->bit32 = 1, ar->bit64 = 1; /* s390x */ | |
610 | if (strstr(buf, " sun4v ") || strstr(buf, " sun4u ")) | |
611 | ar->bit32 = 1, ar->bit64 = 1; /* sparc64 */ | |
612 | } | |
613 | ||
614 | if (ar->name && !cxt->noalive) { | |
615 | if (strcmp(ar->name, "ppc64") == 0) | |
616 | ar->bit32 = 1, ar->bit64 = 1; | |
617 | else if (strcmp(ar->name, "ppc") == 0) | |
618 | ar->bit32 = 1; | |
619 | } | |
620 | ||
a94bb435 KZ |
621 | DBG(GATHER, ul_debugobj(ar, "arch: name=%s %s %s", |
622 | ar->name, | |
623 | ar->bit64 ? "64-bit" : "", | |
624 | ar->bit64 ? "32-bit" : "")); | |
625 | return ar; | |
626 | } | |
627 | ||
628 | void lscpu_free_architecture(struct lscpu_arch *ar) | |
629 | { | |
630 | if (!ar) | |
631 | return; | |
632 | free(ar->name); | |
633 | free(ar); | |
d34b7f1b KZ |
634 | } |
635 | ||
551f2b4d KZ |
636 | int lscpu_read_cpulists(struct lscpu_cxt *cxt) |
637 | { | |
551f2b4d KZ |
638 | cpu_set_t *cpuset = NULL; |
639 | ||
640 | assert(cxt); | |
641 | DBG(GATHER, ul_debugobj(cxt, "reading cpulists")); | |
642 | ||
643 | if (ul_path_read_s32(cxt->syscpu, &cxt->maxcpus, "kernel_max") == 0) | |
644 | /* note that kernel_max is maximum index [NR_CPUS-1] */ | |
645 | cxt->maxcpus += 1; | |
646 | ||
647 | else if (!cxt->noalive) | |
648 | /* the root is '/' so we are working with data from the current kernel */ | |
649 | cxt->maxcpus = get_max_number_of_cpus(); | |
650 | ||
651 | if (cxt->maxcpus <= 0) | |
652 | /* error or we are reading some /sys snapshot instead of the | |
653 | * real /sys, let's use any crazy number... */ | |
654 | cxt->maxcpus = 2048; | |
655 | ||
eaf32c65 | 656 | cxt->setsize = CPU_ALLOC_SIZE(cxt->maxcpus); |
551f2b4d | 657 | |
801b125f | 658 | /* create CPUs from possible mask */ |
eaf32c65 KZ |
659 | if (ul_path_readf_cpulist(cxt->syscpu, &cpuset, cxt->maxcpus, "possible") == 0) { |
660 | lscpu_create_cpus(cxt, cpuset, cxt->setsize); | |
551f2b4d KZ |
661 | cpuset_free(cpuset); |
662 | cpuset = NULL; | |
663 | } else | |
664 | err(EXIT_FAILURE, _("failed to determine number of CPUs: %s"), | |
665 | _PATH_SYS_CPU "/possible"); | |
666 | ||
667 | ||
668 | /* get mask for present CPUs */ | |
eaf32c65 KZ |
669 | if (ul_path_readf_cpulist(cxt->syscpu, &cxt->present, cxt->maxcpus, "present") == 0) |
670 | cxt->npresents = CPU_COUNT_S(cxt->setsize, cxt->present); | |
551f2b4d KZ |
671 | |
672 | /* get mask for online CPUs */ | |
eaf32c65 KZ |
673 | if (ul_path_readf_cpulist(cxt->syscpu, &cxt->online, cxt->maxcpus, "online") == 0) |
674 | cxt->nonlines = CPU_COUNT_S(cxt->setsize, cxt->online); | |
551f2b4d KZ |
675 | |
676 | return 0; | |
677 | } | |
678 | ||
5a6da72c KZ |
679 | #if defined(HAVE_LIBRTAS) |
680 | # define PROCESSOR_MODULE_INFO 43 | |
681 | static int strbe16toh(const char *buf, int offset) | |
682 | { | |
683 | return (buf[offset] << 8) + buf[offset+1]; | |
684 | } | |
685 | #endif | |
686 | ||
687 | /* some extra information for the default CPU type */ | |
9c5b106b | 688 | int lscpu_read_archext(struct lscpu_cxt *cxt) |
5a6da72c KZ |
689 | { |
690 | FILE *f; | |
691 | char buf[BUFSIZ]; | |
692 | struct lscpu_cputype *ct; | |
693 | ||
406b088c KZ |
694 | DBG(GATHER, ul_debugobj(cxt, "reading extra arch info")); |
695 | ||
5a6da72c KZ |
696 | assert(cxt); |
697 | ct = lscpu_cputype_get_default(cxt); | |
698 | if (!ct) | |
699 | return -EINVAL; | |
700 | ||
701 | /* get dispatching mode */ | |
702 | if (ul_path_read_s32(cxt->syscpu, &ct->dispatching, "dispatching") != 0) | |
703 | ct->dispatching = -1; | |
704 | ||
705 | /* get cpufreq boost mode */ | |
706 | if (ul_path_read_s32(cxt->syscpu, &ct->freqboost, "cpufreq/boost") != 0) | |
707 | ct->freqboost = -1; | |
708 | ||
709 | if ((f = ul_path_fopen(cxt->procfs, "r", "sysinfo"))) { | |
710 | while (fgets(buf, sizeof(buf), f) != NULL) { | |
711 | if (lookup(buf, "Type", &ct->machinetype)) | |
712 | break; | |
713 | } | |
714 | fclose(f); | |
715 | } | |
716 | ||
717 | #if defined(HAVE_LIBRTAS) | |
718 | /* Get PowerPC speficic info */ | |
719 | if (!cxt->noalive) { | |
720 | int rc, len, ntypes; | |
721 | ||
722 | ct->physsockets = ct->physchips = ct->physcoresperchip = 0; | |
723 | ||
724 | rc = rtas_get_sysparm(PROCESSOR_MODULE_INFO, sizeof(buf), buf); | |
725 | if (rc < 0) | |
726 | goto nortas; | |
727 | ||
728 | len = strbe16toh(buf, 0); | |
729 | if (len < 8) | |
730 | goto nortas; | |
731 | ||
732 | ntypes = strbe16toh(buf, 2); | |
5a6da72c KZ |
733 | if (!ntypes) |
734 | goto nortas; | |
735 | ||
0796923a KZ |
736 | ct->physsockets = strbe16toh(buf, 4); |
737 | ct->physchips = strbe16toh(buf, 6); | |
738 | ct->physcoresperchip = strbe16toh(buf, 8); | |
5a6da72c KZ |
739 | nortas: |
740 | } | |
741 | #endif | |
742 | return 0; | |
743 | } | |
744 | ||
0796923a KZ |
745 | static int cmp_vulnerability_name(const void *a0, const void *b0) |
746 | { | |
747 | const struct lscpu_vulnerability | |
748 | *a = (const struct lscpu_vulnerability *) a0, | |
749 | *b = (const struct lscpu_vulnerability *) b0; | |
750 | return strcmp(a->name, b->name); | |
751 | } | |
752 | ||
753 | int lscpu_read_vulnerabilities(struct lscpu_cxt *cxt) | |
754 | { | |
755 | struct dirent *d; | |
756 | DIR *dir; | |
757 | size_t n = 0; | |
758 | ||
759 | assert(cxt); | |
760 | ||
406b088c KZ |
761 | DBG(GATHER, ul_debugobj(cxt, "reading vulnerabilities")); |
762 | ||
0796923a KZ |
763 | dir = ul_path_opendir(cxt->syscpu, "vulnerabilities"); |
764 | if (!dir) | |
765 | return 0; | |
766 | ||
767 | cxt->nvuls = n = 0; | |
768 | while (xreaddir(dir)) | |
769 | n++; | |
770 | if (!n) | |
771 | return 0; | |
772 | ||
773 | rewinddir(dir); | |
774 | cxt->vuls = xcalloc(n, sizeof(struct lscpu_vulnerability)); | |
775 | ||
776 | while (cxt->nvuls < n && (d = xreaddir(dir))) { | |
777 | char *str, *p; | |
778 | struct lscpu_vulnerability *vu; | |
779 | ||
780 | #ifdef _DIRENT_HAVE_D_TYPE | |
781 | if (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) | |
782 | continue; | |
783 | #endif | |
784 | if (ul_path_readf_string(cxt->syscpu, &str, | |
785 | "vulnerabilities/%s", d->d_name) <= 0) | |
786 | continue; | |
787 | ||
788 | vu = &cxt->vuls[cxt->nvuls++]; | |
789 | ||
790 | /* Name */ | |
791 | vu->name = xstrdup(d->d_name); | |
792 | *vu->name = toupper(*vu->name); | |
793 | strrep(vu->name, '_', ' '); | |
794 | ||
795 | /* Description */ | |
796 | vu->text = str; | |
797 | p = (char *) startswith(vu->text, "Mitigation"); | |
798 | if (p) { | |
799 | *p = ';'; | |
800 | strrem(vu->text, ':'); | |
801 | } | |
802 | } | |
803 | closedir(dir); | |
804 | ||
805 | qsort(cxt->vuls, cxt->nvuls, | |
806 | sizeof(struct lscpu_vulnerability), cmp_vulnerability_name); | |
807 | ||
808 | return 0; | |
809 | } | |
5a6da72c | 810 | |
154ee5a9 KZ |
811 | static inline int is_node_dirent(struct dirent *d) |
812 | { | |
813 | return | |
814 | d && | |
815 | #ifdef _DIRENT_HAVE_D_TYPE | |
816 | (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) && | |
817 | #endif | |
818 | strncmp(d->d_name, "node", 4) == 0 && | |
819 | isdigit_string(d->d_name + 4); | |
820 | } | |
821 | ||
822 | static int nodecmp(const void *ap, const void *bp) | |
823 | { | |
824 | int *a = (int *) ap, *b = (int *) bp; | |
825 | return *a - *b; | |
826 | } | |
827 | ||
828 | int lscpu_read_numas(struct lscpu_cxt *cxt) | |
829 | { | |
830 | size_t i = 0; | |
831 | DIR *dir; | |
832 | struct dirent *d; | |
833 | struct path_cxt *sys; | |
834 | ||
835 | assert(!cxt->nnodes); | |
836 | ||
406b088c | 837 | |
154ee5a9 KZ |
838 | sys = ul_new_path(_PATH_SYS_NODE); |
839 | if (!sys) | |
840 | err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_NODE); | |
841 | ||
842 | ul_path_set_prefix(sys, cxt->prefix); | |
843 | ||
844 | dir = ul_path_opendir(sys, NULL); | |
845 | if (!dir) | |
846 | goto done; | |
847 | ||
848 | while ((d = readdir(dir))) { | |
849 | if (is_node_dirent(d)) | |
850 | cxt->nnodes++; | |
851 | } | |
852 | ||
853 | if (!cxt->nnodes) { | |
854 | closedir(dir); | |
855 | goto done; | |
856 | } | |
857 | ||
858 | cxt->nodemaps = xcalloc(cxt->nnodes, sizeof(cpu_set_t *)); | |
859 | cxt->idx2nodenum = xmalloc(cxt->nnodes * sizeof(int)); | |
860 | ||
861 | rewinddir(dir); | |
406b088c | 862 | for (i = 0; (d = readdir(dir)) && i < cxt->nnodes;) { |
154ee5a9 | 863 | if (is_node_dirent(d)) |
406b088c | 864 | cxt->idx2nodenum[i++] = strtol_or_err(((d->d_name) + 4), |
154ee5a9 KZ |
865 | _("Failed to extract the node number")); |
866 | } | |
867 | closedir(dir); | |
868 | qsort(cxt->idx2nodenum, cxt->nnodes, sizeof(int), nodecmp); | |
869 | ||
870 | /* information about how nodes share different CPUs */ | |
871 | for (i = 0; i < cxt->nnodes; i++) | |
872 | ul_path_readf_cpuset(sys, &cxt->nodemaps[i], cxt->maxcpus, | |
873 | "node%d/cpumap", cxt->idx2nodenum[i]); | |
874 | done: | |
406b088c KZ |
875 | DBG(GATHER, ul_debugobj(cxt, "read %zu numas", cxt->nnodes)); |
876 | ||
154ee5a9 KZ |
877 | ul_unref_path(sys); |
878 | return 0; | |
879 | } |