evsel.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/threadmap.h>
  15. #include <internal/lib.h>
  16. #include <linux/string.h>
  17. #include <sys/ioctl.h>
  18. void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
  19. {
  20. INIT_LIST_HEAD(&evsel->node);
  21. evsel->attr = *attr;
  22. }
  23. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
  24. {
  25. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  26. if (evsel != NULL)
  27. perf_evsel__init(evsel, attr);
  28. return evsel;
  29. }
  30. void perf_evsel__delete(struct perf_evsel *evsel)
  31. {
  32. free(evsel);
  33. }
  34. #define FD(e, x, y) (*(int *) xyarray__entry(e->fd, x, y))
  35. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  36. {
  37. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  38. if (evsel->fd) {
  39. int cpu, thread;
  40. for (cpu = 0; cpu < ncpus; cpu++) {
  41. for (thread = 0; thread < nthreads; thread++) {
  42. FD(evsel, cpu, thread) = -1;
  43. }
  44. }
  45. }
  46. return evsel->fd != NULL ? 0 : -ENOMEM;
  47. }
  48. static int
  49. sys_perf_event_open(struct perf_event_attr *attr,
  50. pid_t pid, int cpu, int group_fd,
  51. unsigned long flags)
  52. {
  53. return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
  54. }
  55. int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
  56. struct perf_thread_map *threads)
  57. {
  58. int cpu, thread, err = 0;
  59. if (cpus == NULL) {
  60. static struct perf_cpu_map *empty_cpu_map;
  61. if (empty_cpu_map == NULL) {
  62. empty_cpu_map = perf_cpu_map__dummy_new();
  63. if (empty_cpu_map == NULL)
  64. return -ENOMEM;
  65. }
  66. cpus = empty_cpu_map;
  67. }
  68. if (threads == NULL) {
  69. static struct perf_thread_map *empty_thread_map;
  70. if (empty_thread_map == NULL) {
  71. empty_thread_map = perf_thread_map__new_dummy();
  72. if (empty_thread_map == NULL)
  73. return -ENOMEM;
  74. }
  75. threads = empty_thread_map;
  76. }
  77. if (evsel->fd == NULL &&
  78. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  79. return -ENOMEM;
  80. for (cpu = 0; cpu < cpus->nr; cpu++) {
  81. for (thread = 0; thread < threads->nr; thread++) {
  82. int fd;
  83. fd = sys_perf_event_open(&evsel->attr,
  84. threads->map[thread].pid,
  85. cpus->map[cpu], -1, 0);
  86. if (fd < 0)
  87. return -errno;
  88. FD(evsel, cpu, thread) = fd;
  89. }
  90. }
  91. return err;
  92. }
  93. void perf_evsel__close_fd(struct perf_evsel *evsel)
  94. {
  95. int cpu, thread;
  96. for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++)
  97. for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
  98. close(FD(evsel, cpu, thread));
  99. FD(evsel, cpu, thread) = -1;
  100. }
  101. }
  102. void perf_evsel__free_fd(struct perf_evsel *evsel)
  103. {
  104. xyarray__delete(evsel->fd);
  105. evsel->fd = NULL;
  106. }
  107. void perf_evsel__close(struct perf_evsel *evsel)
  108. {
  109. if (evsel->fd == NULL)
  110. return;
  111. perf_evsel__close_fd(evsel);
  112. perf_evsel__free_fd(evsel);
  113. }
  114. int perf_evsel__read_size(struct perf_evsel *evsel)
  115. {
  116. u64 read_format = evsel->attr.read_format;
  117. int entry = sizeof(u64); /* value */
  118. int size = 0;
  119. int nr = 1;
  120. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  121. size += sizeof(u64);
  122. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  123. size += sizeof(u64);
  124. if (read_format & PERF_FORMAT_ID)
  125. entry += sizeof(u64);
  126. if (read_format & PERF_FORMAT_GROUP) {
  127. nr = evsel->nr_members;
  128. size += sizeof(u64);
  129. }
  130. size += entry * nr;
  131. return size;
  132. }
  133. int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
  134. struct perf_counts_values *count)
  135. {
  136. size_t size = perf_evsel__read_size(evsel);
  137. memset(count, 0, sizeof(*count));
  138. if (FD(evsel, cpu, thread) < 0)
  139. return -EINVAL;
  140. if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
  141. return -errno;
  142. return 0;
  143. }
  144. static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
  145. int ioc, void *arg)
  146. {
  147. int cpu, thread;
  148. for (cpu = 0; cpu < xyarray__max_x(evsel->fd); cpu++) {
  149. for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
  150. int fd = FD(evsel, cpu, thread),
  151. err = ioctl(fd, ioc, arg);
  152. if (err)
  153. return err;
  154. }
  155. }
  156. return 0;
  157. }
  158. int perf_evsel__enable(struct perf_evsel *evsel)
  159. {
  160. return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0);
  161. }
  162. int perf_evsel__disable(struct perf_evsel *evsel)
  163. {
  164. return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0);
  165. }
  166. int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
  167. {
  168. return perf_evsel__run_ioctl(evsel,
  169. PERF_EVENT_IOC_SET_FILTER,
  170. (void *)filter);
  171. }
  172. struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
  173. {
  174. return evsel->cpus;
  175. }
  176. struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel)
  177. {
  178. return evsel->threads;
  179. }
  180. struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel)
  181. {
  182. return &evsel->attr;
  183. }
  184. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  185. {
  186. if (ncpus == 0 || nthreads == 0)
  187. return 0;
  188. if (evsel->system_wide)
  189. nthreads = 1;
  190. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  191. if (evsel->sample_id == NULL)
  192. return -ENOMEM;
  193. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  194. if (evsel->id == NULL) {
  195. xyarray__delete(evsel->sample_id);
  196. evsel->sample_id = NULL;
  197. return -ENOMEM;
  198. }
  199. return 0;
  200. }
  201. void perf_evsel__free_id(struct perf_evsel *evsel)
  202. {
  203. xyarray__delete(evsel->sample_id);
  204. evsel->sample_id = NULL;
  205. zfree(&evsel->id);
  206. evsel->ids = 0;
  207. }