1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <perf/cpumap.h>
4 #include <linux/refcount.h>
5 #include <internal/cpumap.h>
13 static struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus)
15 struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus);
19 refcount_set(&cpus->refcnt, 1);
25 struct perf_cpu_map *perf_cpu_map__dummy_new(void)
27 struct perf_cpu_map *cpus = perf_cpu_map__alloc(1);
30 cpus->map[0].cpu = -1;
35 static void cpu_map__delete(struct perf_cpu_map *map)
38 WARN_ONCE(refcount_read(&map->refcnt) != 0,
39 "cpu_map refcnt unbalanced\n");
44 struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map)
47 refcount_inc(&map->refcnt);
51 void perf_cpu_map__put(struct perf_cpu_map *map)
53 if (map && refcount_dec_and_test(&map->refcnt))
57 static struct perf_cpu_map *cpu_map__default_new(void)
59 struct perf_cpu_map *cpus;
62 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
66 cpus = perf_cpu_map__alloc(nr_cpus);
70 for (i = 0; i < nr_cpus; ++i)
77 struct perf_cpu_map *perf_cpu_map__default_new(void)
79 return cpu_map__default_new();
83 static int cmp_cpu(const void *a, const void *b)
85 const struct perf_cpu *cpu_a = a, *cpu_b = b;
87 return cpu_a->cpu - cpu_b->cpu;
90 static struct perf_cpu_map *cpu_map__trim_new(int nr_cpus, const struct perf_cpu *tmp_cpus)
92 size_t payload_size = nr_cpus * sizeof(struct perf_cpu);
93 struct perf_cpu_map *cpus = perf_cpu_map__alloc(nr_cpus);
97 memcpy(cpus->map, tmp_cpus, payload_size);
98 qsort(cpus->map, nr_cpus, sizeof(struct perf_cpu), cmp_cpu);
101 for (i = 0; i < nr_cpus; i++) {
102 if (i == 0 || cpus->map[i].cpu != cpus->map[i - 1].cpu)
103 cpus->map[j++].cpu = cpus->map[i].cpu;
106 assert(j <= nr_cpus);
111 struct perf_cpu_map *perf_cpu_map__read(FILE *file)
113 struct perf_cpu_map *cpus = NULL;
115 struct perf_cpu *tmp_cpus = NULL, *tmp;
123 n = fscanf(file, "%u%c", &cpu, &sep);
127 int new_max = nr_cpus + cpu - prev - 1;
129 WARN_ONCE(new_max >= MAX_NR_CPUS, "Perf can support %d CPUs. "
130 "Consider raising MAX_NR_CPUS\n", MAX_NR_CPUS);
132 if (new_max >= max_entries) {
133 max_entries = new_max + MAX_NR_CPUS / 2;
134 tmp = realloc(tmp_cpus, max_entries * sizeof(struct perf_cpu));
141 tmp_cpus[nr_cpus++].cpu = prev;
143 if (nr_cpus == max_entries) {
144 max_entries += MAX_NR_CPUS;
145 tmp = realloc(tmp_cpus, max_entries * sizeof(struct perf_cpu));
151 tmp_cpus[nr_cpus++].cpu = cpu;
152 if (n == 2 && sep == '-')
156 if (n == 1 || sep == '\n')
161 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
163 cpus = cpu_map__default_new();
169 static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
171 struct perf_cpu_map *cpus = NULL;
174 onlnf = fopen("/sys/devices/system/cpu/online", "r");
176 return cpu_map__default_new();
178 cpus = perf_cpu_map__read(onlnf);
183 struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
185 struct perf_cpu_map *cpus = NULL;
186 unsigned long start_cpu, end_cpu = 0;
189 struct perf_cpu *tmp_cpus = NULL, *tmp;
193 return cpu_map__read_all_cpu_map();
196 * must handle the case of empty cpumap to cover
197 * TOPOLOGY header for NUMA nodes with no CPU
198 * ( e.g., because of CPU hotplug)
200 if (!isdigit(*cpu_list) && *cpu_list != '\0')
203 while (isdigit(*cpu_list)) {
205 start_cpu = strtoul(cpu_list, &p, 0);
206 if (start_cpu >= INT_MAX
207 || (*p != '\0' && *p != ',' && *p != '-'))
213 end_cpu = strtoul(cpu_list, &p, 0);
215 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
218 if (end_cpu < start_cpu)
224 WARN_ONCE(end_cpu >= MAX_NR_CPUS, "Perf can support %d CPUs. "
225 "Consider raising MAX_NR_CPUS\n", MAX_NR_CPUS);
227 for (; start_cpu <= end_cpu; start_cpu++) {
228 /* check for duplicates */
229 for (i = 0; i < nr_cpus; i++)
230 if (tmp_cpus[i].cpu == (int)start_cpu)
233 if (nr_cpus == max_entries) {
234 max_entries += MAX_NR_CPUS;
235 tmp = realloc(tmp_cpus, max_entries * sizeof(struct perf_cpu));
240 tmp_cpus[nr_cpus++].cpu = (int)start_cpu;
249 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
250 else if (*cpu_list != '\0')
251 cpus = cpu_map__default_new();
253 cpus = perf_cpu_map__dummy_new();
260 struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
262 struct perf_cpu result = {
266 if (cpus && idx < cpus->nr)
267 return cpus->map[idx];
272 int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
274 return cpus ? cpus->nr : 1;
277 bool perf_cpu_map__empty(const struct perf_cpu_map *map)
279 return map ? map->map[0].cpu == -1 : true;
282 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
292 int idx = (low + high) / 2;
293 struct perf_cpu cpu_at_idx = cpus->map[idx];
295 if (cpu_at_idx.cpu == cpu.cpu)
298 if (cpu_at_idx.cpu > cpu.cpu)
307 bool perf_cpu_map__has(const struct perf_cpu_map *cpus, struct perf_cpu cpu)
309 return perf_cpu_map__idx(cpus, cpu) != -1;
312 struct perf_cpu perf_cpu_map__max(struct perf_cpu_map *map)
314 struct perf_cpu result = {
318 // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well.
319 return map->nr > 0 ? map->map[map->nr - 1] : result;
322 /** Is 'b' a subset of 'a'. */
323 bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpu_map *b)
327 if (!a || b->nr > a->nr)
330 for (int i = 0, j = 0; i < a->nr; i++) {
331 if (a->map[i].cpu > b->map[j].cpu)
333 if (a->map[i].cpu == b->map[j].cpu) {
345 * orig either gets freed and replaced with a new map, or reused
346 * with no reference count change (similar to "realloc")
347 * other has its reference count increased.
350 struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
351 struct perf_cpu_map *other)
353 struct perf_cpu *tmp_cpus;
356 struct perf_cpu_map *merged;
358 if (perf_cpu_map__is_subset(orig, other))
360 if (perf_cpu_map__is_subset(other, orig)) {
361 perf_cpu_map__put(orig);
362 return perf_cpu_map__get(other);
365 tmp_len = orig->nr + other->nr;
366 tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));
370 /* Standard merge algorithm from wikipedia */
372 while (i < orig->nr && j < other->nr) {
373 if (orig->map[i].cpu <= other->map[j].cpu) {
374 if (orig->map[i].cpu == other->map[j].cpu)
376 tmp_cpus[k++] = orig->map[i++];
378 tmp_cpus[k++] = other->map[j++];
382 tmp_cpus[k++] = orig->map[i++];
384 while (j < other->nr)
385 tmp_cpus[k++] = other->map[j++];
386 assert(k <= tmp_len);
388 merged = cpu_map__trim_new(k, tmp_cpus);
390 perf_cpu_map__put(orig);