| 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 | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct PerfSampleIpType { |
| 51 | uint64_t ip; |
| 52 | }; |
| 53 | |
| 54 | struct PerfSampleTidType { |
| 55 | uint32_t pid, tid; |
| 56 | }; |
| 57 | |
| 58 | struct PerfSampleTimeType { |
| 59 | uint64_t time; |
| 60 | }; |
| 61 | |
| 62 | struct PerfSampleAddrType { |
| 63 | uint64_t addr; |
| 64 | }; |
| 65 | |
| 66 | struct PerfSampleIdType { |
| 67 | uint64_t id; |
| 68 | }; |
| 69 | |
| 70 | struct PerfSampleStreamIdType { |
| 71 | uint64_t stream_id; |
| 72 | }; |
| 73 | |
| 74 | struct PerfSampleCpuType { |
| 75 | uint32_t cpu, res; |
| 76 | }; |
| 77 | |
| 78 | struct PerfSamplePeriodType { |
| 79 | uint64_t period; |
| 80 | }; |
| 81 | |
| Yabin Cui | 6e8a9a4 | 2015-06-15 14:36:43 -0700 | [diff] [blame] | 82 | struct PerfSampleCallChainType { |
| 83 | std::vector<uint64_t> ips; |
| 84 | }; |
| 85 | |
| Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame] | 86 | struct PerfSampleRawType { |
| 87 | std::vector<char> data; |
| 88 | }; |
| 89 | |
| Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 90 | struct BranchStackItemType { |
| 91 | uint64_t from; |
| 92 | uint64_t to; |
| 93 | uint64_t flags; |
| 94 | }; |
| 95 | |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 96 | struct PerfSampleBranchStackType { |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 97 | std::vector<BranchStackItemType> stack; |
| 98 | }; |
| 99 | |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 100 | struct PerfSampleRegsUserType { |
| 101 | uint64_t abi; |
| 102 | uint64_t reg_mask; |
| 103 | std::vector<uint64_t> regs; |
| 104 | }; |
| 105 | |
| 106 | struct PerfSampleStackUserType { |
| 107 | std::vector<char> data; |
| 108 | uint64_t dyn_size; |
| 109 | }; |
| 110 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 111 | // SampleId is optional at the end of a record in binary format. Its content is determined by |
| 112 | // sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to |
| 113 | // perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure. |
| 114 | struct SampleId { |
| 115 | bool sample_id_all; |
| 116 | uint64_t sample_type; |
| 117 | |
| 118 | PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID. |
| 119 | PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME. |
| 120 | PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID. |
| 121 | PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID. |
| 122 | PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU. |
| 123 | |
| 124 | SampleId(); |
| 125 | |
| 126 | // 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] | 127 | size_t CreateContent(const perf_event_attr& attr, uint64_t event_id); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 128 | |
| 129 | // Parse sample_id from binary format in the buffer pointed by p. |
| 130 | void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end); |
| 131 | |
| 132 | // Write the binary format of sample_id to the buffer pointed by p. |
| 133 | void WriteToBinaryFormat(char*& p) const; |
| 134 | void Dump(size_t indent) const; |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 135 | size_t Size() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | // Usually one record contains the following three parts in order in binary format: |
| 139 | // perf_event_header (at the head of a record, containing type and size information) |
| 140 | // data depends on the record type |
| 141 | // sample_id (optional part at the end of a record) |
| 142 | // We hold the common parts (perf_event_header and sample_id) in the base class Record, and |
| 143 | // hold the type specific data part in classes derived from Record. |
| 144 | struct Record { |
| 145 | perf_event_header header; |
| 146 | SampleId sample_id; |
| 147 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame^] | 148 | Record() { |
| 149 | memset(&header, 0, sizeof(header)); |
| 150 | } |
| 151 | Record(const perf_event_header* pheader) { |
| 152 | header = *pheader; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame^] | 155 | virtual ~Record() { |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | uint32_t type() const { |
| 159 | return header.type; |
| 160 | } |
| 161 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame^] | 162 | uint16_t misc() const { |
| 163 | return header.misc; |
| 164 | } |
| 165 | |
| 166 | size_t size() const { |
| 167 | return header.size; |
| 168 | } |
| 169 | |
| 170 | static uint32_t header_size() { |
| 171 | return sizeof(perf_event_header); |
| 172 | } |
| 173 | |
| Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 174 | bool InKernel() const { |
| 175 | return (header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL; |
| 176 | } |
| 177 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame^] | 178 | void SetTypeAndMisc(uint32_t type, uint16_t misc) { |
| 179 | header.type = type; |
| 180 | header.misc = misc; |
| 181 | } |
| 182 | |
| 183 | void SetSize(uint32_t size) { |
| 184 | CHECK_LT(size, 1u << 16); |
| 185 | header.size = size; |
| 186 | } |
| 187 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 188 | void Dump(size_t indent = 0) const; |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 189 | virtual std::vector<char> BinaryFormat() const = 0; |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 190 | virtual uint64_t Timestamp() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 191 | |
| 192 | protected: |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 193 | virtual void DumpData(size_t) const = 0; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | struct MmapRecord : public Record { |
| 197 | struct MmapRecordDataType { |
| 198 | uint32_t pid, tid; |
| 199 | uint64_t addr; |
| 200 | uint64_t len; |
| 201 | uint64_t pgoff; |
| 202 | } data; |
| 203 | std::string filename; |
| 204 | |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 205 | MmapRecord() { // For CreateMmapRecord. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 209 | std::vector<char> BinaryFormat() const override; |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 210 | void AdjustSizeBasedOnData(); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 211 | |
| 212 | protected: |
| 213 | void DumpData(size_t indent) const override; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 216 | struct Mmap2Record : public Record { |
| 217 | struct Mmap2RecordDataType { |
| 218 | uint32_t pid, tid; |
| 219 | uint64_t addr; |
| 220 | uint64_t len; |
| 221 | uint64_t pgoff; |
| 222 | uint32_t maj; |
| 223 | uint32_t min; |
| 224 | uint64_t ino; |
| 225 | uint64_t ino_generation; |
| 226 | uint32_t prot, flags; |
| 227 | } data; |
| 228 | std::string filename; |
| 229 | |
| 230 | Mmap2Record() { |
| 231 | } |
| 232 | |
| 233 | Mmap2Record(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 234 | std::vector<char> BinaryFormat() const override; |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 235 | void AdjustSizeBasedOnData(); |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 236 | |
| 237 | protected: |
| 238 | void DumpData(size_t indent) const override; |
| 239 | }; |
| 240 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 241 | struct CommRecord : public Record { |
| 242 | struct CommRecordDataType { |
| 243 | uint32_t pid, tid; |
| 244 | } data; |
| 245 | std::string comm; |
| 246 | |
| 247 | CommRecord() { |
| 248 | } |
| 249 | |
| 250 | CommRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 251 | std::vector<char> BinaryFormat() const override; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 252 | |
| 253 | protected: |
| 254 | void DumpData(size_t indent) const override; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 257 | struct ExitOrForkRecord : public Record { |
| 258 | struct ExitOrForkRecordDataType { |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 259 | uint32_t pid, ppid; |
| 260 | uint32_t tid, ptid; |
| 261 | uint64_t time; |
| 262 | } data; |
| 263 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 264 | ExitOrForkRecord() { |
| 265 | } |
| 266 | ExitOrForkRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 267 | std::vector<char> BinaryFormat() const override; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 268 | |
| 269 | protected: |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 270 | void DumpData(size_t indent) const override; |
| 271 | }; |
| 272 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 273 | struct ExitRecord : public ExitOrForkRecord { |
| 274 | ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader) |
| 275 | : ExitOrForkRecord(attr, pheader) { |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | struct ForkRecord : public ExitOrForkRecord { |
| 280 | ForkRecord() { |
| 281 | } |
| 282 | ForkRecord(const perf_event_attr& attr, const perf_event_header* pheader) |
| 283 | : ExitOrForkRecord(attr, pheader) { |
| 284 | } |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 287 | struct SampleRecord : public Record { |
| 288 | uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid. |
| 289 | |
| 290 | PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP. |
| 291 | PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID. |
| 292 | PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME. |
| 293 | PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR. |
| 294 | PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID. |
| 295 | PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID. |
| 296 | PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU. |
| 297 | PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD. |
| 298 | |
| Yabin Cui | 6e8a9a4 | 2015-06-15 14:36:43 -0700 | [diff] [blame] | 299 | PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN. |
| Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame] | 300 | PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW. |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 301 | PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK. |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 302 | PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER. |
| 303 | PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER. |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 304 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 305 | SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 306 | std::vector<char> BinaryFormat() const override; |
| 307 | void AdjustSizeBasedOnData(); |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 308 | uint64_t Timestamp() const override; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 309 | |
| 310 | protected: |
| 311 | void DumpData(size_t indent) const override; |
| 312 | }; |
| 313 | |
| 314 | // BuildIdRecord is defined in user-space, stored in BuildId feature section in record file. |
| 315 | struct BuildIdRecord : public Record { |
| 316 | uint32_t pid; |
| 317 | BuildId build_id; |
| 318 | std::string filename; |
| 319 | |
| 320 | BuildIdRecord() { |
| 321 | } |
| 322 | |
| 323 | BuildIdRecord(const perf_event_header* pheader); |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 324 | std::vector<char> BinaryFormat() const override; |
| 325 | |
| 326 | protected: |
| 327 | void DumpData(size_t indent) const override; |
| 328 | }; |
| 329 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame^] | 330 | struct KernelSymbolRecord : public Record { |
| 331 | bool end_of_symbols; |
| 332 | std::string kallsyms; |
| 333 | |
| 334 | KernelSymbolRecord() { |
| 335 | } |
| 336 | |
| 337 | KernelSymbolRecord(const perf_event_header* pheader); |
| 338 | std::vector<char> BinaryFormat() const override; |
| 339 | |
| 340 | protected: |
| 341 | void DumpData(size_t indent) const override; |
| 342 | }; |
| 343 | |
| Yabin Cui | cb84c98 | 2015-09-30 17:22:35 -0700 | [diff] [blame] | 344 | // UnknownRecord is used for unknown record types, it makes sure all unknown records |
| 345 | // are not changed when modifying perf.data. |
| 346 | struct UnknownRecord : public Record { |
| 347 | std::vector<char> data; |
| 348 | |
| 349 | UnknownRecord(const perf_event_header* pheader); |
| 350 | std::vector<char> BinaryFormat() const override; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 351 | |
| 352 | protected: |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 353 | void DumpData(size_t indent) const override; |
| 354 | }; |
| 355 | |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 356 | // RecordCache is a cache used when receiving records from the kernel. |
| 357 | // It sorts received records based on type and timestamp, and pops records |
| 358 | // in sorted order. Records from the kernel need to be sorted because |
| 359 | // records may come from different cpus at the same time, and it is affected |
| 360 | // by the order in which we collect records from different cpus. |
| 361 | // RecordCache pushes records and pops sorted record online. It uses two checks to help |
| 362 | // ensure that records are popped in order. Each time we pop a record A, it is the earliest record |
| 363 | // among all records in the cache. In addition, we have checks for min_cache_size and |
| 364 | // min_time_diff. For min_cache_size check, we check if the cache size >= min_cache_size, |
| 365 | // which is based on the assumption that if we have received (min_cache_size - 1) records |
| 366 | // after record A, we are not likely to receive a record earlier than A. For min_time_diff |
| 367 | // check, we check if record A is generated min_time_diff ns earlier than the latest |
| 368 | // record, which is based on the assumption that if we have received a record for time t, |
| 369 | // we are not likely to receive a record for time (t - min_time_diff) or earlier. |
| 370 | class RecordCache { |
| 371 | public: |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 372 | RecordCache(bool has_timestamp, size_t min_cache_size = 1000u, |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 373 | uint64_t min_time_diff_in_ns = 1000000u); |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 374 | ~RecordCache(); |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 375 | void Push(std::unique_ptr<Record> record); |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 376 | void Push(std::vector<std::unique_ptr<Record>> records); |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 377 | std::unique_ptr<Record> Pop(); |
| 378 | std::vector<std::unique_ptr<Record>> PopAll(); |
| 379 | |
| 380 | private: |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 381 | struct RecordWithSeq { |
| 382 | uint32_t seq; |
| 383 | Record *record; |
| 384 | |
| 385 | bool IsHappensBefore(const RecordWithSeq& other) const; |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 388 | struct RecordComparator { |
| 389 | bool operator()(const RecordWithSeq& r1, const RecordWithSeq& r2); |
| 390 | }; |
| 391 | |
| 392 | RecordWithSeq CreateRecordWithSeq(Record *r); |
| 393 | |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 394 | bool has_timestamp_; |
| 395 | size_t min_cache_size_; |
| 396 | uint64_t min_time_diff_in_ns_; |
| 397 | uint64_t last_time_; |
| Yabin Cui | 4913c12 | 2016-01-11 17:15:55 -0800 | [diff] [blame] | 398 | uint32_t cur_seq_; |
| 399 | std::priority_queue<RecordWithSeq, std::vector<RecordWithSeq>, |
| 400 | RecordComparator> queue_; |
| Yabin Cui | f469c3d | 2015-10-07 15:00:46 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 403 | std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, |
| 404 | const perf_event_header* pheader); |
| Yabin Cui | 73d8078 | 2015-07-23 21:39:57 -0700 | [diff] [blame] | 405 | std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr, |
| 406 | const char* buf, size_t buf_size); |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 407 | MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid, |
| 408 | uint64_t addr, uint64_t len, uint64_t pgoff, |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 409 | const std::string& filename, uint64_t event_id); |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 410 | CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 411 | const std::string& comm, uint64_t event_id); |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 412 | ForkRecord CreateForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid, |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 413 | uint32_t ptid, uint64_t event_id); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 414 | BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id, |
| 415 | const std::string& filename); |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame^] | 416 | std::vector<KernelSymbolRecord> CreateKernelSymbolRecords(const std::string& kallsyms); |
| Yabin Cui | 73d8078 | 2015-07-23 21:39:57 -0700 | [diff] [blame] | 417 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 418 | #endif // SIMPLE_PERF_RECORD_H_ |