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