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 #ifndef SIMPLE_PERF_EVENT_FD_H_ 18 #define SIMPLE_PERF_EVENT_FD_H_ 19 20 #include <sys/types.h> 21 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include <android-base/macros.h> 27 28 #include "IOEventLoop.h" 29 #include "perf_event.h" 30 31 struct PerfCounter { 32 uint64_t value; // The value of the event specified by the perf_event_file. 33 uint64_t time_enabled; // The enabled time. 34 uint64_t time_running; // The running time. 35 uint64_t id; // The id of the perf_event_file. 36 }; 37 38 // EventFd represents an opened perf_event_file. 39 class EventFd { 40 public: 41 static std::unique_ptr<EventFd> OpenEventFile(const perf_event_attr& attr, pid_t tid, int cpu, 42 EventFd* group_event_fd, 43 const std::string& event_name, 44 bool report_error = true); 45 46 virtual ~EventFd(); 47 48 // Give information about this perf_event_file, like (event_name, tid, cpu). 49 std::string Name() const; 50 51 uint64_t Id() const; 52 ThreadId()53 pid_t ThreadId() const { return tid_; } 54 Cpu()55 int Cpu() const { return cpu_; } 56 attr()57 const perf_event_attr& attr() const { return attr_; } 58 59 // It tells the kernel to start counting and recording events specified by 60 // this file. 61 bool SetEnableEvent(bool enable); 62 bool SetFilter(const std::string& filter); 63 64 bool ReadCounter(PerfCounter* counter); 65 66 // Create mapped buffer used to receive records sent by the kernel. 67 // mmap_pages should be power of 2. 68 virtual bool CreateMappedBuffer(size_t mmap_pages, bool report_error); 69 70 // Share the mapped buffer used by event_fd. The two EventFds should monitor 71 // the same event on the same cpu, but have different thread ids. 72 bool ShareMappedBuffer(const EventFd& event_fd, bool report_error); 73 HasMappedBuffer()74 bool HasMappedBuffer() const { return mmap_data_buffer_size_ != 0; } GetMappedBuffer(size_t & buffer_size)75 char* GetMappedBuffer(size_t& buffer_size) { 76 buffer_size = mmap_data_buffer_size_; 77 return mmap_data_buffer_; 78 } 79 80 virtual void DestroyMappedBuffer(); 81 82 // Return available data in the kernel buffer. 83 std::vector<char> GetAvailableMmapData(); 84 // Return the size of available data in the buffer, and set data_pos to the first available data 85 // position in mmap_data_buffer_. 86 virtual size_t GetAvailableMmapDataSize(size_t& data_pos); 87 // Discard the size of the data we have read, so the kernel can reuse the space for new data. 88 virtual void DiscardMmapData(size_t discard_size); 89 90 // Manage the aux buffer, which receive auxiliary data sent by the kernel. 91 // aux_buffer_size: should be power of two, and mod PAGE_SIZE is zero. 92 virtual bool CreateAuxBuffer(size_t aux_buffer_size, bool report_error); HasAuxBuffer()93 bool HasAuxBuffer() const { return aux_buffer_size_ != 0; } 94 virtual void DestroyAuxBuffer(); 95 96 // Get available aux data, which can appear in one or two continuous buffers. 97 // buf1: return pointer to the first buffer 98 // size1: return data size in the first buffer 99 // buf2: return pointer to the second buffer 100 // size2: return data size in the second buffer 101 // Return value: return how many bytes of aux data has been read before. 102 virtual uint64_t GetAvailableAuxData(char** buf1, size_t* size1, char** buf2, size_t* size2); 103 virtual void DiscardAuxData(size_t discard_size); 104 105 // [callback] is called when there is data available in the mapped buffer. 106 virtual bool StartPolling(IOEventLoop& loop, const std::function<bool()>& callback); 107 virtual bool StopPolling(); 108 109 protected: EventFd(const perf_event_attr & attr,int perf_event_fd,const std::string & event_name,pid_t tid,int cpu)110 EventFd(const perf_event_attr& attr, int perf_event_fd, 111 const std::string& event_name, pid_t tid, int cpu) 112 : attr_(attr), 113 perf_event_fd_(perf_event_fd), 114 id_(0), 115 event_name_(event_name), 116 tid_(tid), 117 cpu_(cpu), 118 mmap_addr_(nullptr), 119 mmap_len_(0), 120 mmap_metadata_page_(nullptr), 121 mmap_data_buffer_(nullptr), 122 mmap_data_buffer_size_(0), 123 ioevent_ref_(nullptr), 124 last_counter_value_(0) {} 125 126 bool InnerReadCounter(PerfCounter* counter) const; 127 128 const perf_event_attr attr_; 129 int perf_event_fd_; 130 mutable uint64_t id_; 131 const std::string event_name_; 132 pid_t tid_; 133 int cpu_; 134 135 void* mmap_addr_; 136 size_t mmap_len_; 137 // the first page of mapped area, whose content can be changed by the kernel at any time 138 volatile perf_event_mmap_page* mmap_metadata_page_; 139 // starting from the second page of mapped area, containing records written by the kernel 140 char* mmap_data_buffer_; 141 size_t mmap_data_buffer_size_; 142 // receiving auxiliary data (like instruction tracing data generated by etm) from the kernel 143 char* aux_buffer_ = nullptr; 144 size_t aux_buffer_size_ = 0; 145 146 IOEventRef ioevent_ref_; 147 148 // Used by atrace to generate value difference between two ReadCounter() calls. 149 uint64_t last_counter_value_; 150 151 DISALLOW_COPY_AND_ASSIGN(EventFd); 152 }; 153 154 bool IsEventAttrSupported(const perf_event_attr& attr, const std::string& event_name); 155 156 #endif // SIMPLE_PERF_EVENT_FD_H_ 157