#include <algorithm>
#include <unordered_map>
-#include <base/logging.h>
-#include <base/stringprintf.h>
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
#include "environment.h"
#include "perf_regs.h"
sample_id_all = attr.sample_id_all;
sample_type = attr.sample_type;
// Other data are not necessary. TODO: Set missing SampleId data.
- size_t size = 0;
- if (sample_id_all) {
- if (sample_type & PERF_SAMPLE_TID) {
- size += sizeof(PerfSampleTidType);
- }
- if (sample_type & PERF_SAMPLE_TIME) {
- size += sizeof(PerfSampleTimeType);
- }
- if (sample_type & PERF_SAMPLE_ID) {
- size += sizeof(PerfSampleIdType);
- }
- if (sample_type & PERF_SAMPLE_STREAM_ID) {
- size += sizeof(PerfSampleStreamIdType);
- }
- if (sample_type & PERF_SAMPLE_CPU) {
- size += sizeof(PerfSampleCpuType);
- }
- }
- return size;
+ return Size();
}
void SampleId::ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end) {
}
}
+size_t SampleId::Size() const {
+ size_t size = 0;
+ if (sample_id_all) {
+ if (sample_type & PERF_SAMPLE_TID) {
+ size += sizeof(PerfSampleTidType);
+ }
+ if (sample_type & PERF_SAMPLE_TIME) {
+ size += sizeof(PerfSampleTimeType);
+ }
+ if (sample_type & PERF_SAMPLE_ID) {
+ size += sizeof(PerfSampleIdType);
+ }
+ if (sample_type & PERF_SAMPLE_STREAM_ID) {
+ size += sizeof(PerfSampleStreamIdType);
+ }
+ if (sample_type & PERF_SAMPLE_CPU) {
+ size += sizeof(PerfSampleCpuType);
+ }
+ }
+ return size;
+}
+
Record::Record() {
memset(&header, 0, sizeof(header));
}
return buf;
}
+void MmapRecord::AdjustSizeBasedOnData() {
+ header.size = sizeof(header) + sizeof(data) + ALIGN(filename.size() + 1, 8) + sample_id.Size();
+}
+
void MmapRecord::DumpData(size_t indent) const {
PrintIndented(indent, "pid %u, tid %u, addr 0x%" PRIx64 ", len 0x%" PRIx64 "\n", data.pid,
data.tid, data.addr, data.len);
return buf;
}
+void Mmap2Record::AdjustSizeBasedOnData() {
+ header.size = sizeof(header) + sizeof(data) + ALIGN(filename.size() + 1, 8) + sample_id.Size();
+}
+
void Mmap2Record::DumpData(size_t indent) const {
PrintIndented(indent, "pid %u, tid %u, addr 0x%" PRIx64 ", len 0x%" PRIx64 "\n", data.pid,
data.tid, data.addr, data.len);
void SampleRecord::AdjustSizeBasedOnData() {
size_t size = BinaryFormat().size();
- LOG(DEBUG) << "SampleRecord size is changed from " << header.size << " to " << size;
+ LOG(DEBUG) << "Record (type " << RecordTypeToString(header.type) << ") size is changed from "
+ << header.size << " to " << size;
header.size = size;
}
const char* p = reinterpret_cast<const char*>(pheader + 1);
const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
MoveFromBinaryFormat(pid, p);
- build_id = BuildId(p);
+ build_id = BuildId(p, BUILD_ID_SIZE);
p += ALIGN(build_id.Size(), 8);
filename = p;
p += ALIGN(filename.size() + 1, 64);
}
}
-static bool IsRecordHappensBefore(const Record* r1, const Record* r2) {
- bool is_r1_sample = (r1->header.type == PERF_RECORD_SAMPLE);
- bool is_r2_sample = (r2->header.type == PERF_RECORD_SAMPLE);
- uint64_t time1 = r1->Timestamp();
- uint64_t time2 = r2->Timestamp();
- // The record with smaller time happens first.
- if (time1 != time2) {
- return time1 < time2;
- }
- // If happening at the same time, make non-sample records before sample records,
- // because non-sample records may contain useful information to parse sample records.
- if (is_r1_sample != is_r2_sample) {
- return is_r1_sample ? false : true;
- }
- // Otherwise, don't care of the order.
- return false;
-}
-
-static std::vector<std::unique_ptr<Record>> ReadUnsortedRecordsFromBuffer(
- const perf_event_attr& attr, const char* buf, size_t buf_size) {
+std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr,
+ const char* buf, size_t buf_size) {
std::vector<std::unique_ptr<Record>> result;
const char* p = buf;
const char* end = buf + buf_size;
return result;
}
-std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr,
- const char* buf, size_t buf_size) {
- std::vector<std::unique_ptr<Record>> result = ReadUnsortedRecordsFromBuffer(attr, buf, buf_size);
- if ((attr.sample_type & PERF_SAMPLE_TIME) && attr.sample_id_all) {
- std::sort(result.begin(), result.end(),
- [](const std::unique_ptr<Record>& r1, const std::unique_ptr<Record>& r2) {
- return IsRecordHappensBefore(r1.get(), r2.get());
- });
+std::unique_ptr<Record> ReadRecordFromFile(const perf_event_attr& attr, FILE* fp) {
+ std::vector<char> buf(sizeof(perf_event_header));
+ perf_event_header* header = reinterpret_cast<perf_event_header*>(&buf[0]);
+ if (fread(header, sizeof(perf_event_header), 1, fp) != 1) {
+ PLOG(ERROR) << "Failed to read record file";
+ return nullptr;
}
- return result;
+ buf.resize(header->size);
+ header = reinterpret_cast<perf_event_header*>(&buf[0]);
+ if (fread(&buf[sizeof(perf_event_header)], buf.size() - sizeof(perf_event_header), 1, fp) != 1) {
+ PLOG(ERROR) << "Failed to read record file";
+ return nullptr;
+ }
+ return ReadRecordFromBuffer(attr, header);
}
MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
return record;
}
-bool RecordCache::RecordComparator::operator()(const Record* r1, const Record* r2) {
- return IsRecordHappensBefore(r2, r1);
+bool RecordCache::RecordWithSeq::IsHappensBefore(const RecordWithSeq& other) const {
+ bool is_sample = (record->header.type == PERF_RECORD_SAMPLE);
+ bool is_other_sample = (other.record->header.type == PERF_RECORD_SAMPLE);
+ uint64_t time = record->Timestamp();
+ uint64_t other_time = other.record->Timestamp();
+ // The record with smaller time happens first.
+ if (time != other_time) {
+ return time < other_time;
+ }
+ // If happening at the same time, make non-sample records before sample records,
+ // because non-sample records may contain useful information to parse sample records.
+ if (is_sample != is_other_sample) {
+ return is_sample ? false : true;
+ }
+ // Otherwise, use the same order as they enter the cache.
+ return seq < other.seq;
+}
+
+bool RecordCache::RecordComparator::operator()(const RecordWithSeq& r1,
+ const RecordWithSeq& r2) {
+ return r2.IsHappensBefore(r1);
}
RecordCache::RecordCache(const perf_event_attr& attr, size_t min_cache_size,
min_cache_size_(min_cache_size),
min_time_diff_in_ns_(min_time_diff_in_ns),
last_time_(0),
+ cur_seq_(0),
queue_(RecordComparator()) {
}
}
void RecordCache::Push(const char* data, size_t size) {
- std::vector<std::unique_ptr<Record>> records = ReadUnsortedRecordsFromBuffer(attr_, data, size);
+ std::vector<std::unique_ptr<Record>> records = ReadRecordsFromBuffer(attr_, data, size);
if (has_timestamp_) {
for (const auto& r : records) {
last_time_ = std::max(last_time_, r->Timestamp());
}
}
for (auto& r : records) {
- queue_.push(r.release());
+ queue_.push(CreateRecordWithSeq(r.release()));
}
}
+void RecordCache::Push(std::unique_ptr<Record> record) {
+ queue_.push(CreateRecordWithSeq(record.release()));
+}
+
std::unique_ptr<Record> RecordCache::Pop() {
if (queue_.size() < min_cache_size_) {
return nullptr;
}
- Record* r = queue_.top();
+ Record* r = queue_.top().record;
if (has_timestamp_) {
if (r->Timestamp() + min_time_diff_in_ns_ > last_time_) {
return nullptr;
std::vector<std::unique_ptr<Record>> RecordCache::PopAll() {
std::vector<std::unique_ptr<Record>> result;
while (!queue_.empty()) {
- result.emplace_back(queue_.top());
+ result.emplace_back(queue_.top().record);
queue_.pop();
}
return result;
}
+
+RecordCache::RecordWithSeq RecordCache::CreateRecordWithSeq(Record *r) {
+ RecordWithSeq result;
+ result.seq = cur_seq_++;
+ result.record = r;
+ return result;
+}