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.
22 #include <sys/ioctl.h>
24 #include <sys/syscall.h>
25 #include <sys/types.h>
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/stringprintf.h>
33 #include "event_type.h"
34 #include "perf_event.h"
37 std::vector<char> EventFd::data_process_buffer_;
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);
44 std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu,
46 perf_event_attr perf_attr = attr;
47 std::string event_name = "unknown event";
48 const EventType* event_type = FindEventTypeByConfig(perf_attr.type, perf_attr.config);
49 if (event_type != nullptr) {
50 event_name = event_type->name;
52 int perf_event_fd = perf_event_open(&perf_attr, tid, cpu, -1, 0);
53 if (perf_event_fd == -1) {
55 PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
58 PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
63 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
65 PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
66 << tid << ", cpu " << cpu << ") failed";
68 PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
69 << tid << ", cpu " << cpu << ") failed";
73 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, tid, cpu));
77 if (mmap_addr_ != nullptr) {
78 munmap(mmap_addr_, mmap_len_);
80 close(perf_event_fd_);
83 std::string EventFd::Name() const {
84 return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)",
85 event_name_.c_str(), tid_, cpu_);
88 uint64_t EventFd::Id() const {
91 if (ReadCounter(&counter)) {
98 bool EventFd::ReadCounter(PerfCounter* counter) const {
99 CHECK(counter != nullptr);
100 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
101 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
107 bool EventFd::MmapContent(size_t mmap_pages) {
108 CHECK(IsPowerOfTwo(mmap_pages));
109 size_t page_size = sysconf(_SC_PAGE_SIZE);
110 size_t mmap_len = (mmap_pages + 1) * page_size;
111 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0);
112 if (mmap_addr == MAP_FAILED) {
113 PLOG(ERROR) << "mmap() failed for " << Name();
116 mmap_addr_ = mmap_addr;
117 mmap_len_ = mmap_len;
118 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
119 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
120 mmap_data_buffer_size_ = mmap_len_ - page_size;
121 if (data_process_buffer_.size() < mmap_data_buffer_size_) {
122 data_process_buffer_.resize(mmap_data_buffer_size_);
127 size_t EventFd::GetAvailableMmapData(char** pdata) {
128 // The mmap_data_buffer is used as a ring buffer like below. The kernel continuously writes
129 // records to the buffer, and the user continuously read records out.
130 // _________________________________________
131 // buffer | can write | can read | can write |
133 // read_head write_head
135 // So the user can read records in [read_head, write_head), and the kernel can write records
136 // in [write_head, read_head). The kernel is responsible for updating write_head, and the user
137 // is responsible for updating read_head.
139 size_t buf_mask = mmap_data_buffer_size_ - 1;
140 size_t write_head = static_cast<size_t>(mmap_metadata_page_->data_head & buf_mask);
141 size_t read_head = static_cast<size_t>(mmap_metadata_page_->data_tail & buf_mask);
143 if (read_head == write_head) {
144 // No available data.
148 // Make sure we can see the data after the fence.
149 std::atomic_thread_fence(std::memory_order_acquire);
151 // Copy records from mapped buffer to data_process_buffer. Note that records can be wrapped
152 // at the end of the mapped buffer.
153 char* to = data_process_buffer_.data();
154 if (read_head < write_head) {
155 char* from = mmap_data_buffer_ + read_head;
156 size_t n = write_head - read_head;
160 char* from = mmap_data_buffer_ + read_head;
161 size_t n = mmap_data_buffer_size_ - read_head;
164 from = mmap_data_buffer_;
169 size_t read_bytes = to - data_process_buffer_.data();
170 *pdata = data_process_buffer_.data();
171 DiscardMmapData(read_bytes);
175 void EventFd::DiscardMmapData(size_t discard_size) {
176 mmap_metadata_page_->data_tail += discard_size;
179 void EventFd::PreparePollForMmapData(pollfd* poll_fd) {
180 memset(poll_fd, 0, sizeof(pollfd));
181 poll_fd->fd = perf_event_fd_;
182 poll_fd->events = POLLIN;
185 bool IsEventAttrSupportedByKernel(perf_event_attr attr) {
186 auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, false);
187 return event_fd != nullptr;