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.
21 #include <sys/ioctl.h>
23 #include <sys/syscall.h>
24 #include <sys/types.h>
28 #include <base/file.h>
29 #include <base/logging.h>
30 #include <base/stringprintf.h>
32 #include "event_type.h"
33 #include "perf_event.h"
36 static int perf_event_open(perf_event_attr* attr, pid_t pid, int cpu, int group_fd,
37 unsigned long flags) {
38 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
41 std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu,
43 perf_event_attr perf_attr = attr;
44 std::string event_name = "unknown event";
45 const EventType* event_type = FindEventTypeByConfig(perf_attr.type, perf_attr.config);
46 if (event_type != nullptr) {
47 event_name = event_type->name;
49 int perf_event_fd = perf_event_open(&perf_attr, tid, cpu, -1, 0);
50 if (perf_event_fd == -1) {
51 (report_error ? PLOG(ERROR) : PLOG(DEBUG)) << "open perf_event_file (event " << event_name
52 << ", tid " << tid << ", cpu " << cpu << ") failed";
55 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
56 (report_error ? PLOG(ERROR) : PLOG(DEBUG)) << "fcntl(FD_CLOEXEC) for perf_event_file (event "
57 << event_name << ", tid " << tid << ", cpu " << cpu
61 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, tid, cpu));
65 if (mmap_addr_ != nullptr) {
66 munmap(mmap_addr_, mmap_len_);
68 close(perf_event_fd_);
71 std::string EventFd::Name() const {
72 return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)",
73 event_name_.c_str(), tid_, cpu_);
76 uint64_t EventFd::Id() const {
79 if (ReadCounter(&counter)) {
86 bool EventFd::EnableEvent() {
87 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_ENABLE, 0);
89 PLOG(ERROR) << "ioctl(enable) " << Name() << " failed";
95 bool EventFd::DisableEvent() {
96 int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_DISABLE, 0);
98 PLOG(ERROR) << "ioctl(disable) " << Name() << " failed";
104 bool EventFd::ReadCounter(PerfCounter* counter) const {
105 CHECK(counter != nullptr);
106 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
107 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
113 bool EventFd::MmapContent(size_t mmap_pages) {
114 CHECK(IsPowerOfTwo(mmap_pages));
115 size_t page_size = sysconf(_SC_PAGE_SIZE);
116 size_t mmap_len = (mmap_pages + 1) * page_size;
117 void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0);
118 if (mmap_addr == MAP_FAILED) {
119 PLOG(ERROR) << "mmap() failed for " << Name();
122 mmap_addr_ = mmap_addr;
123 mmap_len_ = mmap_len;
124 mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
125 mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
126 mmap_data_buffer_size_ = mmap_len_ - page_size;
130 size_t EventFd::GetAvailableMmapData(char** pdata) {
131 // The mmap_data_buffer is used as a ring buffer like below. The kernel continuously writes
132 // records to the buffer, and the user continuously read records out.
133 // _________________________________________
134 // buffer | can write | can read | can write |
136 // read_head write_head
138 // So the user can read records in [read_head, write_head), and the kernel can write records
139 // in [write_head, read_head). The kernel is responsible for updating write_head, and the user
140 // is responsible for updating read_head.
142 uint64_t buf_mask = mmap_data_buffer_size_ - 1;
143 uint64_t write_head = mmap_metadata_page_->data_head & buf_mask;
144 uint64_t read_head = mmap_metadata_page_->data_tail & buf_mask;
146 if (read_head == write_head) {
147 // No available data.
151 // Make sure we can see the data after the fence.
152 std::atomic_thread_fence(std::memory_order_acquire);
154 *pdata = mmap_data_buffer_ + read_head;
155 if (read_head < write_head) {
156 return write_head - read_head;
158 return mmap_data_buffer_size_ - read_head;
162 void EventFd::DiscardMmapData(size_t discard_size) {
163 mmap_metadata_page_->data_tail += discard_size;
166 void EventFd::PreparePollForMmapData(pollfd* poll_fd) {
167 memset(poll_fd, 0, sizeof(pollfd));
168 poll_fd->fd = perf_event_fd_;
169 poll_fd->events = POLLIN;
172 bool IsEventAttrSupportedByKernel(perf_event_attr attr) {
173 auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, false);
174 return event_fd != nullptr;