OSDN Git Service

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