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