gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/logging.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
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  {
173  if (errno == EACCES)
174  {
175  panic("PerfKvmCounter::attach recieved error EACCESS\n"
176  " This error may be caused by a too restrictive setting\n"
177  " in the file '/proc/sys/kernel/perf_event_paranoid'\n"
178  " The default value was changed to 2 in kernel 4.6\n"
179  " A value greater than 1 prevents gem5 from making\n"
180  " the syscall to perf_event_open");
181  }
182  panic("PerfKvmCounter::attach failed (%i)\n", errno);
183  }
184 
185  mmapPerf(1);
186 }
187 
188 pid_t
190 {
191  return syscall(__NR_gettid);
192 }
193 
194 void
196 {
197  assert(attached());
198  assert(ringBuffer == NULL);
199 
200  if (pageSize == -1) {
201  pageSize = sysconf(_SC_PAGE_SIZE);
202  if (pageSize == -1)
203  panic("PerfKvmCounter: Failed to determine page size (%i)\n",
204  errno);
205  }
206 
207  ringNumPages = pages + 1;
208  ringBuffer = (struct perf_event_mmap_page *)mmap(
209  NULL, ringNumPages * 4096,
210  PROT_READ | PROT_WRITE, MAP_SHARED,
211  fd, 0);
212  if (ringBuffer == MAP_FAILED)
213  panic("PerfKvmCounter: MMAP failed (%i)\n",
214  errno);
215 }
216 
217 int
218 PerfKvmCounter::fcntl(int cmd, long p1)
219 {
220  assert(attached());
221  return ::fcntl(fd, cmd, p1);
222 }
223 
224 int
225 PerfKvmCounter::ioctl(int request, long p1)
226 {
227  assert(attached());
228  return ::ioctl(fd, request, p1);
229 }
230 
231 void
232 PerfKvmCounter::read(void *buf, size_t size) const
233 {
234  char *_buf = (char *)buf;
235  size_t _size = size;
236 
237  assert(attached());
238 
239  do {
240  ssize_t ret;
241  ret = ::read(fd, _buf, _size);
242  switch (ret) {
243  case -1:
244  if (errno != EAGAIN)
245  panic("PerfKvmCounter::read failed (%i)\n", errno);
246  break;
247 
248  case 0:
249  panic("PerfKvmCounter::read unexpected EOF.\n");
250 
251  default:
252  _size -= ret;
253  _buf += ret;
254  break;
255  }
256  } while (_size);
257 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
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
PerfKvmCounter()
Create a new counter, but don&#39;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
uint8_t type
Definition: inet.hh:333
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:195
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
bool attached() const
Check if a counter is attached.
Definition: perfevent.hh:230
uint64_t read() const
Read the current value of a counter.
Definition: perfevent.cc:137
An instance of a performance counter.
Definition: perfevent.hh:170
void attach(PerfKvmCounterConfig &config, pid_t tid)
Attach a counter.
Definition: perfevent.hh:206
PerfEvent counter configuration.
Definition: perfevent.hh:53
pid_t sysGettid()
Get the TID of the current thread.
Definition: perfevent.cc:189
int ioctl(int request, long p1)
PerfEvent ioctl interface.
Definition: perfevent.cc:225
void stop()
Stop counting.
Definition: perfevent.cc:116
#define warn(...)
Definition: logging.hh:212
void start()
Start counting.
Definition: perfevent.cc:109
Bitfield< 14, 12 > fd
Definition: types.hh:160
int fcntl(int cmd, long p1)
PerfEvent fnctl interface.
Definition: perfevent.cc:218

Generated on Fri Feb 28 2020 16:26:59 for gem5 by doxygen 1.8.13