blob: b78da4c0bbee976f0ebe23d983945c7e9205299e [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
Wei Wangcf69e412016-09-29 16:34:15 -070016#define ATRACE_TAG ATRACE_TAG_ALWAYS
Yabin Cui67d3abd2015-04-16 15:26:31 -070017#include "event_fd.h"
18
19#include <fcntl.h>
20#include <stdio.h>
Yabin Cuif469c3d2015-10-07 15:00:46 -070021#include <string.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070022#include <sys/ioctl.h>
Yabin Cui9759e1b2015-04-28 15:54:13 -070023#include <sys/mman.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070024#include <sys/syscall.h>
25#include <sys/types.h>
Yabin Cuif569b472015-04-30 09:43:26 -070026#include <atomic>
Yabin Cui67d3abd2015-04-16 15:26:31 -070027#include <memory>
Wei Wangcf69e412016-09-29 16:34:15 -070028#include <cutils/trace.h>
29#include <utils/Trace.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070030
Elliott Hughes66dd09e2015-12-04 14:00:57 -080031#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/stringprintf.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070034
Yabin Cuic10a9dc2016-06-15 12:10:33 -070035#include "event_attr.h"
Yabin Cui67d3abd2015-04-16 15:26:31 -070036#include "event_type.h"
Yabin Cui67d3abd2015-04-16 15:26:31 -070037#include "perf_event.h"
38#include "utils.h"
39
Yabin Cuidbbda302016-07-28 12:55:41 -070040static int perf_event_open(const perf_event_attr& attr, pid_t pid, int cpu,
41 int group_fd, unsigned long flags) { // NOLINT
42 return syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, flags);
Yabin Cui67d3abd2015-04-16 15:26:31 -070043}
44
Yabin Cuidbbda302016-07-28 12:55:41 -070045std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr,
46 pid_t tid, int cpu,
47 EventFd* group_event_fd,
48 bool report_error) {
Yabin Cuic10a9dc2016-06-15 12:10:33 -070049 std::string event_name = GetEventNameByAttr(attr);
Yabin Cui877751b2016-06-13 18:03:47 -070050 int group_fd = -1;
51 if (group_event_fd != nullptr) {
52 group_fd = group_event_fd->perf_event_fd_;
53 }
Yabin Cuidbbda302016-07-28 12:55:41 -070054 int perf_event_fd = perf_event_open(attr, tid, cpu, group_fd, 0);
Yabin Cui67d3abd2015-04-16 15:26:31 -070055 if (perf_event_fd == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070056 if (report_error) {
Yabin Cuidbbda302016-07-28 12:55:41 -070057 PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid "
58 << tid << ", cpu " << cpu << ", group_fd " << group_fd
59 << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070060 } else {
Yabin Cuidbbda302016-07-28 12:55:41 -070061 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid "
62 << tid << ", cpu " << cpu << ", group_fd " << group_fd
63 << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070064 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070065 return nullptr;
66 }
67 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070068 if (report_error) {
Yabin Cuidbbda302016-07-28 12:55:41 -070069 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
70 << event_name << ", tid " << tid << ", cpu " << cpu
71 << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070072 } else {
Yabin Cuidbbda302016-07-28 12:55:41 -070073 PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
74 << event_name << ", tid " << tid << ", cpu " << cpu
75 << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070076 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070077 return nullptr;
78 }
Yabin Cuidbbda302016-07-28 12:55:41 -070079 return std::unique_ptr<EventFd>(
80 new EventFd(attr, perf_event_fd, event_name, tid, cpu));
Yabin Cui67d3abd2015-04-16 15:26:31 -070081}
82
83EventFd::~EventFd() {
Yabin Cui0a072cd2016-07-13 17:06:50 -070084 DestroyMappedBuffer();
Yabin Cui67d3abd2015-04-16 15:26:31 -070085 close(perf_event_fd_);
86}
87
88std::string EventFd::Name() const {
Yabin Cuidbbda302016-07-28 12:55:41 -070089 return android::base::StringPrintf(
90 "perf_event_file(event %s, tid %d, cpu %d)", event_name_.c_str(), tid_,
91 cpu_);
Yabin Cui67d3abd2015-04-16 15:26:31 -070092}
93
Yabin Cui9759e1b2015-04-28 15:54:13 -070094uint64_t EventFd::Id() const {
95 if (id_ == 0) {
96 PerfCounter counter;
97 if (ReadCounter(&counter)) {
98 id_ = counter.id;
99 }
100 }
101 return id_;
102}
103
Yabin Cui56335862016-04-18 13:43:20 -0700104bool EventFd::EnableEvent() {
105 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
106 if (result < 0) {
107 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
108 return false;
109 }
110 return true;
111}
112
Yabin Cui9759e1b2015-04-28 15:54:13 -0700113bool EventFd::ReadCounter(PerfCounter* counter) const {
Yabin Cui323e9452015-04-20 18:07:17 -0700114 CHECK(counter != nullptr);
Wei Wangcf69e412016-09-29 16:34:15 -0700115 uint64_t pre_counter = counter->value;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700116 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
Yabin Cui323e9452015-04-20 18:07:17 -0700117 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
118 return false;
119 }
Wei Wangcf69e412016-09-29 16:34:15 -0700120 // Trace is always available to systrace if enabled
121 if (tid_ > 0) {
122 ATRACE_INT64(android::base::StringPrintf(
123 "%s_tid%d_cpu%d", event_name_.c_str(), tid_,
124 cpu_).c_str(), counter->value - pre_counter);
125 } else {
126 ATRACE_INT64(android::base::StringPrintf(
127 "%s_cpu%d", event_name_.c_str(),
128 cpu_).c_str(), counter->value - pre_counter);
129 }
Yabin Cui323e9452015-04-20 18:07:17 -0700130 return true;
131}
Yabin Cui9759e1b2015-04-28 15:54:13 -0700132
Yabin Cuidbbda302016-07-28 12:55:41 -0700133bool EventFd::CreateMappedBuffer(size_t mmap_pages, bool report_error) {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700134 CHECK(IsPowerOfTwo(mmap_pages));
135 size_t page_size = sysconf(_SC_PAGE_SIZE);
136 size_t mmap_len = (mmap_pages + 1) * page_size;
Yabin Cuidbbda302016-07-28 12:55:41 -0700137 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED,
138 perf_event_fd_, 0);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700139 if (mmap_addr == MAP_FAILED) {
Yabin Cui27816c92016-07-07 14:42:54 -0700140 bool is_perm_error = (errno == EPERM);
Yabin Cui0a072cd2016-07-13 17:06:50 -0700141 if (report_error) {
142 PLOG(ERROR) << "mmap(" << mmap_pages << ") failed for " << Name();
143 } else {
144 PLOG(DEBUG) << "mmap(" << mmap_pages << ") failed for " << Name();
145 }
146 if (report_error && is_perm_error) {
Yabin Cuidbbda302016-07-28 12:55:41 -0700147 LOG(ERROR)
148 << "It seems the kernel doesn't allow allocating enough "
Yabin Cui825e56b2016-08-26 18:25:21 -0700149 << "buffer for dumping samples, consider decreasing mmap pages(-m).";
Yabin Cui27816c92016-07-07 14:42:54 -0700150 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700151 return false;
152 }
153 mmap_addr_ = mmap_addr;
154 mmap_len_ = mmap_len;
155 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
156 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
157 mmap_data_buffer_size_ = mmap_len_ - page_size;
Yabin Cui61735922016-07-08 13:56:48 -0700158 return true;
159}
160
Yabin Cui0a072cd2016-07-13 17:06:50 -0700161bool EventFd::ShareMappedBuffer(const EventFd& event_fd, bool report_error) {
Yabin Cui61735922016-07-08 13:56:48 -0700162 CHECK(!HasMappedBuffer());
163 CHECK(event_fd.HasMappedBuffer());
Yabin Cuidbbda302016-07-28 12:55:41 -0700164 int result =
165 ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_OUTPUT, event_fd.perf_event_fd_);
Yabin Cui61735922016-07-08 13:56:48 -0700166 if (result != 0) {
Yabin Cui0a072cd2016-07-13 17:06:50 -0700167 if (report_error) {
168 PLOG(ERROR) << "failed to share mapped buffer of "
Yabin Cuidbbda302016-07-28 12:55:41 -0700169 << event_fd.perf_event_fd_ << " with " << perf_event_fd_;
Yabin Cui0a072cd2016-07-13 17:06:50 -0700170 }
Yabin Cui61735922016-07-08 13:56:48 -0700171 return false;
172 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700173 return true;
174}
175
Yabin Cui0a072cd2016-07-13 17:06:50 -0700176void EventFd::DestroyMappedBuffer() {
177 if (HasMappedBuffer()) {
178 munmap(mmap_addr_, mmap_len_);
179 mmap_addr_ = nullptr;
180 mmap_len_ = 0;
181 mmap_metadata_page_ = nullptr;
182 mmap_data_buffer_ = nullptr;
183 mmap_data_buffer_size_ = 0;
184 }
185}
186
Yabin Cui2ea6de12016-10-24 19:13:09 -0700187size_t EventFd::GetAvailableMmapData(std::vector<char>& buffer, size_t& buffer_pos) {
Yabin Cui61735922016-07-08 13:56:48 -0700188 if (!HasMappedBuffer()) {
189 return 0;
190 }
Yabin Cuidbbda302016-07-28 12:55:41 -0700191 // The mmap_data_buffer is used as a ring buffer between the kernel and
192 // simpleperf. The kernel continuously writes records to the buffer, and
193 // simpleperf continuously read records out.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700194 // _________________________________________
195 // buffer | can write | can read | can write |
196 // ^ ^
197 // read_head write_head
198 //
Yabin Cuidbbda302016-07-28 12:55:41 -0700199 // So simpleperf can read records in [read_head, write_head), and the kernel
200 // can write records in [write_head, read_head). The kernel is responsible
201 // for updating write_head, and simpleperf is responsible for updating
202 // read_head.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700203
Yabin Cuif469c3d2015-10-07 15:00:46 -0700204 size_t buf_mask = mmap_data_buffer_size_ - 1;
Yabin Cuidbbda302016-07-28 12:55:41 -0700205 size_t write_head =
206 static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask);
207 size_t read_head =
208 static_cast<size_t>(mmap_metadata_page_->data_tail & buf_mask);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700209
210 if (read_head == write_head) {
211 // No available data.
212 return 0;
213 }
Yabin Cui2ea6de12016-10-24 19:13:09 -0700214 size_t read_bytes;
215 if (read_head < write_head) {
216 read_bytes = write_head - read_head;
217 } else {
218 read_bytes = mmap_data_buffer_size_ - read_head + write_head;
219 }
220 // Extend the buffer if it is not big enough.
221 if (buffer.size() < buffer_pos + read_bytes) {
222 buffer.resize(buffer_pos + read_bytes);
223 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700224
Yabin Cui92f80f42017-01-06 15:01:42 -0800225 // rmb() used to ensure reading data after reading data_head.
226 __sync_synchronize();
Yabin Cui9759e1b2015-04-28 15:54:13 -0700227
Yabin Cui2ea6de12016-10-24 19:13:09 -0700228 // Copy records from mapped buffer. Note that records can be wrapped at the
229 // end of the mapped buffer.
230 char* to = &buffer[buffer_pos];
Yabin Cui9759e1b2015-04-28 15:54:13 -0700231 if (read_head < write_head) {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700232 char* from = mmap_data_buffer_ + read_head;
233 size_t n = write_head - read_head;
234 memcpy(to, from, n);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700235 } else {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700236 char* from = mmap_data_buffer_ + read_head;
237 size_t n = mmap_data_buffer_size_ - read_head;
238 memcpy(to, from, n);
239 to += n;
240 from = mmap_data_buffer_;
241 n = write_head;
242 memcpy(to, from, n);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700243 }
Yabin Cui2ea6de12016-10-24 19:13:09 -0700244 buffer_pos += read_bytes;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700245 DiscardMmapData(read_bytes);
246 return read_bytes;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700247}
248
249void EventFd::DiscardMmapData(size_t discard_size) {
Yabin Cui92f80f42017-01-06 15:01:42 -0800250 // mb() used to ensure finish reading data before writing data_tail.
251 __sync_synchronize();
Yabin Cui9759e1b2015-04-28 15:54:13 -0700252 mmap_metadata_page_->data_tail += discard_size;
253}
254
Yabin Cui825e56b2016-08-26 18:25:21 -0700255bool EventFd::StartPolling(IOEventLoop& loop,
256 const std::function<bool()>& callback) {
257 ioevent_ref_ = loop.AddReadEvent(perf_event_fd_, callback);
258 return ioevent_ref_ != nullptr;
259}
260
261bool EventFd::StopPolling() { return IOEventLoop::DelEvent(ioevent_ref_); }
262
Yabin Cui26968e62017-01-30 11:34:24 -0800263bool IsEventAttrSupported(const perf_event_attr& attr) {
264 if (attr.type == SIMPLEPERF_TYPE_USER_SPACE_SAMPLERS &&
265 attr.config == SIMPLEPERF_CONFIG_INPLACE_SAMPLER) {
266 // User space samplers don't need kernel support.
267 return true;
268 }
269 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, getpid(), -1, nullptr, false);
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700270 return event_fd != nullptr;
271}