Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Wang | cf69e41 | 2016-09-29 16:34:15 -0700 | [diff] [blame] | 16 | #define ATRACE_TAG ATRACE_TAG_ALWAYS |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 17 | #include "event_fd.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <stdio.h> |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 21 | #include <string.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 22 | #include <sys/ioctl.h> |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 24 | #include <sys/syscall.h> |
| 25 | #include <sys/types.h> |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 26 | #include <atomic> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 27 | #include <memory> |
Wei Wang | cf69e41 | 2016-09-29 16:34:15 -0700 | [diff] [blame] | 28 | #include <cutils/trace.h> |
| 29 | #include <utils/Trace.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/stringprintf.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 34 | |
Yabin Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 35 | #include "event_attr.h" |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 36 | #include "event_type.h" |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 37 | #include "perf_event.h" |
| 38 | #include "utils.h" |
| 39 | |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 40 | std::vector<char> EventFd::data_process_buffer_; |
| 41 | |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 42 | static 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 Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 47 | std::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 Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 51 | std::string event_name = GetEventNameByAttr(attr); |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 52 | int group_fd = -1; |
| 53 | if (group_event_fd != nullptr) { |
| 54 | group_fd = group_event_fd->perf_event_fd_; |
| 55 | } |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 56 | int perf_event_fd = perf_event_open(attr, tid, cpu, group_fd, 0); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 57 | if (perf_event_fd == -1) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 58 | if (report_error) { |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 59 | PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " |
| 60 | << tid << ", cpu " << cpu << ", group_fd " << group_fd |
| 61 | << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 62 | } else { |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 63 | PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " |
| 64 | << tid << ", cpu " << cpu << ", group_fd " << group_fd |
| 65 | << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 66 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 67 | return nullptr; |
| 68 | } |
| 69 | if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 70 | if (report_error) { |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 71 | PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " |
| 72 | << event_name << ", tid " << tid << ", cpu " << cpu |
| 73 | << ", group_fd " << group_fd << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 74 | } else { |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 75 | PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " |
| 76 | << event_name << ", tid " << tid << ", cpu " << cpu |
| 77 | << ", group_fd " << group_fd << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 78 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 79 | return nullptr; |
| 80 | } |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 81 | return std::unique_ptr<EventFd>( |
| 82 | new EventFd(attr, perf_event_fd, event_name, tid, cpu)); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | EventFd::~EventFd() { |
Yabin Cui | 0a072cd | 2016-07-13 17:06:50 -0700 | [diff] [blame] | 86 | DestroyMappedBuffer(); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 87 | close(perf_event_fd_); |
| 88 | } |
| 89 | |
| 90 | std::string EventFd::Name() const { |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 91 | return android::base::StringPrintf( |
| 92 | "perf_event_file(event %s, tid %d, cpu %d)", event_name_.c_str(), tid_, |
| 93 | cpu_); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 96 | uint64_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 Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 106 | bool 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 115 | bool EventFd::ReadCounter(PerfCounter* counter) const { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 116 | CHECK(counter != nullptr); |
Wei Wang | cf69e41 | 2016-09-29 16:34:15 -0700 | [diff] [blame] | 117 | uint64_t pre_counter = counter->value; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 118 | if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 119 | PLOG(ERROR) << "ReadCounter from " << Name() << " failed"; |
| 120 | return false; |
| 121 | } |
Wei Wang | cf69e41 | 2016-09-29 16:34:15 -0700 | [diff] [blame] | 122 | // 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 132 | return true; |
| 133 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 134 | |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 135 | bool EventFd::CreateMappedBuffer(size_t mmap_pages, bool report_error) { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 136 | CHECK(IsPowerOfTwo(mmap_pages)); |
| 137 | size_t page_size = sysconf(_SC_PAGE_SIZE); |
| 138 | size_t mmap_len = (mmap_pages + 1) * page_size; |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 139 | void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 140 | perf_event_fd_, 0); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 141 | if (mmap_addr == MAP_FAILED) { |
Yabin Cui | 27816c9 | 2016-07-07 14:42:54 -0700 | [diff] [blame] | 142 | bool is_perm_error = (errno == EPERM); |
Yabin Cui | 0a072cd | 2016-07-13 17:06:50 -0700 | [diff] [blame] | 143 | 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 Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 149 | LOG(ERROR) |
| 150 | << "It seems the kernel doesn't allow allocating enough " |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 151 | << "buffer for dumping samples, consider decreasing mmap pages(-m)."; |
Yabin Cui | 27816c9 | 2016-07-07 14:42:54 -0700 | [diff] [blame] | 152 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 153 | 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 Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 160 | if (data_process_buffer_.size() < mmap_data_buffer_size_) { |
| 161 | data_process_buffer_.resize(mmap_data_buffer_size_); |
| 162 | } |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
Yabin Cui | 0a072cd | 2016-07-13 17:06:50 -0700 | [diff] [blame] | 166 | bool EventFd::ShareMappedBuffer(const EventFd& event_fd, bool report_error) { |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame] | 167 | CHECK(!HasMappedBuffer()); |
| 168 | CHECK(event_fd.HasMappedBuffer()); |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 169 | int result = |
| 170 | ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_OUTPUT, event_fd.perf_event_fd_); |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame] | 171 | if (result != 0) { |
Yabin Cui | 0a072cd | 2016-07-13 17:06:50 -0700 | [diff] [blame] | 172 | if (report_error) { |
| 173 | PLOG(ERROR) << "failed to share mapped buffer of " |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 174 | << event_fd.perf_event_fd_ << " with " << perf_event_fd_; |
Yabin Cui | 0a072cd | 2016-07-13 17:06:50 -0700 | [diff] [blame] | 175 | } |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
Yabin Cui | 0a072cd | 2016-07-13 17:06:50 -0700 | [diff] [blame] | 181 | void 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 Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 192 | size_t EventFd::GetAvailableMmapData(const char** pdata) { |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame] | 193 | if (!HasMappedBuffer()) { |
| 194 | return 0; |
| 195 | } |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 196 | // 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 199 | // _________________________________________ |
| 200 | // buffer | can write | can read | can write | |
| 201 | // ^ ^ |
| 202 | // read_head write_head |
| 203 | // |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 204 | // 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 208 | |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 209 | size_t buf_mask = mmap_data_buffer_size_ - 1; |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 210 | 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 214 | |
| 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 Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 223 | // Copy records from mapped buffer to data_process_buffer. Note that records |
| 224 | // can be wrapped at the end of the mapped buffer. |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 225 | char* to = data_process_buffer_.data(); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 226 | if (read_head < write_head) { |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 227 | char* from = mmap_data_buffer_ + read_head; |
| 228 | size_t n = write_head - read_head; |
| 229 | memcpy(to, from, n); |
| 230 | to += n; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 231 | } else { |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 232 | 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 240 | } |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 241 | size_t read_bytes = to - data_process_buffer_.data(); |
| 242 | *pdata = data_process_buffer_.data(); |
| 243 | DiscardMmapData(read_bytes); |
| 244 | return read_bytes; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void EventFd::DiscardMmapData(size_t discard_size) { |
| 248 | mmap_metadata_page_->data_tail += discard_size; |
| 249 | } |
| 250 | |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 251 | bool 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 | |
| 257 | bool EventFd::StopPolling() { return IOEventLoop::DelEvent(ioevent_ref_); } |
| 258 | |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 259 | bool IsEventAttrSupportedByKernel(perf_event_attr attr) { |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 260 | auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, nullptr, false); |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 261 | return event_fd != nullptr; |
| 262 | } |