xref: /openbmc/linux/tools/perf/util/cpumap.h (revision a0c41cae)
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>
8c4d9d95fSIan Rogers #include <linux/refcount.h>
99ae7d335SArnaldo Carvalho de Melo 
1049679da3SIan Rogers /** Identify where counts are aggregated, -1 implies not to aggregate. */
11fa265e59SJames Clark struct aggr_cpu_id {
1249679da3SIan Rogers 	/** A value in the range 0 to number of threads. */
13fa2edc07SNamhyung Kim 	int thread_idx;
1449679da3SIan Rogers 	/** The numa node X as read from /sys/devices/system/node/nodeX. */
15fcd83a35SJames Clark 	int node;
1649679da3SIan Rogers 	/**
1749679da3SIan Rogers 	 * The socket number as read from
1849679da3SIan Rogers 	 * /sys/devices/system/cpu/cpuX/topology/physical_package_id.
1949679da3SIan Rogers 	 */
201a270cb6SJames Clark 	int socket;
2149679da3SIan Rogers 	/** The die id as read from /sys/devices/system/cpu/cpuX/topology/die_id. */
22ba2ee166SJames Clark 	int die;
23995ed074SK Prateek Nayak 	/** The cache level as read from /sys/devices/system/cpu/cpuX/cache/indexY/level */
24995ed074SK Prateek Nayak 	int cache_lvl;
25995ed074SK Prateek Nayak 	/**
26995ed074SK Prateek Nayak 	 * The cache instance ID, which is the first CPU in the
27995ed074SK Prateek Nayak 	 * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
28995ed074SK Prateek Nayak 	 */
29995ed074SK Prateek Nayak 	int cache;
3049679da3SIan Rogers 	/** The core id as read from /sys/devices/system/cpu/cpuX/topology/core_id. */
31b9933817SJames Clark 	int core;
3234794913SIan Rogers 	/** CPU aggregation, note there is one CPU for each SMT thread. */
336d18804bSIan Rogers 	struct perf_cpu cpu;
34fa265e59SJames Clark };
35fa265e59SJames Clark 
3692aad5c3SIan Rogers /** A collection of aggr_cpu_id values, the "built" version is sorted and uniqued. */
37cea6575fSJames Clark struct cpu_aggr_map {
38cea6575fSJames Clark 	refcount_t refcnt;
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);
58*a0c41caeSIan 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  */
cpu_map__is_dummy(const struct perf_cpu_map * cpus)69*a0c41caeSIan 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 /**
904e90e5ccSIan Rogers  * cpu__get_core_id - Returns the core id as read from
914e90e5ccSIan Rogers  * /sys/devices/system/cpu/cpuX/topology/core_id for the given CPU.
924e90e5ccSIan Rogers  */
936d18804bSIan Rogers int cpu__get_core_id(struct perf_cpu cpu);
944e90e5ccSIan Rogers 
9592aad5c3SIan Rogers /**
9692aad5c3SIan Rogers  * cpu_aggr_map__empty_new - Create a cpu_aggr_map of size nr with every entry
9792aad5c3SIan Rogers  * being empty.
9892aad5c3SIan Rogers  */
9992aad5c3SIan Rogers struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr);
10092aad5c3SIan Rogers 
1016d18804bSIan Rogers typedef struct aggr_cpu_id (*aggr_cpu_id_get_t)(struct perf_cpu cpu, void *data);
1027780c25bSDon Zickus 
1035f50e15cSIan Rogers /**
1045f50e15cSIan Rogers  * cpu_aggr_map__new - Create a cpu_aggr_map with an aggr_cpu_id for each cpu in
1055f50e15cSIan Rogers  * cpus. The aggr_cpu_id is created with 'get_id' that may have a data value
1065f50e15cSIan Rogers  * passed to it. The cpu_aggr_map is sorted with duplicate values removed.
1075f50e15cSIan Rogers  */
1085f50e15cSIan Rogers struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
1095f50e15cSIan Rogers 				       aggr_cpu_id_get_t get_id,
110505ac48bSNamhyung Kim 				       void *data, bool needs_sort);
111e632aa69SJiri Olsa 
1123ac23d19SIan Rogers bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b);
11351b826faSIan Rogers bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a);
11451b826faSIan Rogers struct aggr_cpu_id aggr_cpu_id__empty(void);
115fa265e59SJames Clark 
116973aeb3cSIan Rogers 
117973aeb3cSIan Rogers /**
118973aeb3cSIan Rogers  * aggr_cpu_id__socket - Create an aggr_cpu_id with the socket populated with
119973aeb3cSIan Rogers  * the socket for cpu. The function signature is compatible with
120973aeb3cSIan Rogers  * aggr_cpu_id_get_t.
121973aeb3cSIan Rogers  */
1226d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__socket(struct perf_cpu cpu, void *data);
123973aeb3cSIan Rogers /**
124973aeb3cSIan Rogers  * aggr_cpu_id__die - Create an aggr_cpu_id with the die and socket populated
125973aeb3cSIan Rogers  * with the die and socket for cpu. The function signature is compatible with
126973aeb3cSIan Rogers  * aggr_cpu_id_get_t.
127973aeb3cSIan Rogers  */
1286d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__die(struct perf_cpu cpu, void *data);
129973aeb3cSIan Rogers /**
130973aeb3cSIan Rogers  * aggr_cpu_id__core - Create an aggr_cpu_id with the core, die and socket
131973aeb3cSIan Rogers  * populated with the core, die and socket for cpu. The function signature is
132973aeb3cSIan Rogers  * compatible with aggr_cpu_id_get_t.
133973aeb3cSIan Rogers  */
1346d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__core(struct perf_cpu cpu, void *data);
135973aeb3cSIan Rogers /**
13634794913SIan Rogers  * aggr_cpu_id__core - Create an aggr_cpu_id with the cpu, core, die and socket
13734794913SIan Rogers  * populated with the cpu, core, die and socket for cpu. The function signature
13834794913SIan Rogers  * is compatible with aggr_cpu_id_get_t.
13934794913SIan Rogers  */
1406d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__cpu(struct perf_cpu cpu, void *data);
14134794913SIan Rogers /**
142973aeb3cSIan Rogers  * aggr_cpu_id__node - Create an aggr_cpu_id with the numa node populated for
143973aeb3cSIan Rogers  * cpu. The function signature is compatible with aggr_cpu_id_get_t.
144973aeb3cSIan Rogers  */
1456d18804bSIan Rogers struct aggr_cpu_id aggr_cpu_id__node(struct perf_cpu cpu, void *data);
146375369abSNamhyung Kim /**
147375369abSNamhyung Kim  * aggr_cpu_id__global - Create an aggr_cpu_id for global aggregation.
148375369abSNamhyung Kim  * The function signature is compatible with aggr_cpu_id_get_t.
149375369abSNamhyung Kim  */
150375369abSNamhyung Kim struct aggr_cpu_id aggr_cpu_id__global(struct perf_cpu cpu, void *data);
151a12b51c4SPaul Mackerras #endif /* __PERF_CPUMAP_H */
152