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