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> 21#include <sys/ioctl.h> 22#include <sys/mman.h> 23#include <sys/syscall.h> 24#include <sys/types.h> 25#include <memory> 26 27#include <base/file.h> 28#include <base/logging.h> 29#include <base/stringprintf.h> 30 31#include "event_type.h" 32#include "perf_event.h" 33#include "utils.h" 34 35static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd, 36 unsigned long flags) { 37 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); 38} 39 40std::unique_ptr<EventFd> EventFd::OpenEventFileForProcess(const perf_event_attr& attr, pid_t pid) { 41 return OpenEventFile(attr, pid, -1); 42} 43 44std::unique_ptr<EventFd> EventFd::OpenEventFileForCpu(const perf_event_attr& attr, int cpu) { 45 return OpenEventFile(attr, -1, cpu); 46} 47 48std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t pid, int cpu) { 49 perf_event_attr perf_attr = attr; 50 std::string event_name = "unknown event"; 51 const EventType* event_type = 52 EventTypeFactory::FindEventTypeByConfig(perf_attr.type, perf_attr.config); 53 if (event_type != nullptr) { 54 event_name = event_type->name; 55 } 56 int perf_event_fd = perf_event_open(&perf_attr, pid, cpu, -1, 0); 57 if (perf_event_fd == -1) { 58 // It depends whether the perf_event_file configuration is supported by the kernel and the 59 // machine. So fail to open the file is not an error. 60 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", pid " << pid << ", cpu " 61 << cpu << ") failed"; 62 return nullptr; 63 } 64 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) { 65 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", pid " << pid 66 << ", cpu " << cpu << ") failed"; 67 return nullptr; 68 } 69 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, pid, cpu)); 70} 71 72EventFd::~EventFd() { 73 if (mmap_addr_ != nullptr) { 74 munmap(mmap_addr_, mmap_len_); 75 } 76 close(perf_event_fd_); 77} 78 79std::string EventFd::Name() const { 80 return android::base::StringPrintf("perf_event_file(event %s, pid %d, cpu %d)", 81 event_name_.c_str(), pid_, cpu_); 82} 83 84uint64_t EventFd::Id() const { 85 if (id_ == 0) { 86 PerfCounter counter; 87 if (ReadCounter(&counter)) { 88 id_ = counter.id; 89 } 90 } 91 return id_; 92} 93 94bool EventFd::EnableEvent() { 95 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0); 96 if (result < 0) { 97 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed"; 98 return false; 99 } 100 return true; 101} 102 103bool EventFd::DisableEvent() { 104 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_DISABLE, 0); 105 if (result < 0) { 106 PLOG(ERROR) << "ioctl(disable) " << Name() << " failed"; 107 return false; 108 } 109 return true; 110} 111 112bool EventFd::ReadCounter(PerfCounter* counter) const { 113 CHECK(counter != nullptr); 114 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) { 115 PLOG(ERROR) << "ReadCounter from " << Name() << " failed"; 116 return false; 117 } 118 return true; 119} 120 121bool EventFd::MmapContent(size_t mmap_pages) { 122 CHECK(IsPowerOfTwo(mmap_pages)); 123 size_t page_size = sysconf(_SC_PAGE_SIZE); 124 size_t mmap_len = (mmap_pages + 1) * page_size; 125 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0); 126 if (mmap_addr == MAP_FAILED) { 127 PLOG(ERROR) << "mmap() failed for " << Name(); 128 return false; 129 } 130 mmap_addr_ = mmap_addr; 131 mmap_len_ = mmap_len; 132 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_); 133 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size; 134 mmap_data_buffer_size_ = mmap_len_ - page_size; 135 return true; 136} 137 138size_t EventFd::GetAvailableMmapData(char** pdata) { 139 // The mmap_data_buffer is used as a ring buffer like below. The kernel continuously writes 140 // records to the buffer, and the user continuously read records out. 141 // _________________________________________ 142 // buffer | can write | can read | can write | 143 // ^ ^ 144 // read_head write_head 145 // 146 // So the user can read records in [read_head, write_head), and the kernel can write records 147 // in [write_head, read_head). The kernel is responsible for updating write_head, and the user 148 // is responsible for updating read_head. 149 150 uint64_t buf_mask = mmap_data_buffer_size_ - 1; 151 uint64_t write_head = mmap_metadata_page_->data_head & buf_mask; 152 uint64_t read_head = mmap_metadata_page_->data_tail & buf_mask; 153 154 if (read_head == write_head) { 155 // No available data. 156 return 0; 157 } 158 159 // Make sure we can see the data after the fence. 160 std::atomic_thread_fence(std::memory_order_acquire); 161 162 *pdata = mmap_data_buffer_ + read_head; 163 if (read_head < write_head) { 164 return write_head - read_head; 165 } else { 166 return mmap_data_buffer_size_ - read_head; 167 } 168} 169 170void EventFd::DiscardMmapData(size_t discard_size) { 171 mmap_metadata_page_->data_tail += discard_size; 172} 173 174void EventFd::PreparePollForMmapData(pollfd* poll_fd) { 175 memset(poll_fd, 0, sizeof(pollfd)); 176 poll_fd->fd = perf_event_fd_; 177 poll_fd->events = POLLIN; 178} 179