cpumap.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. struct cpu_map *cpu_map__empty_new(int nr)
  173. {
  174. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
  175. if (cpus != NULL) {
  176. int i;
  177. cpus->nr = nr;
  178. for (i = 0; i < nr; i++)
  179. cpus->map[i] = -1;
  180. atomic_set(&cpus->refcnt, 1);
  181. }
  182. return cpus;
  183. }
  184. static void cpu_map__delete(struct cpu_map *map)
  185. {
  186. if (map) {
  187. WARN_ONCE(atomic_read(&map->refcnt) != 0,
  188. "cpu_map refcnt unbalanced\n");
  189. free(map);
  190. }
  191. }
  192. struct cpu_map *cpu_map__get(struct cpu_map *map)
  193. {
  194. if (map)
  195. atomic_inc(&map->refcnt);
  196. return map;
  197. }
  198. void cpu_map__put(struct cpu_map *map)
  199. {
  200. if (map && atomic_dec_and_test(&map->refcnt))
  201. cpu_map__delete(map);
  202. }
  203. static int cpu__get_topology_int(int cpu, const char *name, int *value)
  204. {
  205. char path[PATH_MAX];
  206. snprintf(path, PATH_MAX,
  207. "devices/system/cpu/cpu%d/topology/%s", cpu, name);
  208. return sysfs__read_int(path, value);
  209. }
  210. int cpu_map__get_socket_id(int cpu)
  211. {
  212. int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
  213. return ret ?: value;
  214. }
  215. int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
  216. {
  217. int cpu;
  218. if (idx > map->nr)
  219. return -1;
  220. cpu = map->map[idx];
  221. return cpu_map__get_socket_id(cpu);
  222. }
  223. static int cmp_ids(const void *a, const void *b)
  224. {
  225. return *(int *)a - *(int *)b;
  226. }
  227. int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
  228. int (*f)(struct cpu_map *map, int cpu, void *data),
  229. void *data)
  230. {
  231. struct cpu_map *c;
  232. int nr = cpus->nr;
  233. int cpu, s1, s2;
  234. /* allocate as much as possible */
  235. c = calloc(1, sizeof(*c) + nr * sizeof(int));
  236. if (!c)
  237. return -1;
  238. for (cpu = 0; cpu < nr; cpu++) {
  239. s1 = f(cpus, cpu, data);
  240. for (s2 = 0; s2 < c->nr; s2++) {
  241. if (s1 == c->map[s2])
  242. break;
  243. }
  244. if (s2 == c->nr) {
  245. c->map[c->nr] = s1;
  246. c->nr++;
  247. }
  248. }
  249. /* ensure we process id in increasing order */
  250. qsort(c->map, c->nr, sizeof(int), cmp_ids);
  251. atomic_set(&c->refcnt, 1);
  252. *res = c;
  253. return 0;
  254. }
  255. int cpu_map__get_core_id(int cpu)
  256. {
  257. int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
  258. return ret ?: value;
  259. }
  260. int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
  261. {
  262. int cpu, s;
  263. if (idx > map->nr)
  264. return -1;
  265. cpu = map->map[idx];
  266. cpu = cpu_map__get_core_id(cpu);
  267. s = cpu_map__get_socket(map, idx, data);
  268. if (s == -1)
  269. return -1;
  270. /*
  271. * encode socket in upper 16 bits
  272. * core_id is relative to socket, and
  273. * we need a global id. So we combine
  274. * socket+ core id
  275. */
  276. return (s << 16) | (cpu & 0xffff);
  277. }
  278. int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
  279. {
  280. return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
  281. }
  282. int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
  283. {
  284. return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
  285. }
  286. /* setup simple routines to easily access node numbers given a cpu number */
  287. static int get_max_num(char *path, int *max)
  288. {
  289. size_t num;
  290. char *buf;
  291. int err = 0;
  292. if (filename__read_str(path, &buf, &num))
  293. return -1;
  294. buf[num] = '\0';
  295. /* start on the right, to find highest node num */
  296. while (--num) {
  297. if ((buf[num] == ',') || (buf[num] == '-')) {
  298. num++;
  299. break;
  300. }
  301. }
  302. if (sscanf(&buf[num], "%d", max) < 1) {
  303. err = -1;
  304. goto out;
  305. }
  306. /* convert from 0-based to 1-based */
  307. (*max)++;
  308. out:
  309. free(buf);
  310. return err;
  311. }
  312. /* Determine highest possible cpu in the system for sparse allocation */
  313. static void set_max_cpu_num(void)
  314. {
  315. const char *mnt;
  316. char path[PATH_MAX];
  317. int ret = -1;
  318. /* set up default */
  319. max_cpu_num = 4096;
  320. mnt = sysfs__mountpoint();
  321. if (!mnt)
  322. goto out;
  323. /* get the highest possible cpu number for a sparse allocation */
  324. ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
  325. if (ret == PATH_MAX) {
  326. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  327. goto out;
  328. }
  329. ret = get_max_num(path, &max_cpu_num);
  330. out:
  331. if (ret)
  332. pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
  333. }
  334. /* Determine highest possible node in the system for sparse allocation */
  335. static void set_max_node_num(void)
  336. {
  337. const char *mnt;
  338. char path[PATH_MAX];
  339. int ret = -1;
  340. /* set up default */
  341. max_node_num = 8;
  342. mnt = sysfs__mountpoint();
  343. if (!mnt)
  344. goto out;
  345. /* get the highest possible cpu number for a sparse allocation */
  346. ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
  347. if (ret == PATH_MAX) {
  348. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  349. goto out;
  350. }
  351. ret = get_max_num(path, &max_node_num);
  352. out:
  353. if (ret)
  354. pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
  355. }
  356. static int init_cpunode_map(void)
  357. {
  358. int i;
  359. set_max_cpu_num();
  360. set_max_node_num();
  361. cpunode_map = calloc(max_cpu_num, sizeof(int));
  362. if (!cpunode_map) {
  363. pr_err("%s: calloc failed\n", __func__);
  364. return -1;
  365. }
  366. for (i = 0; i < max_cpu_num; i++)
  367. cpunode_map[i] = -1;
  368. return 0;
  369. }
  370. int cpu__setup_cpunode_map(void)
  371. {
  372. struct dirent *dent1, *dent2;
  373. DIR *dir1, *dir2;
  374. unsigned int cpu, mem;
  375. char buf[PATH_MAX];
  376. char path[PATH_MAX];
  377. const char *mnt;
  378. int n;
  379. /* initialize globals */
  380. if (init_cpunode_map())
  381. return -1;
  382. mnt = sysfs__mountpoint();
  383. if (!mnt)
  384. return 0;
  385. n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
  386. if (n == PATH_MAX) {
  387. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  388. return -1;
  389. }
  390. dir1 = opendir(path);
  391. if (!dir1)
  392. return 0;
  393. /* walk tree and setup map */
  394. while ((dent1 = readdir(dir1)) != NULL) {
  395. if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
  396. continue;
  397. n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
  398. if (n == PATH_MAX) {
  399. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  400. continue;
  401. }
  402. dir2 = opendir(buf);
  403. if (!dir2)
  404. continue;
  405. while ((dent2 = readdir(dir2)) != NULL) {
  406. if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
  407. continue;
  408. cpunode_map[cpu] = mem;
  409. }
  410. closedir(dir2);
  411. }
  412. closedir(dir1);
  413. return 0;
  414. }