2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #define ATRACE_TAG ATRACE_TAG_ALWAYS
22 #include <sys/ioctl.h>
24 #include <sys/syscall.h>
25 #include <sys/types.h>
28 #include <cutils/trace.h>
29 #include <utils/Trace.h>
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
33 #include <android-base/stringprintf.h>
35 #include "environment.h"
36 #include "event_attr.h"
37 #include "event_type.h"
38 #include "perf_event.h"
41 static int perf_event_open(const perf_event_attr& attr, pid_t pid, int cpu,
42 int group_fd, unsigned long flags) { // NOLINT
43 return syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, flags);
46 std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr,
48 EventFd* group_event_fd,
50 std::string event_name = GetEventNameByAttr(attr);
52 if (group_event_fd != nullptr) {
53 group_fd = group_event_fd->perf_event_fd_;
55 perf_event_attr real_attr = attr;
57 uint64_t max_sample_freq;
58 if (GetMaxSampleFrequency(&max_sample_freq) && max_sample_freq < attr.sample_freq) {
59 PLOG(INFO) << "Adjust sample freq to max allowed sample freq " << max_sample_freq;
60 real_attr.sample_freq = max_sample_freq;
63 int perf_event_fd = perf_event_open(real_attr, tid, cpu, group_fd, 0);
64 if (perf_event_fd == -1) {
66 PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid "
67 << tid << ", cpu " << cpu << ", group_fd " << group_fd
70 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid "
71 << tid << ", cpu " << cpu << ", group_fd " << group_fd
76 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
78 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
79 << event_name << ", tid " << tid << ", cpu " << cpu
80 << ", group_fd " << group_fd << ") failed";
82 PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
83 << event_name << ", tid " << tid << ", cpu " << cpu
84 << ", group_fd " << group_fd << ") failed";
88 return std::unique_ptr<EventFd>(
89 new EventFd(real_attr, perf_event_fd, event_name, tid, cpu));
93 DestroyMappedBuffer();
94 close(perf_event_fd_);
97 std::string EventFd::Name() const {
98 return android::base::StringPrintf(
99 "perf_event_file(event %s, tid %d, cpu %d)", event_name_.c_str(), tid_,
103 uint64_t EventFd::Id() const {
106 if (ReadCounter(&counter)) {
113 bool EventFd::EnableEvent() {
114 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
116 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
122 bool EventFd::ReadCounter(PerfCounter* counter) const {
123 CHECK(counter != nullptr);
124 uint64_t pre_counter = counter->value;
125 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
126 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
129 // Trace is always available to systrace if enabled
131 ATRACE_INT64(android::base::StringPrintf(
132 "%s_tid%d_cpu%d", event_name_.c_str(), tid_,
133 cpu_).c_str(), counter->value - pre_counter);
135 ATRACE_INT64(android::base::StringPrintf(
136 "%s_cpu%d", event_name_.c_str(),
137 cpu_).c_str(), counter->value - pre_counter);
142 bool EventFd::CreateMappedBuffer(size_t mmap_pages, bool report_error) {
143 CHECK(IsPowerOfTwo(mmap_pages));
144 size_t page_size = sysconf(_SC_PAGE_SIZE);
145 size_t mmap_len = (mmap_pages + 1) * page_size;
146 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED,
148 if (mmap_addr == MAP_FAILED) {
149 bool is_perm_error = (errno == EPERM);
151 PLOG(ERROR) << "mmap(" << mmap_pages << ") failed for " << Name();
153 PLOG(DEBUG) << "mmap(" << mmap_pages << ") failed for " << Name();
155 if (report_error && is_perm_error) {
157 << "It seems the kernel doesn't allow allocating enough "
158 << "buffer for dumping samples, consider decreasing mmap pages(-m).";
162 mmap_addr_ = mmap_addr;
163 mmap_len_ = mmap_len;
164 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
165 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
166 mmap_data_buffer_size_ = mmap_len_ - page_size;
170 bool EventFd::ShareMappedBuffer(const EventFd& event_fd, bool report_error) {
171 CHECK(!HasMappedBuffer());
172 CHECK(event_fd.HasMappedBuffer());
174 ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_OUTPUT, event_fd.perf_event_fd_);
177 PLOG(ERROR) << "failed to share mapped buffer of "
178 << event_fd.perf_event_fd_ << " with " << perf_event_fd_;
185 void EventFd::DestroyMappedBuffer() {
186 if (HasMappedBuffer()) {
187 munmap(mmap_addr_, mmap_len_);
188 mmap_addr_ = nullptr;
190 mmap_metadata_page_ = nullptr;
191 mmap_data_buffer_ = nullptr;
192 mmap_data_buffer_size_ = 0;
196 size_t EventFd::GetAvailableMmapData(std::vector<char>& buffer, size_t& buffer_pos) {
197 if (!HasMappedBuffer()) {
200 // The mmap_data_buffer is used as a ring buffer between the kernel and
201 // simpleperf. The kernel continuously writes records to the buffer, and
202 // simpleperf continuously read records out.
203 // _________________________________________
204 // buffer | can write | can read | can write |
206 // read_head write_head
208 // So simpleperf can read records in [read_head, write_head), and the kernel
209 // can write records in [write_head, read_head). The kernel is responsible
210 // for updating write_head, and simpleperf is responsible for updating
213 size_t buf_mask = mmap_data_buffer_size_ - 1;
215 static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask);
217 static_cast<size_t>(mmap_metadata_page_->data_tail & buf_mask);
219 if (read_head == write_head) {
220 // No available data.
224 if (read_head < write_head) {
225 read_bytes = write_head - read_head;
227 read_bytes = mmap_data_buffer_size_ - read_head + write_head;
229 // Extend the buffer if it is not big enough.
230 if (buffer.size() < buffer_pos + read_bytes) {
231 buffer.resize(buffer_pos + read_bytes);
234 // rmb() used to ensure reading data after reading data_head.
235 __sync_synchronize();
237 // Copy records from mapped buffer. Note that records can be wrapped at the
238 // end of the mapped buffer.
239 char* to = &buffer[buffer_pos];
240 if (read_head < write_head) {
241 char* from = mmap_data_buffer_ + read_head;
242 size_t n = write_head - read_head;
245 char* from = mmap_data_buffer_ + read_head;
246 size_t n = mmap_data_buffer_size_ - read_head;
249 from = mmap_data_buffer_;
253 buffer_pos += read_bytes;
254 DiscardMmapData(read_bytes);
258 void EventFd::DiscardMmapData(size_t discard_size) {
259 // mb() used to ensure finish reading data before writing data_tail.
260 __sync_synchronize();
261 mmap_metadata_page_->data_tail += discard_size;
264 bool EventFd::StartPolling(IOEventLoop& loop,
265 const std::function<bool()>& callback) {
266 ioevent_ref_ = loop.AddReadEvent(perf_event_fd_, callback);
267 return ioevent_ref_ != nullptr;
270 bool EventFd::StopPolling() { return IOEventLoop::DelEvent(ioevent_ref_); }
272 bool IsEventAttrSupported(const perf_event_attr& attr) {
273 if (attr.type == SIMPLEPERF_TYPE_USER_SPACE_SAMPLERS &&
274 attr.config == SIMPLEPERF_CONFIG_INPLACE_SAMPLER) {
275 // User space samplers don't need kernel support.
278 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, getpid(), -1, nullptr, false);
279 return event_fd != nullptr;