cpumap.c 8.1 KB

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