| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -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_RECORD_H_ |
| 18 | #define SIMPLE_PERF_RECORD_H_ |
| 19 | |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| Dan Albert | 918e4b7 | 2015-08-11 15:59:43 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | |
| Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 23 | #include <memory> |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 24 | #include <queue> |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> |
| 29 | |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 30 | #include "build_id.h" |
| Yabin Cui | 81a9d33 | 2017-12-10 13:09:07 -0800 | [diff] [blame] | 31 | #include "CallChainJoiner.h" |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 32 | #include "OfflineUnwinder.h" |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 33 | #include "perf_event.h" |
| 34 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 35 | enum user_record_type { |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 36 | PERF_RECORD_USER_DEFINED_TYPE_START = 64, |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 37 | PERF_RECORD_ATTR = 64, |
| 38 | PERF_RECORD_EVENT_TYPE, |
| 39 | PERF_RECORD_TRACING_DATA, |
| 40 | PERF_RECORD_BUILD_ID, |
| 41 | PERF_RECORD_FINISHED_ROUND, |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 42 | |
| 43 | SIMPLE_PERF_RECORD_TYPE_START = 32768, |
| 44 | SIMPLE_PERF_RECORD_KERNEL_SYMBOL, |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 45 | // TODO: remove DsoRecord and SymbolRecord. |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 46 | SIMPLE_PERF_RECORD_DSO, |
| 47 | SIMPLE_PERF_RECORD_SYMBOL, |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 48 | SIMPLE_PERF_RECORD_SPLIT, |
| 49 | SIMPLE_PERF_RECORD_SPLIT_END, |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 50 | SIMPLE_PERF_RECORD_EVENT_ID, |
| Yabin Cui | 81a9d33 | 2017-12-10 13:09:07 -0800 | [diff] [blame] | 51 | SIMPLE_PERF_RECORD_CALLCHAIN, |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 52 | SIMPLE_PERF_RECORD_UNWINDING_RESULT, |
| Yabin Cui | 8cd9233 | 2018-03-21 14:34:29 -0700 | [diff] [blame] | 53 | SIMPLE_PERF_RECORD_TRACING_DATA, |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 56 | // perf_event_header uses u16 to store record size. However, that is not |
| 57 | // enough for storing records like KERNEL_SYMBOL or TRACING_DATA. So define |
| 58 | // a simpleperf_record_header struct to store record header for simpleperf |
| 59 | // defined records (type > SIMPLE_PERF_RECORD_TYPE_START). |
| 60 | struct simpleperf_record_header { |
| 61 | uint32_t type; |
| 62 | uint16_t size1; |
| 63 | uint16_t size0; |
| 64 | }; |
| 65 | |
| 66 | static_assert( |
| 67 | sizeof(simpleperf_record_header) == sizeof(perf_event_header), |
| 68 | "simpleperf_record_header should have the same size as perf_event_header"); |
| 69 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 70 | struct PerfSampleIpType { |
| 71 | uint64_t ip; |
| 72 | }; |
| 73 | |
| 74 | struct PerfSampleTidType { |
| 75 | uint32_t pid, tid; |
| 76 | }; |
| 77 | |
| 78 | struct PerfSampleTimeType { |
| 79 | uint64_t time; |
| 80 | }; |
| 81 | |
| 82 | struct PerfSampleAddrType { |
| 83 | uint64_t addr; |
| 84 | }; |
| 85 | |
| 86 | struct PerfSampleIdType { |
| 87 | uint64_t id; |
| 88 | }; |
| 89 | |
| 90 | struct PerfSampleStreamIdType { |
| 91 | uint64_t stream_id; |
| 92 | }; |
| 93 | |
| 94 | struct PerfSampleCpuType { |
| 95 | uint32_t cpu, res; |
| 96 | }; |
| 97 | |
| 98 | struct PerfSamplePeriodType { |
| 99 | uint64_t period; |
| 100 | }; |
| 101 | |
| Yabin Cui | 6e8a9a4 | 2015-06-15 14:36:43 -0700 | [diff] [blame] | 102 | struct PerfSampleCallChainType { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 103 | uint64_t ip_nr; |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 104 | uint64_t* ips; |
| Yabin Cui | 6e8a9a4 | 2015-06-15 14:36:43 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame] | 107 | struct PerfSampleRawType { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 108 | uint32_t size; |
| 109 | const char* data; |
| Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 112 | struct BranchStackItemType { |
| 113 | uint64_t from; |
| 114 | uint64_t to; |
| 115 | uint64_t flags; |
| 116 | }; |
| 117 | |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 118 | struct PerfSampleBranchStackType { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 119 | uint64_t stack_nr; |
| 120 | const BranchStackItemType* stack; |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 123 | struct PerfSampleRegsUserType { |
| 124 | uint64_t abi; |
| 125 | uint64_t reg_mask; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 126 | uint64_t reg_nr; |
| 127 | const uint64_t* regs; |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct PerfSampleStackUserType { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 131 | uint64_t size; |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 132 | char* data; |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 133 | uint64_t dyn_size; |
| 134 | }; |
| 135 | |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 136 | struct RecordHeader { |
| 137 | public: |
| 138 | uint32_t type; |
| 139 | uint16_t misc; |
| 140 | uint32_t size; |
| 141 | |
| 142 | RecordHeader() : type(0), misc(0), size(0) {} |
| 143 | |
| Chih-Hung Hsieh | 5674ed8 | 2016-07-12 11:35:16 -0700 | [diff] [blame] | 144 | explicit RecordHeader(const char* p) { |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 145 | auto pheader = reinterpret_cast<const perf_event_header*>(p); |
| 146 | if (pheader->type < SIMPLE_PERF_RECORD_TYPE_START) { |
| 147 | type = pheader->type; |
| 148 | misc = pheader->misc; |
| 149 | size = pheader->size; |
| 150 | } else { |
| 151 | auto sheader = reinterpret_cast<const simpleperf_record_header*>(p); |
| 152 | type = sheader->type; |
| 153 | misc = 0; |
| 154 | size = (sheader->size1 << 16) | sheader->size0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void MoveToBinaryFormat(char*& p) const { |
| 159 | if (type < SIMPLE_PERF_RECORD_TYPE_START) { |
| 160 | auto pheader = reinterpret_cast<perf_event_header*>(p); |
| 161 | pheader->type = type; |
| 162 | pheader->misc = misc; |
| 163 | CHECK_LT(size, 1u << 16); |
| 164 | pheader->size = static_cast<uint16_t>(size); |
| 165 | } else { |
| 166 | auto sheader = reinterpret_cast<simpleperf_record_header*>(p); |
| 167 | sheader->type = type; |
| 168 | CHECK_EQ(misc, 0u); |
| 169 | sheader->size1 = size >> 16; |
| 170 | sheader->size0 = size & 0xffff; |
| 171 | } |
| 172 | p += sizeof(perf_event_header); |
| 173 | } |
| 174 | }; |
| 175 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 176 | // SampleId is optional at the end of a record in binary format. Its content is |
| 177 | // determined by sample_id_all and sample_type in perf_event_attr. To avoid the |
| 178 | // complexity of referring to perf_event_attr each time, we copy sample_id_all |
| 179 | // and sample_type inside the SampleId structure. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 180 | struct SampleId { |
| 181 | bool sample_id_all; |
| 182 | uint64_t sample_type; |
| 183 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 184 | PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID. |
| 185 | PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME. |
| 186 | PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID. |
| 187 | PerfSampleStreamIdType |
| 188 | stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID. |
| 189 | PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 190 | |
| 191 | SampleId(); |
| 192 | |
| 193 | // Create the content of sample_id. It depends on the attr we use. |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 194 | size_t CreateContent(const perf_event_attr& attr, uint64_t event_id); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 195 | |
| 196 | // Parse sample_id from binary format in the buffer pointed by p. |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 197 | void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, |
| 198 | const char* end); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 199 | |
| 200 | // Write the binary format of sample_id to the buffer pointed by p. |
| 201 | void WriteToBinaryFormat(char*& p) const; |
| 202 | void Dump(size_t indent) const; |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 203 | size_t Size() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 206 | // Usually one record contains the following three parts in order in binary |
| 207 | // format: |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 208 | // RecordHeader (at the head of a record, containing type and size info) |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 209 | // data depends on the record type |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 210 | // SampleId (optional part at the end of a record) |
| 211 | // We hold the common parts (RecordHeader and SampleId) in the base class |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 212 | // Record, and hold the type specific data part in classes derived from Record. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 213 | struct Record { |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 214 | RecordHeader header; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 215 | SampleId sample_id; |
| 216 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 217 | Record() : binary_(nullptr), own_binary_(false) {} |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 218 | explicit Record(char* p) : header(p), binary_(p), own_binary_(false) {} |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 219 | Record(Record&& other); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 220 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 221 | virtual ~Record() { |
| 222 | if (own_binary_) { |
| 223 | delete[] binary_; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void OwnBinary() { own_binary_ = true; } |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 228 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 229 | uint32_t type() const { return header.type; } |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 230 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 231 | uint16_t misc() const { return header.misc; } |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 232 | |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 233 | uint32_t size() const { return header.size; } |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 234 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 235 | static uint32_t header_size() { return sizeof(perf_event_header); } |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 236 | |
| Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 237 | bool InKernel() const { |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 238 | return (header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == |
| 239 | PERF_RECORD_MISC_KERNEL; |
| Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 242 | void SetTypeAndMisc(uint32_t type, uint16_t misc) { |
| 243 | header.type = type; |
| 244 | header.misc = misc; |
| 245 | } |
| 246 | |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 247 | void SetSize(uint32_t size) { header.size = size; } |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 248 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 249 | void Dump(size_t indent = 0) const; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 250 | |
| 251 | const char* Binary() const { return binary_; } |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 252 | char* BinaryForTestingOnly() { return binary_; } |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 253 | |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 254 | virtual uint64_t Timestamp() const; |
| Yabin Cui | fc22b8f | 2016-08-04 14:47:50 -0700 | [diff] [blame] | 255 | virtual uint32_t Cpu() const; |
| Yabin Cui | ada97db | 2017-02-23 15:54:11 -0800 | [diff] [blame] | 256 | virtual uint64_t Id() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 257 | |
| 258 | protected: |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 259 | void UpdateBinary(char* new_binary); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 260 | virtual void DumpData(size_t) const = 0; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 261 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 262 | char* binary_; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 263 | bool own_binary_; |
| 264 | |
| 265 | DISALLOW_COPY_AND_ASSIGN(Record); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | struct MmapRecord : public Record { |
| 269 | struct MmapRecordDataType { |
| 270 | uint32_t pid, tid; |
| 271 | uint64_t addr; |
| 272 | uint64_t len; |
| 273 | uint64_t pgoff; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 274 | }; |
| 275 | const MmapRecordDataType* data; |
| 276 | const char* filename; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 277 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 278 | MmapRecord(const perf_event_attr& attr, char* p); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 279 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 280 | MmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, |
| 281 | uint32_t tid, uint64_t addr, uint64_t len, uint64_t pgoff, |
| 282 | const std::string& filename, uint64_t event_id, uint64_t time = 0); |
| 283 | |
| 284 | void SetDataAndFilename(const MmapRecordDataType& data, |
| 285 | const std::string& filename); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 286 | |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 287 | protected: |
| 288 | void DumpData(size_t indent) const override; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 291 | struct Mmap2Record : public Record { |
| 292 | struct Mmap2RecordDataType { |
| 293 | uint32_t pid, tid; |
| 294 | uint64_t addr; |
| 295 | uint64_t len; |
| 296 | uint64_t pgoff; |
| 297 | uint32_t maj; |
| 298 | uint32_t min; |
| 299 | uint64_t ino; |
| 300 | uint64_t ino_generation; |
| 301 | uint32_t prot, flags; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 302 | }; |
| 303 | const Mmap2RecordDataType* data; |
| 304 | const char* filename; |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 305 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 306 | Mmap2Record(const perf_event_attr& attr, char* p); |
| Yabin Cui | cdc11a3 | 2018-03-20 15:29:03 -0700 | [diff] [blame] | 307 | Mmap2Record(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid, |
| 308 | uint64_t addr, uint64_t len, uint64_t pgoff, uint32_t prot, |
| 309 | const std::string& filename, uint64_t event_id, uint64_t time = 0); |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 310 | |
| 311 | void SetDataAndFilename(const Mmap2RecordDataType& data, |
| 312 | const std::string& filename); |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 313 | |
| 314 | protected: |
| 315 | void DumpData(size_t indent) const override; |
| 316 | }; |
| 317 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 318 | struct CommRecord : public Record { |
| 319 | struct CommRecordDataType { |
| 320 | uint32_t pid, tid; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 321 | }; |
| 322 | const CommRecordDataType* data; |
| 323 | const char* comm; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 324 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 325 | CommRecord(const perf_event_attr& attr, char* p); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 326 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 327 | CommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, |
| Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 328 | const std::string& comm, uint64_t event_id, uint64_t time); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 329 | |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 330 | protected: |
| 331 | void DumpData(size_t indent) const override; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 334 | struct ExitOrForkRecord : public Record { |
| 335 | struct ExitOrForkRecordDataType { |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 336 | uint32_t pid, ppid; |
| 337 | uint32_t tid, ptid; |
| 338 | uint64_t time; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 339 | }; |
| 340 | const ExitOrForkRecordDataType* data; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 341 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 342 | ExitOrForkRecord(const perf_event_attr& attr, char* p); |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 343 | |
| 344 | ExitOrForkRecord() : data(nullptr) {} |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 345 | |
| 346 | protected: |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 347 | void DumpData(size_t indent) const override; |
| 348 | }; |
| 349 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 350 | struct ExitRecord : public ExitOrForkRecord { |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 351 | ExitRecord(const perf_event_attr& attr, char* p) |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 352 | : ExitOrForkRecord(attr, p) {} |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | struct ForkRecord : public ExitOrForkRecord { |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 356 | ForkRecord(const perf_event_attr& attr, char* p) |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 357 | : ExitOrForkRecord(attr, p) {} |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 358 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 359 | ForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, |
| 360 | uint32_t ppid, uint32_t ptid, uint64_t event_id); |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
| Yabin Cui | e5adc13 | 2016-06-22 11:37:26 -0700 | [diff] [blame] | 363 | struct LostRecord : public Record { |
| 364 | uint64_t id; |
| 365 | uint64_t lost; |
| 366 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 367 | LostRecord(const perf_event_attr& attr, char* p); |
| Yabin Cui | e5adc13 | 2016-06-22 11:37:26 -0700 | [diff] [blame] | 368 | |
| 369 | protected: |
| 370 | void DumpData(size_t indent) const override; |
| 371 | }; |
| 372 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 373 | struct SampleRecord : public Record { |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 374 | uint64_t sample_type; // sample_type is a bit mask determining which fields |
| 375 | // below are valid. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 376 | |
| 377 | PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP. |
| 378 | PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID. |
| 379 | PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME. |
| 380 | PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR. |
| 381 | PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID. |
| 382 | PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID. |
| 383 | PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU. |
| 384 | PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD. |
| 385 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 386 | PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN. |
| 387 | PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW. |
| 388 | PerfSampleBranchStackType |
| 389 | branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK. |
| 390 | PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER. |
| 391 | PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER. |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 392 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 393 | SampleRecord(const perf_event_attr& attr, char* p); |
| Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 394 | SampleRecord(const perf_event_attr& attr, uint64_t id, uint64_t ip, |
| 395 | uint32_t pid, uint32_t tid, uint64_t time, uint32_t cpu, |
| 396 | uint64_t period, const std::vector<uint64_t>& ips); |
| 397 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 398 | void ReplaceRegAndStackWithCallChain(const std::vector<uint64_t>& ips); |
| Yabin Cui | d3cb3b0 | 2017-07-24 14:59:46 -0700 | [diff] [blame] | 399 | size_t ExcludeKernelCallChain(); |
| Yabin Cui | 81a9d33 | 2017-12-10 13:09:07 -0800 | [diff] [blame] | 400 | bool HasUserCallChain() const; |
| 401 | void UpdateUserCallChain(const std::vector<uint64_t>& user_ips); |
| Yabin Cui | 5a5e92c | 2018-01-29 16:02:16 -0800 | [diff] [blame] | 402 | void RemoveInvalidStackData(); |
| Yabin Cui | 81a9d33 | 2017-12-10 13:09:07 -0800 | [diff] [blame] | 403 | |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 404 | uint64_t Timestamp() const override; |
| Yabin Cui | fc22b8f | 2016-08-04 14:47:50 -0700 | [diff] [blame] | 405 | uint32_t Cpu() const override; |
| Yabin Cui | ada97db | 2017-02-23 15:54:11 -0800 | [diff] [blame] | 406 | uint64_t Id() const override; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 407 | |
| Yabin Cui | cf31e9d | 2016-07-14 14:29:33 -0700 | [diff] [blame] | 408 | uint64_t GetValidStackSize() const { |
| 409 | // If stack_user_data.dyn_size == 0, it may be because the kernel misses |
| 410 | // the patch to update dyn_size, like in N9 (See b/22612370). So assume |
| 411 | // all stack data is valid if dyn_size == 0. |
| 412 | if (stack_user_data.dyn_size == 0) { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 413 | return stack_user_data.size; |
| Yabin Cui | cf31e9d | 2016-07-14 14:29:33 -0700 | [diff] [blame] | 414 | } |
| 415 | return stack_user_data.dyn_size; |
| 416 | } |
| 417 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 418 | void AdjustCallChainGeneratedByKernel(); |
| Yabin Cui | 7c8847b | 2018-04-13 11:23:06 -0700 | [diff] [blame] | 419 | std::vector<uint64_t> GetCallChain(size_t* kernel_ip_count) const; |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 420 | |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 421 | protected: |
| 422 | void DumpData(size_t indent) const override; |
| 423 | }; |
| 424 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 425 | // BuildIdRecord is defined in user-space, stored in BuildId feature section in |
| 426 | // record file. |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 427 | struct BuildIdRecord : public Record { |
| 428 | uint32_t pid; |
| 429 | BuildId build_id; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 430 | const char* filename; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 431 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 432 | explicit BuildIdRecord(char* p); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 433 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 434 | BuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id, |
| 435 | const std::string& filename); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 436 | |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 437 | protected: |
| 438 | void DumpData(size_t indent) const override; |
| 439 | }; |
| 440 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 441 | struct KernelSymbolRecord : public Record { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 442 | uint32_t kallsyms_size; |
| 443 | const char* kallsyms; |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 444 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 445 | explicit KernelSymbolRecord(char* p); |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 446 | |
| Chih-Hung Hsieh | 1786a88 | 2016-08-11 11:06:00 -0700 | [diff] [blame] | 447 | explicit KernelSymbolRecord(const std::string& kallsyms); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 448 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 449 | protected: |
| 450 | void DumpData(size_t indent) const override; |
| 451 | }; |
| 452 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 453 | struct DsoRecord : public Record { |
| 454 | uint64_t dso_type; |
| 455 | uint64_t dso_id; |
| Yabin Cui | c855ecc | 2016-07-11 17:04:54 -0700 | [diff] [blame] | 456 | uint64_t min_vaddr; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 457 | const char* dso_name; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 458 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 459 | explicit DsoRecord(char* p); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 460 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 461 | DsoRecord(uint64_t dso_type, uint64_t dso_id, const std::string& dso_name, |
| 462 | uint64_t min_vaddr); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 463 | |
| 464 | protected: |
| 465 | void DumpData(size_t indent) const override; |
| 466 | }; |
| 467 | |
| 468 | struct SymbolRecord : public Record { |
| 469 | uint64_t addr; |
| 470 | uint64_t len; |
| 471 | uint64_t dso_id; |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 472 | const char* name; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 473 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 474 | explicit SymbolRecord(char* p); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 475 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 476 | SymbolRecord(uint64_t addr, uint64_t len, const std::string& name, |
| 477 | uint64_t dso_id); |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 478 | |
| Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 479 | protected: |
| 480 | void DumpData(size_t indent) const override; |
| 481 | }; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 482 | |
| Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 483 | struct TracingDataRecord : public Record { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 484 | uint32_t data_size; |
| 485 | const char* data; |
| Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 486 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 487 | explicit TracingDataRecord(char* p); |
| Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 488 | |
| Chih-Hung Hsieh | 1786a88 | 2016-08-11 11:06:00 -0700 | [diff] [blame] | 489 | explicit TracingDataRecord(const std::vector<char>& tracing_data); |
| Yabin Cui | e5adc13 | 2016-06-22 11:37:26 -0700 | [diff] [blame] | 490 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 491 | protected: |
| 492 | void DumpData(size_t indent) const override; |
| 493 | }; |
| 494 | |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 495 | struct EventIdRecord : public Record { |
| 496 | uint64_t count; |
| 497 | struct EventIdData { |
| 498 | uint64_t attr_id; |
| 499 | uint64_t event_id; |
| 500 | } const* data; |
| 501 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 502 | explicit EventIdRecord(char* p); |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 503 | |
| 504 | explicit EventIdRecord(const std::vector<uint64_t>& data); |
| 505 | |
| 506 | protected: |
| 507 | void DumpData(size_t indent) const override; |
| 508 | }; |
| 509 | |
| Yabin Cui | 81a9d33 | 2017-12-10 13:09:07 -0800 | [diff] [blame] | 510 | struct CallChainRecord : public Record { |
| 511 | uint32_t pid; |
| 512 | uint32_t tid; |
| 513 | uint64_t chain_type; |
| 514 | uint64_t time; |
| 515 | uint64_t ip_nr; |
| 516 | uint64_t* ips; |
| 517 | uint64_t* sps; |
| 518 | |
| 519 | explicit CallChainRecord(char* p); |
| 520 | |
| 521 | CallChainRecord(pid_t pid, pid_t tid, simpleperf::CallChainJoiner::ChainType type, uint64_t time, |
| 522 | const std::vector<uint64_t>& ips, const std::vector<uint64_t>& sps); |
| 523 | |
| 524 | uint64_t Timestamp() const override { |
| 525 | return time; |
| 526 | } |
| 527 | |
| 528 | protected: |
| 529 | void DumpData(size_t indent) const override; |
| 530 | }; |
| 531 | |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 532 | struct UnwindingResultRecord : public Record { |
| 533 | uint64_t time; |
| Yabin Cui | 85faad9 | 2018-01-04 17:17:55 -0800 | [diff] [blame] | 534 | simpleperf::UnwindingResult unwinding_result; |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 535 | |
| 536 | explicit UnwindingResultRecord(char* p); |
| 537 | |
| Yabin Cui | 85faad9 | 2018-01-04 17:17:55 -0800 | [diff] [blame] | 538 | UnwindingResultRecord(uint64_t time, const simpleperf::UnwindingResult& unwinding_result); |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 539 | |
| 540 | uint64_t Timestamp() const override { |
| 541 | return time; |
| 542 | } |
| 543 | |
| 544 | protected: |
| 545 | void DumpData(size_t indent) const override; |
| 546 | }; |
| 547 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 548 | // UnknownRecord is used for unknown record types, it makes sure all unknown |
| Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 549 | // records are not changed when modifying perf.data. |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 550 | struct UnknownRecord : public Record { |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 551 | const char* data; |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 552 | |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 553 | explicit UnknownRecord(char* p); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 554 | |
| 555 | protected: |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 556 | void DumpData(size_t indent) const override; |
| 557 | }; |
| 558 | |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 559 | // Read record from the buffer pointed by [p]. But the record doesn't own |
| 560 | // the buffer. |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 561 | std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, uint32_t type, char* p); |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 562 | |
| 563 | // Read record from the buffer pointed by [p]. And the record owns the buffer. |
| 564 | std::unique_ptr<Record> ReadRecordFromOwnedBuffer(const perf_event_attr& attr, |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 565 | uint32_t type, char* p); |
| Yabin Cui | 190a848 | 2016-08-04 10:22:17 -0700 | [diff] [blame] | 566 | |
| 567 | // Read records from the buffer pointed by [buf]. None of the records own |
| 568 | // the buffer. |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 569 | std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer( |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 570 | const perf_event_attr& attr, char* buf, size_t buf_size); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 571 | |
| Yabin Cui | 2ea6de1 | 2016-10-24 19:13:09 -0700 | [diff] [blame] | 572 | // Read one record from the buffer pointed by [p]. But the record doesn't |
| 573 | // own the buffer. |
| Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 574 | std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, char* p); |
| Yabin Cui | 2ea6de1 | 2016-10-24 19:13:09 -0700 | [diff] [blame] | 575 | |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 576 | // RecordCache is a cache used when receiving records from the kernel. |
| 577 | // It sorts received records based on type and timestamp, and pops records |
| 578 | // in sorted order. Records from the kernel need to be sorted because |
| 579 | // records may come from different cpus at the same time, and it is affected |
| 580 | // by the order in which we collect records from different cpus. |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 581 | // RecordCache pushes records and pops sorted record online. It uses two checks |
| 582 | // to help ensure that records are popped in order. Each time we pop a record A, |
| 583 | // it is the earliest record among all records in the cache. In addition, we |
| 584 | // have checks for min_cache_size and min_time_diff. For min_cache_size check, |
| 585 | // we check if the cache size >= min_cache_size, which is based on the |
| 586 | // assumption that if we have received (min_cache_size - 1) records after |
| 587 | // record A, we are not likely to receive a record earlier than A. For |
| 588 | // min_time_diff check, we check if record A is generated min_time_diff ns |
| 589 | // earlier than the latest record, which is based on the assumption that if we |
| 590 | // have received a record for time t, we are not likely to receive a record for |
| 591 | // time (t - min_time_diff) or earlier. |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 592 | class RecordCache { |
| 593 | public: |
| Chih-Hung Hsieh | 5674ed8 | 2016-07-12 11:35:16 -0700 | [diff] [blame] | 594 | explicit RecordCache(bool has_timestamp, size_t min_cache_size = 1000u, |
| Yabin Cui | cf31e9d | 2016-07-14 14:29:33 -0700 | [diff] [blame] | 595 | uint64_t min_time_diff_in_ns = 1000000u); |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 596 | ~RecordCache(); |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 597 | void Push(std::unique_ptr<Record> record); |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 598 | void Push(std::vector<std::unique_ptr<Record>> records); |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 599 | std::unique_ptr<Record> Pop(); |
| 600 | std::vector<std::unique_ptr<Record>> PopAll(); |
| Yabin Cui | fc22b8f | 2016-08-04 14:47:50 -0700 | [diff] [blame] | 601 | std::unique_ptr<Record> ForcedPop(); |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 602 | |
| 603 | private: |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 604 | struct RecordWithSeq { |
| 605 | uint32_t seq; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 606 | Record* record; |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 607 | |
| Yabin Cui | cf31e9d | 2016-07-14 14:29:33 -0700 | [diff] [blame] | 608 | RecordWithSeq(uint32_t seq, Record* record) : seq(seq), record(record) {} |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 609 | bool IsHappensBefore(const RecordWithSeq& other) const; |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 610 | }; |
| 611 | |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 612 | struct RecordComparator { |
| 613 | bool operator()(const RecordWithSeq& r1, const RecordWithSeq& r2); |
| 614 | }; |
| 615 | |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 616 | bool has_timestamp_; |
| 617 | size_t min_cache_size_; |
| 618 | uint64_t min_time_diff_in_ns_; |
| 619 | uint64_t last_time_; |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 620 | uint32_t cur_seq_; |
| 621 | std::priority_queue<RecordWithSeq, std::vector<RecordWithSeq>, |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 622 | RecordComparator> queue_; |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 625 | #endif // SIMPLE_PERF_RECORD_H_ |