mirror of
https://gitee.com/openharmony/arkcompiler_ets_runtime
synced 2025-03-03 10:08:22 +00:00
!4045 Optimize the process of getSamplingProfile
Merge pull request !4045 from hunili/master
This commit is contained in:
commit
1377d22a05
@ -253,7 +253,7 @@ void HeapProfiler::StopHeapSampling()
|
||||
heapSampling_.reset();
|
||||
}
|
||||
|
||||
std::unique_ptr<struct SamplingInfo> HeapProfiler::GetAllocationProfile()
|
||||
const struct SamplingInfo *HeapProfiler::GetAllocationProfile()
|
||||
{
|
||||
if (!heapSampling_.get()) {
|
||||
LOG_ECMA(ERROR) << "Heap sampling was not started, please start firstly.";
|
||||
|
@ -51,7 +51,7 @@ public:
|
||||
bool UpdateHeapTracking(Stream *stream) override;
|
||||
bool StartHeapSampling(uint64_t samplingInterval, int stackDepth = 128) override;
|
||||
void StopHeapSampling() override;
|
||||
std::unique_ptr<struct SamplingInfo> GetAllocationProfile() override;
|
||||
const struct SamplingInfo *GetAllocationProfile() override;
|
||||
Chunk *GetChunk() const
|
||||
{
|
||||
return const_cast<Chunk *>(&chunk_);
|
||||
|
@ -45,7 +45,7 @@ public:
|
||||
virtual bool StopHeapTracking(Stream *stream, Progress *progress = nullptr, bool newThread = true) = 0;
|
||||
virtual bool StartHeapSampling(uint64_t samplingInterval, int stackDepth = 128) = 0;
|
||||
virtual void StopHeapSampling() = 0;
|
||||
virtual std::unique_ptr<struct SamplingInfo> GetAllocationProfile() = 0;
|
||||
virtual const struct SamplingInfo *GetAllocationProfile() = 0;
|
||||
|
||||
NO_MOVE_SEMANTIC(HeapProfilerInterface);
|
||||
NO_COPY_SEMANTIC(HeapProfilerInterface);
|
||||
|
@ -24,23 +24,18 @@ HeapSampling::HeapSampling(const EcmaVM *vm, Heap *const heap, uint64_t interval
|
||||
rate_(interval),
|
||||
stackDepth_(stackDepth)
|
||||
{
|
||||
rootNode_.callFrameInfo_.functionName_ = "(root)";
|
||||
rootNode_.id_ = CreateNodeId();
|
||||
samplingInfo_ = std::make_unique<struct SamplingInfo>();
|
||||
samplingInfo_->head_.callFrameInfo_.functionName_ = "(root)";
|
||||
samplingInfo_->head_.id_ = CreateNodeId();
|
||||
}
|
||||
|
||||
HeapSampling::~HeapSampling()
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<SamplingInfo> HeapSampling::GetAllocationProfile()
|
||||
const struct SamplingInfo *HeapSampling::GetAllocationProfile()
|
||||
{
|
||||
std::unique_ptr<SamplingInfo> samplingInfo = std::make_unique<SamplingInfo>();
|
||||
TransferSamplingNode(&(samplingInfo->head_), &rootNode_);
|
||||
CVector<struct Sample> &samples = samplingInfo->samples_;
|
||||
for (size_t i = 0; i < samples_.size(); ++i) {
|
||||
samples.emplace_back(*(samples_[i]));
|
||||
}
|
||||
return samplingInfo;
|
||||
return samplingInfo_.get();
|
||||
}
|
||||
|
||||
void HeapSampling::ImplementSampling([[maybe_unused]]Address addr, [[maybe_unused]]size_t size)
|
||||
@ -48,7 +43,8 @@ void HeapSampling::ImplementSampling([[maybe_unused]]Address addr, [[maybe_unuse
|
||||
GetStack();
|
||||
SamplingNode *node = PushAndGetNode();
|
||||
node->allocations_[size]++;
|
||||
samples_.emplace_back(std::make_unique<struct Sample>(size, node->id_, CreateSampleId()));
|
||||
node->selfSize_ += size;
|
||||
samplingInfo_->samples_.emplace_back(Sample(size, node->id_, CreateSampleId()));
|
||||
}
|
||||
|
||||
bool HeapSampling::PushStackInfo(const struct MethodKey &methodKey)
|
||||
@ -168,7 +164,7 @@ std::string HeapSampling::AddRunningState(char *functionName, RunningState state
|
||||
SamplingNode *HeapSampling::PushAndGetNode()
|
||||
{
|
||||
FillScriptIdAndStore();
|
||||
SamplingNode *node = &rootNode_;
|
||||
SamplingNode *node = &(samplingInfo_->head_);
|
||||
int frameLen = static_cast<int>(frameStack_.size()) - 1;
|
||||
for (; frameLen >= 0; frameLen--) {
|
||||
node = FindOrAddNode(node, frameStack_[frameLen]);
|
||||
@ -202,20 +198,6 @@ struct SamplingNode *HeapSampling::FindOrAddNode(struct SamplingNode *node, cons
|
||||
return childNode;
|
||||
}
|
||||
|
||||
void HeapSampling::TransferSamplingNode(AllocationNode* node, const SamplingNode *samplingNode)
|
||||
{
|
||||
node->callFrameInfo_ = samplingNode->callFrameInfo_;
|
||||
node->id_ = samplingNode->id_;
|
||||
for (auto &it : samplingNode->allocations_) {
|
||||
node->selfSize_ += it.first * it.second;
|
||||
}
|
||||
node->children_.resize(samplingNode->children_.size());
|
||||
int count = 0;
|
||||
for (auto &it : samplingNode->children_) {
|
||||
TransferSamplingNode(&node->children_[count++], it.second.get());
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t HeapSampling::CreateNodeId()
|
||||
{
|
||||
return ++nodeId_;
|
||||
|
@ -30,38 +30,32 @@ struct CallFrameInfo {
|
||||
};
|
||||
|
||||
struct Sample {
|
||||
Sample(size_t size, size_t nodeId, size_t ordinal)
|
||||
Sample(size_t size, uint32_t nodeId, uint64_t ordinal)
|
||||
: size_(size),
|
||||
nodeId_(nodeId),
|
||||
ordinal_(ordinal) {}
|
||||
const size_t size_;
|
||||
const size_t nodeId_;
|
||||
const size_t ordinal_;
|
||||
const uint32_t nodeId_;
|
||||
const uint64_t ordinal_;
|
||||
};
|
||||
|
||||
struct SamplingNode {
|
||||
std::map<size_t, unsigned int> allocations_;
|
||||
std::map<MethodKey, std::unique_ptr<struct SamplingNode>> children_;
|
||||
CallFrameInfo callFrameInfo_;
|
||||
size_t id_ = 0;
|
||||
};
|
||||
|
||||
struct AllocationNode {
|
||||
uint32_t id_ = 0;
|
||||
size_t selfSize_ = 0;
|
||||
CVector<struct AllocationNode> children_;
|
||||
CallFrameInfo callFrameInfo_;
|
||||
size_t id_ = 0;
|
||||
};
|
||||
|
||||
struct SamplingInfo {
|
||||
struct AllocationNode head_;
|
||||
struct SamplingNode head_;
|
||||
CVector<struct Sample> samples_;
|
||||
};
|
||||
|
||||
class HeapSampling {
|
||||
public:
|
||||
HeapSampling(const EcmaVM *vm, Heap *const heap, uint64_t interval, int stackDepth);
|
||||
std::unique_ptr<SamplingInfo> GetAllocationProfile();
|
||||
const struct SamplingInfo* GetAllocationProfile();
|
||||
void ImplementSampling(Address addr, size_t size);
|
||||
virtual ~HeapSampling();
|
||||
|
||||
@ -77,14 +71,11 @@ private:
|
||||
uint64_t CreateSampleId();
|
||||
void FillScriptIdAndStore();
|
||||
std::string AddRunningState(char *functionName, RunningState state, kungfu::DeoptType type);
|
||||
void TransferSamplingNode(AllocationNode* node,
|
||||
const SamplingNode *samplingNode);
|
||||
private:
|
||||
const EcmaVM *vm_;
|
||||
[[maybe_unused]] Heap *const heap_;
|
||||
[[maybe_unused]] uint64_t rate_;
|
||||
SamplingNode rootNode_;
|
||||
CVector<std::unique_ptr<Sample>> samples_;
|
||||
std::unique_ptr<struct SamplingInfo> samplingInfo_;
|
||||
int stackDepth_;
|
||||
uint32_t nodeId_ = 0;
|
||||
size_t sampleId_ = 0;
|
||||
|
@ -114,7 +114,8 @@ bool JsStackGetter::CheckAndCopy(char *dest, size_t length, const char *src)
|
||||
return true;
|
||||
}
|
||||
|
||||
void JsStackGetter::GetNativeStack(const EcmaVM *vm, const FrameIterator &it, char *functionName, size_t size, bool isCpuProfiler)
|
||||
void JsStackGetter::GetNativeStack(const EcmaVM *vm, const FrameIterator &it, char *functionName, size_t size,
|
||||
bool isCpuProfiler)
|
||||
{
|
||||
std::stringstream stream;
|
||||
JSFunction* function = JSFunction::Cast(it.GetFunction().GetTaggedObject());
|
||||
|
@ -416,7 +416,7 @@ bool DFXJSNApi::StartSampling([[maybe_unused]] const EcmaVM *vm, [[maybe_unused]
|
||||
#endif
|
||||
}
|
||||
|
||||
std::unique_ptr<SamplingInfo> DFXJSNApi::GetAllocationProfile([[maybe_unused]] const EcmaVM *vm)
|
||||
const SamplingInfo *DFXJSNApi::GetAllocationProfile([[maybe_unused]] const EcmaVM *vm)
|
||||
{
|
||||
#if defined(ECMASCRIPT_SUPPORT_HEAPSAMPLING)
|
||||
ecmascript::HeapProfilerInterface *heapProfile = ecmascript::HeapProfilerInterface::GetInstance(
|
||||
|
@ -83,7 +83,7 @@ public:
|
||||
static std::unique_ptr<ProfileInfo> StopCpuProfilerForInfo(const EcmaVM *vm);
|
||||
static void SetCpuSamplingInterval(const EcmaVM *vm, int interval);
|
||||
static bool StartSampling(const EcmaVM *vm, uint64_t samplingInterval);
|
||||
static std::unique_ptr<SamplingInfo> GetAllocationProfile(const EcmaVM *vm);
|
||||
static const SamplingInfo *GetAllocationProfile(const EcmaVM *vm);
|
||||
static void StopSampling(const EcmaVM *vm);
|
||||
|
||||
static void ResumeVM(const EcmaVM *vm);
|
||||
|
Loading…
x
Reference in New Issue
Block a user