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