gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
54namespace 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
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
94
95void
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
109void
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
116void
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
123void
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
130void
132{
133 if (ioctl(PERF_EVENT_IOC_REFRESH, refresh) == -1)
134 panic("KVM: Failed to refresh performance counter (%i)\n", errno);
135}
136
137uint64_t
139{
140 uint64_t value;
141
142 read(&value, sizeof(uint64_t));
143 return value;
144}
145
146void
147PerfKvmCounter::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
161void
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 received 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.\n"
182 " Alternatively, you can set the usePerf flag of the KVM\n"
183 " CPU to False. Setting this flag to False will limit some\n"
184 " functionalities of KVM CPU, such as counting the number of\n"
185 " cycles and the number of instructions, as well as the\n"
186 " ability of exiting to gem5 after a certain amount of cycles\n"
187 " or instructions when using KVM CPU. An example can be found\n"
188 " here, configs/example/gem5_library/"
189 "x86-ubuntu-run-with-kvm-no-perf.py.");
190 }
191 panic("PerfKvmCounter::attach failed (%i)\n", errno);
192 }
193
194 mmapPerf(1);
195}
196
197pid_t
199{
200 return syscall(__NR_gettid);
201}
202
203void
205{
206 assert(attached());
207 assert(ringBuffer == NULL);
208
209 if (pageSize == -1) {
210 pageSize = sysconf(_SC_PAGE_SIZE);
211 if (pageSize == -1)
212 panic("PerfKvmCounter: Failed to determine page size (%i)\n",
213 errno);
214 }
215
216 ringNumPages = pages + 1;
217 ringBuffer = (struct perf_event_mmap_page *)mmap(
218 NULL, ringNumPages * 4096,
219 PROT_READ | PROT_WRITE, MAP_SHARED,
220 fd, 0);
221 if (ringBuffer == MAP_FAILED)
222 panic("PerfKvmCounter: MMAP failed (%i)\n",
223 errno);
224}
225
226int
227PerfKvmCounter::fcntl(int cmd, long p1)
228{
229 assert(attached());
230 return ::fcntl(fd, cmd, p1);
231}
232
233int
234PerfKvmCounter::ioctl(int request, long p1)
235{
236 assert(attached());
237 return ::ioctl(fd, request, p1);
238}
239
240void
241PerfKvmCounter::read(void *buf, size_t size) const
242{
243 char *_buf = (char *)buf;
244 size_t _size = size;
245
246 assert(attached());
247
248 do {
249 ssize_t ret;
250 ret = ::read(fd, _buf, _size);
251 switch (ret) {
252 case -1:
253 if (errno != EAGAIN)
254 panic("PerfKvmCounter::read failed (%i)\n", errno);
255 break;
256
257 case 0:
258 panic("PerfKvmCounter::read unexpected EOF.\n");
259
260 default:
261 _size -= ret;
262 _buf += ret;
263 break;
264 }
265 } while (_size);
266}
267
268} // 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:198
int fcntl(int cmd, long p1)
PerfEvent fnctl interface.
Definition perfevent.cc:227
void mmapPerf(int pages)
MMAP the PerfEvent file descriptor.
Definition perfevent.cc:204
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:234
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:188
#define warn(...)
Definition logging.hh:256
Bitfield< 14, 12 > fd
Definition types.hh:150
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0