1 // SPDX-License-Identifier: GPL-2.0
2 #include "cpumap.h"
3 #include "debug.h"
4 #include "env.h"
5 #include "util/header.h"
6 #include "linux/compiler.h"
7 #include <linux/ctype.h>
8 #include <linux/string.h>
9 #include <linux/zalloc.h>
10 #include "cgroup.h"
11 #include <errno.h>
12 #include <sys/utsname.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "pmu.h"
16 #include "pmus.h"
17 #include "strbuf.h"
18 #include "trace/beauty/beauty.h"
19 
20 struct perf_env perf_env;
21 
22 #ifdef HAVE_LIBBPF_SUPPORT
23 #include "bpf-event.h"
24 #include "bpf-utils.h"
25 #include <bpf/libbpf.h>
26 
perf_env__insert_bpf_prog_info(struct perf_env * env,struct bpf_prog_info_node * info_node)27 bool perf_env__insert_bpf_prog_info(struct perf_env *env,
28 				    struct bpf_prog_info_node *info_node)
29 {
30 	bool ret;
31 
32 	down_write(&env->bpf_progs.lock);
33 	ret = __perf_env__insert_bpf_prog_info(env, info_node);
34 	up_write(&env->bpf_progs.lock);
35 
36 	return ret;
37 }
38 
__perf_env__insert_bpf_prog_info(struct perf_env * env,struct bpf_prog_info_node * info_node)39 bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node)
40 {
41 	__u32 prog_id = info_node->info_linear->info.id;
42 	struct bpf_prog_info_node *node;
43 	struct rb_node *parent = NULL;
44 	struct rb_node **p;
45 
46 	p = &env->bpf_progs.infos.rb_node;
47 
48 	while (*p != NULL) {
49 		parent = *p;
50 		node = rb_entry(parent, struct bpf_prog_info_node, rb_node);
51 		if (prog_id < node->info_linear->info.id) {
52 			p = &(*p)->rb_left;
53 		} else if (prog_id > node->info_linear->info.id) {
54 			p = &(*p)->rb_right;
55 		} else {
56 			pr_debug("duplicated bpf prog info %u\n", prog_id);
57 			return false;
58 		}
59 	}
60 
61 	rb_link_node(&info_node->rb_node, parent, p);
62 	rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos);
63 	env->bpf_progs.infos_cnt++;
64 	return true;
65 }
66 
perf_env__find_bpf_prog_info(struct perf_env * env,__u32 prog_id)67 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
68 							__u32 prog_id)
69 {
70 	struct bpf_prog_info_node *node = NULL;
71 	struct rb_node *n;
72 
73 	down_read(&env->bpf_progs.lock);
74 	n = env->bpf_progs.infos.rb_node;
75 
76 	while (n) {
77 		node = rb_entry(n, struct bpf_prog_info_node, rb_node);
78 		if (prog_id < node->info_linear->info.id)
79 			n = n->rb_left;
80 		else if (prog_id > node->info_linear->info.id)
81 			n = n->rb_right;
82 		else
83 			goto out;
84 	}
85 	node = NULL;
86 
87 out:
88 	up_read(&env->bpf_progs.lock);
89 	return node;
90 }
91 
perf_env__insert_btf(struct perf_env * env,struct btf_node * btf_node)92 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
93 {
94 	bool ret;
95 
96 	down_write(&env->bpf_progs.lock);
97 	ret = __perf_env__insert_btf(env, btf_node);
98 	up_write(&env->bpf_progs.lock);
99 	return ret;
100 }
101 
__perf_env__insert_btf(struct perf_env * env,struct btf_node * btf_node)102 bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
103 {
104 	struct rb_node *parent = NULL;
105 	__u32 btf_id = btf_node->id;
106 	struct btf_node *node;
107 	struct rb_node **p;
108 
109 	p = &env->bpf_progs.btfs.rb_node;
110 
111 	while (*p != NULL) {
112 		parent = *p;
113 		node = rb_entry(parent, struct btf_node, rb_node);
114 		if (btf_id < node->id) {
115 			p = &(*p)->rb_left;
116 		} else if (btf_id > node->id) {
117 			p = &(*p)->rb_right;
118 		} else {
119 			pr_debug("duplicated btf %u\n", btf_id);
120 			return false;
121 		}
122 	}
123 
124 	rb_link_node(&btf_node->rb_node, parent, p);
125 	rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs);
126 	env->bpf_progs.btfs_cnt++;
127 	return true;
128 }
129 
perf_env__find_btf(struct perf_env * env,__u32 btf_id)130 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id)
131 {
132 	struct btf_node *res;
133 
134 	down_read(&env->bpf_progs.lock);
135 	res = __perf_env__find_btf(env, btf_id);
136 	up_read(&env->bpf_progs.lock);
137 	return res;
138 }
139 
__perf_env__find_btf(struct perf_env * env,__u32 btf_id)140 struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id)
141 {
142 	struct btf_node *node = NULL;
143 	struct rb_node *n;
144 
145 	n = env->bpf_progs.btfs.rb_node;
146 
147 	while (n) {
148 		node = rb_entry(n, struct btf_node, rb_node);
149 		if (btf_id < node->id)
150 			n = n->rb_left;
151 		else if (btf_id > node->id)
152 			n = n->rb_right;
153 		else
154 			return node;
155 	}
156 	return NULL;
157 }
158 
159 /* purge data in bpf_progs.infos tree */
perf_env__purge_bpf(struct perf_env * env)160 static void perf_env__purge_bpf(struct perf_env *env)
161 {
162 	struct rb_root *root;
163 	struct rb_node *next;
164 
165 	down_write(&env->bpf_progs.lock);
166 
167 	root = &env->bpf_progs.infos;
168 	next = rb_first(root);
169 
170 	while (next) {
171 		struct bpf_prog_info_node *node;
172 
173 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
174 		next = rb_next(&node->rb_node);
175 		rb_erase(&node->rb_node, root);
176 		zfree(&node->info_linear);
177 		free(node);
178 	}
179 
180 	env->bpf_progs.infos_cnt = 0;
181 
182 	root = &env->bpf_progs.btfs;
183 	next = rb_first(root);
184 
185 	while (next) {
186 		struct btf_node *node;
187 
188 		node = rb_entry(next, struct btf_node, rb_node);
189 		next = rb_next(&node->rb_node);
190 		rb_erase(&node->rb_node, root);
191 		free(node);
192 	}
193 
194 	env->bpf_progs.btfs_cnt = 0;
195 
196 	up_write(&env->bpf_progs.lock);
197 }
198 #else // HAVE_LIBBPF_SUPPORT
perf_env__purge_bpf(struct perf_env * env __maybe_unused)199 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused)
200 {
201 }
202 #endif // HAVE_LIBBPF_SUPPORT
203 
perf_env__exit(struct perf_env * env)204 void perf_env__exit(struct perf_env *env)
205 {
206 	int i, j;
207 
208 	perf_env__purge_bpf(env);
209 	perf_env__purge_cgroups(env);
210 	zfree(&env->hostname);
211 	zfree(&env->os_release);
212 	zfree(&env->version);
213 	zfree(&env->arch);
214 	zfree(&env->cpu_desc);
215 	zfree(&env->cpuid);
216 	zfree(&env->cmdline);
217 	zfree(&env->cmdline_argv);
218 	zfree(&env->sibling_dies);
219 	zfree(&env->sibling_cores);
220 	zfree(&env->sibling_threads);
221 	zfree(&env->pmu_mappings);
222 	zfree(&env->cpu);
223 	for (i = 0; i < env->nr_cpu_pmu_caps; i++)
224 		zfree(&env->cpu_pmu_caps[i]);
225 	zfree(&env->cpu_pmu_caps);
226 	zfree(&env->numa_map);
227 
228 	for (i = 0; i < env->nr_numa_nodes; i++)
229 		perf_cpu_map__put(env->numa_nodes[i].map);
230 	zfree(&env->numa_nodes);
231 
232 	for (i = 0; i < env->caches_cnt; i++)
233 		cpu_cache_level__free(&env->caches[i]);
234 	zfree(&env->caches);
235 
236 	for (i = 0; i < env->nr_memory_nodes; i++)
237 		zfree(&env->memory_nodes[i].set);
238 	zfree(&env->memory_nodes);
239 
240 	for (i = 0; i < env->nr_hybrid_nodes; i++) {
241 		zfree(&env->hybrid_nodes[i].pmu_name);
242 		zfree(&env->hybrid_nodes[i].cpus);
243 	}
244 	zfree(&env->hybrid_nodes);
245 
246 	for (i = 0; i < env->nr_pmus_with_caps; i++) {
247 		for (j = 0; j < env->pmu_caps[i].nr_caps; j++)
248 			zfree(&env->pmu_caps[i].caps[j]);
249 		zfree(&env->pmu_caps[i].caps);
250 		zfree(&env->pmu_caps[i].pmu_name);
251 	}
252 	zfree(&env->pmu_caps);
253 }
254 
perf_env__init(struct perf_env * env)255 void perf_env__init(struct perf_env *env)
256 {
257 #ifdef HAVE_LIBBPF_SUPPORT
258 	env->bpf_progs.infos = RB_ROOT;
259 	env->bpf_progs.btfs = RB_ROOT;
260 	init_rwsem(&env->bpf_progs.lock);
261 #endif
262 	env->kernel_is_64_bit = -1;
263 }
264 
perf_env__init_kernel_mode(struct perf_env * env)265 static void perf_env__init_kernel_mode(struct perf_env *env)
266 {
267 	const char *arch = perf_env__raw_arch(env);
268 
269 	if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) ||
270 	    !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) ||
271 	    !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) ||
272 	    !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7))
273 		env->kernel_is_64_bit = 1;
274 	else
275 		env->kernel_is_64_bit = 0;
276 }
277 
perf_env__kernel_is_64_bit(struct perf_env * env)278 int perf_env__kernel_is_64_bit(struct perf_env *env)
279 {
280 	if (env->kernel_is_64_bit == -1)
281 		perf_env__init_kernel_mode(env);
282 
283 	return env->kernel_is_64_bit;
284 }
285 
perf_env__set_cmdline(struct perf_env * env,int argc,const char * argv[])286 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
287 {
288 	int i;
289 
290 	/* do not include NULL termination */
291 	env->cmdline_argv = calloc(argc, sizeof(char *));
292 	if (env->cmdline_argv == NULL)
293 		goto out_enomem;
294 
295 	/*
296 	 * Must copy argv contents because it gets moved around during option
297 	 * parsing:
298 	 */
299 	for (i = 0; i < argc ; i++) {
300 		env->cmdline_argv[i] = argv[i];
301 		if (env->cmdline_argv[i] == NULL)
302 			goto out_free;
303 	}
304 
305 	env->nr_cmdline = argc;
306 
307 	return 0;
308 out_free:
309 	zfree(&env->cmdline_argv);
310 out_enomem:
311 	return -ENOMEM;
312 }
313 
perf_env__read_cpu_topology_map(struct perf_env * env)314 int perf_env__read_cpu_topology_map(struct perf_env *env)
315 {
316 	int idx, nr_cpus;
317 
318 	if (env->cpu != NULL)
319 		return 0;
320 
321 	if (env->nr_cpus_avail == 0)
322 		env->nr_cpus_avail = cpu__max_present_cpu().cpu;
323 
324 	nr_cpus = env->nr_cpus_avail;
325 	if (nr_cpus == -1)
326 		return -EINVAL;
327 
328 	env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
329 	if (env->cpu == NULL)
330 		return -ENOMEM;
331 
332 	for (idx = 0; idx < nr_cpus; ++idx) {
333 		struct perf_cpu cpu = { .cpu = idx };
334 		int core_id   = cpu__get_core_id(cpu);
335 		int socket_id = cpu__get_socket_id(cpu);
336 		int die_id    = cpu__get_die_id(cpu);
337 
338 		env->cpu[idx].core_id	= core_id >= 0 ? core_id : -1;
339 		env->cpu[idx].socket_id	= socket_id >= 0 ? socket_id : -1;
340 		env->cpu[idx].die_id	= die_id >= 0 ? die_id : -1;
341 	}
342 
343 	env->nr_cpus_avail = nr_cpus;
344 	return 0;
345 }
346 
perf_env__read_pmu_mappings(struct perf_env * env)347 int perf_env__read_pmu_mappings(struct perf_env *env)
348 {
349 	struct perf_pmu *pmu = NULL;
350 	u32 pmu_num = 0;
351 	struct strbuf sb;
352 
353 	while ((pmu = perf_pmus__scan(pmu)))
354 		pmu_num++;
355 
356 	if (!pmu_num) {
357 		pr_debug("pmu mappings not available\n");
358 		return -ENOENT;
359 	}
360 	env->nr_pmu_mappings = pmu_num;
361 
362 	if (strbuf_init(&sb, 128 * pmu_num) < 0)
363 		return -ENOMEM;
364 
365 	while ((pmu = perf_pmus__scan(pmu))) {
366 		if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
367 			goto error;
368 		/* include a NULL character at the end */
369 		if (strbuf_add(&sb, "", 1) < 0)
370 			goto error;
371 	}
372 
373 	env->pmu_mappings = strbuf_detach(&sb, NULL);
374 
375 	return 0;
376 
377 error:
378 	strbuf_release(&sb);
379 	return -1;
380 }
381 
perf_env__read_cpuid(struct perf_env * env)382 int perf_env__read_cpuid(struct perf_env *env)
383 {
384 	char cpuid[128];
385 	struct perf_cpu cpu = {-1};
386 	int err = get_cpuid(cpuid, sizeof(cpuid), cpu);
387 
388 	if (err)
389 		return err;
390 
391 	free(env->cpuid);
392 	env->cpuid = strdup(cpuid);
393 	if (env->cpuid == NULL)
394 		return ENOMEM;
395 	return 0;
396 }
397 
perf_env__read_arch(struct perf_env * env)398 static int perf_env__read_arch(struct perf_env *env)
399 {
400 	struct utsname uts;
401 
402 	if (env->arch)
403 		return 0;
404 
405 	if (!uname(&uts))
406 		env->arch = strdup(uts.machine);
407 
408 	return env->arch ? 0 : -ENOMEM;
409 }
410 
perf_env__read_nr_cpus_avail(struct perf_env * env)411 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
412 {
413 	if (env->nr_cpus_avail == 0)
414 		env->nr_cpus_avail = cpu__max_present_cpu().cpu;
415 
416 	return env->nr_cpus_avail ? 0 : -ENOENT;
417 }
418 
perf_env__raw_arch(struct perf_env * env)419 const char *perf_env__raw_arch(struct perf_env *env)
420 {
421 	return env && !perf_env__read_arch(env) ? env->arch : "unknown";
422 }
423 
perf_env__nr_cpus_avail(struct perf_env * env)424 int perf_env__nr_cpus_avail(struct perf_env *env)
425 {
426 	return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
427 }
428 
cpu_cache_level__free(struct cpu_cache_level * cache)429 void cpu_cache_level__free(struct cpu_cache_level *cache)
430 {
431 	zfree(&cache->type);
432 	zfree(&cache->map);
433 	zfree(&cache->size);
434 }
435 
436 /*
437  * Return architecture name in a normalized form.
438  * The conversion logic comes from the Makefile.
439  */
normalize_arch(char * arch)440 static const char *normalize_arch(char *arch)
441 {
442 	if (!strcmp(arch, "x86_64"))
443 		return "x86";
444 	if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
445 		return "x86";
446 	if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
447 		return "sparc";
448 	if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5))
449 		return "arm64";
450 	if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
451 		return "arm";
452 	if (!strncmp(arch, "s390", 4))
453 		return "s390";
454 	if (!strncmp(arch, "parisc", 6))
455 		return "parisc";
456 	if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
457 		return "powerpc";
458 	if (!strncmp(arch, "mips", 4))
459 		return "mips";
460 	if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
461 		return "sh";
462 	if (!strncmp(arch, "loongarch", 9))
463 		return "loongarch";
464 
465 	return arch;
466 }
467 
perf_env__arch(struct perf_env * env)468 const char *perf_env__arch(struct perf_env *env)
469 {
470 	char *arch_name;
471 
472 	if (!env || !env->arch) { /* Assume local operation */
473 		static struct utsname uts = { .machine[0] = '\0', };
474 		if (uts.machine[0] == '\0' && uname(&uts) < 0)
475 			return NULL;
476 		arch_name = uts.machine;
477 	} else
478 		arch_name = env->arch;
479 
480 	return normalize_arch(arch_name);
481 }
482 
483 #if defined(HAVE_LIBTRACEEVENT)
484 #include "trace/beauty/arch_errno_names.c"
485 #endif
486 
perf_env__arch_strerrno(struct perf_env * env __maybe_unused,int err __maybe_unused)487 const char *perf_env__arch_strerrno(struct perf_env *env __maybe_unused, int err __maybe_unused)
488 {
489 #if defined(HAVE_LIBTRACEEVENT)
490 	if (env->arch_strerrno == NULL)
491 		env->arch_strerrno = arch_syscalls__strerrno_function(perf_env__arch(env));
492 
493 	return env->arch_strerrno ? env->arch_strerrno(err) : "no arch specific strerrno function";
494 #else
495 	return "!HAVE_LIBTRACEEVENT";
496 #endif
497 }
498 
perf_env__cpuid(struct perf_env * env)499 const char *perf_env__cpuid(struct perf_env *env)
500 {
501 	int status;
502 
503 	if (!env->cpuid) { /* Assume local operation */
504 		status = perf_env__read_cpuid(env);
505 		if (status)
506 			return NULL;
507 	}
508 
509 	return env->cpuid;
510 }
511 
perf_env__nr_pmu_mappings(struct perf_env * env)512 int perf_env__nr_pmu_mappings(struct perf_env *env)
513 {
514 	int status;
515 
516 	if (!env->nr_pmu_mappings) { /* Assume local operation */
517 		status = perf_env__read_pmu_mappings(env);
518 		if (status)
519 			return 0;
520 	}
521 
522 	return env->nr_pmu_mappings;
523 }
524 
perf_env__pmu_mappings(struct perf_env * env)525 const char *perf_env__pmu_mappings(struct perf_env *env)
526 {
527 	int status;
528 
529 	if (!env->pmu_mappings) { /* Assume local operation */
530 		status = perf_env__read_pmu_mappings(env);
531 		if (status)
532 			return NULL;
533 	}
534 
535 	return env->pmu_mappings;
536 }
537 
perf_env__numa_node(struct perf_env * env,struct perf_cpu cpu)538 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu)
539 {
540 	if (!env->nr_numa_map) {
541 		struct numa_node *nn;
542 		int i, nr = 0;
543 
544 		for (i = 0; i < env->nr_numa_nodes; i++) {
545 			nn = &env->numa_nodes[i];
546 			nr = max(nr, perf_cpu_map__max(nn->map).cpu);
547 		}
548 
549 		nr++;
550 
551 		/*
552 		 * We initialize the numa_map array to prepare
553 		 * it for missing cpus, which return node -1
554 		 */
555 		env->numa_map = malloc(nr * sizeof(int));
556 		if (!env->numa_map)
557 			return -1;
558 
559 		for (i = 0; i < nr; i++)
560 			env->numa_map[i] = -1;
561 
562 		env->nr_numa_map = nr;
563 
564 		for (i = 0; i < env->nr_numa_nodes; i++) {
565 			struct perf_cpu tmp;
566 			int j;
567 
568 			nn = &env->numa_nodes[i];
569 			perf_cpu_map__for_each_cpu(tmp, j, nn->map)
570 				env->numa_map[tmp.cpu] = i;
571 		}
572 	}
573 
574 	return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1;
575 }
576 
perf_env__has_pmu_mapping(struct perf_env * env,const char * pmu_name)577 bool perf_env__has_pmu_mapping(struct perf_env *env, const char *pmu_name)
578 {
579 	char *pmu_mapping = env->pmu_mappings, *colon;
580 
581 	for (int i = 0; i < env->nr_pmu_mappings; ++i) {
582 		if (strtoul(pmu_mapping, &colon, 0) == ULONG_MAX || *colon != ':')
583 			goto out_error;
584 
585 		pmu_mapping = colon + 1;
586 		if (strcmp(pmu_mapping, pmu_name) == 0)
587 			return true;
588 
589 		pmu_mapping += strlen(pmu_mapping) + 1;
590 	}
591 out_error:
592 	return false;
593 }
594 
perf_env__find_pmu_cap(struct perf_env * env,const char * pmu_name,const char * cap)595 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
596 			     const char *cap)
597 {
598 	char *cap_eq;
599 	int cap_size;
600 	char **ptr;
601 	int i, j;
602 
603 	if (!pmu_name || !cap)
604 		return NULL;
605 
606 	cap_size = strlen(cap);
607 	cap_eq = zalloc(cap_size + 2);
608 	if (!cap_eq)
609 		return NULL;
610 
611 	memcpy(cap_eq, cap, cap_size);
612 	cap_eq[cap_size] = '=';
613 
614 	if (!strcmp(pmu_name, "cpu")) {
615 		for (i = 0; i < env->nr_cpu_pmu_caps; i++) {
616 			if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) {
617 				free(cap_eq);
618 				return &env->cpu_pmu_caps[i][cap_size + 1];
619 			}
620 		}
621 		goto out;
622 	}
623 
624 	for (i = 0; i < env->nr_pmus_with_caps; i++) {
625 		if (strcmp(env->pmu_caps[i].pmu_name, pmu_name))
626 			continue;
627 
628 		ptr = env->pmu_caps[i].caps;
629 
630 		for (j = 0; j < env->pmu_caps[i].nr_caps; j++) {
631 			if (!strncmp(ptr[j], cap_eq, cap_size + 1)) {
632 				free(cap_eq);
633 				return &ptr[j][cap_size + 1];
634 			}
635 		}
636 	}
637 
638 out:
639 	free(cap_eq);
640 	return NULL;
641 }
642 
perf_env__find_br_cntr_info(struct perf_env * env,unsigned int * nr,unsigned int * width)643 void perf_env__find_br_cntr_info(struct perf_env *env,
644 				 unsigned int *nr,
645 				 unsigned int *width)
646 {
647 	if (nr) {
648 		*nr = env->cpu_pmu_caps ? env->br_cntr_nr :
649 					  env->pmu_caps->br_cntr_nr;
650 	}
651 
652 	if (width) {
653 		*width = env->cpu_pmu_caps ? env->br_cntr_width :
654 					     env->pmu_caps->br_cntr_width;
655 	}
656 }
657 
perf_env__is_x86_amd_cpu(struct perf_env * env)658 bool perf_env__is_x86_amd_cpu(struct perf_env *env)
659 {
660 	static int is_amd; /* 0: Uninitialized, 1: Yes, -1: No */
661 
662 	if (is_amd == 0)
663 		is_amd = env->cpuid && strstarts(env->cpuid, "AuthenticAMD") ? 1 : -1;
664 
665 	return is_amd >= 1 ? true : false;
666 }
667 
x86__is_amd_cpu(void)668 bool x86__is_amd_cpu(void)
669 {
670 	struct perf_env env = { .total_mem = 0, };
671 	bool is_amd;
672 
673 	perf_env__cpuid(&env);
674 	is_amd = perf_env__is_x86_amd_cpu(&env);
675 	perf_env__exit(&env);
676 
677 	return is_amd;
678 }
679