cpumap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util.h"
  3. #include <api/fs/fs.h>
  4. #include "../perf.h"
  5. #include "cpumap.h"
  6. #include <assert.h>
  7. #include <dirent.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <linux/bitmap.h>
  11. #include "asm/bug.h"
  12. #include "sane_ctype.h"
  13. static int max_cpu_num;
  14. static int max_present_cpu_num;
  15. static int max_node_num;
  16. static int *cpunode_map;
  17. static struct cpu_map *cpu_map__default_new(void)
  18. {
  19. struct cpu_map *cpus;
  20. int nr_cpus;
  21. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  22. if (nr_cpus < 0)
  23. return NULL;
  24. cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
  25. if (cpus != NULL) {
  26. int i;
  27. for (i = 0; i < nr_cpus; ++i)
  28. cpus->map[i] = i;
  29. cpus->nr = nr_cpus;
  30. refcount_set(&cpus->refcnt, 1);
  31. }
  32. return cpus;
  33. }
  34. static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
  35. {
  36. size_t payload_size = nr_cpus * sizeof(int);
  37. struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
  38. if (cpus != NULL) {
  39. cpus->nr = nr_cpus;
  40. memcpy(cpus->map, tmp_cpus, payload_size);
  41. refcount_set(&cpus->refcnt, 1);
  42. }
  43. return cpus;
  44. }
  45. struct cpu_map *cpu_map__read(FILE *file)
  46. {
  47. struct cpu_map *cpus = NULL;
  48. int nr_cpus = 0;
  49. int *tmp_cpus = NULL, *tmp;
  50. int max_entries = 0;
  51. int n, cpu, prev;
  52. char sep;
  53. sep = 0;
  54. prev = -1;
  55. for (;;) {
  56. n = fscanf(file, "%u%c", &cpu, &sep);
  57. if (n <= 0)
  58. break;
  59. if (prev >= 0) {
  60. int new_max = nr_cpus + cpu - prev - 1;
  61. if (new_max >= max_entries) {
  62. max_entries = new_max + MAX_NR_CPUS / 2;
  63. tmp = realloc(tmp_cpus, max_entries * sizeof(int));
  64. if (tmp == NULL)
  65. goto out_free_tmp;
  66. tmp_cpus = tmp;
  67. }
  68. while (++prev < cpu)
  69. tmp_cpus[nr_cpus++] = prev;
  70. }
  71. if (nr_cpus == max_entries) {
  72. max_entries += MAX_NR_CPUS;
  73. tmp = realloc(tmp_cpus, max_entries * sizeof(int));
  74. if (tmp == NULL)
  75. goto out_free_tmp;
  76. tmp_cpus = tmp;
  77. }
  78. tmp_cpus[nr_cpus++] = cpu;
  79. if (n == 2 && sep == '-')
  80. prev = cpu;
  81. else
  82. prev = -1;
  83. if (n == 1 || sep == '\n')
  84. break;
  85. }
  86. if (nr_cpus > 0)
  87. cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
  88. else
  89. cpus = cpu_map__default_new();
  90. out_free_tmp:
  91. free(tmp_cpus);
  92. return cpus;
  93. }
  94. static struct cpu_map *cpu_map__read_all_cpu_map(void)
  95. {
  96. struct cpu_map *cpus = NULL;
  97. FILE *onlnf;
  98. onlnf = fopen("/sys/devices/system/cpu/online", "r");
  99. if (!onlnf)
  100. return cpu_map__default_new();
  101. cpus = cpu_map__read(onlnf);
  102. fclose(onlnf);
  103. return cpus;
  104. }
  105. struct cpu_map *cpu_map__new(const char *cpu_list)
  106. {
  107. struct cpu_map *cpus = NULL;
  108. unsigned long start_cpu, end_cpu = 0;
  109. char *p = NULL;
  110. int i, nr_cpus = 0;
  111. int *tmp_cpus = NULL, *tmp;
  112. int max_entries = 0;
  113. if (!cpu_list)
  114. return cpu_map__read_all_cpu_map();
  115. if (!isdigit(*cpu_list))
  116. goto out;
  117. while (isdigit(*cpu_list)) {
  118. p = NULL;
  119. start_cpu = strtoul(cpu_list, &p, 0);
  120. if (start_cpu >= INT_MAX
  121. || (*p != '\0' && *p != ',' && *p != '-'))
  122. goto invalid;
  123. if (*p == '-') {
  124. cpu_list = ++p;
  125. p = NULL;
  126. end_cpu = strtoul(cpu_list, &p, 0);
  127. if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
  128. goto invalid;
  129. if (end_cpu < start_cpu)
  130. goto invalid;
  131. } else {
  132. end_cpu = start_cpu;
  133. }
  134. for (; start_cpu <= end_cpu; start_cpu++) {
  135. /* check for duplicates */
  136. for (i = 0; i < nr_cpus; i++)
  137. if (tmp_cpus[i] == (int)start_cpu)
  138. goto invalid;
  139. if (nr_cpus == max_entries) {
  140. max_entries += MAX_NR_CPUS;
  141. tmp = realloc(tmp_cpus, max_entries * sizeof(int));
  142. if (tmp == NULL)
  143. goto invalid;
  144. tmp_cpus = tmp;
  145. }
  146. tmp_cpus[nr_cpus++] = (int)start_cpu;
  147. }
  148. if (*p)
  149. ++p;
  150. cpu_list = p;
  151. }
  152. if (nr_cpus > 0)
  153. cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
  154. else
  155. cpus = cpu_map__default_new();
  156. invalid:
  157. free(tmp_cpus);
  158. out:
  159. return cpus;
  160. }
  161. static struct cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
  162. {
  163. struct cpu_map *map;
  164. map = cpu_map__empty_new(cpus->nr);
  165. if (map) {
  166. unsigned i;
  167. for (i = 0; i < cpus->nr; i++) {
  168. /*
  169. * Special treatment for -1, which is not real cpu number,
  170. * and we need to use (int) -1 to initialize map[i],
  171. * otherwise it would become 65535.
  172. */
  173. if (cpus->cpu[i] == (u16) -1)
  174. map->map[i] = -1;
  175. else
  176. map->map[i] = (int) cpus->cpu[i];
  177. }
  178. }
  179. return map;
  180. }
  181. static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
  182. {
  183. struct cpu_map *map;
  184. int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
  185. nr = bitmap_weight(mask->mask, nbits);
  186. map = cpu_map__empty_new(nr);
  187. if (map) {
  188. int cpu, i = 0;
  189. for_each_set_bit(cpu, mask->mask, nbits)
  190. map->map[i++] = cpu;
  191. }
  192. return map;
  193. }
  194. struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
  195. {
  196. if (data->type == PERF_CPU_MAP__CPUS)
  197. return cpu_map__from_entries((struct cpu_map_entries *)data->data);
  198. else
  199. return cpu_map__from_mask((struct cpu_map_mask *)data->data);
  200. }
  201. size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
  202. {
  203. #define BUFSIZE 1024
  204. char buf[BUFSIZE];
  205. cpu_map__snprint(map, buf, sizeof(buf));
  206. return fprintf(fp, "%s\n", buf);
  207. #undef BUFSIZE
  208. }
  209. struct cpu_map *cpu_map__dummy_new(void)
  210. {
  211. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
  212. if (cpus != NULL) {
  213. cpus->nr = 1;
  214. cpus->map[0] = -1;
  215. refcount_set(&cpus->refcnt, 1);
  216. }
  217. return cpus;
  218. }
  219. struct cpu_map *cpu_map__empty_new(int nr)
  220. {
  221. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
  222. if (cpus != NULL) {
  223. int i;
  224. cpus->nr = nr;
  225. for (i = 0; i < nr; i++)
  226. cpus->map[i] = -1;
  227. refcount_set(&cpus->refcnt, 1);
  228. }
  229. return cpus;
  230. }
  231. static void cpu_map__delete(struct cpu_map *map)
  232. {
  233. if (map) {
  234. WARN_ONCE(refcount_read(&map->refcnt) != 0,
  235. "cpu_map refcnt unbalanced\n");
  236. free(map);
  237. }
  238. }
  239. struct cpu_map *cpu_map__get(struct cpu_map *map)
  240. {
  241. if (map)
  242. refcount_inc(&map->refcnt);
  243. return map;
  244. }
  245. void cpu_map__put(struct cpu_map *map)
  246. {
  247. if (map && refcount_dec_and_test(&map->refcnt))
  248. cpu_map__delete(map);
  249. }
  250. static int cpu__get_topology_int(int cpu, const char *name, int *value)
  251. {
  252. char path[PATH_MAX];
  253. snprintf(path, PATH_MAX,
  254. "devices/system/cpu/cpu%d/topology/%s", cpu, name);
  255. return sysfs__read_int(path, value);
  256. }
  257. int cpu_map__get_socket_id(int cpu)
  258. {
  259. int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
  260. return ret ?: value;
  261. }
  262. int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
  263. {
  264. int cpu;
  265. if (idx > map->nr)
  266. return -1;
  267. cpu = map->map[idx];
  268. return cpu_map__get_socket_id(cpu);
  269. }
  270. static int cmp_ids(const void *a, const void *b)
  271. {
  272. return *(int *)a - *(int *)b;
  273. }
  274. int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
  275. int (*f)(struct cpu_map *map, int cpu, void *data),
  276. void *data)
  277. {
  278. struct cpu_map *c;
  279. int nr = cpus->nr;
  280. int cpu, s1, s2;
  281. /* allocate as much as possible */
  282. c = calloc(1, sizeof(*c) + nr * sizeof(int));
  283. if (!c)
  284. return -1;
  285. for (cpu = 0; cpu < nr; cpu++) {
  286. s1 = f(cpus, cpu, data);
  287. for (s2 = 0; s2 < c->nr; s2++) {
  288. if (s1 == c->map[s2])
  289. break;
  290. }
  291. if (s2 == c->nr) {
  292. c->map[c->nr] = s1;
  293. c->nr++;
  294. }
  295. }
  296. /* ensure we process id in increasing order */
  297. qsort(c->map, c->nr, sizeof(int), cmp_ids);
  298. refcount_set(&c->refcnt, 1);
  299. *res = c;
  300. return 0;
  301. }
  302. int cpu_map__get_core_id(int cpu)
  303. {
  304. int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
  305. return ret ?: value;
  306. }
  307. int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
  308. {
  309. int cpu, s;
  310. if (idx > map->nr)
  311. return -1;
  312. cpu = map->map[idx];
  313. cpu = cpu_map__get_core_id(cpu);
  314. s = cpu_map__get_socket(map, idx, data);
  315. if (s == -1)
  316. return -1;
  317. /*
  318. * encode socket in upper 16 bits
  319. * core_id is relative to socket, and
  320. * we need a global id. So we combine
  321. * socket+ core id
  322. */
  323. return (s << 16) | (cpu & 0xffff);
  324. }
  325. int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
  326. {
  327. return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
  328. }
  329. int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
  330. {
  331. return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
  332. }
  333. /* setup simple routines to easily access node numbers given a cpu number */
  334. static int get_max_num(char *path, int *max)
  335. {
  336. size_t num;
  337. char *buf;
  338. int err = 0;
  339. if (filename__read_str(path, &buf, &num))
  340. return -1;
  341. buf[num] = '\0';
  342. /* start on the right, to find highest node num */
  343. while (--num) {
  344. if ((buf[num] == ',') || (buf[num] == '-')) {
  345. num++;
  346. break;
  347. }
  348. }
  349. if (sscanf(&buf[num], "%d", max) < 1) {
  350. err = -1;
  351. goto out;
  352. }
  353. /* convert from 0-based to 1-based */
  354. (*max)++;
  355. out:
  356. free(buf);
  357. return err;
  358. }
  359. /* Determine highest possible cpu in the system for sparse allocation */
  360. static void set_max_cpu_num(void)
  361. {
  362. const char *mnt;
  363. char path[PATH_MAX];
  364. int ret = -1;
  365. /* set up default */
  366. max_cpu_num = 4096;
  367. max_present_cpu_num = 4096;
  368. mnt = sysfs__mountpoint();
  369. if (!mnt)
  370. goto out;
  371. /* get the highest possible cpu number for a sparse allocation */
  372. ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
  373. if (ret == PATH_MAX) {
  374. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  375. goto out;
  376. }
  377. ret = get_max_num(path, &max_cpu_num);
  378. if (ret)
  379. goto out;
  380. /* get the highest present cpu number for a sparse allocation */
  381. ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
  382. if (ret == PATH_MAX) {
  383. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  384. goto out;
  385. }
  386. ret = get_max_num(path, &max_present_cpu_num);
  387. out:
  388. if (ret)
  389. pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
  390. }
  391. /* Determine highest possible node in the system for sparse allocation */
  392. static void set_max_node_num(void)
  393. {
  394. const char *mnt;
  395. char path[PATH_MAX];
  396. int ret = -1;
  397. /* set up default */
  398. max_node_num = 8;
  399. mnt = sysfs__mountpoint();
  400. if (!mnt)
  401. goto out;
  402. /* get the highest possible cpu number for a sparse allocation */
  403. ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
  404. if (ret == PATH_MAX) {
  405. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  406. goto out;
  407. }
  408. ret = get_max_num(path, &max_node_num);
  409. out:
  410. if (ret)
  411. pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
  412. }
  413. int cpu__max_node(void)
  414. {
  415. if (unlikely(!max_node_num))
  416. set_max_node_num();
  417. return max_node_num;
  418. }
  419. int cpu__max_cpu(void)
  420. {
  421. if (unlikely(!max_cpu_num))
  422. set_max_cpu_num();
  423. return max_cpu_num;
  424. }
  425. int cpu__max_present_cpu(void)
  426. {
  427. if (unlikely(!max_present_cpu_num))
  428. set_max_cpu_num();
  429. return max_present_cpu_num;
  430. }
  431. int cpu__get_node(int cpu)
  432. {
  433. if (unlikely(cpunode_map == NULL)) {
  434. pr_debug("cpu_map not initialized\n");
  435. return -1;
  436. }
  437. return cpunode_map[cpu];
  438. }
  439. static int init_cpunode_map(void)
  440. {
  441. int i;
  442. set_max_cpu_num();
  443. set_max_node_num();
  444. cpunode_map = calloc(max_cpu_num, sizeof(int));
  445. if (!cpunode_map) {
  446. pr_err("%s: calloc failed\n", __func__);
  447. return -1;
  448. }
  449. for (i = 0; i < max_cpu_num; i++)
  450. cpunode_map[i] = -1;
  451. return 0;
  452. }
  453. int cpu__setup_cpunode_map(void)
  454. {
  455. struct dirent *dent1, *dent2;
  456. DIR *dir1, *dir2;
  457. unsigned int cpu, mem;
  458. char buf[PATH_MAX];
  459. char path[PATH_MAX];
  460. const char *mnt;
  461. int n;
  462. /* initialize globals */
  463. if (init_cpunode_map())
  464. return -1;
  465. mnt = sysfs__mountpoint();
  466. if (!mnt)
  467. return 0;
  468. n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
  469. if (n == PATH_MAX) {
  470. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  471. return -1;
  472. }
  473. dir1 = opendir(path);
  474. if (!dir1)
  475. return 0;
  476. /* walk tree and setup map */
  477. while ((dent1 = readdir(dir1)) != NULL) {
  478. if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
  479. continue;
  480. n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
  481. if (n == PATH_MAX) {
  482. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  483. continue;
  484. }
  485. dir2 = opendir(buf);
  486. if (!dir2)
  487. continue;
  488. while ((dent2 = readdir(dir2)) != NULL) {
  489. if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
  490. continue;
  491. cpunode_map[cpu] = mem;
  492. }
  493. closedir(dir2);
  494. }
  495. closedir(dir1);
  496. return 0;
  497. }
  498. bool cpu_map__has(struct cpu_map *cpus, int cpu)
  499. {
  500. return cpu_map__idx(cpus, cpu) != -1;
  501. }
  502. int cpu_map__idx(struct cpu_map *cpus, int cpu)
  503. {
  504. int i;
  505. for (i = 0; i < cpus->nr; ++i) {
  506. if (cpus->map[i] == cpu)
  507. return i;
  508. }
  509. return -1;
  510. }
  511. int cpu_map__cpu(struct cpu_map *cpus, int idx)
  512. {
  513. return cpus->map[idx];
  514. }
  515. size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
  516. {
  517. int i, cpu, start = -1;
  518. bool first = true;
  519. size_t ret = 0;
  520. #define COMMA first ? "" : ","
  521. for (i = 0; i < map->nr + 1; i++) {
  522. bool last = i == map->nr;
  523. cpu = last ? INT_MAX : map->map[i];
  524. if (start == -1) {
  525. start = i;
  526. if (last) {
  527. ret += snprintf(buf + ret, size - ret,
  528. "%s%d", COMMA,
  529. map->map[i]);
  530. }
  531. } else if (((i - start) != (cpu - map->map[start])) || last) {
  532. int end = i - 1;
  533. if (start == end) {
  534. ret += snprintf(buf + ret, size - ret,
  535. "%s%d", COMMA,
  536. map->map[start]);
  537. } else {
  538. ret += snprintf(buf + ret, size - ret,
  539. "%s%d-%d", COMMA,
  540. map->map[start], map->map[end]);
  541. }
  542. first = false;
  543. start = i;
  544. }
  545. }
  546. #undef COMMA
  547. pr_debug("cpumask list: %s\n", buf);
  548. return ret;
  549. }
  550. static char hex_char(unsigned char val)
  551. {
  552. if (val < 10)
  553. return val + '0';
  554. if (val < 16)
  555. return val - 10 + 'a';
  556. return '?';
  557. }
  558. size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size)
  559. {
  560. int i, cpu;
  561. char *ptr = buf;
  562. unsigned char *bitmap;
  563. int last_cpu = cpu_map__cpu(map, map->nr - 1);
  564. bitmap = zalloc((last_cpu + 7) / 8);
  565. if (bitmap == NULL) {
  566. buf[0] = '\0';
  567. return 0;
  568. }
  569. for (i = 0; i < map->nr; i++) {
  570. cpu = cpu_map__cpu(map, i);
  571. bitmap[cpu / 8] |= 1 << (cpu % 8);
  572. }
  573. for (cpu = last_cpu / 4 * 4; cpu >= 0; cpu -= 4) {
  574. unsigned char bits = bitmap[cpu / 8];
  575. if (cpu % 8)
  576. bits >>= 4;
  577. else
  578. bits &= 0xf;
  579. *ptr++ = hex_char(bits);
  580. if ((cpu % 32) == 0 && cpu > 0)
  581. *ptr++ = ',';
  582. }
  583. *ptr = '\0';
  584. free(bitmap);
  585. buf[size - 1] = '\0';
  586. return ptr - buf;
  587. }