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 | */ |
| 16 | |
| 17 | #include "event_fd.h" |
| 18 | |
| 19 | #include <fcntl.h> |
Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 20 | #include <poll.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 22 | #include <string.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 23 | #include <sys/ioctl.h> |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 24 | #include <sys/mman.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 25 | #include <sys/syscall.h> |
| 26 | #include <sys/types.h> |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 27 | #include <atomic> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 28 | #include <memory> |
| 29 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
| 31 | #include <android-base/logging.h> |
| 32 | #include <android-base/stringprintf.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 33 | |
Yabin Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 34 | #include "event_attr.h" |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 35 | #include "event_type.h" |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 36 | #include "perf_event.h" |
| 37 | #include "utils.h" |
| 38 | |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 39 | std::vector<char> EventFd::data_process_buffer_; |
| 40 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 41 | static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd, |
Chih-Hung Hsieh | 7d6c8ab | 2016-04-15 16:12:03 -0700 | [diff] [blame] | 42 | unsigned long flags) { // NOLINT |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 43 | return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); |
| 44 | } |
| 45 | |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 46 | std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu, |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 47 | EventFd* group_event_fd, bool report_error) { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 48 | perf_event_attr perf_attr = attr; |
Yabin Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 49 | std::string event_name = GetEventNameByAttr(attr); |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 50 | 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 Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 55 | if (perf_event_fd == -1) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 56 | if (report_error) { |
| 57 | PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu " |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 58 | << cpu << ", group_fd " << group_fd << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 59 | } else { |
| 60 | PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu " |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 61 | << cpu << ", group_fd " << group_fd << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 62 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 63 | return nullptr; |
| 64 | } |
| 65 | if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 66 | if (report_error) { |
| 67 | PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid " |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 68 | << tid << ", cpu " << cpu << ", group_fd " << group_fd << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 69 | } else { |
| 70 | PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid " |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 71 | << tid << ", cpu " << cpu << ", group_fd " << group_fd << ") failed"; |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 72 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 73 | return nullptr; |
| 74 | } |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 75 | return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, tid, cpu)); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | EventFd::~EventFd() { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 79 | if (mmap_addr_ != nullptr) { |
| 80 | munmap(mmap_addr_, mmap_len_); |
| 81 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 82 | close(perf_event_fd_); |
| 83 | } |
| 84 | |
| 85 | std::string EventFd::Name() const { |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 86 | return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)", |
| 87 | event_name_.c_str(), tid_, cpu_); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 90 | uint64_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 Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 100 | bool 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 109 | bool EventFd::ReadCounter(PerfCounter* counter) const { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 110 | CHECK(counter != nullptr); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 111 | if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 112 | PLOG(ERROR) << "ReadCounter from " << Name() << " failed"; |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 117 | |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame^] | 118 | bool EventFd::CreateMappedBuffer(size_t mmap_pages, pollfd* poll_fd) { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 119 | 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 Cui | 27816c9 | 2016-07-07 14:42:54 -0700 | [diff] [blame] | 124 | bool is_perm_error = (errno == EPERM); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 125 | PLOG(ERROR) << "mmap() failed for " << Name(); |
Yabin Cui | 27816c9 | 2016-07-07 14:42:54 -0700 | [diff] [blame] | 126 | 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 Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame^] | 129 | << "monitored threads(-t), or decreasing mmap pages(-m), or " |
| 130 | << "decreasing the number of events(-e)."; |
Yabin Cui | 27816c9 | 2016-07-07 14:42:54 -0700 | [diff] [blame] | 131 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 132 | 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 Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 139 | if (data_process_buffer_.size() < mmap_data_buffer_size_) { |
| 140 | data_process_buffer_.resize(mmap_data_buffer_size_); |
| 141 | } |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame^] | 142 | memset(poll_fd, 0, sizeof(pollfd)); |
| 143 | poll_fd->fd = perf_event_fd_; |
| 144 | poll_fd->events = POLLIN; |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | bool 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | size_t EventFd::GetAvailableMmapData(char** pdata) { |
Yabin Cui | 6173592 | 2016-07-08 13:56:48 -0700 | [diff] [blame^] | 161 | if (!HasMappedBuffer()) { |
| 162 | return 0; |
| 163 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 164 | // 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 Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 175 | 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 178 | |
| 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 Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 187 | // 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 190 | if (read_head < write_head) { |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 191 | char* from = mmap_data_buffer_ + read_head; |
| 192 | size_t n = write_head - read_head; |
| 193 | memcpy(to, from, n); |
| 194 | to += n; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 195 | } else { |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 196 | 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 Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 204 | } |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 205 | size_t read_bytes = to - data_process_buffer_.data(); |
| 206 | *pdata = data_process_buffer_.data(); |
| 207 | DiscardMmapData(read_bytes); |
| 208 | return read_bytes; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void EventFd::DiscardMmapData(size_t discard_size) { |
| 212 | mmap_metadata_page_->data_tail += discard_size; |
| 213 | } |
| 214 | |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 215 | bool IsEventAttrSupportedByKernel(perf_event_attr attr) { |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 216 | auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, nullptr, false); |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 217 | return event_fd != nullptr; |
| 218 | } |