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> |
| 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> |
| 28 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame^] | 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/stringprintf.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 32 | |
| 33 | #include "event_type.h" |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 34 | #include "perf_event.h" |
| 35 | #include "utils.h" |
| 36 | |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 37 | std::vector<char> EventFd::data_process_buffer_; |
| 38 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 39 | static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd, |
| 40 | unsigned long flags) { |
| 41 | return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); |
| 42 | } |
| 43 | |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 44 | std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu, |
Yabin Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 45 | bool report_error) { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 46 | perf_event_attr perf_attr = attr; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 47 | std::string event_name = "unknown event"; |
Yabin Cui | d115b2f | 2015-06-15 13:57:23 -0700 | [diff] [blame] | 48 | const EventType* event_type = FindEventTypeByConfig(perf_attr.type, perf_attr.config); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 49 | if (event_type != nullptr) { |
| 50 | event_name = event_type->name; |
| 51 | } |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 52 | int perf_event_fd = perf_event_open(&perf_attr, tid, cpu, -1, 0); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 53 | if (perf_event_fd == -1) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 54 | if (report_error) { |
| 55 | PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu " |
| 56 | << cpu << ") failed"; |
| 57 | } else { |
| 58 | PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu " |
| 59 | << cpu << ") failed"; |
| 60 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 61 | return nullptr; |
| 62 | } |
| 63 | if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 64 | if (report_error) { |
| 65 | PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid " |
| 66 | << tid << ", cpu " << cpu << ") failed"; |
| 67 | } else { |
| 68 | PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid " |
| 69 | << tid << ", cpu " << cpu << ") failed"; |
| 70 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 71 | return nullptr; |
| 72 | } |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 73 | 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] | 74 | } |
| 75 | |
| 76 | EventFd::~EventFd() { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 77 | if (mmap_addr_ != nullptr) { |
| 78 | munmap(mmap_addr_, mmap_len_); |
| 79 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 80 | close(perf_event_fd_); |
| 81 | } |
| 82 | |
| 83 | std::string EventFd::Name() const { |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 84 | return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)", |
| 85 | event_name_.c_str(), tid_, cpu_); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 88 | uint64_t EventFd::Id() const { |
| 89 | if (id_ == 0) { |
| 90 | PerfCounter counter; |
| 91 | if (ReadCounter(&counter)) { |
| 92 | id_ = counter.id; |
| 93 | } |
| 94 | } |
| 95 | return id_; |
| 96 | } |
| 97 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 98 | bool EventFd::EnableEvent() { |
| 99 | int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0); |
| 100 | if (result < 0) { |
| 101 | PLOG(ERROR) << "ioctl(enable) " << Name() << " failed"; |
| 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool EventFd::DisableEvent() { |
| 108 | int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_DISABLE, 0); |
| 109 | if (result < 0) { |
| 110 | PLOG(ERROR) << "ioctl(disable) " << Name() << " failed"; |
| 111 | return false; |
| 112 | } |
| 113 | return true; |
| 114 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 115 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 116 | bool EventFd::ReadCounter(PerfCounter* counter) const { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 117 | CHECK(counter != nullptr); |
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 | } |
| 122 | return true; |
| 123 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 124 | |
| 125 | bool EventFd::MmapContent(size_t mmap_pages) { |
| 126 | CHECK(IsPowerOfTwo(mmap_pages)); |
| 127 | size_t page_size = sysconf(_SC_PAGE_SIZE); |
| 128 | size_t mmap_len = (mmap_pages + 1) * page_size; |
| 129 | void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0); |
| 130 | if (mmap_addr == MAP_FAILED) { |
| 131 | PLOG(ERROR) << "mmap() failed for " << Name(); |
| 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 | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | size_t EventFd::GetAvailableMmapData(char** pdata) { |
| 146 | // The mmap_data_buffer is used as a ring buffer like below. The kernel continuously writes |
| 147 | // records to the buffer, and the user continuously read records out. |
| 148 | // _________________________________________ |
| 149 | // buffer | can write | can read | can write | |
| 150 | // ^ ^ |
| 151 | // read_head write_head |
| 152 | // |
| 153 | // So the user can read records in [read_head, write_head), and the kernel can write records |
| 154 | // in [write_head, read_head). The kernel is responsible for updating write_head, and the user |
| 155 | // is responsible for updating read_head. |
| 156 | |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 157 | size_t buf_mask = mmap_data_buffer_size_ - 1; |
| 158 | size_t write_head = static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask); |
| 159 | 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] | 160 | |
| 161 | if (read_head == write_head) { |
| 162 | // No available data. |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | // Make sure we can see the data after the fence. |
| 167 | std::atomic_thread_fence(std::memory_order_acquire); |
| 168 | |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 169 | // Copy records from mapped buffer to data_process_buffer. Note that records can be wrapped |
| 170 | // at the end of the mapped buffer. |
| 171 | char* to = data_process_buffer_.data(); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 172 | if (read_head < write_head) { |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 173 | char* from = mmap_data_buffer_ + read_head; |
| 174 | size_t n = write_head - read_head; |
| 175 | memcpy(to, from, n); |
| 176 | to += n; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 177 | } else { |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 178 | char* from = mmap_data_buffer_ + read_head; |
| 179 | size_t n = mmap_data_buffer_size_ - read_head; |
| 180 | memcpy(to, from, n); |
| 181 | to += n; |
| 182 | from = mmap_data_buffer_; |
| 183 | n = write_head; |
| 184 | memcpy(to, from, n); |
| 185 | to += n; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 186 | } |
Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 187 | size_t read_bytes = to - data_process_buffer_.data(); |
| 188 | *pdata = data_process_buffer_.data(); |
| 189 | DiscardMmapData(read_bytes); |
| 190 | return read_bytes; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void EventFd::DiscardMmapData(size_t discard_size) { |
| 194 | mmap_metadata_page_->data_tail += discard_size; |
| 195 | } |
| 196 | |
| 197 | void EventFd::PreparePollForMmapData(pollfd* poll_fd) { |
| 198 | memset(poll_fd, 0, sizeof(pollfd)); |
| 199 | poll_fd->fd = perf_event_fd_; |
| 200 | poll_fd->events = POLLIN; |
| 201 | } |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 202 | |
| 203 | bool IsEventAttrSupportedByKernel(perf_event_attr attr) { |
| 204 | auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, false); |
| 205 | return event_fd != nullptr; |
| 206 | } |