cpumap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. int i;
  200. size_t printed = fprintf(fp, "%d cpu%s: ",
  201. map->nr, map->nr > 1 ? "s" : "");
  202. for (i = 0; i < map->nr; ++i)
  203. printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
  204. return printed + fprintf(fp, "\n");
  205. }
  206. struct cpu_map *cpu_map__dummy_new(void)
  207. {
  208. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
  209. if (cpus != NULL) {
  210. cpus->nr = 1;
  211. cpus->map[0] = -1;
  212. atomic_set(&cpus->refcnt, 1);
  213. }
  214. return cpus;
  215. }
  216. struct cpu_map *cpu_map__empty_new(int nr)
  217. {
  218. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
  219. if (cpus != NULL) {
  220. int i;
  221. cpus->nr = nr;
  222. for (i = 0; i < nr; i++)
  223. cpus->map[i] = -1;
  224. atomic_set(&cpus->refcnt, 1);
  225. }
  226. return cpus;
  227. }
  228. static void cpu_map__delete(struct cpu_map *map)
  229. {
  230. if (map) {
  231. WARN_ONCE(atomic_read(&map->refcnt) != 0,
  232. "cpu_map refcnt unbalanced\n");
  233. free(map);
  234. }
  235. }
  236. struct cpu_map *cpu_map__get(struct cpu_map *map)
  237. {
  238. if (map)
  239. atomic_inc(&map->refcnt);
  240. return map;
  241. }
  242. void cpu_map__put(struct cpu_map *map)
  243. {
  244. if (map && atomic_dec_and_test(&map->refcnt))
  245. cpu_map__delete(map);
  246. }
  247. static int cpu__get_topology_int(int cpu, const char *name, int *value)
  248. {
  249. char path[PATH_MAX];
  250. snprintf(path, PATH_MAX,
  251. "devices/system/cpu/cpu%d/topology/%s", cpu, name);
  252. return sysfs__read_int(path, value);
  253. }
  254. int cpu_map__get_socket_id(int cpu)
  255. {
  256. int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
  257. return ret ?: value;
  258. }
  259. int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
  260. {
  261. int cpu;
  262. if (idx > map->nr)
  263. return -1;
  264. cpu = map->map[idx];
  265. return cpu_map__get_socket_id(cpu);
  266. }
  267. static int cmp_ids(const void *a, const void *b)
  268. {
  269. return *(int *)a - *(int *)b;
  270. }
  271. int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
  272. int (*f)(struct cpu_map *map, int cpu, void *data),
  273. void *data)
  274. {
  275. struct cpu_map *c;
  276. int nr = cpus->nr;
  277. int cpu, s1, s2;
  278. /* allocate as much as possible */
  279. c = calloc(1, sizeof(*c) + nr * sizeof(int));
  280. if (!c)
  281. return -1;
  282. for (cpu = 0; cpu < nr; cpu++) {
  283. s1 = f(cpus, cpu, data);
  284. for (s2 = 0; s2 < c->nr; s2++) {
  285. if (s1 == c->map[s2])
  286. break;
  287. }
  288. if (s2 == c->nr) {
  289. c->map[c->nr] = s1;
  290. c->nr++;
  291. }
  292. }
  293. /* ensure we process id in increasing order */
  294. qsort(c->map, c->nr, sizeof(int), cmp_ids);
  295. atomic_set(&c->refcnt, 1);
  296. *res = c;
  297. return 0;
  298. }
  299. int cpu_map__get_core_id(int cpu)
  300. {
  301. int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
  302. return ret ?: value;
  303. }
  304. int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
  305. {
  306. int cpu, s;
  307. if (idx > map->nr)
  308. return -1;
  309. cpu = map->map[idx];
  310. cpu = cpu_map__get_core_id(cpu);
  311. s = cpu_map__get_socket(map, idx, data);
  312. if (s == -1)
  313. return -1;
  314. /*
  315. * encode socket in upper 16 bits
  316. * core_id is relative to socket, and
  317. * we need a global id. So we combine
  318. * socket+ core id
  319. */
  320. return (s << 16) | (cpu & 0xffff);
  321. }
  322. int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
  323. {
  324. return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
  325. }
  326. int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
  327. {
  328. return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
  329. }
  330. /* setup simple routines to easily access node numbers given a cpu number */
  331. static int get_max_num(char *path, int *max)
  332. {
  333. size_t num;
  334. char *buf;
  335. int err = 0;
  336. if (filename__read_str(path, &buf, &num))
  337. return -1;
  338. buf[num] = '\0';
  339. /* start on the right, to find highest node num */
  340. while (--num) {
  341. if ((buf[num] == ',') || (buf[num] == '-')) {
  342. num++;
  343. break;
  344. }
  345. }
  346. if (sscanf(&buf[num], "%d", max) < 1) {
  347. err = -1;
  348. goto out;
  349. }
  350. /* convert from 0-based to 1-based */
  351. (*max)++;
  352. out:
  353. free(buf);
  354. return err;
  355. }
  356. /* Determine highest possible cpu in the system for sparse allocation */
  357. static void set_max_cpu_num(void)
  358. {
  359. const char *mnt;
  360. char path[PATH_MAX];
  361. int ret = -1;
  362. /* set up default */
  363. max_cpu_num = 4096;
  364. mnt = sysfs__mountpoint();
  365. if (!mnt)
  366. goto out;
  367. /* get the highest possible cpu number for a sparse allocation */
  368. ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
  369. if (ret == PATH_MAX) {
  370. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  371. goto out;
  372. }
  373. ret = get_max_num(path, &max_cpu_num);
  374. out:
  375. if (ret)
  376. pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
  377. }
  378. /* Determine highest possible node in the system for sparse allocation */
  379. static void set_max_node_num(void)
  380. {
  381. const char *mnt;
  382. char path[PATH_MAX];
  383. int ret = -1;
  384. /* set up default */
  385. max_node_num = 8;
  386. mnt = sysfs__mountpoint();
  387. if (!mnt)
  388. goto out;
  389. /* get the highest possible cpu number for a sparse allocation */
  390. ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
  391. if (ret == PATH_MAX) {
  392. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  393. goto out;
  394. }
  395. ret = get_max_num(path, &max_node_num);
  396. out:
  397. if (ret)
  398. pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
  399. }
  400. int cpu__max_node(void)
  401. {
  402. if (unlikely(!max_node_num))
  403. set_max_node_num();
  404. return max_node_num;
  405. }
  406. int cpu__max_cpu(void)
  407. {
  408. if (unlikely(!max_cpu_num))
  409. set_max_cpu_num();
  410. return max_cpu_num;
  411. }
  412. int cpu__get_node(int cpu)
  413. {
  414. if (unlikely(cpunode_map == NULL)) {
  415. pr_debug("cpu_map not initialized\n");
  416. return -1;
  417. }
  418. return cpunode_map[cpu];
  419. }
  420. static int init_cpunode_map(void)
  421. {
  422. int i;
  423. set_max_cpu_num();
  424. set_max_node_num();
  425. cpunode_map = calloc(max_cpu_num, sizeof(int));
  426. if (!cpunode_map) {
  427. pr_err("%s: calloc failed\n", __func__);
  428. return -1;
  429. }
  430. for (i = 0; i < max_cpu_num; i++)
  431. cpunode_map[i] = -1;
  432. return 0;
  433. }
  434. int cpu__setup_cpunode_map(void)
  435. {
  436. struct dirent *dent1, *dent2;
  437. DIR *dir1, *dir2;
  438. unsigned int cpu, mem;
  439. char buf[PATH_MAX];
  440. char path[PATH_MAX];
  441. const char *mnt;
  442. int n;
  443. /* initialize globals */
  444. if (init_cpunode_map())
  445. return -1;
  446. mnt = sysfs__mountpoint();
  447. if (!mnt)
  448. return 0;
  449. n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
  450. if (n == PATH_MAX) {
  451. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  452. return -1;
  453. }
  454. dir1 = opendir(path);
  455. if (!dir1)
  456. return 0;
  457. /* walk tree and setup map */
  458. while ((dent1 = readdir(dir1)) != NULL) {
  459. if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
  460. continue;
  461. n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
  462. if (n == PATH_MAX) {
  463. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  464. continue;
  465. }
  466. dir2 = opendir(buf);
  467. if (!dir2)
  468. continue;
  469. while ((dent2 = readdir(dir2)) != NULL) {
  470. if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
  471. continue;
  472. cpunode_map[cpu] = mem;
  473. }
  474. closedir(dir2);
  475. }
  476. closedir(dir1);
  477. return 0;
  478. }
  479. bool cpu_map__has(struct cpu_map *cpus, int cpu)
  480. {
  481. int i;
  482. for (i = 0; i < cpus->nr; ++i) {
  483. if (cpus->map[i] == cpu)
  484. return true;
  485. }
  486. return false;
  487. }