rom优化

Signed-off-by: yuanye <yuanye64@huawei.com>
This commit is contained in:
yuanye 2024-11-16 23:10:07 +08:00
parent 45e403fdbf
commit 5fc78205e2
2 changed files with 27 additions and 128 deletions

View File

@ -38,7 +38,6 @@
namespace OHOS {
namespace Developtools {
namespace HiPerf {
// using namespace OHOS::HiviewDFX;
using PerfRecordType = int32_t;
static constexpr uint32_t RECORD_SIZE_LIMIT = 65535;
@ -66,12 +65,6 @@ enum perf_event_hiperf_ext_type {
PERF_RECORD_HIPERF_CALLSTACK = UINT32_MAX / 2,
};
// static inline const std::string RECORD_TYPE_NAME = type;
// const std::string& GetName() const override
// {
// return RECORD_TYPE_NAME;
// }
struct AttrWithId {
perf_event_attr attr;
std::vector<uint64_t> ids;
@ -103,8 +96,7 @@ public:
virtual void DumpLog(const std::string &prefix) const = 0;
};
template <typename DataType, const char* NAME>
template <typename DataType, const char* RECORD_TYPE_NAME>
class PerfEventRecordTemplate : public PerfEventRecord {
public:
PerfEventRecordTemplate(const PerfEventRecordTemplate &) = delete;
@ -163,12 +155,8 @@ public:
protected:
void Init(perf_event_type type, bool inKernel);
void Init(perf_event_hiperf_ext_type type);
private:
const char* RECORD_TYPE_NAME = NAME;
};
// PerfEventRecord
template <typename DataType, const char* NAME>
void PerfEventRecordTemplate<DataType, NAME>::Init(perf_event_type type, bool inKernel)
@ -196,8 +184,17 @@ void PerfEventRecordTemplate<DataType, NAME>::Init(uint8_t *p, const perf_event_
return;
}
header_ = *(reinterpret_cast<perf_event_header *>(p));
}
size_t dataSize = GetSize();
if (dataSize >= sizeof(header_)) {
size_t copySize = dataSize - sizeof(header_);
if (memcpy_s(reinterpret_cast<uint8_t *>(&data_), sizeof(data_), p + sizeof(header_), copySize) != 0) {
HLOGE("##PerfRecordType## memcpy_s return failed!");
}
} else {
HLOGE("##PerfRecordType## return failed!");
}
}
template <typename DataType, const char* NAME>
void PerfEventRecordTemplate<DataType, NAME>::GetHeaderBinary(std::vector<uint8_t> &buf) const
@ -209,7 +206,6 @@ void PerfEventRecordTemplate<DataType, NAME>::GetHeaderBinary(std::vector<uint8_
*(reinterpret_cast<perf_event_header *>(p)) = header_;
}
template <typename DataType, const char* NAME>
void PerfEventRecordTemplate<DataType, NAME>::Dump(int indent, std::string outputFilename, FILE *outputDump) const
{
@ -238,7 +234,6 @@ void PerfEventRecordTemplate<DataType, NAME>::DumpLog(const std::string &prefix)
// define convert from linux/perf_event.h
// description from https://man7.org/linux/man-pages/man2/perf_event_open.2.html
constexpr __u64 SAMPLE_ID = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_ID |
PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER;
@ -251,7 +246,6 @@ class PerfRecordAuxtrace : public PerfEventRecordTemplate<PerfRecordAuxtraceData
public:
u8* rawData_ = nullptr;
PerfRecordAuxtrace() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
PerfRecordAuxtrace(u64 size, u64 offset, u64 reference, u32 idx, u32 tid, u32 cpu, u32 pid);
bool GetBinary1(std::vector<uint8_t> &buf) const;
@ -265,7 +259,6 @@ public:
class PerfRecordMmap : public PerfEventRecordTemplate<PerfRecordMmapData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordMmap() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
PerfRecordMmap(bool inKernel, u32 pid, u32 tid, u64 addr, u64 len, u64 pgoff,
const std::string &filename);
@ -278,7 +271,6 @@ class PerfRecordMmap2 : public PerfEventRecordTemplate<PerfRecordMmap2Data, PERF
public:
PerfRecordMmap2() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
PerfRecordMmap2(bool inKernel, u32 pid, u32 tid, u64 addr, u64 len, u64 pgoff, u32 maj, u32 min,
u64 ino, u32 prot, u32 flags, const std::string &filename);
@ -295,7 +287,6 @@ class PerfRecordLost : public PerfEventRecordTemplate<PerfRecordLostData, PERF_R
public:
PerfRecordLost() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -308,7 +299,6 @@ class PerfRecordComm : public PerfEventRecordTemplate<PerfRecordCommData, PERF_R
public:
PerfRecordComm() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
PerfRecordComm(bool inKernel, u32 pid, u32 tid, const std::string &comm);
@ -359,10 +349,7 @@ private:
class PerfRecordExit : public PerfEventRecordTemplate<PerfRecordExitData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordExit() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -371,8 +358,6 @@ public:
class PerfRecordThrottle : public PerfEventRecordTemplate<PerfRecordThrottleData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordThrottle() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -381,8 +366,6 @@ public:
class PerfRecordUnthrottle : public PerfEventRecordTemplate<PerfRecordThrottleData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordUnthrottle() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -391,8 +374,6 @@ public:
class PerfRecordFork : public PerfEventRecordTemplate<PerfRecordForkData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordFork() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -404,7 +385,6 @@ public:
class PerfRecordRead : public PerfEventRecordTemplate<PerfRecordReadData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordRead() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -433,7 +413,6 @@ class PerfRecordAux : public PerfEventRecordTemplate<PerfRecordAuxData, PERF_REC
public:
uint64_t sampleType_ = SAMPLE_ID;
PerfRecordAux() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -453,7 +432,6 @@ public:
class PerfRecordItraceStart : public PerfEventRecordTemplate<PerfRecordItraceStartData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordItraceStart() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -467,7 +445,6 @@ public:
class PerfRecordLostSamples : public PerfEventRecordTemplate<PerfRecordLostSamplesData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordLostSamples() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
@ -482,10 +459,9 @@ public:
class PerfRecordSwitch : public PerfEventRecordTemplate<PerfRecordSwitchData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordSwitch() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData([[maybe_unused]] int indent) const override {};
void DumpData(int) const override {};
};
/*
@ -509,16 +485,23 @@ public:
class PerfRecordSwitchCpuWide : public PerfEventRecordTemplate<PerfRecordSwitchCpuWideData, PERF_RECORD_TYPE_MMAP> {
public:
PerfRecordSwitchCpuWide() = default;
void Init(uint8_t *data, const perf_event_attr&) override;
bool GetBinary(std::vector<uint8_t> &buf) const override;
void DumpData(int indent) const override;
};
class PerfRecordNull : public PerfEventRecordTemplate<PerfRecordSwitchCpuWideData, nullptr> {
public:
PerfRecordNull() = default;
bool GetBinary(std::vector<uint8_t>&) const override { return false; };
void DumpData(int indent) const override {};
};
class PerfEventRecordFactory {
public:
static PerfEventRecord& GetPerfEventRecord(PerfRecordType type, uint8_t *p,
const perf_event_attr &attr);
static PerfEventRecord& GetPerfEventRecord(PerfRecordType type, uint8_t* data,
const perf_event_attr& attr);
private:
static thread_local std::unordered_map<PerfRecordType, PerfEventRecord*> recordMap_;
};

View File

@ -29,22 +29,6 @@ namespace HiPerf {
bool PerfRecordSample::dumpRemoveStack_ = false;
thread_local std::unordered_map<PerfRecordType, PerfEventRecord*> PerfEventRecordFactory::recordMap_ = {};
#define INIT_PERF_RECORD_DATA(PerfRecordType) \
void PerfRecordType::Init(uint8_t* data, const perf_event_attr&) \
{ \
PerfEventRecordTemplate::Init(data); \
size_t dataSize = GetSize(); \
if (dataSize >= sizeof(header_)) { \
size_t copySize = dataSize - sizeof(header_); \
if (memcpy_s(reinterpret_cast<uint8_t *>(&data_), sizeof(data_), data + sizeof(header_), copySize) != 0) { \
HLOGE("##PerfRecordType## memcpy_s return failed!"); \
} \
} else { \
HLOGE("##PerfRecordType## return failed!"); \
} \
} \
PerfEventRecord* CreatePerfEventRecord(PerfRecordType type)
{
switch (type) {
@ -82,11 +66,10 @@ PerfEventRecord* CreatePerfEventRecord(PerfRecordType type)
return new PerfRecordSwitchCpuWide();
default:
HLOGE("unknown record type %d\n", type);
return nullptr;
return new PerfRecordNull();
}
}
template<typename T>
inline void PushToBinary(bool condition, uint8_t *&p, const T &v)
{
@ -127,11 +110,6 @@ inline void PopFromBinary2(bool condition, uint8_t *&p, T1 &v1, T2 &v2)
}
}
INIT_PERF_RECORD_DATA(PerfRecordAuxtrace)
PerfRecordAuxtrace::PerfRecordAuxtrace(u64 size, u64 offset, u64 reference, u32 idx, u32 tid, u32 cpu, u32 pid)
{
PerfEventRecordTemplate::Init(PERF_RECORD_AUXTRACE);
@ -207,9 +185,6 @@ size_t PerfRecordAuxtrace::GetSize() const
return header_.size + data_.size;
}
INIT_PERF_RECORD_DATA(PerfRecordMmap)
PerfRecordMmap::PerfRecordMmap(bool inKernel, u32 pid, u32 tid, u64 addr, u64 len, u64 pgoff,
const std::string &filename)
{
@ -259,11 +234,6 @@ void PerfRecordMmap::DumpLog(const std::string &prefix) const
header_.size, data_.pid, data_.tid, data_.filename, data_.addr, data_.addr + data_.len, data_.pgoff);
}
INIT_PERF_RECORD_DATA(PerfRecordMmap2)
PerfRecordMmap2::PerfRecordMmap2(bool inKernel, u32 pid, u32 tid, u64 addr, u64 len, u64 pgoff,
u32 maj, u32 min, u64 ino, u32 prot, u32 flags,
const std::string &filename)
@ -350,6 +320,7 @@ void PerfRecordMmap2::DumpData(int indent) const
}
#endif
}
void PerfRecordMmap2::DumpLog(const std::string &prefix) const
{
HLOGV("%s: MMAP2: size %d pid %u tid %u dso '%s' (0x%llx-0x%llx)@0x%llx", prefix.c_str(),
@ -357,8 +328,6 @@ void PerfRecordMmap2::DumpLog(const std::string &prefix) const
data_.pgoff);
}
INIT_PERF_RECORD_DATA(PerfRecordLost)
bool PerfRecordLost::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -387,10 +356,6 @@ PerfRecordLost::PerfRecordLost(bool inKernel, u64 id, u64 lost)
header_.size = sizeof(header_) + sizeof(data_);
}
INIT_PERF_RECORD_DATA(PerfRecordComm)
PerfRecordComm::PerfRecordComm(bool inKernel, u32 pid, u32 tid, const std::string &comm)
{
@ -447,7 +412,6 @@ PerfRecordSample::PerfRecordSample(const PerfRecordSample& sample)
removeStack_ = sample.removeStack_;
}
void PerfRecordSample::Init(uint8_t *p, const perf_event_attr &attr)
{
PerfEventRecordTemplate::Init(p);
@ -508,7 +472,6 @@ void PerfRecordSample::Init(uint8_t *p, const perf_event_attr &attr)
}
}
void PerfRecordSample::SetDumpRemoveStack(bool dumpRemoveStack)
{
dumpRemoveStack_ = dumpRemoveStack;
@ -519,7 +482,6 @@ bool PerfRecordSample::IsDumpRemoveStack()
return dumpRemoveStack_;
}
bool PerfRecordSample::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -816,12 +778,6 @@ pid_t PerfRecordSample::GetServerPidof(unsigned int ipNr)
}
}
INIT_PERF_RECORD_DATA(PerfRecordExit)
bool PerfRecordExit::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -842,11 +798,6 @@ void PerfRecordExit::DumpData(int indent) const
data_.tid, data_.ptid, data_.time);
}
INIT_PERF_RECORD_DATA(PerfRecordThrottle)
bool PerfRecordThrottle::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -867,10 +818,6 @@ void PerfRecordThrottle::DumpData(int indent) const
data_.stream_id);
}
INIT_PERF_RECORD_DATA(PerfRecordUnthrottle)
bool PerfRecordUnthrottle::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -884,15 +831,13 @@ bool PerfRecordUnthrottle::GetBinary(std::vector<uint8_t> &buf) const
*pDest = data_;
return true;
}
void PerfRecordUnthrottle::DumpData(int indent) const
{
PRINT_INDENT(indent, "time 0x%llx, id %llx, stream_id %llx\n", data_.time, data_.id,
data_.stream_id);
}
INIT_PERF_RECORD_DATA(PerfRecordFork)
bool PerfRecordFork::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -913,10 +858,6 @@ void PerfRecordFork::DumpData(int indent) const
data_.ptid);
}
INIT_PERF_RECORD_DATA(PerfRecordRead)
bool PerfRecordRead::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -938,10 +879,6 @@ void PerfRecordRead::DumpData(int indent) const
data_.values.value, data_.values.timeEnabled, data_.values.timeRunning, data_.values.id);
}
INIT_PERF_RECORD_DATA(PerfRecordAux)
bool PerfRecordAux::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -971,12 +908,6 @@ void PerfRecordAux::DumpData(int indent) const
data_.sample_id.time);
}
INIT_PERF_RECORD_DATA(PerfRecordItraceStart)
bool PerfRecordItraceStart::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -996,10 +927,6 @@ void PerfRecordItraceStart::DumpData(int indent) const
PRINT_INDENT(indent, "pid %u, tid %u\n", data_.pid, data_.tid);
}
INIT_PERF_RECORD_DATA(PerfRecordLostSamples)
bool PerfRecordLostSamples::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -1019,10 +946,6 @@ void PerfRecordLostSamples::DumpData(int indent) const
PRINT_INDENT(indent, "lost %llu\n", data_.lost);
}
INIT_PERF_RECORD_DATA(PerfRecordSwitch)
bool PerfRecordSwitch::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -1037,9 +960,6 @@ bool PerfRecordSwitch::GetBinary(std::vector<uint8_t> &buf) const
return true;
}
INIT_PERF_RECORD_DATA(PerfRecordSwitchCpuWide)
bool PerfRecordSwitchCpuWide::GetBinary(std::vector<uint8_t> &buf) const
{
if (buf.size() < GetSize()) {
@ -1060,8 +980,8 @@ void PerfRecordSwitchCpuWide::DumpData(int indent) const
data_.next_prev_tid);
}
PerfEventRecord& PerfEventRecordFactory::GetPerfEventRecord(PerfRecordType type, uint8_t *data,
const perf_event_attr &attr)
PerfEventRecord& PerfEventRecordFactory::GetPerfEventRecord(PerfRecordType type, uint8_t* data,
const perf_event_attr &attr)
{
HLOG_ASSERT(data == nullptr);
PerfEventRecord* record = nullptr;
@ -1076,10 +996,6 @@ PerfEventRecord& PerfEventRecordFactory::GetPerfEventRecord(PerfRecordType type,
return *record;
}
} // namespace HiPerf
} // namespace Developtools
} // namespace OHOS