]>
Commit | Line | Data |
---|---|---|
69aad6f1 | 1 | #include "evsel.h" |
48290609 | 2 | #include "../perf.h" |
69aad6f1 | 3 | #include "util.h" |
86bd5e86 ACM |
4 | #include "cpumap.h" |
5 | #include "thread.h" | |
69aad6f1 | 6 | |
c52b12ed ACM |
7 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
8 | ||
23a2f3ab | 9 | struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) |
69aad6f1 ACM |
10 | { |
11 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); | |
12 | ||
13 | if (evsel != NULL) { | |
14 | evsel->idx = idx; | |
23a2f3ab | 15 | evsel->attr = *attr; |
69aad6f1 ACM |
16 | INIT_LIST_HEAD(&evsel->node); |
17 | } | |
18 | ||
19 | return evsel; | |
20 | } | |
21 | ||
22 | int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) | |
23 | { | |
24 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); | |
25 | return evsel->fd != NULL ? 0 : -ENOMEM; | |
26 | } | |
27 | ||
c52b12ed ACM |
28 | int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) |
29 | { | |
30 | evsel->counts = zalloc((sizeof(*evsel->counts) + | |
31 | (ncpus * sizeof(struct perf_counts_values)))); | |
32 | return evsel->counts != NULL ? 0 : -ENOMEM; | |
33 | } | |
34 | ||
69aad6f1 ACM |
35 | void perf_evsel__free_fd(struct perf_evsel *evsel) |
36 | { | |
37 | xyarray__delete(evsel->fd); | |
38 | evsel->fd = NULL; | |
39 | } | |
40 | ||
c52b12ed ACM |
41 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
42 | { | |
43 | int cpu, thread; | |
44 | ||
45 | for (cpu = 0; cpu < ncpus; cpu++) | |
46 | for (thread = 0; thread < nthreads; ++thread) { | |
47 | close(FD(evsel, cpu, thread)); | |
48 | FD(evsel, cpu, thread) = -1; | |
49 | } | |
50 | } | |
51 | ||
69aad6f1 ACM |
52 | void perf_evsel__delete(struct perf_evsel *evsel) |
53 | { | |
54 | assert(list_empty(&evsel->node)); | |
55 | xyarray__delete(evsel->fd); | |
56 | free(evsel); | |
57 | } | |
c52b12ed ACM |
58 | |
59 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, | |
60 | int cpu, int thread, bool scale) | |
61 | { | |
62 | struct perf_counts_values count; | |
63 | size_t nv = scale ? 3 : 1; | |
64 | ||
65 | if (FD(evsel, cpu, thread) < 0) | |
66 | return -EINVAL; | |
67 | ||
4eed11d5 ACM |
68 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0) |
69 | return -ENOMEM; | |
70 | ||
c52b12ed ACM |
71 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
72 | return -errno; | |
73 | ||
74 | if (scale) { | |
75 | if (count.run == 0) | |
76 | count.val = 0; | |
77 | else if (count.run < count.ena) | |
78 | count.val = (u64)((double)count.val * count.ena / count.run + 0.5); | |
79 | } else | |
80 | count.ena = count.run = 0; | |
81 | ||
82 | evsel->counts->cpu[cpu] = count; | |
83 | return 0; | |
84 | } | |
85 | ||
86 | int __perf_evsel__read(struct perf_evsel *evsel, | |
87 | int ncpus, int nthreads, bool scale) | |
88 | { | |
89 | size_t nv = scale ? 3 : 1; | |
90 | int cpu, thread; | |
91 | struct perf_counts_values *aggr = &evsel->counts->aggr, count; | |
92 | ||
93 | aggr->val = 0; | |
94 | ||
95 | for (cpu = 0; cpu < ncpus; cpu++) { | |
96 | for (thread = 0; thread < nthreads; thread++) { | |
97 | if (FD(evsel, cpu, thread) < 0) | |
98 | continue; | |
99 | ||
100 | if (readn(FD(evsel, cpu, thread), | |
101 | &count, nv * sizeof(u64)) < 0) | |
102 | return -errno; | |
103 | ||
104 | aggr->val += count.val; | |
105 | if (scale) { | |
106 | aggr->ena += count.ena; | |
107 | aggr->run += count.run; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | evsel->counts->scaled = 0; | |
113 | if (scale) { | |
114 | if (aggr->run == 0) { | |
115 | evsel->counts->scaled = -1; | |
116 | aggr->val = 0; | |
117 | return 0; | |
118 | } | |
119 | ||
120 | if (aggr->run < aggr->ena) { | |
121 | evsel->counts->scaled = 1; | |
122 | aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5); | |
123 | } | |
124 | } else | |
125 | aggr->ena = aggr->run = 0; | |
126 | ||
127 | return 0; | |
128 | } | |
48290609 | 129 | |
86bd5e86 | 130 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, struct cpu_map *cpus) |
48290609 ACM |
131 | { |
132 | int cpu; | |
133 | ||
4eed11d5 ACM |
134 | if (evsel->fd == NULL && perf_evsel__alloc_fd(evsel, cpus->nr, 1) < 0) |
135 | return -1; | |
136 | ||
86bd5e86 | 137 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
48290609 | 138 | FD(evsel, cpu, 0) = sys_perf_event_open(&evsel->attr, -1, |
86bd5e86 | 139 | cpus->map[cpu], -1, 0); |
48290609 ACM |
140 | if (FD(evsel, cpu, 0) < 0) |
141 | goto out_close; | |
142 | } | |
143 | ||
144 | return 0; | |
145 | ||
146 | out_close: | |
147 | while (--cpu >= 0) { | |
148 | close(FD(evsel, cpu, 0)); | |
149 | FD(evsel, cpu, 0) = -1; | |
150 | } | |
151 | return -1; | |
152 | } | |
153 | ||
86bd5e86 | 154 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, struct thread_map *threads) |
48290609 ACM |
155 | { |
156 | int thread; | |
157 | ||
4eed11d5 ACM |
158 | if (evsel->fd == NULL && perf_evsel__alloc_fd(evsel, 1, threads->nr)) |
159 | return -1; | |
160 | ||
86bd5e86 | 161 | for (thread = 0; thread < threads->nr; thread++) { |
48290609 | 162 | FD(evsel, 0, thread) = sys_perf_event_open(&evsel->attr, |
86bd5e86 | 163 | threads->map[thread], -1, -1, 0); |
48290609 ACM |
164 | if (FD(evsel, 0, thread) < 0) |
165 | goto out_close; | |
166 | } | |
167 | ||
168 | return 0; | |
169 | ||
170 | out_close: | |
171 | while (--thread >= 0) { | |
172 | close(FD(evsel, 0, thread)); | |
173 | FD(evsel, 0, thread) = -1; | |
174 | } | |
175 | return -1; | |
176 | } | |
177 | ||
86bd5e86 ACM |
178 | int perf_evsel__open(struct perf_evsel *evsel, |
179 | struct cpu_map *cpus, struct thread_map *threads) | |
48290609 | 180 | { |
86bd5e86 ACM |
181 | if (threads == NULL) |
182 | return perf_evsel__open_per_cpu(evsel, cpus); | |
48290609 | 183 | |
86bd5e86 | 184 | return perf_evsel__open_per_thread(evsel, threads); |
48290609 | 185 | } |