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