evsel.c 12 KB

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