cpumap.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. /*
  108. * must handle the case of empty cpumap to cover
  109. * TOPOLOGY header for NUMA nodes with no CPU
  110. * ( e.g., because of CPU hotplug)
  111. */
  112. if (!isdigit(*cpu_list) && *cpu_list != '\0')
  113. goto out;
  114. while (isdigit(*cpu_list)) {
  115. p = NULL;
  116. start_cpu = strtoul(cpu_list, &p, 0);
  117. if (start_cpu >= INT_MAX
  118. || (*p != '\0' && *p != ',' && *p != '-'))
  119. goto invalid;
  120. if (*p == '-') {
  121. cpu_list = ++p;
  122. p = NULL;
  123. end_cpu = strtoul(cpu_list, &p, 0);
  124. if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
  125. goto invalid;
  126. if (end_cpu < start_cpu)
  127. goto invalid;
  128. } else {
  129. end_cpu = start_cpu;
  130. }
  131. for (; start_cpu <= end_cpu; start_cpu++) {
  132. /* check for duplicates */
  133. for (i = 0; i < nr_cpus; i++)
  134. if (tmp_cpus[i] == (int)start_cpu)
  135. goto invalid;
  136. if (nr_cpus == max_entries) {
  137. max_entries += MAX_NR_CPUS;
  138. tmp = realloc(tmp_cpus, max_entries * sizeof(int));
  139. if (tmp == NULL)
  140. goto invalid;
  141. tmp_cpus = tmp;
  142. }
  143. tmp_cpus[nr_cpus++] = (int)start_cpu;
  144. }
  145. if (*p)
  146. ++p;
  147. cpu_list = p;
  148. }
  149. if (nr_cpus > 0)
  150. cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
  151. else if (*cpu_list != '\0')
  152. cpus = cpu_map__default_new();
  153. else
  154. cpus = cpu_map__dummy_new();
  155. invalid:
  156. free(tmp_cpus);
  157. out:
  158. return cpus;
  159. }
  160. size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
  161. {
  162. int i;
  163. size_t printed = fprintf(fp, "%d cpu%s: ",
  164. map->nr, map->nr > 1 ? "s" : "");
  165. for (i = 0; i < map->nr; ++i)
  166. printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
  167. return printed + fprintf(fp, "\n");
  168. }
  169. struct cpu_map *cpu_map__dummy_new(void)
  170. {
  171. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
  172. if (cpus != NULL) {
  173. cpus->nr = 1;
  174. cpus->map[0] = -1;
  175. atomic_set(&cpus->refcnt, 1);
  176. }
  177. return cpus;
  178. }
  179. struct cpu_map *cpu_map__empty_new(int nr)
  180. {
  181. struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
  182. if (cpus != NULL) {
  183. int i;
  184. cpus->nr = nr;
  185. for (i = 0; i < nr; i++)
  186. cpus->map[i] = -1;
  187. atomic_set(&cpus->refcnt, 1);
  188. }
  189. return cpus;
  190. }
  191. static void cpu_map__delete(struct cpu_map *map)
  192. {
  193. if (map) {
  194. WARN_ONCE(atomic_read(&map->refcnt) != 0,
  195. "cpu_map refcnt unbalanced\n");
  196. free(map);
  197. }
  198. }
  199. struct cpu_map *cpu_map__get(struct cpu_map *map)
  200. {
  201. if (map)
  202. atomic_inc(&map->refcnt);
  203. return map;
  204. }
  205. void cpu_map__put(struct cpu_map *map)
  206. {
  207. if (map && atomic_dec_and_test(&map->refcnt))
  208. cpu_map__delete(map);
  209. }
  210. static int cpu__get_topology_int(int cpu, const char *name, int *value)
  211. {
  212. char path[PATH_MAX];
  213. snprintf(path, PATH_MAX,
  214. "devices/system/cpu/cpu%d/topology/%s", cpu, name);
  215. return sysfs__read_int(path, value);
  216. }
  217. int cpu_map__get_socket_id(int cpu)
  218. {
  219. int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
  220. return ret ?: value;
  221. }
  222. int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
  223. {
  224. int cpu;
  225. if (idx > map->nr)
  226. return -1;
  227. cpu = map->map[idx];
  228. return cpu_map__get_socket_id(cpu);
  229. }
  230. static int cmp_ids(const void *a, const void *b)
  231. {
  232. return *(int *)a - *(int *)b;
  233. }
  234. int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
  235. int (*f)(struct cpu_map *map, int cpu, void *data),
  236. void *data)
  237. {
  238. struct cpu_map *c;
  239. int nr = cpus->nr;
  240. int cpu, s1, s2;
  241. /* allocate as much as possible */
  242. c = calloc(1, sizeof(*c) + nr * sizeof(int));
  243. if (!c)
  244. return -1;
  245. for (cpu = 0; cpu < nr; cpu++) {
  246. s1 = f(cpus, cpu, data);
  247. for (s2 = 0; s2 < c->nr; s2++) {
  248. if (s1 == c->map[s2])
  249. break;
  250. }
  251. if (s2 == c->nr) {
  252. c->map[c->nr] = s1;
  253. c->nr++;
  254. }
  255. }
  256. /* ensure we process id in increasing order */
  257. qsort(c->map, c->nr, sizeof(int), cmp_ids);
  258. atomic_set(&c->refcnt, 1);
  259. *res = c;
  260. return 0;
  261. }
  262. int cpu_map__get_core_id(int cpu)
  263. {
  264. int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
  265. return ret ?: value;
  266. }
  267. int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
  268. {
  269. int cpu, s;
  270. if (idx > map->nr)
  271. return -1;
  272. cpu = map->map[idx];
  273. cpu = cpu_map__get_core_id(cpu);
  274. s = cpu_map__get_socket(map, idx, data);
  275. if (s == -1)
  276. return -1;
  277. /*
  278. * encode socket in upper 16 bits
  279. * core_id is relative to socket, and
  280. * we need a global id. So we combine
  281. * socket+ core id
  282. */
  283. return (s << 16) | (cpu & 0xffff);
  284. }
  285. int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
  286. {
  287. return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
  288. }
  289. int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
  290. {
  291. return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
  292. }
  293. /* setup simple routines to easily access node numbers given a cpu number */
  294. static int get_max_num(char *path, int *max)
  295. {
  296. size_t num;
  297. char *buf;
  298. int err = 0;
  299. if (filename__read_str(path, &buf, &num))
  300. return -1;
  301. buf[num] = '\0';
  302. /* start on the right, to find highest node num */
  303. while (--num) {
  304. if ((buf[num] == ',') || (buf[num] == '-')) {
  305. num++;
  306. break;
  307. }
  308. }
  309. if (sscanf(&buf[num], "%d", max) < 1) {
  310. err = -1;
  311. goto out;
  312. }
  313. /* convert from 0-based to 1-based */
  314. (*max)++;
  315. out:
  316. free(buf);
  317. return err;
  318. }
  319. /* Determine highest possible cpu in the system for sparse allocation */
  320. static void set_max_cpu_num(void)
  321. {
  322. const char *mnt;
  323. char path[PATH_MAX];
  324. int ret = -1;
  325. /* set up default */
  326. max_cpu_num = 4096;
  327. mnt = sysfs__mountpoint();
  328. if (!mnt)
  329. goto out;
  330. /* get the highest possible cpu number for a sparse allocation */
  331. ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
  332. if (ret == PATH_MAX) {
  333. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  334. goto out;
  335. }
  336. ret = get_max_num(path, &max_cpu_num);
  337. out:
  338. if (ret)
  339. pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
  340. }
  341. /* Determine highest possible node in the system for sparse allocation */
  342. static void set_max_node_num(void)
  343. {
  344. const char *mnt;
  345. char path[PATH_MAX];
  346. int ret = -1;
  347. /* set up default */
  348. max_node_num = 8;
  349. mnt = sysfs__mountpoint();
  350. if (!mnt)
  351. goto out;
  352. /* get the highest possible cpu number for a sparse allocation */
  353. ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
  354. if (ret == PATH_MAX) {
  355. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  356. goto out;
  357. }
  358. ret = get_max_num(path, &max_node_num);
  359. out:
  360. if (ret)
  361. pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
  362. }
  363. static int init_cpunode_map(void)
  364. {
  365. int i;
  366. set_max_cpu_num();
  367. set_max_node_num();
  368. cpunode_map = calloc(max_cpu_num, sizeof(int));
  369. if (!cpunode_map) {
  370. pr_err("%s: calloc failed\n", __func__);
  371. return -1;
  372. }
  373. for (i = 0; i < max_cpu_num; i++)
  374. cpunode_map[i] = -1;
  375. return 0;
  376. }
  377. int cpu__setup_cpunode_map(void)
  378. {
  379. struct dirent *dent1, *dent2;
  380. DIR *dir1, *dir2;
  381. unsigned int cpu, mem;
  382. char buf[PATH_MAX];
  383. char path[PATH_MAX];
  384. const char *mnt;
  385. int n;
  386. /* initialize globals */
  387. if (init_cpunode_map())
  388. return -1;
  389. mnt = sysfs__mountpoint();
  390. if (!mnt)
  391. return 0;
  392. n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
  393. if (n == PATH_MAX) {
  394. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  395. return -1;
  396. }
  397. dir1 = opendir(path);
  398. if (!dir1)
  399. return 0;
  400. /* walk tree and setup map */
  401. while ((dent1 = readdir(dir1)) != NULL) {
  402. if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
  403. continue;
  404. n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
  405. if (n == PATH_MAX) {
  406. pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
  407. continue;
  408. }
  409. dir2 = opendir(buf);
  410. if (!dir2)
  411. continue;
  412. while ((dent2 = readdir(dir2)) != NULL) {
  413. if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
  414. continue;
  415. cpunode_map[cpu] = mem;
  416. }
  417. closedir(dir2);
  418. }
  419. closedir(dir1);
  420. return 0;
  421. }