perfevent.hh revision 9655:78c9adc85718
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. This problem was fixed for 221 * ARM in version 3.7 of the kernel. 222 * 223 * @warning This method doesn't work at all on some 2.6.3x kernels 224 * since it has inverted check for the return value when copying 225 * parameters from userspace. 226 * 227 * @param period Overflow period in events 228 */ 229 void period(uint64_t period); 230 231 /** 232 * Enable a counter for a fixed number of events. 233 * 234 * When this method is called, perf event enables the counter if 235 * it was disabled. It then leaves the counter enabled until it 236 * has overflowed a refresh times. 237 * 238 * @note This does not update the period of the counter. 239 * 240 * @param refresh Number of overflows before disabling the 241 * counter. 242 */ 243 void refresh(int refresh); 244 245 /** 246 * Read the current value of a counter. 247 */ 248 uint64_t read() const; 249 250 /** 251 * Enable signal delivery to a thread on counter overflow. 252 * 253 * @param tid Thread to deliver signal to 254 * @param signal Signal to send upon overflow 255 */ 256 void enableSignals(pid_t tid, int signal); 257 258 /** 259 * Enable signal delivery on counter overflow. Identical to 260 * enableSignals(pid_t) when called with the current TID as its 261 * parameter. 262 * 263 * @param signal Signal to send upon overflow 264 */ 265 void enableSignals(int signal) { enableSignals(gettid(), signal); } 266 267private: 268 // Disallow copying 269 PerfKvmCounter(const PerfKvmCounter &that); 270 // Disallow assignment 271 PerfKvmCounter &operator=(const PerfKvmCounter &that); 272 273 void attach(PerfKvmCounterConfig &config, pid_t tid, int group_fd); 274 275 /** 276 * Get the TID of the current thread. 277 * 278 * @return Current thread's TID 279 */ 280 pid_t gettid(); 281 282 /** 283 * MMAP the PerfEvent file descriptor. 284 * 285 * @note We currently don't use the ring buffer, but PerfEvent 286 * requires this to be mapped for overflow handling to work. 287 * 288 * @note Overflow handling requires at least one buf_page to be 289 * mapped. 290 * 291 * @param pages number of pages in circular sample buffer. Must be 292 * an even power of 2. 293 */ 294 void mmapPerf(int pages); 295 296 /** @{ */ 297 /** 298 * PerfEvent fnctl interface. 299 * 300 * @param cmd fcntl command 301 * @param p1 Request parameter 302 * 303 * @return -1 on error (error number in errno), ioctl dependent 304 * value otherwise. 305 */ 306 int fcntl(int cmd, long p1); 307 int fcntl(int cmd, void *p1) { return fcntl(cmd, (long)p1); } 308 /** @} */ 309 310 /** @{ */ 311 /** 312 * PerfEvent ioctl interface. 313 * 314 * @param request PerfEvent request 315 * @param p1 Optional request parameter 316 * 317 * @return -1 on error (error number in errno), ioctl dependent 318 * value otherwise. 319 */ 320 int ioctl(int request, long p1); 321 int ioctl(int request, void *p1) { return ioctl(request, (long)p1); } 322 int ioctl(int request) { return ioctl(request, 0L); } 323 /** @} */ 324 325 /** 326 * Perform a read from the counter file descriptor. 327 * 328 * @param buf Destination buffer 329 * @param size Amount of data to read 330 */ 331 void read(void *buf, size_t size) const; 332 333 /** 334 * PerfEvent file descriptor associated with counter. -1 if not 335 * attached to PerfEvent. 336 */ 337 int fd; 338 339 /** Memory mapped PerfEvent sample ring buffer */ 340 struct perf_event_mmap_page *ringBuffer; 341 /** Total number of pages in ring buffer */ 342 int ringNumPages; 343 344 /** Cached host page size */ 345 long pageSize; 346}; 347 348#endif 349