cpumap.c 12 KB

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