cpumap.c 12 KB

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