xref: /linux/tools/perf/util/cpumap.h (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2a12b51c4SPaul Mackerras #ifndef __PERF_CPUMAP_H
3a12b51c4SPaul Mackerras #define __PERF_CPUMAP_H
4a12b51c4SPaul Mackerras 
537be5858SArnaldo Carvalho de Melo #include <stdbool.h>
69ae7d335SArnaldo Carvalho de Melo #include <stdio.h>
7397721e0SJiri Olsa #include <perf/cpumap.h>
89ae7d335SArnaldo Carvalho de Melo 
949679da3SIan Rogers /** Identify where counts are aggregated, -1 implies not to aggregate. */
10fa265e59SJames Clark struct aggr_cpu_id {
1149679da3SIan Rogers 	/** A value in the range 0 to number of threads. */
12fa2edc07SNamhyung Kim 	int thread_idx;
1349679da3SIan Rogers 	/** The numa node X as read from /sys/devices/system/node/nodeX. */
14fcd83a35SJames Clark 	int node;
1549679da3SIan Rogers 	/**
1649679da3SIan Rogers 	 * The socket number as read from
1749679da3SIan Rogers 	 * /sys/devices/system/cpu/cpuX/topology/physical_package_id.
1849679da3SIan Rogers 	 */
191a270cb6SJames Clark 	int socket;
2049679da3SIan Rogers 	/** The die id as read from /sys/devices/system/cpu/cpuX/topology/die_id. */
21ba2ee166SJames Clark 	int die;
22*cbc917a1SYicong Yang 	/** The cluster id as read from /sys/devices/system/cpu/cpuX/topology/cluster_id */
23*cbc917a1SYicong Yang 	int cluster;
24995ed074SK Prateek Nayak 	/** The cache level as read from /sys/devices/system/cpu/cpuX/cache/indexY/level */
25995ed074SK Prateek Nayak 	int cache_lvl;
26995ed074SK Prateek Nayak 	/**
27995ed074SK Prateek Nayak 	 * The cache instance ID, which is the first CPU in the
28995ed074SK Prateek Nayak 	 * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
29995ed074SK Prateek Nayak 	 */
30995ed074SK Prateek Nayak 	int cache;
3149679da3SIan Rogers 	/** The core id as read from /sys/devices/system/cpu/cpuX/topology/core_id. */
32b9933817SJames Clark 	int core;
3334794913SIan Rogers 	/** CPU aggregation, note there is one CPU for each SMT thread. */
346d18804bSIan Rogers 	struct perf_cpu cpu;
35fa265e59SJames Clark };
36fa265e59SJames Clark 
3792aad5c3SIan Rogers /** A collection of aggr_cpu_id values, the "built" version is sorted and uniqued. */
38cea6575fSJames Clark struct cpu_aggr_map {
3992aad5c3SIan Rogers 	/** Number of valid entries. */
40cea6575fSJames Clark 	int nr;
4192aad5c3SIan Rogers 	/** The entries. */
42ff523295SJames Clark 	struct aggr_cpu_id map[];
43cea6575fSJames Clark };
44cea6575fSJames Clark 
458945bef3SIan Rogers #define cpu_aggr_map__for_each_idx(idx, aggr_map)				\
468945bef3SIan Rogers 	for ((idx) = 0; (idx) < aggr_map->nr; (idx)++)
478945bef3SIan Rogers 
4872932371SJiri Olsa struct perf_record_cpu_map_data;
497780c25bSDon Zickus 
50b2f10cd4SIan Rogers bool perf_record_cpu_map_data__test_bit(int i, const struct perf_record_cpu_map_data *data);
51b2f10cd4SIan Rogers 
52315c0a1fSJiri Olsa struct perf_cpu_map *perf_cpu_map__empty_new(int nr);
53cea6575fSJames Clark 
54b2f10cd4SIan Rogers struct perf_cpu_map *cpu_map__new_data(const struct perf_record_cpu_map_data *data);
55f854839bSJiri Olsa size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size);
56f854839bSJiri Olsa size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size);
57f854839bSJiri Olsa size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp);
58a0c41caeSIan Rogers struct perf_cpu_map *cpu_map__online(void); /* thread unsafe */
595ac59a8aSStephane Eranian 
607780c25bSDon Zickus int cpu__setup_cpunode_map(void);
617780c25bSDon Zickus 
625ac76283SArnaldo Carvalho de Melo int cpu__max_node(void);
636d18804bSIan Rogers struct perf_cpu cpu__max_cpu(void);
646d18804bSIan Rogers struct perf_cpu cpu__max_present_cpu(void);
6537be5858SArnaldo Carvalho de Melo 
6637be5858SArnaldo Carvalho de Melo /**
6737be5858SArnaldo Carvalho de Melo  * cpu_map__is_dummy - Events associated with a pid, rather than a CPU, use a single dummy map with an entry of -1.
6837be5858SArnaldo Carvalho de Melo  */
69a0c41caeSIan Rogers static inline bool cpu_map__is_dummy(const struct perf_cpu_map *cpus)
7037be5858SArnaldo Carvalho de Melo {
7144028699SIan Rogers 	return perf_cpu_map__nr(cpus) == 1 && perf_cpu_map__cpu(cpus, 0).cpu == -1;
7237be5858SArnaldo Carvalho de Melo }
7337be5858SArnaldo Carvalho de Melo 
74194a3a20SIan Rogers /**
75194a3a20SIan Rogers  * cpu__get_node - Returns the numa node X as read from
76194a3a20SIan Rogers  * /sys/devices/system/node/nodeX for the given CPU.
77194a3a20SIan Rogers  */
786d18804bSIan Rogers int cpu__get_node(struct perf_cpu cpu);
794e90e5ccSIan Rogers /**
804e90e5ccSIan Rogers  * cpu__get_socket_id - Returns the socket number as read from
814e90e5ccSIan Rogers  * /sys/devices/system/cpu/cpuX/topology/physical_package_id for the given CPU.
824e90e5ccSIan Rogers  */
836d18804bSIan Rogers int cpu__get_socket_id(struct perf_cpu cpu);
844e90e5ccSIan Rogers /**
854e90e5ccSIan Rogers  * cpu__get_die_id - Returns the die id as read from
864e90e5ccSIan Rogers  * /sys/devices/system/cpu/cpuX/topology/die_id for the given CPU.
874e90e5ccSIan Rogers  */
886d18804bSIan Rogers int cpu__get_die_id(struct perf_cpu cpu);
894e90e5ccSIan Rogers /**
90*cbc917a1SYicong Yang  * cpu__get_cluster_id - Returns the cluster id as read from
91*cbc917a1SYicong Yang  * /sys/devices/system/cpu/cpuX/topology/cluster_id for the given CPU
92*cbc917a1SYicong Yang  */
93*cbc917a1SYicong Yang int cpu__get_cluster_id(struct perf_cpu cpu);
94*cbc917a1SYicong Yang /**
954e90e5ccSIan Rogers  * cpu__get_core_id - Returns the core id as read from
964e90e5ccSIan Rogers  * /sys/devices/system/cpu/cpuX/topology/core_id for the given CPU.
974e90e5ccSIan Rogers  */
986d18804bSIan Rogers int cpu__get_core_id(struct perf_cpu cpu);
994e90e5ccSIan Rogers 
10092aad5c3SIan Rogers /**
10192aad5c3SIan Rogers  * cpu_aggr_map__empty_new - Create a cpu_aggr_map of size nr with every entry
10292aad5c3SIan Rogers  * being empty.
10392aad5c3SIan Rogers  */
10492aad5c3SIan Rogers struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr);
10592aad5c3SIan Rogers 
1066d18804bSIan Rogers typedef struct aggr_cpu_id (*aggr_cpu_id_get_t)(struct perf_cpu cpu, void *data);
1077780c25bSDon Zickus 
1085f50e15cSIan Rogers /**
1095f50e15cSIan Rogers  * cpu_aggr_map__new - Create a cpu_aggr_map with an aggr_cpu_id for each cpu in
1105f50e15cSIan Rogers  * cpus. The aggr_cpu_id is created with 'get_id' that may have a data value
1115f50e15cSIan Rogers  * passed to it. The cpu_aggr_map is sorted with duplicate values removed.
1125f50e15cSIan Rogers  */
1135f50e15cSIan Rogers struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
1145f50e15cSIan Rogers 				       aggr_cpu_id_get_t get_id,
115505ac48bSNamhyung Kim 				       void *data, bool needs_sort);
116e632aa69SJiri Olsa 
1173ac23d19SIan Rogers bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b);
11851b826faSIan Rogers bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a);
11951b826faSIan Rogers struct aggr_cpu_id aggr_cpu_id__empty(void);
120fa265e59SJames Clark 
121973aeb3cSIan Rogers 
122973aeb3cSIan Rogers /**
123973aeb3cSIan Rogers  * aggr_cpu_id__socket - Create an aggr_cpu_id with the socket populated with
124973aeb3cSIan Rogers  * the socket for cpu. The function signature is compatible with
125973aeb3cSIan Rogers  * aggr_cpu_id_get_t.
126973aeb3cSIan Rogers  */
1276d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__socket(struct perf_cpu cpu, void *data);
128973aeb3cSIan Rogers /**
129973aeb3cSIan Rogers  * aggr_cpu_id__die - Create an aggr_cpu_id with the die and socket populated
130973aeb3cSIan Rogers  * with the die and socket for cpu. The function signature is compatible with
131973aeb3cSIan Rogers  * aggr_cpu_id_get_t.
132973aeb3cSIan Rogers  */
1336d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__die(struct perf_cpu cpu, void *data);
134973aeb3cSIan Rogers /**
135*cbc917a1SYicong Yang  * aggr_cpu_id__cluster - Create an aggr_cpu_id with cluster, die and socket
136*cbc917a1SYicong Yang  * populated with the cluster, die and socket for cpu. The function signature
137*cbc917a1SYicong Yang  * is compatible with aggr_cpu_id_get_t.
138*cbc917a1SYicong Yang  */
139*cbc917a1SYicong Yang struct aggr_cpu_id aggr_cpu_id__cluster(struct perf_cpu cpu, void *data);
140*cbc917a1SYicong Yang /**
141*cbc917a1SYicong Yang  * aggr_cpu_id__core - Create an aggr_cpu_id with the core, cluster, die and
142*cbc917a1SYicong Yang  * socket populated with the core, die and socket for cpu. The function
143*cbc917a1SYicong Yang  * signature is compatible with aggr_cpu_id_get_t.
144973aeb3cSIan Rogers  */
1456d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__core(struct perf_cpu cpu, void *data);
146973aeb3cSIan Rogers /**
14734794913SIan Rogers  * aggr_cpu_id__core - Create an aggr_cpu_id with the cpu, core, die and socket
14834794913SIan Rogers  * populated with the cpu, core, die and socket for cpu. The function signature
14934794913SIan Rogers  * is compatible with aggr_cpu_id_get_t.
15034794913SIan Rogers  */
1516d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__cpu(struct perf_cpu cpu, void *data);
15234794913SIan Rogers /**
153973aeb3cSIan Rogers  * aggr_cpu_id__node - Create an aggr_cpu_id with the numa node populated for
154973aeb3cSIan Rogers  * cpu. The function signature is compatible with aggr_cpu_id_get_t.
155973aeb3cSIan Rogers  */
1566d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__node(struct perf_cpu cpu, void *data);
157375369abSNamhyung Kim /**
158375369abSNamhyung Kim  * aggr_cpu_id__global - Create an aggr_cpu_id for global aggregation.
159375369abSNamhyung Kim  * The function signature is compatible with aggr_cpu_id_get_t.
160375369abSNamhyung Kim  */
161375369abSNamhyung Kim struct aggr_cpu_id aggr_cpu_id__global(struct perf_cpu cpu, void *data);
162a12b51c4SPaul Mackerras #endif /* __PERF_CPUMAP_H */
163