cpumap.c 9.5 KB

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