xref: /linux/tools/lib/perf/include/perf/cpumap.h (revision 7685b334d1e4927cc73b62c65293ba65748d9c52)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBPERF_CPUMAP_H
3 #define __LIBPERF_CPUMAP_H
4 
5 #include <perf/core.h>
6 #include <stdbool.h>
7 
8 /** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
9 struct perf_cpu {
10 	int cpu;
11 };
12 
13 struct perf_cache {
14 	int cache_lvl;
15 	int cache;
16 };
17 
18 struct perf_cpu_map;
19 
20 /**
21  * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value.
22  */
23 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(void);
24 /**
25  * perf_cpu_map__new_online_cpus - a map read from
26  *                                 /sys/devices/system/cpu/online if
27  *                                 available. If reading wasn't possible a map
28  *                                 is created using the online processors
29  *                                 assuming the first 'n' processors are all
30  *                                 online.
31  */
32 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void);
33 /**
34  * perf_cpu_map__new - create a map from the given cpu_list such as "0-7". If no
35  *                     cpu_list argument is provided then
36  *                     perf_cpu_map__new_online_cpus is returned.
37  */
38 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
39 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
40 LIBPERF_API int perf_cpu_map__merge(struct perf_cpu_map **orig,
41 				    struct perf_cpu_map *other);
42 LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig,
43 							 struct perf_cpu_map *other);
44 LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);
45 /**
46  * perf_cpu_map__cpu - get the CPU value at the given index. Returns -1 if index
47  *                     is invalid.
48  */
49 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx);
50 /**
51  * perf_cpu_map__nr - for an empty map returns 1, as perf_cpu_map__cpu returns a
52  *                    cpu of -1 for an invalid index, this makes an empty map
53  *                    look like it contains the "any CPU"/dummy value. Otherwise
54  *                    the result is the number CPUs in the map plus one if the
55  *                    "any CPU"/dummy value is present.
56  */
57 LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
58 /**
59  * perf_cpu_map__has_any_cpu_or_is_empty - is map either empty or has the "any CPU"/dummy value.
60  */
61 LIBPERF_API bool perf_cpu_map__has_any_cpu_or_is_empty(const struct perf_cpu_map *map);
62 /**
63  * perf_cpu_map__is_any_cpu_or_is_empty - is map either empty or the "any CPU"/dummy value.
64  */
65 LIBPERF_API bool perf_cpu_map__is_any_cpu_or_is_empty(const struct perf_cpu_map *map);
66 /**
67  * perf_cpu_map__is_empty - does the map contain no values and it doesn't
68  *                          contain the special "any CPU"/dummy value.
69  */
70 LIBPERF_API bool perf_cpu_map__is_empty(const struct perf_cpu_map *map);
71 /**
72  * perf_cpu_map__min - the minimum CPU value or -1 if empty or just the "any CPU"/dummy value.
73  */
74 LIBPERF_API struct perf_cpu perf_cpu_map__min(const struct perf_cpu_map *map);
75 /**
76  * perf_cpu_map__max - the maximum CPU value or -1 if empty or just the "any CPU"/dummy value.
77  */
78 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map);
79 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);
80 LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs,
81 				     const struct perf_cpu_map *rhs);
82 /**
83  * perf_cpu_map__any_cpu - Does the map contain the "any CPU"/dummy -1 value?
84  */
85 LIBPERF_API bool perf_cpu_map__has_any_cpu(const struct perf_cpu_map *map);
86 
87 #define perf_cpu_map__for_each_cpu(cpu, idx, cpus)		\
88 	for ((idx) = 0, (cpu) = perf_cpu_map__cpu(cpus, idx);	\
89 	     (idx) < perf_cpu_map__nr(cpus);			\
90 	     (idx)++, (cpu) = perf_cpu_map__cpu(cpus, idx))
91 
92 #define perf_cpu_map__for_each_cpu_skip_any(_cpu, idx, cpus)	\
93 	for ((idx) = 0, (_cpu) = perf_cpu_map__cpu(cpus, idx);	\
94 	     (idx) < perf_cpu_map__nr(cpus);			\
95 	     (idx)++, (_cpu) = perf_cpu_map__cpu(cpus, idx))	\
96 		if ((_cpu).cpu != -1)
97 
98 #define perf_cpu_map__for_each_idx(idx, cpus)				\
99 	for ((idx) = 0; (idx) < perf_cpu_map__nr(cpus); (idx)++)
100 
101 #endif /* __LIBPERF_CPUMAP_H */
102