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