evsel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <sys/syscall.h>
  5. #include <perf/evsel.h>
  6. #include <perf/cpumap.h>
  7. #include <perf/threadmap.h>
  8. #include <linux/hash.h>
  9. #include <linux/list.h>
  10. #include <internal/evsel.h>
  11. #include <linux/zalloc.h>
  12. #include <stdlib.h>
  13. #include <internal/xyarray.h>
  14. #include <internal/cpumap.h>
  15. #include <internal/mmap.h>
  16. #include <internal/threadmap.h>
  17. #include <internal/lib.h>
  18. #include <linux/string.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/mman.h>
  21. #include <asm/bug.h>
  22. void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr,
  23. int idx)
  24. {
  25. INIT_LIST_HEAD(&evsel->node);
  26. INIT_LIST_HEAD(&evsel->per_stream_periods);
  27. evsel->attr = *attr;
  28. evsel->idx = idx;
  29. evsel->leader = evsel;
  30. }
  31. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
  32. {
  33. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  34. if (evsel != NULL)
  35. perf_evsel__init(evsel, attr, 0);
  36. return evsel;
  37. }
  38. void perf_evsel__exit(struct perf_evsel *evsel)
  39. {
  40. assert(evsel->fd == NULL); /* If not fds were not closed. */
  41. assert(evsel->mmap == NULL); /* If not munmap wasn't called. */
  42. assert(evsel->sample_id == NULL); /* If not free_id wasn't called. */
  43. perf_cpu_map__put(evsel->cpus);
  44. perf_cpu_map__put(evsel->pmu_cpus);
  45. perf_thread_map__put(evsel->threads);
  46. }
  47. void perf_evsel__delete(struct perf_evsel *evsel)
  48. {
  49. perf_evsel__exit(evsel);
  50. free(evsel);
  51. }
  52. #define FD(_evsel, _cpu_map_idx, _thread) \
  53. ((int *)xyarray__entry(_evsel->fd, _cpu_map_idx, _thread))
  54. #define MMAP(_evsel, _cpu_map_idx, _thread) \
  55. (_evsel->mmap ? ((struct perf_mmap *) xyarray__entry(_evsel->mmap, _cpu_map_idx, _thread)) \
  56. : NULL)
  57. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  58. {
  59. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  60. if (evsel->fd) {
  61. int idx, thread;
  62. for (idx = 0; idx < ncpus; idx++) {
  63. for (thread = 0; thread < nthreads; thread++) {
  64. int *fd = FD(evsel, idx, thread);
  65. if (fd)
  66. *fd = -1;
  67. }
  68. }
  69. }
  70. return evsel->fd != NULL ? 0 : -ENOMEM;
  71. }
  72. static int perf_evsel__alloc_mmap(struct perf_evsel *evsel, int ncpus, int nthreads)
  73. {
  74. evsel->mmap = xyarray__new(ncpus, nthreads, sizeof(struct perf_mmap));
  75. return evsel->mmap != NULL ? 0 : -ENOMEM;
  76. }
  77. static int
  78. sys_perf_event_open(struct perf_event_attr *attr,
  79. pid_t pid, struct perf_cpu cpu, int group_fd,
  80. unsigned long flags)
  81. {
  82. return syscall(__NR_perf_event_open, attr, pid, cpu.cpu, group_fd, flags);
  83. }
  84. static int get_group_fd(struct perf_evsel *evsel, int cpu_map_idx, int thread, int *group_fd)
  85. {
  86. struct perf_evsel *leader = evsel->leader;
  87. int *fd;
  88. if (evsel == leader) {
  89. *group_fd = -1;
  90. return 0;
  91. }
  92. /*
  93. * Leader must be already processed/open,
  94. * if not it's a bug.
  95. */
  96. if (!leader->fd)
  97. return -ENOTCONN;
  98. fd = FD(leader, cpu_map_idx, thread);
  99. if (fd == NULL || *fd == -1)
  100. return -EBADF;
  101. *group_fd = *fd;
  102. return 0;
  103. }
  104. int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
  105. struct perf_thread_map *threads)
  106. {
  107. struct perf_cpu cpu;
  108. int idx, thread, err = 0;
  109. if (cpus == NULL) {
  110. static struct perf_cpu_map *empty_cpu_map;
  111. if (empty_cpu_map == NULL) {
  112. empty_cpu_map = perf_cpu_map__new_any_cpu();
  113. if (empty_cpu_map == NULL)
  114. return -ENOMEM;
  115. }
  116. cpus = empty_cpu_map;
  117. }
  118. if (threads == NULL) {
  119. static struct perf_thread_map *empty_thread_map;
  120. if (empty_thread_map == NULL) {
  121. empty_thread_map = perf_thread_map__new_dummy();
  122. if (empty_thread_map == NULL)
  123. return -ENOMEM;
  124. }
  125. threads = empty_thread_map;
  126. }
  127. if (evsel->fd == NULL &&
  128. perf_evsel__alloc_fd(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
  129. return -ENOMEM;
  130. perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
  131. for (thread = 0; thread < threads->nr; thread++) {
  132. int fd, group_fd, *evsel_fd;
  133. evsel_fd = FD(evsel, idx, thread);
  134. if (evsel_fd == NULL) {
  135. err = -EINVAL;
  136. goto out;
  137. }
  138. err = get_group_fd(evsel, idx, thread, &group_fd);
  139. if (err < 0)
  140. goto out;
  141. fd = sys_perf_event_open(&evsel->attr,
  142. threads->map[thread].pid,
  143. cpu, group_fd, 0);
  144. if (fd < 0) {
  145. err = -errno;
  146. goto out;
  147. }
  148. *evsel_fd = fd;
  149. }
  150. }
  151. out:
  152. if (err)
  153. perf_evsel__close(evsel);
  154. return err;
  155. }
  156. static void perf_evsel__close_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx)
  157. {
  158. int thread;
  159. for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
  160. int *fd = FD(evsel, cpu_map_idx, thread);
  161. if (fd && *fd >= 0) {
  162. close(*fd);
  163. *fd = -1;
  164. }
  165. }
  166. }
  167. void perf_evsel__close_fd(struct perf_evsel *evsel)
  168. {
  169. for (int idx = 0; idx < xyarray__max_x(evsel->fd); idx++)
  170. perf_evsel__close_fd_cpu(evsel, idx);
  171. }
  172. void perf_evsel__free_fd(struct perf_evsel *evsel)
  173. {
  174. xyarray__delete(evsel->fd);
  175. evsel->fd = NULL;
  176. }
  177. void perf_evsel__close(struct perf_evsel *evsel)
  178. {
  179. if (evsel->fd == NULL)
  180. return;
  181. perf_evsel__close_fd(evsel);
  182. perf_evsel__free_fd(evsel);
  183. }
  184. void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu_map_idx)
  185. {
  186. if (evsel->fd == NULL)
  187. return;
  188. perf_evsel__close_fd_cpu(evsel, cpu_map_idx);
  189. }
  190. void perf_evsel__munmap(struct perf_evsel *evsel)
  191. {
  192. int idx, thread;
  193. if (evsel->fd == NULL || evsel->mmap == NULL)
  194. return;
  195. for (idx = 0; idx < xyarray__max_x(evsel->fd); idx++) {
  196. for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
  197. int *fd = FD(evsel, idx, thread);
  198. if (fd == NULL || *fd < 0)
  199. continue;
  200. perf_mmap__munmap(MMAP(evsel, idx, thread));
  201. }
  202. }
  203. xyarray__delete(evsel->mmap);
  204. evsel->mmap = NULL;
  205. }
  206. int perf_evsel__mmap(struct perf_evsel *evsel, int pages)
  207. {
  208. int ret, idx, thread;
  209. struct perf_mmap_param mp = {
  210. .prot = PROT_READ | PROT_WRITE,
  211. .mask = (pages * page_size) - 1,
  212. };
  213. if (evsel->fd == NULL || evsel->mmap)
  214. return -EINVAL;
  215. if (perf_evsel__alloc_mmap(evsel, xyarray__max_x(evsel->fd), xyarray__max_y(evsel->fd)) < 0)
  216. return -ENOMEM;
  217. for (idx = 0; idx < xyarray__max_x(evsel->fd); idx++) {
  218. for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
  219. int *fd = FD(evsel, idx, thread);
  220. struct perf_mmap *map;
  221. struct perf_cpu cpu = perf_cpu_map__cpu(evsel->cpus, idx);
  222. if (fd == NULL || *fd < 0)
  223. continue;
  224. map = MMAP(evsel, idx, thread);
  225. perf_mmap__init(map, NULL, false, NULL);
  226. ret = perf_mmap__mmap(map, &mp, *fd, cpu);
  227. if (ret) {
  228. perf_evsel__munmap(evsel);
  229. return ret;
  230. }
  231. }
  232. }
  233. return 0;
  234. }
  235. void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu_map_idx, int thread)
  236. {
  237. int *fd = FD(evsel, cpu_map_idx, thread);
  238. if (fd == NULL || *fd < 0 || MMAP(evsel, cpu_map_idx, thread) == NULL)
  239. return NULL;
  240. return MMAP(evsel, cpu_map_idx, thread)->base;
  241. }
  242. int perf_evsel__read_size(struct perf_evsel *evsel)
  243. {
  244. u64 read_format = evsel->attr.read_format;
  245. int entry = sizeof(u64); /* value */
  246. int size = 0;
  247. int nr = 1;
  248. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  249. size += sizeof(u64);
  250. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  251. size += sizeof(u64);
  252. if (read_format & PERF_FORMAT_ID)
  253. entry += sizeof(u64);
  254. if (read_format & PERF_FORMAT_LOST)
  255. entry += sizeof(u64);
  256. if (read_format & PERF_FORMAT_GROUP) {
  257. nr = evsel->nr_members;
  258. size += sizeof(u64);
  259. }
  260. size += entry * nr;
  261. return size;
  262. }
  263. /* This only reads values for the leader */
  264. static int perf_evsel__read_group(struct perf_evsel *evsel, int cpu_map_idx,
  265. int thread, struct perf_counts_values *count)
  266. {
  267. size_t size = perf_evsel__read_size(evsel);
  268. int *fd = FD(evsel, cpu_map_idx, thread);
  269. u64 read_format = evsel->attr.read_format;
  270. u64 *data;
  271. int idx = 1;
  272. if (fd == NULL || *fd < 0)
  273. return -EINVAL;
  274. data = calloc(1, size);
  275. if (data == NULL)
  276. return -ENOMEM;
  277. if (readn(*fd, data, size) <= 0) {
  278. free(data);
  279. return -errno;
  280. }
  281. /*
  282. * This reads only the leader event intentionally since we don't have
  283. * perf counts values for sibling events.
  284. */
  285. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  286. count->ena = data[idx++];
  287. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  288. count->run = data[idx++];
  289. /* value is always available */
  290. count->val = data[idx++];
  291. if (read_format & PERF_FORMAT_ID)
  292. count->id = data[idx++];
  293. if (read_format & PERF_FORMAT_LOST)
  294. count->lost = data[idx++];
  295. free(data);
  296. return 0;
  297. }
  298. /*
  299. * The perf read format is very flexible. It needs to set the proper
  300. * values according to the read format.
  301. */
  302. static void perf_evsel__adjust_values(struct perf_evsel *evsel, u64 *buf,
  303. struct perf_counts_values *count)
  304. {
  305. u64 read_format = evsel->attr.read_format;
  306. int n = 0;
  307. count->val = buf[n++];
  308. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  309. count->ena = buf[n++];
  310. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  311. count->run = buf[n++];
  312. if (read_format & PERF_FORMAT_ID)
  313. count->id = buf[n++];
  314. if (read_format & PERF_FORMAT_LOST)
  315. count->lost = buf[n++];
  316. }
  317. int perf_evsel__read(struct perf_evsel *evsel, int cpu_map_idx, int thread,
  318. struct perf_counts_values *count)
  319. {
  320. size_t size = perf_evsel__read_size(evsel);
  321. int *fd = FD(evsel, cpu_map_idx, thread);
  322. u64 read_format = evsel->attr.read_format;
  323. struct perf_counts_values buf;
  324. memset(count, 0, sizeof(*count));
  325. if (fd == NULL || *fd < 0)
  326. return -EINVAL;
  327. if (read_format & PERF_FORMAT_GROUP)
  328. return perf_evsel__read_group(evsel, cpu_map_idx, thread, count);
  329. if (MMAP(evsel, cpu_map_idx, thread) &&
  330. !(read_format & (PERF_FORMAT_ID | PERF_FORMAT_LOST)) &&
  331. !perf_mmap__read_self(MMAP(evsel, cpu_map_idx, thread), count))
  332. return 0;
  333. if (readn(*fd, buf.values, size) <= 0)
  334. return -errno;
  335. perf_evsel__adjust_values(evsel, buf.values, count);
  336. return 0;
  337. }
  338. static int perf_evsel__ioctl(struct perf_evsel *evsel, int ioc, void *arg,
  339. int cpu_map_idx, int thread)
  340. {
  341. int *fd = FD(evsel, cpu_map_idx, thread);
  342. if (fd == NULL || *fd < 0)
  343. return -1;
  344. return ioctl(*fd, ioc, arg);
  345. }
  346. static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
  347. int ioc, void *arg,
  348. int cpu_map_idx)
  349. {
  350. int thread;
  351. for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
  352. int err = perf_evsel__ioctl(evsel, ioc, arg, cpu_map_idx, thread);
  353. if (err)
  354. return err;
  355. }
  356. return 0;
  357. }
  358. int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
  359. {
  360. return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu_map_idx);
  361. }
  362. int perf_evsel__enable_thread(struct perf_evsel *evsel, int thread)
  363. {
  364. struct perf_cpu cpu __maybe_unused;
  365. int idx;
  366. int err;
  367. perf_cpu_map__for_each_cpu(cpu, idx, evsel->cpus) {
  368. err = perf_evsel__ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, idx, thread);
  369. if (err)
  370. return err;
  371. }
  372. return 0;
  373. }
  374. int perf_evsel__enable(struct perf_evsel *evsel)
  375. {
  376. int i;
  377. int err = 0;
  378. for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
  379. err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, i);
  380. return err;
  381. }
  382. int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
  383. {
  384. return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, cpu_map_idx);
  385. }
  386. int perf_evsel__disable(struct perf_evsel *evsel)
  387. {
  388. int i;
  389. int err = 0;
  390. for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
  391. err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, i);
  392. return err;
  393. }
  394. int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
  395. {
  396. int err = 0, i;
  397. for (i = 0; i < perf_cpu_map__nr(evsel->cpus) && !err; i++)
  398. err = perf_evsel__run_ioctl(evsel,
  399. PERF_EVENT_IOC_SET_FILTER,
  400. (void *)filter, i);
  401. return err;
  402. }
  403. struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
  404. {
  405. return evsel->cpus;
  406. }
  407. struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel)
  408. {
  409. return evsel->threads;
  410. }
  411. struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel)
  412. {
  413. return &evsel->attr;
  414. }
  415. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  416. {
  417. if (ncpus == 0 || nthreads == 0)
  418. return 0;
  419. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  420. if (evsel->sample_id == NULL)
  421. return -ENOMEM;
  422. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  423. if (evsel->id == NULL) {
  424. xyarray__delete(evsel->sample_id);
  425. evsel->sample_id = NULL;
  426. return -ENOMEM;
  427. }
  428. return 0;
  429. }
  430. void perf_evsel__free_id(struct perf_evsel *evsel)
  431. {
  432. struct perf_sample_id_period *pos, *n;
  433. xyarray__delete(evsel->sample_id);
  434. evsel->sample_id = NULL;
  435. zfree(&evsel->id);
  436. evsel->ids = 0;
  437. perf_evsel_for_each_per_thread_period_safe(evsel, n, pos) {
  438. list_del_init(&pos->node);
  439. free(pos);
  440. }
  441. }
  442. bool perf_evsel__attr_has_per_thread_sample_period(struct perf_evsel *evsel)
  443. {
  444. return (evsel->attr.sample_type & PERF_SAMPLE_READ) &&
  445. (evsel->attr.sample_type & PERF_SAMPLE_TID) &&
  446. evsel->attr.inherit;
  447. }
  448. u64 *perf_sample_id__get_period_storage(struct perf_sample_id *sid, u32 tid, bool per_thread)
  449. {
  450. struct hlist_head *head;
  451. struct perf_sample_id_period *res;
  452. int hash;
  453. if (!per_thread)
  454. return &sid->period;
  455. hash = hash_32(tid, PERF_SAMPLE_ID__HLIST_BITS);
  456. head = &sid->periods[hash];
  457. hlist_for_each_entry(res, head, hnode)
  458. if (res->tid == tid)
  459. return &res->period;
  460. if (sid->evsel == NULL)
  461. return NULL;
  462. res = zalloc(sizeof(struct perf_sample_id_period));
  463. if (res == NULL)
  464. return NULL;
  465. INIT_LIST_HEAD(&res->node);
  466. res->tid = tid;
  467. list_add_tail(&res->node, &sid->evsel->per_stream_periods);
  468. hlist_add_head(&res->hnode, &sid->periods[hash]);
  469. return &res->period;
  470. }
  471. void perf_counts_values__scale(struct perf_counts_values *count,
  472. bool scale, __s8 *pscaled)
  473. {
  474. s8 scaled = 0;
  475. if (scale) {
  476. if (count->run == 0) {
  477. scaled = -1;
  478. count->val = 0;
  479. } else if (count->run < count->ena) {
  480. scaled = 1;
  481. count->val = (u64)((double)count->val * count->ena / count->run);
  482. }
  483. }
  484. if (pscaled)
  485. *pscaled = scaled;
  486. }