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