perfevent.hh revision 9651:f551c8ad12a5
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __CPU_KVM_PERFEVENT_HH__
41#define __CPU_KVM_PERFEVENT_HH__
42
43#include <linux/perf_event.h>
44#include <sys/types.h>
45
46#include <inttypes.h>
47
48/**
49 * PerfEvent counter configuration.
50 */
51class PerfKvmCounterConfig
52{
53  public:
54    /**
55     * Initialize PerfEvent counter configuration structure
56     *
57     * PerfEvent has the concept of counter types, which is a way to
58     * abstract hardware performance counters or access software
59     * events. The type field in the configuration specifies what type
60     * of counter this is. For hardware performance counters, it's
61     * typically PERF_TYPE_HARDWARE, PERF_TYPE_HW_CACHE, or
62     * PERF_TYPE_RAW.
63     *
64     * The 'config' field has different meanings depending on the type
65     * of counter. Possible values are listed in perf_event.h in the
66     * kernel headers. When using raw counters, the value is the raw
67     * value written to the performance counter configuration register
68     * (some bits dealing with sampling and similar features are
69     * usually masked).
70     *
71     * @param type Counter type.
72     * @param config Counter configuration
73     */
74    PerfKvmCounterConfig(uint32_t type, uint64_t config);
75    ~PerfKvmCounterConfig();
76
77    /**
78     * Set the initial sample period (overflow count) of an event. If
79     * this is set to 0, the event acts as a normal counting event and
80     * does not trigger overflows.
81     *
82     * @param period Number of counter events before the counter
83     * overflows
84     */
85    PerfKvmCounterConfig &samplePeriod(uint64_t period) {
86        attr.freq = 0;
87        attr.sample_period = period;
88        return *this;
89    }
90
91    /**
92     * Set the number of samples that need to be triggered before
93     * reporting data as being available on the perf event
94     * FD. Defaults to 0, which disables overflow reporting.
95     *
96     * @param events Number of overflows before signaling a wake up
97     */
98    PerfKvmCounterConfig &wakeupEvents(uint32_t events) {
99        attr.watermark = 0;
100        attr.wakeup_events = events;
101        return *this;
102    }
103
104    /**
105     * Don't start the performance counter automatically when
106     * attaching it.
107     *
108     * @param val true to disable, false to enable the counter
109     */
110    PerfKvmCounterConfig &disabled(bool val) {
111        attr.disabled = val;
112        return *this;
113    }
114
115    /**
116     * Force the group to be on the active all the time (i.e.,
117     * disallow multiplexing).
118     *
119     * Only applies to group leaders.
120     *
121     * @param val true to pin the counter
122     */
123    PerfKvmCounterConfig &pinned(bool val) {
124        attr.pinned = val;
125        return *this;
126    }
127
128    /** Underlying perf_event_attr structure describing the counter */
129    struct perf_event_attr attr;
130};
131
132/**
133 * An instance of a performance counter.
134 */
135class PerfKvmCounter
136{
137public:
138    /**
139     * Create and attach a new counter group.
140     *
141     * @param config Counter configuration
142     * @param tid Thread to sample (0 indicates current thread)
143     */
144    PerfKvmCounter(PerfKvmCounterConfig &config, pid_t tid);
145    /**
146     * Create and attach a new counter and make it a member of an
147     * exist counter group.
148     *
149     * @param config Counter configuration
150     * @param tid Thread to sample (0 indicates current thread)
151     * @param parent Group leader
152     */
153    PerfKvmCounter(PerfKvmCounterConfig &config,
154                pid_t tid, const PerfKvmCounter &parent);
155    /**
156     * Create a new counter, but don't attach it.
157     */
158    PerfKvmCounter();
159    ~PerfKvmCounter();
160
161
162    /**
163     * Attach a counter.
164     *
165     * @note This operation is only supported if the counter isn't
166     * already attached.
167     *
168     * @param config Counter configuration
169     * @param tid Thread to sample (0 indicates current thread)
170     */
171    void attach(PerfKvmCounterConfig &config, pid_t tid) {
172        attach(config, tid, -1);
173    }
174
175    /**
176     * Attach a counter and make it a member of an existing counter
177     * group.
178     *
179     * @note This operation is only supported if the counter isn't
180     * already attached.
181     *
182     * @param config Counter configuration
183     * @param tid Thread to sample (0 indicates current thread)
184     * @param parent Group leader
185     */
186    void attach(PerfKvmCounterConfig &config,
187                pid_t tid, const PerfKvmCounter &parent) {
188        attach(config, tid, parent.fd);
189    }
190
191    /** Detach a counter from PerfEvent. */
192    void detach();
193
194    /** Check if a counter is attached. */
195    bool attached() const { return fd != -1; }
196
197    /**
198     * Start counting.
199     *
200     * @note If this counter is a group leader, it will start the
201     * entire group.
202     */
203    void start();
204
205    /**
206     * Stop counting.
207     *
208     * @note If this counter is a group leader, it will stop the
209     * entire group.
210     */
211    void stop();
212
213    /**
214     * Update the period of an overflow counter.
215     *
216     * @warning This ioctl has some pretty bizarre semantics. It seems
217     * like the new period isn't effective until after the next
218     * counter overflow. If you use this method to change the sample
219     * period, you will see one sample with the old period and then
220     * start sampling with the new period.
221     *
222     * @warning This method doesn't work at all on some 2.6.3x kernels
223     * since it has inverted check for the return value when copying
224     * parameters from userspace.
225     *
226     * @param period Overflow period in events
227     */
228    void period(uint64_t period);
229
230    /**
231     * Enable a counter for a fixed number of events.
232     *
233     * When this method is called, perf event enables the counter if
234     * it was disabled. It then leaves the counter enabled until it
235     * has overflowed a refresh times.
236     *
237     * @note This does not update the period of the counter.
238     *
239     * @param refresh Number of overflows before disabling the
240     * counter.
241     */
242    void refresh(int refresh);
243
244    /**
245     * Read the current value of a counter.
246     */
247    uint64_t read() const;
248
249    /**
250     * Enable signal delivery to a thread on counter overflow.
251     *
252     * @param tid Thread to deliver signal to
253     * @param signal Signal to send upon overflow
254     */
255    void enableSignals(pid_t tid, int signal);
256
257    /**
258     * Enable signal delivery on counter overflow. Identical to
259     * enableSignals(pid_t) when called with the current TID as its
260     * parameter.
261     *
262     * @param signal Signal to send upon overflow
263     */
264    void enableSignals(int signal) { enableSignals(gettid(), signal); }
265
266private:
267    // Disallow copying
268    PerfKvmCounter(const PerfKvmCounter &that);
269    // Disallow assignment
270    PerfKvmCounter &operator=(const PerfKvmCounter &that);
271
272    void attach(PerfKvmCounterConfig &config, pid_t tid, int group_fd);
273
274    /**
275     * Get the TID of the current thread.
276     *
277     * @return Current thread's TID
278     */
279    pid_t gettid();
280
281    /**
282     * MMAP the PerfEvent file descriptor.
283     *
284     * @note We currently don't use the ring buffer, but PerfEvent
285     * requires this to be mapped for overflow handling to work.
286     *
287     * @note Overflow handling requires at least one buf_page to be
288     * mapped.
289     *
290     * @param pages number of pages in circular sample buffer. Must be
291     * an even power of 2.
292     */
293    void mmapPerf(int pages);
294
295    /** @{ */
296    /**
297     * PerfEvent fnctl interface.
298     *
299     * @param cmd fcntl command
300     * @param p1 Request parameter
301     *
302     * @return -1 on error (error number in errno), ioctl dependent
303     * value otherwise.
304     */
305    int fcntl(int cmd, long p1);
306    int fcntl(int cmd, void *p1) { return fcntl(cmd, (long)p1); }
307    /** @} */
308
309    /** @{ */
310    /**
311     * PerfEvent ioctl interface.
312     *
313     * @param request PerfEvent request
314     * @param p1 Optional request parameter
315     *
316     * @return -1 on error (error number in errno), ioctl dependent
317     * value otherwise.
318     */
319    int ioctl(int request, long p1);
320    int ioctl(int request, void *p1) { return ioctl(request, (long)p1); }
321    int ioctl(int request) { return ioctl(request, 0L); }
322    /** @} */
323
324    /**
325     * Perform a read from the counter file descriptor.
326     *
327     * @param buf Destination buffer
328     * @param size Amount of data to read
329     */
330    void read(void *buf, size_t size) const;
331
332    /**
333     * PerfEvent file descriptor associated with counter. -1 if not
334     * attached to PerfEvent.
335     */
336    int fd;
337
338    /** Memory mapped PerfEvent sample ring buffer */
339    struct perf_event_mmap_page *ringBuffer;
340    /** Total number of pages in ring buffer */
341    int ringNumPages;
342
343    /** Cached host page size */
344    long pageSize;
345};
346
347#endif
348