blob: 205fa965c5994624fd90c8b4283e536ddc47f0a0 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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 Cuif469c3d2015-10-07 15:00:46 -070021#include <string.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070022#include <sys/ioctl.h>
Yabin Cui9759e1b2015-04-28 15:54:13 -070023#include <sys/mman.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070024#include <sys/syscall.h>
25#include <sys/types.h>
Yabin Cuif569b472015-04-30 09:43:26 -070026#include <atomic>
Yabin Cui67d3abd2015-04-16 15:26:31 -070027#include <memory>
28
Elliott Hughes66dd09e2015-12-04 14:00:57 -080029#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/stringprintf.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070032
33#include "event_type.h"
Yabin Cui67d3abd2015-04-16 15:26:31 -070034#include "perf_event.h"
35#include "utils.h"
36
Yabin Cuif469c3d2015-10-07 15:00:46 -070037std::vector<char> EventFd::data_process_buffer_;
38
Yabin Cui67d3abd2015-04-16 15:26:31 -070039static 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 Cuib032de72015-06-17 21:15:09 -070044std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu,
Yabin Cui7d4904d2015-06-09 13:38:42 -070045 bool report_error) {
Yabin Cui9759e1b2015-04-28 15:54:13 -070046 perf_event_attr perf_attr = attr;
Yabin Cui67d3abd2015-04-16 15:26:31 -070047 std::string event_name = "unknown event";
Yabin Cuid115b2f2015-06-15 13:57:23 -070048 const EventType* event_type = FindEventTypeByConfig(perf_attr.type, perf_attr.config);
Yabin Cui67d3abd2015-04-16 15:26:31 -070049 if (event_type != nullptr) {
50 event_name = event_type->name;
51 }
Yabin Cuib032de72015-06-17 21:15:09 -070052 int perf_event_fd = perf_event_open(&perf_attr, tid, cpu, -1, 0);
Yabin Cui67d3abd2015-04-16 15:26:31 -070053 if (perf_event_fd == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070054 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 Cui67d3abd2015-04-16 15:26:31 -070061 return nullptr;
62 }
63 if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
Yabin Cui42aa1272015-09-18 11:10:55 -070064 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 Cui67d3abd2015-04-16 15:26:31 -070071 return nullptr;
72 }
Yabin Cuib032de72015-06-17 21:15:09 -070073 return std::unique_ptr<EventFd>(new EventFd(perf_event_fd, event_name, tid, cpu));
Yabin Cui67d3abd2015-04-16 15:26:31 -070074}
75
76EventFd::~EventFd() {
Yabin Cui9759e1b2015-04-28 15:54:13 -070077 if (mmap_addr_ != nullptr) {
78 munmap(mmap_addr_, mmap_len_);
79 }
Yabin Cui67d3abd2015-04-16 15:26:31 -070080 close(perf_event_fd_);
81}
82
83std::string EventFd::Name() const {
Yabin Cuib032de72015-06-17 21:15:09 -070084 return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)",
85 event_name_.c_str(), tid_, cpu_);
Yabin Cui67d3abd2015-04-16 15:26:31 -070086}
87
Yabin Cui9759e1b2015-04-28 15:54:13 -070088uint64_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 Cui67d3abd2015-04-16 15:26:31 -070098bool 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
107bool 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 Cui323e9452015-04-20 18:07:17 -0700115
Yabin Cui9759e1b2015-04-28 15:54:13 -0700116bool EventFd::ReadCounter(PerfCounter* counter) const {
Yabin Cui323e9452015-04-20 18:07:17 -0700117 CHECK(counter != nullptr);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700118 if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
Yabin Cui323e9452015-04-20 18:07:17 -0700119 PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
120 return false;
121 }
122 return true;
123}
Yabin Cui9759e1b2015-04-28 15:54:13 -0700124
125bool 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 Cuif469c3d2015-10-07 15:00:46 -0700139 if (data_process_buffer_.size() < mmap_data_buffer_size_) {
140 data_process_buffer_.resize(mmap_data_buffer_size_);
141 }
Yabin Cui9759e1b2015-04-28 15:54:13 -0700142 return true;
143}
144
145size_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 Cuif469c3d2015-10-07 15:00:46 -0700157 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 Cui9759e1b2015-04-28 15:54:13 -0700160
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 Cuif469c3d2015-10-07 15:00:46 -0700169 // 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 Cui9759e1b2015-04-28 15:54:13 -0700172 if (read_head < write_head) {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700173 char* from = mmap_data_buffer_ + read_head;
174 size_t n = write_head - read_head;
175 memcpy(to, from, n);
176 to += n;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700177 } else {
Yabin Cuif469c3d2015-10-07 15:00:46 -0700178 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 Cui9759e1b2015-04-28 15:54:13 -0700186 }
Yabin Cuif469c3d2015-10-07 15:00:46 -0700187 size_t read_bytes = to - data_process_buffer_.data();
188 *pdata = data_process_buffer_.data();
189 DiscardMmapData(read_bytes);
190 return read_bytes;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700191}
192
193void EventFd::DiscardMmapData(size_t discard_size) {
194 mmap_metadata_page_->data_tail += discard_size;
195}
196
197void 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 Cui9fd3cc12015-06-25 17:42:23 -0700202
203bool IsEventAttrSupportedByKernel(perf_event_attr attr) {
204 auto event_fd = EventFd::OpenEventFile(attr, getpid(), -1, false);
205 return event_fd != nullptr;
206}