14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_DFX_HPROF_HEAP_SAMPLING_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_DFX_HPROF_HEAP_SAMPLING_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/dfx/stackinfo/js_stackgetter.h" 204514f5e3Sopenharmony_ci#include "ecmascript/mem/heap.h" 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_cinamespace panda::ecmascript { 234514f5e3Sopenharmony_cistruct CallFrameInfo { 244514f5e3Sopenharmony_ci std::string codeType_ = ""; 254514f5e3Sopenharmony_ci std::string functionName_ = ""; 264514f5e3Sopenharmony_ci std::string moduleName_ = ""; 274514f5e3Sopenharmony_ci int columnNumber_ = -1; 284514f5e3Sopenharmony_ci int lineNumber_ = -1; 294514f5e3Sopenharmony_ci int scriptId_ = 0; 304514f5e3Sopenharmony_ci std::string url_ = ""; 314514f5e3Sopenharmony_ci}; 324514f5e3Sopenharmony_ci 334514f5e3Sopenharmony_cistruct Sample { 344514f5e3Sopenharmony_ci Sample(size_t size, uint32_t nodeId, uint64_t ordinal, unsigned int count) 354514f5e3Sopenharmony_ci : size_(size), 364514f5e3Sopenharmony_ci nodeId_(nodeId), 374514f5e3Sopenharmony_ci ordinal_(ordinal), 384514f5e3Sopenharmony_ci count_(count) {} 394514f5e3Sopenharmony_ci const size_t size_; 404514f5e3Sopenharmony_ci const uint32_t nodeId_; 414514f5e3Sopenharmony_ci const uint64_t ordinal_; 424514f5e3Sopenharmony_ci const unsigned int count_; 434514f5e3Sopenharmony_ci}; 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_cistruct SamplingNode { 464514f5e3Sopenharmony_ci std::map<size_t, unsigned int> allocations_; 474514f5e3Sopenharmony_ci std::map<MethodKey, std::unique_ptr<struct SamplingNode>> children_; 484514f5e3Sopenharmony_ci CallFrameInfo callFrameInfo_; 494514f5e3Sopenharmony_ci uint32_t id_ = 0; 504514f5e3Sopenharmony_ci size_t selfSize_ = 0; 514514f5e3Sopenharmony_ci}; 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_cistruct SamplingInfo { 544514f5e3Sopenharmony_ci struct SamplingNode head_; 554514f5e3Sopenharmony_ci CVector<struct Sample> samples_; 564514f5e3Sopenharmony_ci}; 574514f5e3Sopenharmony_ci 584514f5e3Sopenharmony_ciclass HeapSampling { 594514f5e3Sopenharmony_cipublic: 604514f5e3Sopenharmony_ci HeapSampling(const EcmaVM *vm, Heap *const heap, uint64_t interval, int stackDepth); 614514f5e3Sopenharmony_ci const struct SamplingInfo* GetAllocationProfile(); 624514f5e3Sopenharmony_ci void ImplementSampling([[maybe_unused]] Address addr, size_t size); 634514f5e3Sopenharmony_ci virtual ~HeapSampling(); 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_ciprivate: 664514f5e3Sopenharmony_ci void GetStack(); 674514f5e3Sopenharmony_ci struct SamplingNode *PushAndGetNode(); 684514f5e3Sopenharmony_ci CallFrameInfo GetMethodInfo(const MethodKey &methodKey); 694514f5e3Sopenharmony_ci struct SamplingNode *FindOrAddNode(struct SamplingNode *node, const MethodKey &methodKey); 704514f5e3Sopenharmony_ci bool PushStackInfo(const struct MethodKey &methodKey); 714514f5e3Sopenharmony_ci bool PushFrameInfo(const FrameInfoTemp &frameInfoTemp); 724514f5e3Sopenharmony_ci void ResetFrameLength(); 734514f5e3Sopenharmony_ci uint32_t CreateNodeId(); 744514f5e3Sopenharmony_ci uint64_t CreateSampleId(); 754514f5e3Sopenharmony_ci void FillScriptIdAndStore(); 764514f5e3Sopenharmony_ci std::string AddRunningState(char *functionName, RunningState state, kungfu::DeoptType type); 774514f5e3Sopenharmony_ci unsigned int AdjustSampleCount(size_t size, unsigned int count) const; 784514f5e3Sopenharmony_ci void CalNodeSelfSize(SamplingNode *node); 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ciprivate: 814514f5e3Sopenharmony_ci const EcmaVM *vm_; 824514f5e3Sopenharmony_ci [[maybe_unused]] Heap *const heap_; 834514f5e3Sopenharmony_ci [[maybe_unused]] uint64_t rate_; 844514f5e3Sopenharmony_ci std::unique_ptr<struct SamplingInfo> samplingInfo_; 854514f5e3Sopenharmony_ci int stackDepth_; 864514f5e3Sopenharmony_ci AllocationInspector allocationInspector_; 874514f5e3Sopenharmony_ci uint32_t nodeId_ = 0; 884514f5e3Sopenharmony_ci size_t sampleId_ = 0; 894514f5e3Sopenharmony_ci CMap<struct MethodKey, struct CallFrameInfo> stackInfoMap_; 904514f5e3Sopenharmony_ci CVector<struct MethodKey> frameStack_; 914514f5e3Sopenharmony_ci CMap<std::string, int> scriptIdMap_; 924514f5e3Sopenharmony_ci CVector<FrameInfoTemp> frameInfoTemps_; 934514f5e3Sopenharmony_ci}; 944514f5e3Sopenharmony_ci} // namespace panda::ecmascript 954514f5e3Sopenharmony_ci#endif // ECMASCRIPT_DFX_HPROF_HEAP_SAMPLING_H