5a1c6126d5aee7c1afc3ff2146737f70e0d5a7b9
2 * Copyright (c) 2012 ARM Limited
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.
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.
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.
37 * Authors: Andreas Sandberg
41 #include <sys/ioctl.h>
43 #include <sys/syscall.h>
44 #include <sys/types.h>
53 #include "base/logging.hh"
54 #include "perfevent.hh"
56 PerfKvmCounterConfig::PerfKvmCounterConfig(uint32_t type
, uint64_t config
)
58 memset(&attr
, 0, sizeof(attr
));
60 attr
.size
= PERF_ATTR_SIZE_VER0
;
65 PerfKvmCounterConfig::~PerfKvmCounterConfig()
70 PerfKvmCounter::PerfKvmCounter(PerfKvmCounterConfig
&config
, pid_t tid
)
71 : fd(-1), ringBuffer(NULL
), pageSize(-1)
73 attach(config
, tid
, -1);
76 PerfKvmCounter::PerfKvmCounter(PerfKvmCounterConfig
&config
,
77 pid_t tid
, const PerfKvmCounter
&parent
)
78 : fd(-1), ringBuffer(NULL
), pageSize(-1)
80 attach(config
, tid
, parent
);
83 PerfKvmCounter::PerfKvmCounter()
84 : fd(-1), ringBuffer(NULL
), pageSize(-1)
88 PerfKvmCounter::~PerfKvmCounter()
95 PerfKvmCounter::detach()
99 if (munmap(ringBuffer
, ringNumPages
* pageSize
) == -1)
100 warn("PerfKvmCounter: Failed to unmap ring buffer (%i)\n",
109 PerfKvmCounter::start()
111 if (ioctl(PERF_EVENT_IOC_ENABLE
, PERF_IOC_FLAG_GROUP
) == -1)
112 panic("KVM: Failed to enable performance counters (%i)\n", errno
);
116 PerfKvmCounter::stop()
118 if (ioctl(PERF_EVENT_IOC_DISABLE
, PERF_IOC_FLAG_GROUP
) == -1)
119 panic("KVM: Failed to disable performance counters (%i)\n", errno
);
123 PerfKvmCounter::period(uint64_t period
)
125 if (ioctl(PERF_EVENT_IOC_PERIOD
, &period
) == -1)
126 panic("KVM: Failed to set period of performance counter (%i)\n", errno
);
130 PerfKvmCounter::refresh(int refresh
)
132 if (ioctl(PERF_EVENT_IOC_REFRESH
, refresh
) == -1)
133 panic("KVM: Failed to refresh performance counter (%i)\n", errno
);
137 PerfKvmCounter::read() const
141 read(&value
, sizeof(uint64_t));
146 PerfKvmCounter::enableSignals(pid_t tid
, int signal
)
148 struct f_owner_ex sigowner
;
150 sigowner
.type
= F_OWNER_TID
;
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",
161 PerfKvmCounter::attach(PerfKvmCounterConfig
&config
,
162 pid_t tid
, int group_fd
)
166 fd
= syscall(__NR_perf_event_open
,
168 -1, // CPU (-1 => Any CPU that the task happens to run on)
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");
182 panic("PerfKvmCounter::attach failed (%i)\n", errno
);
189 PerfKvmCounter::sysGettid()
191 return syscall(__NR_gettid
);
195 PerfKvmCounter::mmapPerf(int pages
)
198 assert(ringBuffer
== NULL
);
200 if (pageSize
== -1) {
201 pageSize
= sysconf(_SC_PAGE_SIZE
);
203 panic("PerfKvmCounter: Failed to determine page size (%i)\n",
207 ringNumPages
= pages
+ 1;
208 ringBuffer
= (struct perf_event_mmap_page
*)mmap(
209 NULL
, ringNumPages
* 4096,
210 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
212 if (ringBuffer
== MAP_FAILED
)
213 panic("PerfKvmCounter: MMAP failed (%i)\n",
218 PerfKvmCounter::fcntl(int cmd
, long p1
)
221 return ::fcntl(fd
, cmd
, p1
);
225 PerfKvmCounter::ioctl(int request
, long p1
)
228 return ::ioctl(fd
, request
, p1
);
232 PerfKvmCounter::read(void *buf
, size_t size
) const
234 char *_buf
= (char *)buf
;
241 ret
= ::read(fd
, _buf
, _size
);
245 panic("PerfKvmCounter::read failed (%i)\n", errno
);
249 panic("PerfKvmCounter::read unexpected EOF.\n");