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