blob: 2fd4d52294d10e31fce4314fa40ee1c572a6eb09 [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 */
16
17#include "event_fd.h"
18
19#include <fcntl.h>
Yabin Cui19e6b6d2016-03-10 11:49:57 -080020#include <poll.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070021#include <stdio.h>
Yabin Cuif469c3d2015-10-07 15:00:46 -070022#include <string.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070023#include <sys/ioctl.h>
Yabin Cui9759e1b2015-04-28 15:54:13 -070024#include <sys/mman.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070025#include <sys/syscall.h>
26#include <sys/types.h>
Yabin Cuif569b472015-04-30 09:43:26 -070027#include <atomic>
Yabin Cui67d3abd2015-04-16 15:26:31 -070028#include <memory>
29
Elliott Hughes66dd09e2015-12-04 14:00:57 -080030#include <android-base/file.h>
31#include <android-base/logging.h>
32#include <android-base/stringprintf.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070033
Yabin Cuic10a9dc2016-06-15 12:10:33 -070034#include "event_attr.h"
Yabin Cui67d3abd2015-04-16 15:26:31 -070035#include "event_type.h"
Yabin Cui67d3abd2015-04-16 15:26:31 -070036#include "perf_event.h"
37#include "utils.h"
38
Yabin Cuif469c3d2015-10-07 15:00:46 -070039std::vector<char> EventFd::data_process_buffer_;
40
Yabin Cui67d3abd2015-04-16 15:26:31 -070041static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd,
Chih-Hung Hsieh7d6c8ab2016-04-15 16:12:03 -070042 unsigned long flags) { // NOLINT
Yabin Cui67d3abd2015-04-16 15:26:31 -070043 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
44}
45
Yabin Cuib032de72015-06-17 21:15:09 -070046std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu,
Yabin Cui877751b2016-06-13 18:03:47 -070047 EventFd* group_event_fd, bool report_error) {
Yabin Cui9759e1b2015-04-28 15:54:13 -070048 perf_event_attr perf_attr = attr;
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 }
54 int perf_event_fd = perf_event_open(&perf_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) {
57 PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
Yabin Cui877751b2016-06-13 18:03:47 -070058 << cpu << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070059 } else {
60 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
Yabin Cui877751b2016-06-13 18:03:47 -070061 << cpu << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070062 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070063 return nullptr;
64 }
65 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070066 if (report_error) {
67 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
Yabin Cui877751b2016-06-13 18:03:47 -070068 << tid << ", cpu " << cpu << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070069 } else {
70 PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
Yabin Cui877751b2016-06-13 18:03:47 -070071 << tid << ", cpu " << cpu << ", group_fd " << group_fd << ") failed";
Yabin Cui42aa1272015-09-18 11:10:55 -070072 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070073 return nullptr;
74 }
Yabin Cuib032de72015-06-17 21:15:09 -070075 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, tid, cpu));
Yabin Cui67d3abd2015-04-16 15:26:31 -070076}
77
78EventFd::~EventFd() {
Yabin Cui9759e1b2015-04-28 15:54:13 -070079 if (mmap_addr_ != nullptr) {
80 munmap(mmap_addr_, mmap_len_);
81 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070082 close(perf_event_fd_);
83}
84
85std::string EventFd::Name() const {
Yabin Cuib032de72015-06-17 21:15:09 -070086 return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)",
87 event_name_.c_str(), tid_, cpu_);
Yabin Cui67d3abd2015-04-16 15:26:31 -070088}
89
Yabin Cui9759e1b2015-04-28 15:54:13 -070090uint64_t EventFd::Id() const {
91 if (id_ == 0) {
92 PerfCounter counter;
93 if (ReadCounter(&counter)) {
94 id_ = counter.id;
95 }
96 }
97 return id_;
98}
99
Yabin Cui56335862016-04-18 13:43:20 -0700100bool EventFd::EnableEvent() {
101 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
102 if (result < 0) {
103 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
104 return false;
105 }
106 return true;
107}
108
Yabin Cui9759e1b2015-04-28 15:54:13 -0700109bool EventFd::ReadCounter(PerfCounter* counter) const {
Yabin Cui323e9452015-04-20 18:07:17 -0700110 CHECK(counter != nullptr);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700111 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
Yabin Cui323e9452015-04-20 18:07:17 -0700112 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
113 return false;
114 }
115 return true;
116}
Yabin Cui9759e1b2015-04-28 15:54:13 -0700117
Yabin Cui61735922016-07-08 13:56:48 -0700118bool EventFd::CreateMappedBuffer(size_t mmap_pages, pollfd* poll_fd) {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700119 CHECK(IsPowerOfTwo(mmap_pages));
120 size_t page_size = sysconf(_SC_PAGE_SIZE);
121 size_t mmap_len = (mmap_pages + 1) * page_size;
122 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0);
123 if (mmap_addr == MAP_FAILED) {
Yabin Cui27816c92016-07-07 14:42:54 -0700124 bool is_perm_error = (errno == EPERM);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700125 PLOG(ERROR) << "mmap() failed for " << Name();
Yabin Cui27816c92016-07-07 14:42:54 -0700126 if (is_perm_error) {
127 LOG(ERROR) << "It seems the kernel doesn't allow allocating enough "
128 << "buffer for dumping samples, consider decreasing the number of "
Yabin Cui61735922016-07-08 13:56:48 -0700129 << "monitored threads(-t), or decreasing mmap pages(-m), or "
130 << "decreasing the number of events(-e).";
Yabin Cui27816c92016-07-07 14:42:54 -0700131 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700132 return false;
133 }
134 mmap_addr_ = mmap_addr;
135 mmap_len_ = mmap_len;
136 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
137 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
138 mmap_data_buffer_size_ = mmap_len_ - page_size;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700139 if (data_process_buffer_.size() < mmap_data_buffer_size_) {
140 data_process_buffer_.resize(mmap_data_buffer_size_);
141 }
Yabin Cui61735922016-07-08 13:56:48 -0700142 memset(poll_fd, 0, sizeof(pollfd));
143 poll_fd->fd = perf_event_fd_;
144 poll_fd->events = POLLIN;
145 return true;
146}
147
148bool EventFd::ShareMappedBuffer(const EventFd& event_fd) {
149 CHECK(!HasMappedBuffer());
150 CHECK(event_fd.HasMappedBuffer());
151 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_OUTPUT, event_fd.perf_event_fd_);
152 if (result != 0) {
153 PLOG(ERROR) << "failed to share mapped buffer of "
154 << event_fd.perf_event_fd_ << " with " << perf_event_fd_;
155 return false;
156 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700157 return true;
158}
159
160size_t EventFd::GetAvailableMmapData(char** pdata) {
Yabin Cui61735922016-07-08 13:56:48 -0700161 if (!HasMappedBuffer()) {
162 return 0;
163 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700164 // The mmap_data_buffer is used as a ring buffer like below. The kernel continuously writes
165 // records to the buffer, and the user continuously read records out.
166 // _________________________________________
167 // buffer | can write | can read | can write |
168 // ^ ^
169 // read_head write_head
170 //
171 // So the user can read records in [read_head, write_head), and the kernel can write records
172 // in [write_head, read_head). The kernel is responsible for updating write_head, and the user
173 // is responsible for updating read_head.
174
Yabin Cuif469c3d2015-10-07 15:00:46 -0700175 size_t buf_mask = mmap_data_buffer_size_ - 1;
176 size_t write_head = static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask);
177 size_t read_head = static_cast<size_t>(mmap_metadata_page_->data_tail & buf_mask);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700178
179 if (read_head == write_head) {
180 // No available data.
181 return 0;
182 }
183
184 // Make sure we can see the data after the fence.
185 std::atomic_thread_fence(std::memory_order_acquire);
186
Yabin Cuif469c3d2015-10-07 15:00:46 -0700187 // Copy records from mapped buffer to data_process_buffer. Note that records can be wrapped
188 // at the end of the mapped buffer.
189 char* to = data_process_buffer_.data();
Yabin Cui9759e1b2015-04-28 15:54:13 -0700190 if (read_head < write_head) {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700191 char* from = mmap_data_buffer_ + read_head;
192 size_t n = write_head - read_head;
193 memcpy(to, from, n);
194 to += n;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700195 } else {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700196 char* from = mmap_data_buffer_ + read_head;
197 size_t n = mmap_data_buffer_size_ - read_head;
198 memcpy(to, from, n);
199 to += n;
200 from = mmap_data_buffer_;
201 n = write_head;
202 memcpy(to, from, n);
203 to += n;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700204 }
Yabin Cuif469c3d2015-10-07 15:00:46 -0700205 size_t read_bytes = to - data_process_buffer_.data();
206 *pdata = data_process_buffer_.data();
207 DiscardMmapData(read_bytes);
208 return read_bytes;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700209}
210
211void EventFd::DiscardMmapData(size_t discard_size) {
212 mmap_metadata_page_->data_tail += discard_size;
213}
214
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700215bool IsEventAttrSupportedByKernel(perf_event_attr attr) {
Yabin Cui877751b2016-06-13 18:03:47 -0700216 auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, nullptr, false);
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700217 return event_fd != nullptr;
218}