xref: /linux/tools/perf/util/cpumap.h (revision db5742b6849ed7f01d764e6755b8ab2b422f29b2)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2a12b51c4SPaul Mackerras #ifndef __PERF_CPUMAP_H
3a12b51c4SPaul Mackerras #define __PERF_CPUMAP_H
4a12b51c4SPaul Mackerras 
59ae7d335SArnaldo Carvalho de Melo #include <stdio.h>
6a14bb7a6SArnaldo Carvalho de Melo #include <stdbool.h>
7ec09a42aSElena Reshetova #include <linux/refcount.h>
89ae7d335SArnaldo Carvalho de Melo 
97780c25bSDon Zickus #include "perf.h"
107780c25bSDon Zickus #include "util/debug.h"
117780c25bSDon Zickus 
1260d567e2SArnaldo Carvalho de Melo struct cpu_map {
13ec09a42aSElena Reshetova 	refcount_t refcnt;
1460d567e2SArnaldo Carvalho de Melo 	int nr;
1560d567e2SArnaldo Carvalho de Melo 	int map[];
1660d567e2SArnaldo Carvalho de Melo };
1760d567e2SArnaldo Carvalho de Melo 
1860d567e2SArnaldo Carvalho de Melo struct cpu_map *cpu_map__new(const char *cpu_list);
192322f573SJiri Olsa struct cpu_map *cpu_map__empty_new(int nr);
2060d567e2SArnaldo Carvalho de Melo struct cpu_map *cpu_map__dummy_new(void);
21f77b57adSJiri Olsa struct cpu_map *cpu_map__new_data(struct cpu_map_data *data);
227ae92e74SYan, Zheng struct cpu_map *cpu_map__read(FILE *file);
23a24020e6SJiri Olsa size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size);
244400ac8aSNamhyung Kim size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size);
259ae7d335SArnaldo Carvalho de Melo size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp);
26193b6bd3SKan Liang int cpu_map__get_socket_id(int cpu);
271fe7a300SJiri Olsa int cpu_map__get_socket(struct cpu_map *map, int idx, void *data);
28b74d8686SKan Liang int cpu_map__get_die_id(int cpu);
29*db5742b6SKan Liang int cpu_map__get_die(struct cpu_map *map, int idx, void *data);
30193b6bd3SKan Liang int cpu_map__get_core_id(int cpu);
311fe7a300SJiri Olsa int cpu_map__get_core(struct cpu_map *map, int idx, void *data);
325ac59a8aSStephane Eranian int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp);
33*db5742b6SKan Liang int cpu_map__build_die_map(struct cpu_map *cpus, struct cpu_map **diep);
3412c08a9fSStephane Eranian int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep);
35f13de660SAlexey Budankov const struct cpu_map *cpu_map__online(void); /* thread unsafe */
365ac59a8aSStephane Eranian 
37f30a79b0SJiri Olsa struct cpu_map *cpu_map__get(struct cpu_map *map);
38f30a79b0SJiri Olsa void cpu_map__put(struct cpu_map *map);
39f30a79b0SJiri Olsa 
405ac59a8aSStephane Eranian static inline int cpu_map__socket(struct cpu_map *sock, int s)
415ac59a8aSStephane Eranian {
425ac59a8aSStephane Eranian 	if (!sock || s > sock->nr || s < 0)
435ac59a8aSStephane Eranian 		return 0;
445ac59a8aSStephane Eranian 	return sock->map[s];
455ac59a8aSStephane Eranian }
469ae7d335SArnaldo Carvalho de Melo 
4712c08a9fSStephane Eranian static inline int cpu_map__id_to_socket(int id)
4812c08a9fSStephane Eranian {
49*db5742b6SKan Liang 	return id >> 24;
50*db5742b6SKan Liang }
51*db5742b6SKan Liang 
52*db5742b6SKan Liang static inline int cpu_map__id_to_die(int id)
53*db5742b6SKan Liang {
54*db5742b6SKan Liang 	return (id >> 16) & 0xff;
5512c08a9fSStephane Eranian }
5612c08a9fSStephane Eranian 
5712c08a9fSStephane Eranian static inline int cpu_map__id_to_cpu(int id)
5812c08a9fSStephane Eranian {
5912c08a9fSStephane Eranian 	return id & 0xffff;
6012c08a9fSStephane Eranian }
6112c08a9fSStephane Eranian 
62a14bb7a6SArnaldo Carvalho de Melo static inline int cpu_map__nr(const struct cpu_map *map)
63a14bb7a6SArnaldo Carvalho de Melo {
64a14bb7a6SArnaldo Carvalho de Melo 	return map ? map->nr : 1;
65a14bb7a6SArnaldo Carvalho de Melo }
66a14bb7a6SArnaldo Carvalho de Melo 
67ec1e7e43SSukadev Bhattiprolu static inline bool cpu_map__empty(const struct cpu_map *map)
68a14bb7a6SArnaldo Carvalho de Melo {
69a14bb7a6SArnaldo Carvalho de Melo 	return map ? map->map[0] == -1 : true;
70a14bb7a6SArnaldo Carvalho de Melo }
71a14bb7a6SArnaldo Carvalho de Melo 
727780c25bSDon Zickus int cpu__setup_cpunode_map(void);
737780c25bSDon Zickus 
745ac76283SArnaldo Carvalho de Melo int cpu__max_node(void);
755ac76283SArnaldo Carvalho de Melo int cpu__max_cpu(void);
7692a7e127SJan Stancek int cpu__max_present_cpu(void);
775ac76283SArnaldo Carvalho de Melo int cpu__get_node(int cpu);
787780c25bSDon Zickus 
79f1cbb8f3SJiri Olsa int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
801fe7a300SJiri Olsa 		       int (*f)(struct cpu_map *map, int cpu, void *data),
811fe7a300SJiri Olsa 		       void *data);
82e632aa69SJiri Olsa 
839a6c582dSMark Rutland int cpu_map__cpu(struct cpu_map *cpus, int idx);
84e632aa69SJiri Olsa bool cpu_map__has(struct cpu_map *cpus, int cpu);
859a6c582dSMark Rutland int cpu_map__idx(struct cpu_map *cpus, int cpu);
86a12b51c4SPaul Mackerras #endif /* __PERF_CPUMAP_H */
87