cpumap.c 11 KB

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