blob: 39df0852e58f5d6d08a481d384754ebd1f136d3f [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 Cuif469c3d2015-10-07 15:00:46 -070040std::vector<char> EventFd::data_process_buffer_;
41
Yabin Cuidbbda302016-07-28 12:55:41 -070042static int perf_event_open(const perf_event_attr& attr, pid_t pid, int cpu,
43 int group_fd, unsigned long flags) { // NOLINT
44 return syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, flags);
Yabin Cui67d3abd2015-04-16 15:26:31 -070045}
46
Yabin Cuidbbda302016-07-28 12:55:41 -070047std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr,
48 pid_t tid, int cpu,
49 EventFd* group_event_fd,
50 bool report_error) {
Yabin Cuic10a9dc2016-06-15 12:10:33 -070051 std::string event_name = GetEventNameByAttr(attr);
Yabin Cui877751b2016-06-13 18:03:47 -070052 int group_fd = -1;
53 if (group_event_fd != nullptr) {
54 group_fd = group_event_fd->perf_event_fd_;
55 }
Yabin Cuidbbda302016-07-28 12:55:41 -070056 int perf_event_fd = perf_event_open(attr, tid, cpu, group_fd, 0);
Yabin Cui67d3abd2015-04-16 15:26:31 -070057 if (perf_event_fd == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070058 if (report_error) {
Yabin Cuidbbda302016-07-28 12:55:41 -070059 PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid "
60 << tid << ", cpu " << cpu << ", group_fd " << group_fd
61 << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070062 } else {
Yabin Cuidbbda302016-07-28 12:55:41 -070063 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid "
64 << tid << ", cpu " << cpu << ", group_fd " << group_fd
65 << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070066 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070067 return nullptr;
68 }
69 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070070 if (report_error) {
Yabin Cuidbbda302016-07-28 12:55:41 -070071 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
72 << event_name << ", tid " << tid << ", cpu " << cpu
73 << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070074 } else {
Yabin Cuidbbda302016-07-28 12:55:41 -070075 PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
76 << event_name << ", tid " << tid << ", cpu " << cpu
77 << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070078 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070079 return nullptr;
80 }
Yabin Cuidbbda302016-07-28 12:55:41 -070081 return std::unique_ptr<EventFd>(
82 new EventFd(attr, perf_event_fd, event_name, tid, cpu));
Yabin Cui67d3abd2015-04-16 15:26:31 -070083}
84
85EventFd::~EventFd() {
Yabin Cui0a072cd2016-07-13 17:06:50 -070086 DestroyMappedBuffer();
Yabin Cui67d3abd2015-04-16 15:26:31 -070087 close(perf_event_fd_);
88}
89
90std::string EventFd::Name() const {
Yabin Cuidbbda302016-07-28 12:55:41 -070091 return android::base::StringPrintf(
92 "perf_event_file(event %s, tid %d, cpu %d)", event_name_.c_str(), tid_,
93 cpu_);
Yabin Cui67d3abd2015-04-16 15:26:31 -070094}
95
Yabin Cui9759e1b2015-04-28 15:54:13 -070096uint64_t EventFd::Id() const {
97 if (id_ == 0) {
98 PerfCounter counter;
99 if (ReadCounter(&counter)) {
100 id_ = counter.id;
101 }
102 }
103 return id_;
104}
105
Yabin Cui56335862016-04-18 13:43:20 -0700106bool EventFd::EnableEvent() {
107 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
108 if (result < 0) {
109 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
110 return false;
111 }
112 return true;
113}
114
Yabin Cui9759e1b2015-04-28 15:54:13 -0700115bool EventFd::ReadCounter(PerfCounter* counter) const {
Yabin Cui323e9452015-04-20 18:07:17 -0700116 CHECK(counter != nullptr);
Wei Wangcf69e412016-09-29 16:34:15 -0700117 uint64_t pre_counter = counter->value;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700118 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
Yabin Cui323e9452015-04-20 18:07:17 -0700119 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
120 return false;
121 }
Wei Wangcf69e412016-09-29 16:34:15 -0700122 // Trace is always available to systrace if enabled
123 if (tid_ > 0) {
124 ATRACE_INT64(android::base::StringPrintf(
125 "%s_tid%d_cpu%d", event_name_.c_str(), tid_,
126 cpu_).c_str(), counter->value - pre_counter);
127 } else {
128 ATRACE_INT64(android::base::StringPrintf(
129 "%s_cpu%d", event_name_.c_str(),
130 cpu_).c_str(), counter->value - pre_counter);
131 }
Yabin Cui323e9452015-04-20 18:07:17 -0700132 return true;
133}
Yabin Cui9759e1b2015-04-28 15:54:13 -0700134
Yabin Cuidbbda302016-07-28 12:55:41 -0700135bool EventFd::CreateMappedBuffer(size_t mmap_pages, bool report_error) {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700136 CHECK(IsPowerOfTwo(mmap_pages));
137 size_t page_size = sysconf(_SC_PAGE_SIZE);
138 size_t mmap_len = (mmap_pages + 1) * page_size;
Yabin Cuidbbda302016-07-28 12:55:41 -0700139 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED,
140 perf_event_fd_, 0);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700141 if (mmap_addr == MAP_FAILED) {
Yabin Cui27816c92016-07-07 14:42:54 -0700142 bool is_perm_error = (errno == EPERM);
Yabin Cui0a072cd2016-07-13 17:06:50 -0700143 if (report_error) {
144 PLOG(ERROR) << "mmap(" << mmap_pages << ") failed for " << Name();
145 } else {
146 PLOG(DEBUG) << "mmap(" << mmap_pages << ") failed for " << Name();
147 }
148 if (report_error && is_perm_error) {
Yabin Cuidbbda302016-07-28 12:55:41 -0700149 LOG(ERROR)
150 << "It seems the kernel doesn't allow allocating enough "
Yabin Cui825e56b2016-08-26 18:25:21 -0700151 << "buffer for dumping samples, consider decreasing mmap pages(-m).";
Yabin Cui27816c92016-07-07 14:42:54 -0700152 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700153 return false;
154 }
155 mmap_addr_ = mmap_addr;
156 mmap_len_ = mmap_len;
157 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
158 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
159 mmap_data_buffer_size_ = mmap_len_ - page_size;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700160 if (data_process_buffer_.size() < mmap_data_buffer_size_) {
161 data_process_buffer_.resize(mmap_data_buffer_size_);
162 }
Yabin Cui61735922016-07-08 13:56:48 -0700163 return true;
164}
165
Yabin Cui0a072cd2016-07-13 17:06:50 -0700166bool EventFd::ShareMappedBuffer(const EventFd& event_fd, bool report_error) {
Yabin Cui61735922016-07-08 13:56:48 -0700167 CHECK(!HasMappedBuffer());
168 CHECK(event_fd.HasMappedBuffer());
Yabin Cuidbbda302016-07-28 12:55:41 -0700169 int result =
170 ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_OUTPUT, event_fd.perf_event_fd_);
Yabin Cui61735922016-07-08 13:56:48 -0700171 if (result != 0) {
Yabin Cui0a072cd2016-07-13 17:06:50 -0700172 if (report_error) {
173 PLOG(ERROR) << "failed to share mapped buffer of "
Yabin Cuidbbda302016-07-28 12:55:41 -0700174 << event_fd.perf_event_fd_ << " with " << perf_event_fd_;
Yabin Cui0a072cd2016-07-13 17:06:50 -0700175 }
Yabin Cui61735922016-07-08 13:56:48 -0700176 return false;
177 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700178 return true;
179}
180
Yabin Cui0a072cd2016-07-13 17:06:50 -0700181void EventFd::DestroyMappedBuffer() {
182 if (HasMappedBuffer()) {
183 munmap(mmap_addr_, mmap_len_);
184 mmap_addr_ = nullptr;
185 mmap_len_ = 0;
186 mmap_metadata_page_ = nullptr;
187 mmap_data_buffer_ = nullptr;
188 mmap_data_buffer_size_ = 0;
189 }
190}
191
Yabin Cuidbbda302016-07-28 12:55:41 -0700192size_t EventFd::GetAvailableMmapData(const char** pdata) {
Yabin Cui61735922016-07-08 13:56:48 -0700193 if (!HasMappedBuffer()) {
194 return 0;
195 }
Yabin Cuidbbda302016-07-28 12:55:41 -0700196 // The mmap_data_buffer is used as a ring buffer between the kernel and
197 // simpleperf. The kernel continuously writes records to the buffer, and
198 // simpleperf continuously read records out.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700199 // _________________________________________
200 // buffer | can write | can read | can write |
201 // ^ ^
202 // read_head write_head
203 //
Yabin Cuidbbda302016-07-28 12:55:41 -0700204 // So simpleperf can read records in [read_head, write_head), and the kernel
205 // can write records in [write_head, read_head). The kernel is responsible
206 // for updating write_head, and simpleperf is responsible for updating
207 // read_head.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700208
Yabin Cuif469c3d2015-10-07 15:00:46 -0700209 size_t buf_mask = mmap_data_buffer_size_ - 1;
Yabin Cuidbbda302016-07-28 12:55:41 -0700210 size_t write_head =
211 static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask);
212 size_t read_head =
213 static_cast<size_t>(mmap_metadata_page_->data_tail & buf_mask);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700214
215 if (read_head == write_head) {
216 // No available data.
217 return 0;
218 }
219
220 // Make sure we can see the data after the fence.
221 std::atomic_thread_fence(std::memory_order_acquire);
222
Yabin Cuidbbda302016-07-28 12:55:41 -0700223 // Copy records from mapped buffer to data_process_buffer. Note that records
224 // can be wrapped at the end of the mapped buffer.
Yabin Cuif469c3d2015-10-07 15:00:46 -0700225 char* to = data_process_buffer_.data();
Yabin Cui9759e1b2015-04-28 15:54:13 -0700226 if (read_head < write_head) {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700227 char* from = mmap_data_buffer_ + read_head;
228 size_t n = write_head - read_head;
229 memcpy(to, from, n);
230 to += n;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700231 } else {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700232 char* from = mmap_data_buffer_ + read_head;
233 size_t n = mmap_data_buffer_size_ - read_head;
234 memcpy(to, from, n);
235 to += n;
236 from = mmap_data_buffer_;
237 n = write_head;
238 memcpy(to, from, n);
239 to += n;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700240 }
Yabin Cuif469c3d2015-10-07 15:00:46 -0700241 size_t read_bytes = to - data_process_buffer_.data();
242 *pdata = data_process_buffer_.data();
243 DiscardMmapData(read_bytes);
244 return read_bytes;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700245}
246
247void EventFd::DiscardMmapData(size_t discard_size) {
248 mmap_metadata_page_->data_tail += discard_size;
249}
250
Yabin Cui825e56b2016-08-26 18:25:21 -0700251bool EventFd::StartPolling(IOEventLoop& loop,
252 const std::function<bool()>& callback) {
253 ioevent_ref_ = loop.AddReadEvent(perf_event_fd_, callback);
254 return ioevent_ref_ != nullptr;
255}
256
257bool EventFd::StopPolling() { return IOEventLoop::DelEvent(ioevent_ref_); }
258
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700259bool IsEventAttrSupportedByKernel(perf_event_attr attr) {
Yabin Cui877751b2016-06-13 18:03:47 -0700260 auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, nullptr, false);
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700261 return event_fd != nullptr;
262}