• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define ATRACE_TAG ATRACE_TAG_ALWAYS
17 #include "event_fd.h"
18 
19 #include <cutils/trace.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <sys/mman.h>
25 #include <sys/syscall.h>
26 #include <sys/types.h>
27 #include <utils/Trace.h>
28 #include <atomic>
29 #include <memory>
30 
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
33 #include <android-base/stringprintf.h>
34 
35 #include "environment.h"
36 #include "event_attr.h"
37 #include "event_type.h"
38 #include "perf_event.h"
39 #include "utils.h"
40 
41 namespace simpleperf {
42 
perf_event_open(const perf_event_attr & attr,pid_t pid,int cpu,int group_fd,unsigned long flags)43 static int perf_event_open(const perf_event_attr& attr, pid_t pid, int cpu, int group_fd,
44                            unsigned long flags) {  // NOLINT
45   return syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, flags);
46 }
47 
OpenEventFile(const perf_event_attr & attr,pid_t tid,int cpu,EventFd * group_event_fd,const std::string & event_name,bool report_error)48 std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu,
49                                                 EventFd* group_event_fd,
50                                                 const std::string& event_name, bool report_error) {
51   int group_fd = -1;
52   if (group_event_fd != nullptr) {
53     group_fd = group_event_fd->perf_event_fd_;
54   }
55   perf_event_attr real_attr = attr;
56   if (attr.freq) {
57     uint64_t max_sample_freq;
58     if (GetMaxSampleFrequency(&max_sample_freq) && max_sample_freq < attr.sample_freq) {
59       static bool warned = false;
60       if (!warned) {
61         warned = true;
62         LOG(INFO) << "Adjust sample freq to max allowed sample freq " << max_sample_freq;
63       }
64       real_attr.sample_freq = max_sample_freq;
65     }
66   }
67   int perf_event_fd = perf_event_open(real_attr, tid, cpu, group_fd, 0);
68   if (perf_event_fd == -1) {
69     if (report_error) {
70       PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
71                   << cpu << ", group_fd " << group_fd << ") failed";
72     } else {
73       PLOG(DEBUG) << "open perf_event_file (event " << event_name << ", tid " << tid << ", cpu "
74                   << cpu << ", group_fd " << group_fd << ") failed";
75     }
76     return nullptr;
77   }
78   if (fcntl(perf_event_fd, F_SETFD, FD_CLOEXEC) == -1) {
79     if (report_error) {
80       PLOG(ERROR) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
81                   << tid << ", cpu " << cpu << ", group_fd " << group_fd << ") failed";
82     } else {
83       PLOG(DEBUG) << "fcntl(FD_CLOEXEC) for perf_event_file (event " << event_name << ", tid "
84                   << tid << ", cpu " << cpu << ", group_fd " << group_fd << ") failed";
85     }
86     return nullptr;
87   }
88   return std::unique_ptr<EventFd>(new EventFd(real_attr, perf_event_fd, event_name, tid, cpu));
89 }
90 
~EventFd()91 EventFd::~EventFd() {
92   DestroyMappedBuffer();
93   DestroyAuxBuffer();
94   close(perf_event_fd_);
95 }
96 
Name() const97 std::string EventFd::Name() const {
98   return android::base::StringPrintf("perf_event_file(event %s, tid %d, cpu %d)",
99                                      event_name_.c_str(), tid_, cpu_);
100 }
101 
Id() const102 uint64_t EventFd::Id() const {
103   if (id_ == 0) {
104     PerfCounter counter;
105     if (InnerReadCounter(&counter)) {
106       id_ = counter.id;
107     }
108   }
109   return id_;
110 }
111 
SetEnableEvent(bool enable)112 bool EventFd::SetEnableEvent(bool enable) {
113   int result = ioctl(perf_event_fd_, enable ? PERF_EVENT_IOC_ENABLE : PERF_EVENT_IOC_DISABLE, 0);
114   if (result < 0) {
115     PLOG(ERROR) << "ioctl(" << (enable ? "enable" : "disable") << ")" << Name() << " failed";
116     return false;
117   }
118   return true;
119 }
120 
SetFilter(const std::string & filter)121 bool EventFd::SetFilter(const std::string& filter) {
122   bool success = ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_FILTER, filter.c_str()) >= 0;
123   if (!success) {
124     PLOG(ERROR) << "failed to set filter";
125   }
126   return success;
127 }
128 
InnerReadCounter(PerfCounter * counter) const129 bool EventFd::InnerReadCounter(PerfCounter* counter) const {
130   CHECK(counter != nullptr);
131   if (!android::base::ReadFully(perf_event_fd_, counter, sizeof(*counter))) {
132     PLOG(ERROR) << "ReadCounter from " << Name() << " failed";
133     return false;
134   }
135   return true;
136 }
137 
ReadCounter(PerfCounter * counter)138 bool EventFd::ReadCounter(PerfCounter* counter) {
139   if (!InnerReadCounter(counter)) {
140     return false;
141   }
142   // Trace is always available to systrace if enabled
143   if (tid_ > 0) {
144     ATRACE_INT64(
145         android::base::StringPrintf("%s_tid%d_cpu%d", event_name_.c_str(), tid_, cpu_).c_str(),
146         counter->value - last_counter_value_);
147   } else {
148     ATRACE_INT64(android::base::StringPrintf("%s_cpu%d", event_name_.c_str(), cpu_).c_str(),
149                  counter->value - last_counter_value_);
150   }
151   last_counter_value_ = counter->value;
152   return true;
153 }
154 
CreateMappedBuffer(size_t mmap_pages,bool report_error)155 bool EventFd::CreateMappedBuffer(size_t mmap_pages, bool report_error) {
156   CHECK(IsPowerOfTwo(mmap_pages));
157   size_t page_size = sysconf(_SC_PAGE_SIZE);
158   size_t mmap_len = (mmap_pages + 1) * page_size;
159   void* mmap_addr = mmap(nullptr, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, perf_event_fd_, 0);
160   if (mmap_addr == MAP_FAILED) {
161     bool is_perm_error = (errno == EPERM);
162     if (report_error) {
163       PLOG(ERROR) << "mmap(" << mmap_pages << ") failed for " << Name();
164     } else {
165       PLOG(DEBUG) << "mmap(" << mmap_pages << ") failed for " << Name();
166     }
167     if (report_error && is_perm_error) {
168       LOG(ERROR) << "It seems the kernel doesn't allow allocating enough "
169                  << "buffer for dumping samples, consider decreasing mmap pages(-m).";
170     }
171     return false;
172   }
173   mmap_addr_ = mmap_addr;
174   mmap_len_ = mmap_len;
175   mmap_metadata_page_ = reinterpret_cast<perf_event_mmap_page*>(mmap_addr_);
176   mmap_data_buffer_ = reinterpret_cast<char*>(mmap_addr_) + page_size;
177   mmap_data_buffer_size_ = mmap_len_ - page_size;
178   return true;
179 }
180 
ShareMappedBuffer(const EventFd & event_fd,bool report_error)181 bool EventFd::ShareMappedBuffer(const EventFd& event_fd, bool report_error) {
182   CHECK(!HasMappedBuffer());
183   CHECK(event_fd.HasMappedBuffer());
184   int result = ioctl(perf_event_fd_, PERF_EVENT_IOC_SET_OUTPUT, event_fd.perf_event_fd_);
185   if (result != 0) {
186     if (report_error) {
187       PLOG(ERROR) << "failed to share mapped buffer of " << event_fd.perf_event_fd_ << " with "
188                   << perf_event_fd_;
189     }
190     return false;
191   }
192   return true;
193 }
194 
DestroyMappedBuffer()195 void EventFd::DestroyMappedBuffer() {
196   if (HasMappedBuffer()) {
197     munmap(mmap_addr_, mmap_len_);
198     mmap_addr_ = nullptr;
199     mmap_len_ = 0;
200     mmap_metadata_page_ = nullptr;
201     mmap_data_buffer_ = nullptr;
202     mmap_data_buffer_size_ = 0;
203   }
204 }
205 
GetAvailableMmapData()206 std::vector<char> EventFd::GetAvailableMmapData() {
207   size_t data_pos;
208   size_t data_size = GetAvailableMmapDataSize(data_pos);
209   std::vector<char> data(data_size);
210   if (data_size > 0) {
211     size_t copy_size = std::min(data_size, mmap_data_buffer_size_ - data_pos);
212     memcpy(&data[0], mmap_data_buffer_ + data_pos, copy_size);
213     if (copy_size < data_size) {
214       memcpy(&data[copy_size], mmap_data_buffer_, data_size - copy_size);
215     }
216     DiscardMmapData(data_size);
217   }
218   return data;
219 }
220 
GetAvailableMmapDataSize(size_t & data_pos)221 size_t EventFd::GetAvailableMmapDataSize(size_t& data_pos) {
222   // The mmap_data_buffer is used as a ring buffer between the kernel and
223   // simpleperf. The kernel continuously writes records to the buffer, and
224   // simpleperf continuously read records out.
225   //         _________________________________________
226   // buffer | can write   |   can read   |  can write |
227   //                      ^              ^
228   //                    read_head       write_head
229   //
230   // So simpleperf can read records in [read_head, write_head), and the kernel
231   // can write records in [write_head, read_head). The kernel is responsible
232   // for updating write_head, and simpleperf is responsible for updating
233   // read_head.
234 
235   uint64_t write_head = mmap_metadata_page_->data_head;
236   uint64_t read_head = mmap_metadata_page_->data_tail;
237   // The kernel may decrease data_head temporarily (http://b/132446871), making
238   // write_head < read_head. So check it to avoid available data size underflow.
239   if (write_head <= read_head) {
240     // No available data.
241     return 0;
242   }
243   // rmb() used to ensure reading data after reading data_head.
244   __sync_synchronize();
245   data_pos = read_head & (mmap_data_buffer_size_ - 1);
246   return write_head - read_head;
247 }
248 
DiscardMmapData(size_t discard_size)249 void EventFd::DiscardMmapData(size_t discard_size) {
250   // mb() used to ensure finish reading data before writing data_tail.
251   __sync_synchronize();
252   mmap_metadata_page_->data_tail += discard_size;
253 }
254 
CreateAuxBuffer(size_t aux_buffer_size,bool report_error)255 bool EventFd::CreateAuxBuffer(size_t aux_buffer_size, bool report_error) {
256   CHECK(HasMappedBuffer());
257   CHECK(IsPowerOfTwo(aux_buffer_size));
258   mmap_metadata_page_->aux_offset = mmap_len_;
259   mmap_metadata_page_->aux_size = aux_buffer_size;
260   mmap_metadata_page_->aux_head = 0;
261   mmap_metadata_page_->aux_tail = 0;
262   void* mmap_addr = mmap(nullptr, aux_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED,
263                          perf_event_fd_, mmap_metadata_page_->aux_offset);
264   if (mmap_addr == MAP_FAILED) {
265     if (report_error) {
266       PLOG(ERROR) << "failed to mmap aux buffer of size " << aux_buffer_size << " for " << Name();
267     } else {
268       PLOG(DEBUG) << "failed to mmap aux buffer of size " << aux_buffer_size << " for " << Name();
269     }
270     return false;
271   }
272   aux_buffer_ = static_cast<char*>(mmap_addr);
273   aux_buffer_size_ = aux_buffer_size;
274   return true;
275 }
276 
DestroyAuxBuffer()277 void EventFd::DestroyAuxBuffer() {
278   if (HasAuxBuffer()) {
279     munmap(aux_buffer_, aux_buffer_size_);
280     aux_buffer_ = nullptr;
281     aux_buffer_size_ = 0;
282   }
283 }
284 
GetAvailableAuxData(char ** buf1,size_t * size1,char ** buf2,size_t * size2)285 uint64_t EventFd::GetAvailableAuxData(char** buf1, size_t* size1, char** buf2, size_t* size2) {
286   // Aux buffer is similar to mapped_data_buffer. See comments in GetAvailableMmapData().
287   uint64_t write_head = mmap_metadata_page_->aux_head;
288   uint64_t read_head = mmap_metadata_page_->aux_tail;
289   if (write_head <= read_head) {
290     *size1 = *size2 = 0;
291     return 0;  // No available data.
292   }
293   // rmb() used to ensure reading data after reading aux_head.
294   __sync_synchronize();
295   size_t data_pos = read_head & (aux_buffer_size_ - 1);
296   size_t data_size = write_head - read_head;
297   *buf1 = aux_buffer_ + data_pos;
298   if (data_size <= aux_buffer_size_ - data_pos) {
299     *size1 = data_size;
300     *size2 = 0;
301   } else {
302     *size1 = aux_buffer_size_ - data_pos;
303     *buf2 = aux_buffer_;
304     *size2 = data_size - *size1;
305   }
306   return read_head;
307 }
308 
DiscardAuxData(size_t discard_size)309 void EventFd::DiscardAuxData(size_t discard_size) {
310   // mb() used to ensure finish reading data before writing aux_tail.
311   __sync_synchronize();
312   mmap_metadata_page_->aux_tail += discard_size;
313 }
314 
StartPolling(IOEventLoop & loop,const std::function<bool ()> & callback)315 bool EventFd::StartPolling(IOEventLoop& loop, const std::function<bool()>& callback) {
316   ioevent_ref_ = loop.AddReadEvent(perf_event_fd_, callback);
317   return ioevent_ref_ != nullptr;
318 }
319 
StopPolling()320 bool EventFd::StopPolling() {
321   return IOEventLoop::DelEvent(ioevent_ref_);
322 }
323 
IsEventAttrSupported(const perf_event_attr & attr,const std::string & event_name)324 bool IsEventAttrSupported(const perf_event_attr& attr, const std::string& event_name) {
325   return EventFd::OpenEventFile(attr, getpid(), -1, nullptr, event_name, false) != nullptr;
326 }
327 
328 }  // namespace simpleperf
329