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;
2349679da3SIan Rogers /** The cache level as read from /sys/devices/system/cpu/cpuX/cache/indexY/level */
24b9933817SJames Clark int cache_lvl;
2534794913SIan Rogers /**
266d18804bSIan Rogers * The cache instance ID, which is the first CPU in the
27fa265e59SJames Clark * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
28fa265e59SJames Clark */
2992aad5c3SIan Rogers int cache;
30cea6575fSJames Clark /** The core id as read from /sys/devices/system/cpu/cpuX/topology/core_id. */
31cea6575fSJames Clark int core;
3292aad5c3SIan Rogers /** CPU aggregation, note there is one CPU for each SMT thread. */
33cea6575fSJames Clark struct perf_cpu cpu;
3492aad5c3SIan Rogers };
35ff523295SJames Clark
36cea6575fSJames Clark /** A collection of aggr_cpu_id values, the "built" version is sorted and uniqued. */
37cea6575fSJames Clark struct cpu_aggr_map {
38*8945bef3SIan Rogers refcount_t refcnt;
39*8945bef3SIan Rogers /** Number of valid entries. */
40*8945bef3SIan Rogers int nr;
4172932371SJiri Olsa /** The entries. */
427780c25bSDon Zickus struct aggr_cpu_id map[];
43b2f10cd4SIan Rogers };
44b2f10cd4SIan Rogers
45315c0a1fSJiri Olsa #define cpu_aggr_map__for_each_idx(idx, aggr_map) \
46cea6575fSJames Clark for ((idx) = 0; (idx) < aggr_map->nr; (idx)++)
47b2f10cd4SIan Rogers
48f854839bSJiri Olsa struct perf_record_cpu_map_data;
49f854839bSJiri Olsa
50f854839bSJiri Olsa bool perf_record_cpu_map_data__test_bit(int i, const struct perf_record_cpu_map_data *data);
51f854839bSJiri Olsa
525ac59a8aSStephane Eranian struct perf_cpu_map *perf_cpu_map__empty_new(int nr);
537780c25bSDon Zickus
547780c25bSDon Zickus struct perf_cpu_map *cpu_map__new_data(const struct perf_record_cpu_map_data *data);
555ac76283SArnaldo Carvalho de Melo size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size);
566d18804bSIan Rogers size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size);
576d18804bSIan Rogers size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp);
5837be5858SArnaldo Carvalho de Melo struct perf_cpu_map *cpu_map__online(void); /* thread unsafe */
5937be5858SArnaldo Carvalho de Melo
6037be5858SArnaldo Carvalho de Melo int cpu__setup_cpunode_map(void);
6137be5858SArnaldo Carvalho de Melo
6237be5858SArnaldo Carvalho de Melo int cpu__max_node(void);
6337be5858SArnaldo Carvalho de Melo struct perf_cpu cpu__max_cpu(void);
6444028699SIan Rogers struct perf_cpu cpu__max_present_cpu(void);
6537be5858SArnaldo Carvalho de Melo
6637be5858SArnaldo Carvalho de Melo /**
67194a3a20SIan Rogers * cpu_map__is_dummy - Events associated with a pid, rather than a CPU, use a single dummy map with an entry of -1.
68194a3a20SIan Rogers */
cpu_map__is_dummy(const struct perf_cpu_map * cpus)69194a3a20SIan Rogers static inline bool cpu_map__is_dummy(const struct perf_cpu_map *cpus)
70194a3a20SIan Rogers {
716d18804bSIan Rogers return perf_cpu_map__nr(cpus) == 1 && perf_cpu_map__cpu(cpus, 0).cpu == -1;
724e90e5ccSIan Rogers }
734e90e5ccSIan Rogers
744e90e5ccSIan Rogers /**
754e90e5ccSIan Rogers * cpu__get_node - Returns the numa node X as read from
766d18804bSIan Rogers * /sys/devices/system/node/nodeX for the given CPU.
774e90e5ccSIan Rogers */
784e90e5ccSIan Rogers int cpu__get_node(struct perf_cpu cpu);
794e90e5ccSIan Rogers /**
804e90e5ccSIan Rogers * cpu__get_socket_id - Returns the socket number as read from
816d18804bSIan Rogers * /sys/devices/system/cpu/cpuX/topology/physical_package_id for the given CPU.
824e90e5ccSIan Rogers */
834e90e5ccSIan 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
866d18804bSIan Rogers * /sys/devices/system/cpu/cpuX/topology/die_id for the given CPU.
874e90e5ccSIan Rogers */
8892aad5c3SIan Rogers int cpu__get_die_id(struct perf_cpu cpu);
8992aad5c3SIan Rogers /**
9092aad5c3SIan Rogers * cpu__get_core_id - Returns the core id as read from
9192aad5c3SIan Rogers * /sys/devices/system/cpu/cpuX/topology/core_id for the given CPU.
9292aad5c3SIan Rogers */
9392aad5c3SIan Rogers int cpu__get_core_id(struct perf_cpu cpu);
946d18804bSIan Rogers
957780c25bSDon Zickus /**
965f50e15cSIan Rogers * cpu_aggr_map__empty_new - Create a cpu_aggr_map of size nr with every entry
975f50e15cSIan Rogers * being empty.
985f50e15cSIan Rogers */
995f50e15cSIan Rogers struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr);
1005f50e15cSIan Rogers
1015f50e15cSIan Rogers typedef struct aggr_cpu_id (*aggr_cpu_id_get_t)(struct perf_cpu cpu, void *data);
1025f50e15cSIan Rogers
103505ac48bSNamhyung Kim /**
104e632aa69SJiri Olsa * cpu_aggr_map__new - Create a cpu_aggr_map with an aggr_cpu_id for each cpu in
1053ac23d19SIan Rogers * cpus. The aggr_cpu_id is created with 'get_id' that may have a data value
10651b826faSIan Rogers * passed to it. The cpu_aggr_map is sorted with duplicate values removed.
10751b826faSIan Rogers */
108fa265e59SJames Clark struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
109973aeb3cSIan Rogers aggr_cpu_id_get_t get_id,
110973aeb3cSIan Rogers void *data, bool needs_sort);
111973aeb3cSIan Rogers
112973aeb3cSIan Rogers bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b);
113973aeb3cSIan Rogers bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a);
114973aeb3cSIan Rogers struct aggr_cpu_id aggr_cpu_id__empty(void);
1156d18804bSIan Rogers
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.
1216d18804bSIan Rogers */
122973aeb3cSIan 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.
1276d18804bSIan Rogers */
128973aeb3cSIan Rogers struct aggr_cpu_id aggr_cpu_id__die(struct perf_cpu cpu, void *data);
12934794913SIan Rogers /**
13034794913SIan Rogers * aggr_cpu_id__core - Create an aggr_cpu_id with the core, die and socket
13134794913SIan Rogers * populated with the core, die and socket for cpu. The function signature is
13234794913SIan Rogers * compatible with aggr_cpu_id_get_t.
1336d18804bSIan Rogers */
13434794913SIan Rogers struct aggr_cpu_id aggr_cpu_id__core(struct perf_cpu cpu, void *data);
135973aeb3cSIan Rogers /**
136973aeb3cSIan Rogers * aggr_cpu_id__core - Create an aggr_cpu_id with the cpu, core, die and socket
137973aeb3cSIan Rogers * populated with the cpu, core, die and socket for cpu. The function signature
1386d18804bSIan Rogers * is compatible with aggr_cpu_id_get_t.
139375369abSNamhyung Kim */
140375369abSNamhyung Kim struct aggr_cpu_id aggr_cpu_id__cpu(struct perf_cpu cpu, void *data);
141375369abSNamhyung Kim /**
142375369abSNamhyung Kim * aggr_cpu_id__node - Create an aggr_cpu_id with the numa node populated for
143375369abSNamhyung Kim * cpu. The function signature is compatible with aggr_cpu_id_get_t.
144a12b51c4SPaul Mackerras */
145 struct aggr_cpu_id aggr_cpu_id__node(struct perf_cpu cpu, void *data);
146 /**
147 * aggr_cpu_id__global - Create an aggr_cpu_id for global aggregation.
148 * The function signature is compatible with aggr_cpu_id_get_t.
149 */
150 struct aggr_cpu_id aggr_cpu_id__global(struct perf_cpu cpu, void *data);
151 #endif /* __PERF_CPUMAP_H */
152