1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <sys/types.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <stdarg.h>
14 #include <locale.h>
15 #include <api/fs/fs.h>
16 #include "fncache.h"
17 #include "pmu-hybrid.h"
18 
19 LIST_HEAD(perf_pmu__hybrid_pmus);
20 
perf_pmu__hybrid_mounted(const char * name)21 bool perf_pmu__hybrid_mounted(const char *name)
22 {
23 	int cpu;
24 	char pmu_name[PATH_MAX];
25 	struct perf_pmu pmu = {.name = pmu_name};
26 
27 	if (strncmp(name, "cpu_", 4))
28 		return false;
29 
30 	strlcpy(pmu_name, name, sizeof(pmu_name));
31 	return perf_pmu__scan_file(&pmu, "cpus", "%u", &cpu) > 0;
32 }
33 
perf_pmu__find_hybrid_pmu(const char * name)34 struct perf_pmu *perf_pmu__find_hybrid_pmu(const char *name)
35 {
36 	struct perf_pmu *pmu;
37 
38 	if (!name)
39 		return NULL;
40 
41 	perf_pmu__for_each_hybrid_pmu(pmu) {
42 		if (!strcmp(name, pmu->name))
43 			return pmu;
44 	}
45 
46 	return NULL;
47 }
48 
perf_pmu__is_hybrid(const char * name)49 bool perf_pmu__is_hybrid(const char *name)
50 {
51 	return perf_pmu__find_hybrid_pmu(name) != NULL;
52 }
53 
perf_pmu__hybrid_type_to_pmu(const char * type)54 char *perf_pmu__hybrid_type_to_pmu(const char *type)
55 {
56 	char *pmu_name = NULL;
57 
58 	if (asprintf(&pmu_name, "cpu_%s", type) < 0)
59 		return NULL;
60 
61 	if (perf_pmu__is_hybrid(pmu_name))
62 		return pmu_name;
63 
64 	/*
65 	 * pmu may be not scanned, check the sysfs.
66 	 */
67 	if (perf_pmu__hybrid_mounted(pmu_name))
68 		return pmu_name;
69 
70 	free(pmu_name);
71 	return NULL;
72 }
73