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

Generated on Wed Dec 21 2022 10:22:30 for gem5 by doxygen 1.9.1