^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <internal/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct perf_cpu_map *perf_cpu_map__dummy_new(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (cpus != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) cpus->nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) cpus->map[0] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) refcount_set(&cpus->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static void cpu_map__delete(struct perf_cpu_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) WARN_ONCE(refcount_read(&map->refcnt) != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) "cpu_map refcnt unbalanced\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) free(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) refcount_inc(&map->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void perf_cpu_map__put(struct perf_cpu_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (map && refcount_dec_and_test(&map->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) cpu_map__delete(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static struct perf_cpu_map *cpu_map__default_new(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct perf_cpu_map *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int nr_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (nr_cpus < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (cpus != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for (i = 0; i < nr_cpus; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) cpus->map[i] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) cpus->nr = nr_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) refcount_set(&cpus->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int cmp_int(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return *(const int *)a - *(const int*)b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static struct perf_cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) size_t payload_size = nr_cpus * sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (cpus != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) memcpy(cpus->map, tmp_cpus, payload_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) qsort(cpus->map, nr_cpus, sizeof(int), cmp_int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Remove dups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (i = 0; i < nr_cpus; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (i == 0 || cpus->map[i] != cpus->map[i - 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cpus->map[j++] = cpus->map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) cpus->nr = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) assert(j <= nr_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) refcount_set(&cpus->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct perf_cpu_map *perf_cpu_map__read(FILE *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct perf_cpu_map *cpus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int nr_cpus = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int *tmp_cpus = NULL, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int max_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int n, cpu, prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) char sep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) sep = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) prev = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) n = fscanf(file, "%u%c", &cpu, &sep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (n <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (prev >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int new_max = nr_cpus + cpu - prev - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) WARN_ONCE(new_max >= MAX_NR_CPUS, "Perf can support %d CPUs. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "Consider raising MAX_NR_CPUS\n", MAX_NR_CPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (new_max >= max_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) max_entries = new_max + MAX_NR_CPUS / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) tmp = realloc(tmp_cpus, max_entries * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (tmp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto out_free_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) tmp_cpus = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) while (++prev < cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) tmp_cpus[nr_cpus++] = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (nr_cpus == max_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) max_entries += MAX_NR_CPUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) tmp = realloc(tmp_cpus, max_entries * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (tmp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) goto out_free_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tmp_cpus = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) tmp_cpus[nr_cpus++] = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (n == 2 && sep == '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) prev = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) prev = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (n == 1 || sep == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (nr_cpus > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) cpus = cpu_map__default_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) out_free_tmp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) free(tmp_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct perf_cpu_map *cpus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) FILE *onlnf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) onlnf = fopen("/sys/devices/system/cpu/online", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!onlnf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return cpu_map__default_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) cpus = perf_cpu_map__read(onlnf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) fclose(onlnf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct perf_cpu_map *cpus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unsigned long start_cpu, end_cpu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) char *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int i, nr_cpus = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int *tmp_cpus = NULL, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int max_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!cpu_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return cpu_map__read_all_cpu_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * must handle the case of empty cpumap to cover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * TOPOLOGY header for NUMA nodes with no CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * ( e.g., because of CPU hotplug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (!isdigit(*cpu_list) && *cpu_list != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) while (isdigit(*cpu_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) start_cpu = strtoul(cpu_list, &p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (start_cpu >= INT_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) || (*p != '\0' && *p != ',' && *p != '-'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (*p == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) cpu_list = ++p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) end_cpu = strtoul(cpu_list, &p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (end_cpu < start_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) end_cpu = start_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) WARN_ONCE(end_cpu >= MAX_NR_CPUS, "Perf can support %d CPUs. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) "Consider raising MAX_NR_CPUS\n", MAX_NR_CPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) for (; start_cpu <= end_cpu; start_cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* check for duplicates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) for (i = 0; i < nr_cpus; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (tmp_cpus[i] == (int)start_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (nr_cpus == max_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) max_entries += MAX_NR_CPUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) tmp = realloc(tmp_cpus, max_entries * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (tmp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) tmp_cpus = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) tmp_cpus[nr_cpus++] = (int)start_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ++p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) cpu_list = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (nr_cpus > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) else if (*cpu_list != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) cpus = cpu_map__default_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) cpus = perf_cpu_map__dummy_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) free(tmp_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (cpus && idx < cpus->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return cpus->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int perf_cpu_map__nr(const struct perf_cpu_map *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return cpus ? cpus->nr : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) bool perf_cpu_map__empty(const struct perf_cpu_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return map ? map->map[0] == -1 : true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for (i = 0; i < cpus->nr; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (cpus->map[i] == cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int perf_cpu_map__max(struct perf_cpu_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int i, max = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) for (i = 0; i < map->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (map->map[i] > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) max = map->map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Merge two cpumaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * orig either gets freed and replaced with a new map, or reused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * with no reference count change (similar to "realloc")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * other has its reference count increased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct perf_cpu_map *other)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int *tmp_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int tmp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int i, j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct perf_cpu_map *merged;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!orig && !other)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!orig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) perf_cpu_map__get(other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!other)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (orig->nr == other->nr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) !memcmp(orig->map, other->map, orig->nr * sizeof(int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) tmp_len = orig->nr + other->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) tmp_cpus = malloc(tmp_len * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (!tmp_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* Standard merge algorithm from wikipedia */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) i = j = k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) while (i < orig->nr && j < other->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (orig->map[i] <= other->map[j]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (orig->map[i] == other->map[j])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) tmp_cpus[k++] = orig->map[i++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) tmp_cpus[k++] = other->map[j++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) while (i < orig->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) tmp_cpus[k++] = orig->map[i++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) while (j < other->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) tmp_cpus[k++] = other->map[j++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) assert(k <= tmp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) merged = cpu_map__trim_new(k, tmp_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) free(tmp_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) perf_cpu_map__put(orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return merged;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }