1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <perf/cpumap.h>
4 #include <linux/refcount.h>
5 #include <internal/cpumap.h>
13 struct perf_cpu_map *perf_cpu_map__dummy_new(void)
15 struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
20 refcount_set(&cpus->refcnt, 1);
26 static void cpu_map__delete(struct perf_cpu_map *map)
29 WARN_ONCE(refcount_read(&map->refcnt) != 0,
30 "cpu_map refcnt unbalanced\n");
35 struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map)
38 refcount_inc(&map->refcnt);
42 void perf_cpu_map__put(struct perf_cpu_map *map)
44 if (map && refcount_dec_and_test(&map->refcnt))
48 static struct perf_cpu_map *cpu_map__default_new(void)
50 struct perf_cpu_map *cpus;
53 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
57 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
61 for (i = 0; i < nr_cpus; ++i)
65 refcount_set(&cpus->refcnt, 1);
71 struct perf_cpu_map *perf_cpu_map__default_new(void)
73 return cpu_map__default_new();
76 static int cmp_int(const void *a, const void *b)
78 return *(const int *)a - *(const int*)b;
81 static struct perf_cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
83 size_t payload_size = nr_cpus * sizeof(int);
84 struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
88 memcpy(cpus->map, tmp_cpus, payload_size);
89 qsort(cpus->map, nr_cpus, sizeof(int), cmp_int);
92 for (i = 0; i < nr_cpus; i++) {
93 if (i == 0 || cpus->map[i] != cpus->map[i - 1])
94 cpus->map[j++] = cpus->map[i];
98 refcount_set(&cpus->refcnt, 1);
104 struct perf_cpu_map *perf_cpu_map__read(FILE *file)
106 struct perf_cpu_map *cpus = NULL;
108 int *tmp_cpus = NULL, *tmp;
116 n = fscanf(file, "%u%c", &cpu, &sep);
120 int new_max = nr_cpus + cpu - prev - 1;
122 WARN_ONCE(new_max >= MAX_NR_CPUS, "Perf can support %d CPUs. "
123 "Consider raising MAX_NR_CPUS\n", MAX_NR_CPUS);
125 if (new_max >= max_entries) {
126 max_entries = new_max + MAX_NR_CPUS / 2;
127 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
134 tmp_cpus[nr_cpus++] = prev;
136 if (nr_cpus == max_entries) {
137 max_entries += MAX_NR_CPUS;
138 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
144 tmp_cpus[nr_cpus++] = cpu;
145 if (n == 2 && sep == '-')
149 if (n == 1 || sep == '\n')
154 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
156 cpus = cpu_map__default_new();
162 static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
164 struct perf_cpu_map *cpus = NULL;
167 onlnf = fopen("/sys/devices/system/cpu/online", "r");
169 return cpu_map__default_new();
171 cpus = perf_cpu_map__read(onlnf);
176 struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
178 struct perf_cpu_map *cpus = NULL;
179 unsigned long start_cpu, end_cpu = 0;
182 int *tmp_cpus = NULL, *tmp;
186 return cpu_map__read_all_cpu_map();
189 * must handle the case of empty cpumap to cover
190 * TOPOLOGY header for NUMA nodes with no CPU
191 * ( e.g., because of CPU hotplug)
193 if (!isdigit(*cpu_list) && *cpu_list != '\0')
196 while (isdigit(*cpu_list)) {
198 start_cpu = strtoul(cpu_list, &p, 0);
199 if (start_cpu >= INT_MAX
200 || (*p != '\0' && *p != ',' && *p != '-'))
206 end_cpu = strtoul(cpu_list, &p, 0);
208 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
211 if (end_cpu < start_cpu)
217 WARN_ONCE(end_cpu >= MAX_NR_CPUS, "Perf can support %d CPUs. "
218 "Consider raising MAX_NR_CPUS\n", MAX_NR_CPUS);
220 for (; start_cpu <= end_cpu; start_cpu++) {
221 /* check for duplicates */
222 for (i = 0; i < nr_cpus; i++)
223 if (tmp_cpus[i] == (int)start_cpu)
226 if (nr_cpus == max_entries) {
227 max_entries += MAX_NR_CPUS;
228 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
233 tmp_cpus[nr_cpus++] = (int)start_cpu;
242 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
243 else if (*cpu_list != '\0')
244 cpus = cpu_map__default_new();
246 cpus = perf_cpu_map__dummy_new();
253 int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
255 if (cpus && idx < cpus->nr)
256 return cpus->map[idx];
261 int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
263 return cpus ? cpus->nr : 1;
266 bool perf_cpu_map__empty(const struct perf_cpu_map *map)
268 return map ? map->map[0] == -1 : true;
271 int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
273 int low = 0, high = cpus->nr;
276 int idx = (low + high) / 2,
277 cpu_at_idx = cpus->map[idx];
279 if (cpu_at_idx == cpu)
282 if (cpu_at_idx > cpu)
291 int perf_cpu_map__max(struct perf_cpu_map *map)
293 // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well.
294 return map->nr > 0 ? map->map[map->nr - 1] : -1;
300 * orig either gets freed and replaced with a new map, or reused
301 * with no reference count change (similar to "realloc")
302 * other has its reference count increased.
305 struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
306 struct perf_cpu_map *other)
311 struct perf_cpu_map *merged;
316 perf_cpu_map__get(other);
321 if (orig->nr == other->nr &&
322 !memcmp(orig->map, other->map, orig->nr * sizeof(int)))
325 tmp_len = orig->nr + other->nr;
326 tmp_cpus = malloc(tmp_len * sizeof(int));
330 /* Standard merge algorithm from wikipedia */
332 while (i < orig->nr && j < other->nr) {
333 if (orig->map[i] <= other->map[j]) {
334 if (orig->map[i] == other->map[j])
336 tmp_cpus[k++] = orig->map[i++];
338 tmp_cpus[k++] = other->map[j++];
342 tmp_cpus[k++] = orig->map[i++];
344 while (j < other->nr)
345 tmp_cpus[k++] = other->map[j++];
346 assert(k <= tmp_len);
348 merged = cpu_map__trim_new(k, tmp_cpus);
350 perf_cpu_map__put(orig);