| 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 | |
| Dan Albert | 918e4b7 | 2015-08-11 15:59:43 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 25 | #include "build_id.h" |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 26 | #include "perf_event.h" |
| 27 | |
| 28 | struct KernelMmap; |
| 29 | struct ModuleMmap; |
| 30 | struct ThreadComm; |
| 31 | struct ThreadMmap; |
| 32 | |
| 33 | enum user_record_type { |
| 34 | PERF_RECORD_ATTR = 64, |
| 35 | PERF_RECORD_EVENT_TYPE, |
| 36 | PERF_RECORD_TRACING_DATA, |
| 37 | PERF_RECORD_BUILD_ID, |
| 38 | PERF_RECORD_FINISHED_ROUND, |
| 39 | }; |
| 40 | |
| 41 | struct PerfSampleIpType { |
| 42 | uint64_t ip; |
| 43 | }; |
| 44 | |
| 45 | struct PerfSampleTidType { |
| 46 | uint32_t pid, tid; |
| 47 | }; |
| 48 | |
| 49 | struct PerfSampleTimeType { |
| 50 | uint64_t time; |
| 51 | }; |
| 52 | |
| 53 | struct PerfSampleAddrType { |
| 54 | uint64_t addr; |
| 55 | }; |
| 56 | |
| 57 | struct PerfSampleIdType { |
| 58 | uint64_t id; |
| 59 | }; |
| 60 | |
| 61 | struct PerfSampleStreamIdType { |
| 62 | uint64_t stream_id; |
| 63 | }; |
| 64 | |
| 65 | struct PerfSampleCpuType { |
| 66 | uint32_t cpu, res; |
| 67 | }; |
| 68 | |
| 69 | struct PerfSamplePeriodType { |
| 70 | uint64_t period; |
| 71 | }; |
| 72 | |
| Yabin Cui | 6e8a9a4 | 2015-06-15 14:36:43 -0700 | [diff] [blame] | 73 | struct PerfSampleCallChainType { |
| 74 | std::vector<uint64_t> ips; |
| 75 | }; |
| 76 | |
| Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame^] | 77 | struct PerfSampleRawType { |
| 78 | std::vector<char> data; |
| 79 | }; |
| 80 | |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 81 | struct PerfSampleBranchStackType { |
| 82 | struct BranchStackItemType { |
| 83 | uint64_t from; |
| 84 | uint64_t to; |
| 85 | uint64_t flags; |
| 86 | }; |
| 87 | std::vector<BranchStackItemType> stack; |
| 88 | }; |
| 89 | |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 90 | struct PerfSampleRegsUserType { |
| 91 | uint64_t abi; |
| 92 | uint64_t reg_mask; |
| 93 | std::vector<uint64_t> regs; |
| 94 | }; |
| 95 | |
| 96 | struct PerfSampleStackUserType { |
| 97 | std::vector<char> data; |
| 98 | uint64_t dyn_size; |
| 99 | }; |
| 100 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 101 | // SampleId is optional at the end of a record in binary format. Its content is determined by |
| 102 | // sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to |
| 103 | // perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure. |
| 104 | struct SampleId { |
| 105 | bool sample_id_all; |
| 106 | uint64_t sample_type; |
| 107 | |
| 108 | PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID. |
| 109 | PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME. |
| 110 | PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID. |
| 111 | PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID. |
| 112 | PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU. |
| 113 | |
| 114 | SampleId(); |
| 115 | |
| 116 | // Create the content of sample_id. It depends on the attr we use. |
| 117 | size_t CreateContent(const perf_event_attr& attr); |
| 118 | |
| 119 | // Parse sample_id from binary format in the buffer pointed by p. |
| 120 | void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end); |
| 121 | |
| 122 | // Write the binary format of sample_id to the buffer pointed by p. |
| 123 | void WriteToBinaryFormat(char*& p) const; |
| 124 | void Dump(size_t indent) const; |
| 125 | }; |
| 126 | |
| 127 | // Usually one record contains the following three parts in order in binary format: |
| 128 | // perf_event_header (at the head of a record, containing type and size information) |
| 129 | // data depends on the record type |
| 130 | // sample_id (optional part at the end of a record) |
| 131 | // We hold the common parts (perf_event_header and sample_id) in the base class Record, and |
| 132 | // hold the type specific data part in classes derived from Record. |
| 133 | struct Record { |
| 134 | perf_event_header header; |
| 135 | SampleId sample_id; |
| 136 | |
| 137 | Record(); |
| 138 | Record(const perf_event_header* pheader); |
| 139 | |
| 140 | virtual ~Record() { |
| 141 | } |
| 142 | |
| 143 | void Dump(size_t indent = 0) const; |
| 144 | |
| 145 | protected: |
| 146 | virtual void DumpData(size_t) const { |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | struct MmapRecord : public Record { |
| 151 | struct MmapRecordDataType { |
| 152 | uint32_t pid, tid; |
| 153 | uint64_t addr; |
| 154 | uint64_t len; |
| 155 | uint64_t pgoff; |
| 156 | } data; |
| 157 | std::string filename; |
| 158 | |
| 159 | MmapRecord() { // For storage in std::vector. |
| 160 | } |
| 161 | |
| 162 | MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 163 | std::vector<char> BinaryFormat() const; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 164 | |
| 165 | protected: |
| 166 | void DumpData(size_t indent) const override; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 169 | struct Mmap2Record : public Record { |
| 170 | struct Mmap2RecordDataType { |
| 171 | uint32_t pid, tid; |
| 172 | uint64_t addr; |
| 173 | uint64_t len; |
| 174 | uint64_t pgoff; |
| 175 | uint32_t maj; |
| 176 | uint32_t min; |
| 177 | uint64_t ino; |
| 178 | uint64_t ino_generation; |
| 179 | uint32_t prot, flags; |
| 180 | } data; |
| 181 | std::string filename; |
| 182 | |
| 183 | Mmap2Record() { |
| 184 | } |
| 185 | |
| 186 | Mmap2Record(const perf_event_attr& attr, const perf_event_header* pheader); |
| 187 | |
| 188 | protected: |
| 189 | void DumpData(size_t indent) const override; |
| 190 | }; |
| 191 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 192 | struct CommRecord : public Record { |
| 193 | struct CommRecordDataType { |
| 194 | uint32_t pid, tid; |
| 195 | } data; |
| 196 | std::string comm; |
| 197 | |
| 198 | CommRecord() { |
| 199 | } |
| 200 | |
| 201 | CommRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 202 | std::vector<char> BinaryFormat() const; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 203 | |
| 204 | protected: |
| 205 | void DumpData(size_t indent) const override; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 208 | struct ExitOrForkRecord : public Record { |
| 209 | struct ExitOrForkRecordDataType { |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 210 | uint32_t pid, ppid; |
| 211 | uint32_t tid, ptid; |
| 212 | uint64_t time; |
| 213 | } data; |
| 214 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 215 | ExitOrForkRecord() { |
| 216 | } |
| 217 | ExitOrForkRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 218 | |
| 219 | protected: |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 220 | void DumpData(size_t indent) const override; |
| 221 | }; |
| 222 | |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 223 | struct ExitRecord : public ExitOrForkRecord { |
| 224 | ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader) |
| 225 | : ExitOrForkRecord(attr, pheader) { |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | struct ForkRecord : public ExitOrForkRecord { |
| 230 | ForkRecord() { |
| 231 | } |
| 232 | ForkRecord(const perf_event_attr& attr, const perf_event_header* pheader) |
| 233 | : ExitOrForkRecord(attr, pheader) { |
| 234 | } |
| 235 | std::vector<char> BinaryFormat() const; |
| 236 | }; |
| 237 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 238 | struct SampleRecord : public Record { |
| 239 | uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid. |
| 240 | |
| 241 | PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP. |
| 242 | PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID. |
| 243 | PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME. |
| 244 | PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR. |
| 245 | PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID. |
| 246 | PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID. |
| 247 | PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU. |
| 248 | PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD. |
| 249 | |
| Yabin Cui | 6e8a9a4 | 2015-06-15 14:36:43 -0700 | [diff] [blame] | 250 | PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN. |
| Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame^] | 251 | PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW. |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 252 | PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK. |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 253 | PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER. |
| 254 | PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER. |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 255 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 256 | SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 257 | |
| 258 | protected: |
| 259 | void DumpData(size_t indent) const override; |
| 260 | }; |
| 261 | |
| 262 | // BuildIdRecord is defined in user-space, stored in BuildId feature section in record file. |
| 263 | struct BuildIdRecord : public Record { |
| 264 | uint32_t pid; |
| 265 | BuildId build_id; |
| 266 | std::string filename; |
| 267 | |
| 268 | BuildIdRecord() { |
| 269 | } |
| 270 | |
| 271 | BuildIdRecord(const perf_event_header* pheader); |
| 272 | std::vector<char> BinaryFormat() const; |
| 273 | |
| 274 | protected: |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 275 | void DumpData(size_t indent) const override; |
| 276 | }; |
| 277 | |
| Yabin Cui | 73d8078 | 2015-07-23 21:39:57 -0700 | [diff] [blame] | 278 | std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr, |
| 279 | const char* buf, size_t buf_size); |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 280 | MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid, |
| 281 | uint64_t addr, uint64_t len, uint64_t pgoff, |
| 282 | const std::string& filename); |
| 283 | CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, |
| 284 | const std::string& comm); |
| Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 285 | ForkRecord CreateForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid, |
| 286 | uint32_t ptid); |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 287 | BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id, |
| 288 | const std::string& filename); |
| Yabin Cui | 73d8078 | 2015-07-23 21:39:57 -0700 | [diff] [blame] | 289 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 290 | #endif // SIMPLE_PERF_RECORD_H_ |