2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/ioctl.h>
25 #include <sys/syscall.h>
26 #include <sys/types.h>
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32 #include <android-base/stringprintf.h>
34 #include "event_type.h"
35 #include "perf_event.h"
38 std::vector<char> EventFd::data_process_buffer_;
40 static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd,
41 unsigned long flags) {
42 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
45 std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu,
47 perf_event_attr perf_attr = attr;
48 std::string event_name = "unknown event";
49 const EventType* event_type = FindEventTypeByConfig(perf_attr.type, perf_attr.config);
50 if (event_type != nullptr) {
51 event_name = event_type->name;
53 int perf_event_fd = perf_event_open(&perf_attr, tid, cpu, -1, 0);
54 if (perf_event_fd == -1) {
56 PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
59 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
64 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
66 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
67 << tid << ", cpu " << cpu << ") failed";
69 PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
70 << tid << ", cpu " << cpu << ") failed";
74 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, tid, cpu));
78 if (mmap_addr_ != nullptr) {
79 munmap(mmap_addr_, mmap_len_);
81 close(perf_event_fd_);
84 std::string EventFd::Name() const {
85 return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)",
86 event_name_.c_str(), tid_, cpu_);
89 uint64_t EventFd::Id() const {
92 if (ReadCounter(&counter)) {
99 bool EventFd::ReadCounter(PerfCounter* counter) const {
100 CHECK(counter != nullptr);
101 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
102 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
108 bool EventFd::MmapContent(size_t mmap_pages) {
109 CHECK(IsPowerOfTwo(mmap_pages));
110 size_t page_size = sysconf(_SC_PAGE_SIZE);
111 size_t mmap_len = (mmap_pages + 1) * page_size;
112 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0);
113 if (mmap_addr == MAP_FAILED) {
114 PLOG(ERROR) << "mmap() failed for " << Name();
117 mmap_addr_ = mmap_addr;
118 mmap_len_ = mmap_len;
119 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
120 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
121 mmap_data_buffer_size_ = mmap_len_ - page_size;
122 if (data_process_buffer_.size() < mmap_data_buffer_size_) {
123 data_process_buffer_.resize(mmap_data_buffer_size_);
128 size_t EventFd::GetAvailableMmapData(char** pdata) {
129 // The mmap_data_buffer is used as a ring buffer like below. The kernel continuously writes
130 // records to the buffer, and the user continuously read records out.
131 // _________________________________________
132 // buffer | can write | can read | can write |
134 // read_head write_head
136 // So the user can read records in [read_head, write_head), and the kernel can write records
137 // in [write_head, read_head). The kernel is responsible for updating write_head, and the user
138 // is responsible for updating read_head.
140 size_t buf_mask = mmap_data_buffer_size_ - 1;
141 size_t write_head = static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask);
142 size_t read_head = static_cast<size_t>(mmap_metadata_page_->data_tail & buf_mask);
144 if (read_head == write_head) {
145 // No available data.
149 // Make sure we can see the data after the fence.
150 std::atomic_thread_fence(std::memory_order_acquire);
152 // Copy records from mapped buffer to data_process_buffer. Note that records can be wrapped
153 // at the end of the mapped buffer.
154 char* to = data_process_buffer_.data();
155 if (read_head < write_head) {
156 char* from = mmap_data_buffer_ + read_head;
157 size_t n = write_head - read_head;
161 char* from = mmap_data_buffer_ + read_head;
162 size_t n = mmap_data_buffer_size_ - read_head;
165 from = mmap_data_buffer_;
170 size_t read_bytes = to - data_process_buffer_.data();
171 *pdata = data_process_buffer_.data();
172 DiscardMmapData(read_bytes);
176 void EventFd::DiscardMmapData(size_t discard_size) {
177 mmap_metadata_page_->data_tail += discard_size;
180 void EventFd::PreparePollForMmapData(pollfd* poll_fd) {
181 memset(poll_fd, 0, sizeof(pollfd));
182 poll_fd->fd = perf_event_fd_;
183 poll_fd->events = POLLIN;
186 bool IsEventAttrSupportedByKernel(perf_event_attr attr) {
187 auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, false);
188 return event_fd != nullptr;