gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
perfevent.cc
Go to the documentation of this file.
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 #include <fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/mman.h>
43 #include <sys/syscall.h>
44 #include <sys/types.h>
45 #include <syscall.h>
46 #include <unistd.h>
47 
48 #include <cassert>
49 #include <cerrno>
50 #include <csignal>
51 #include <cstring>
52 
53 #include "base/misc.hh"
54 #include "perfevent.hh"
55 
57 {
58  memset(&attr, 0, sizeof(attr));
59 
60  attr.size = PERF_ATTR_SIZE_VER0;
61  attr.type = type;
62  attr.config = config;
63 }
64 
66 {
67 }
68 
69 
71  : fd(-1), ringBuffer(NULL), pageSize(-1)
72 {
73  attach(config, tid, -1);
74 }
75 
77  pid_t tid, const PerfKvmCounter &parent)
78  : fd(-1), ringBuffer(NULL), pageSize(-1)
79 {
80  attach(config, tid, parent);
81 }
82 
84  : fd(-1), ringBuffer(NULL), pageSize(-1)
85 {
86 }
87 
89 {
90  if (attached())
91  detach();
92 }
93 
94 void
96 {
97  assert(attached());
98 
99  if (munmap(ringBuffer, ringNumPages * pageSize) == -1)
100  warn("PerfKvmCounter: Failed to unmap ring buffer (%i)\n",
101  errno);
102  close(fd);
103 
104  fd = -1;
105  ringBuffer = NULL;
106 }
107 
108 void
110 {
111  if (ioctl(PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1)
112  panic("KVM: Failed to enable performance counters (%i)\n", errno);
113 }
114 
115 void
117 {
118  if (ioctl(PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1)
119  panic("KVM: Failed to disable performance counters (%i)\n", errno);
120 }
121 
122 void
123 PerfKvmCounter::period(uint64_t period)
124 {
125  if (ioctl(PERF_EVENT_IOC_PERIOD, &period) == -1)
126  panic("KVM: Failed to set period of performance counter (%i)\n", errno);
127 }
128 
129 void
131 {
132  if (ioctl(PERF_EVENT_IOC_REFRESH, refresh) == -1)
133  panic("KVM: Failed to refresh performance counter (%i)\n", errno);
134 }
135 
136 uint64_t
138 {
139  uint64_t value;
140 
141  read(&value, sizeof(uint64_t));
142  return value;
143 }
144 
145 void
146 PerfKvmCounter::enableSignals(pid_t tid, int signal)
147 {
148  struct f_owner_ex sigowner;
149 
150  sigowner.type = F_OWNER_TID;
151  sigowner.pid = tid;
152 
153  if (fcntl(F_SETOWN_EX, &sigowner) == -1 ||
154  fcntl(F_SETSIG, signal) == -1 ||
155  fcntl(F_SETFL, O_ASYNC) == -1)
156  panic("PerfKvmCounter: Failed to enable signals for counter (%i)\n",
157  errno);
158 }
159 
160 void
162  pid_t tid, int group_fd)
163 {
164  assert(!attached());
165 
166  fd = syscall(__NR_perf_event_open,
167  &config.attr, tid,
168  -1, // CPU (-1 => Any CPU that the task happens to run on)
169  group_fd,
170  0); // Flags
171  if (fd == -1)
172  panic("PerfKvmCounter::open failed (%i)\n", errno);
173 
174  mmapPerf(1);
175 }
176 
177 pid_t
179 {
180  return syscall(__NR_gettid);
181 }
182 
183 void
185 {
186  assert(attached());
187  assert(ringBuffer == NULL);
188 
189  if (pageSize == -1) {
190  pageSize = sysconf(_SC_PAGE_SIZE);
191  if (pageSize == -1)
192  panic("PerfKvmCounter: Failed to determine page size (%i)\n",
193  errno);
194  }
195 
196  ringNumPages = pages + 1;
197  ringBuffer = (struct perf_event_mmap_page *)mmap(
198  NULL, ringNumPages * 4096,
199  PROT_READ | PROT_WRITE, MAP_SHARED,
200  fd, 0);
201  if (ringBuffer == MAP_FAILED)
202  panic("PerfKvmCounter: MMAP failed (%i)\n",
203  errno);
204 }
205 
206 int
207 PerfKvmCounter::fcntl(int cmd, long p1)
208 {
209  assert(attached());
210  return ::fcntl(fd, cmd, p1);
211 }
212 
213 int
214 PerfKvmCounter::ioctl(int request, long p1)
215 {
216  assert(attached());
217  return ::ioctl(fd, request, p1);
218 }
219 
220 void
221 PerfKvmCounter::read(void *buf, size_t size) const
222 {
223  char *_buf = (char *)buf;
224  size_t _size = size;
225 
226  assert(attached());
227 
228  do {
229  ssize_t ret;
230  ret = ::read(fd, _buf, _size);
231  switch (ret) {
232  case -1:
233  if (errno != EAGAIN)
234  panic("PerfKvmCounter::read failed (%i)\n", errno);
235  break;
236 
237  case 0:
238  panic("PerfKvmCounter::read unexpected EOF.\n");
239 
240  default:
241  _size -= ret;
242  _buf += ret;
243  break;
244  }
245  } while (_size);
246 }
void detach()
Detach a counter from PerfEvent.
Definition: perfevent.cc:95
long pageSize
Cached host page size.
Definition: perfevent.hh:380
struct perf_event_attr attr
Underlying perf_event_attr structure describing the counter.
Definition: perfevent.hh:164
#define panic(...)
Definition: misc.hh:153
uint64_t read() const
Read the current value of a counter.
Definition: perfevent.cc:137
PerfKvmCounter()
Create a new counter, but don't attach it.
Definition: perfevent.cc:83
struct perf_event_mmap_page * ringBuffer
Memory mapped PerfEvent sample ring buffer.
Definition: perfevent.hh:375
void enableSignals(pid_t tid, int signal)
Enable signal delivery to a thread on counter overflow.
Definition: perfevent.cc:146
int fd
PerfEvent file descriptor associated with counter.
Definition: perfevent.hh:372
pid_t gettid()
Get the TID of the current thread.
Definition: perfevent.cc:178
#define warn(...)
Definition: misc.hh:219
void refresh(int refresh)
Enable a counter for a fixed number of events.
Definition: perfevent.cc:130
PerfKvmCounterConfig(uint32_t type, uint64_t config)
Initialize PerfEvent counter configuration structure.
Definition: perfevent.cc:56
void mmapPerf(int pages)
MMAP the PerfEvent file descriptor.
Definition: perfevent.cc:184
void period(uint64_t period)
Update the period of an overflow counter.
Definition: perfevent.cc:123
int ringNumPages
Total number of pages in ring buffer.
Definition: perfevent.hh:377
An instance of a performance counter.
Definition: perfevent.hh:170
void attach(PerfKvmCounterConfig &config, pid_t tid)
Attach a counter.
Definition: perfevent.hh:206
type
Definition: misc.hh:728
int size()
Definition: pagetable.hh:146
PerfEvent counter configuration.
Definition: perfevent.hh:53
int ioctl(int request, long p1)
PerfEvent ioctl interface.
Definition: perfevent.cc:214
void stop()
Stop counting.
Definition: perfevent.cc:116
void start()
Start counting.
Definition: perfevent.cc:109
Bitfield< 14, 12 > fd
Definition: types.hh:155
bool attached() const
Check if a counter is attached.
Definition: perfevent.hh:230
int fcntl(int cmd, long p1)
PerfEvent fnctl interface.
Definition: perfevent.cc:207

Generated on Fri Jun 9 2017 13:03:42 for gem5 by doxygen 1.8.6