1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_RUNTIME_TOOLING_SAMPLER_SAMPLE_RECORD_H 17 #define PANDA_RUNTIME_TOOLING_SAMPLER_SAMPLE_RECORD_H 18 19 #include <map> 20 #include <string> 21 #include <unordered_map> 22 23 #include "os/mutex.h" 24 #include "sample_info.h" 25 26 namespace ark::tooling::sampler { 27 const int MAX_NODE_COUNT = 20000; // 20000:the maximum size of the array 28 29 struct FrameInfo { 30 int scriptId = 0; 31 int lineNumber = -1; 32 int columnNumber = -1; 33 std::string functionName; 34 std::string moduleName; 35 std::string url; 36 }; 37 38 struct CpuProfileNode { 39 int id = 0; 40 int parentId = 0; 41 int hitCount = 0; 42 FrameInfo codeEntry; 43 std::vector<int> children; 44 }; 45 46 struct ProfileInfo { 47 uint64_t tid = 0; 48 uint64_t startTime = 0; 49 uint64_t stopTime = 0; 50 std::array<CpuProfileNode, MAX_NODE_COUNT> nodes; 51 int nodeCount = 0; 52 std::vector<int> samples; 53 std::vector<int> timeDeltas; 54 // state time statistic 55 uint64_t gcTime = 0; 56 uint64_t cInterpreterTime = 0; 57 uint64_t asmInterpreterTime = 0; 58 uint64_t aotTime = 0; 59 uint64_t builtinTime = 0; 60 uint64_t napiTime = 0; 61 uint64_t arkuiEngineTime = 0; 62 uint64_t runtimeTime = 0; 63 uint64_t otherTime = 0; 64 }; 65 66 struct MethodKey { 67 // NOLINTBEGIN(misc-non-private-member-variables-in-classes,-warnings-as-errors) 68 SampleInfo::ManagedStackFrameId frameId; 69 int lineNumber = -1; 70 // NOLINTEND(misc-non-private-member-variables-in-classes,-warnings-as-errors) 71 bool operator<(const MethodKey &methodKey) const 72 { 73 return std::tie(lineNumber, frameId) < std::tie(methodKey.lineNumber, methodKey.frameId); 74 } 75 }; 76 77 struct NodeKey { 78 // NOLINTBEGIN(misc-non-private-member-variables-in-classes,-warnings-as-errors) 79 MethodKey methodKey; 80 int parentId = 0; 81 // NOLINTEND(misc-non-private-member-variables-in-classes,-warnings-as-errors) 82 bool operator<(const NodeKey &nodeKey) const 83 { 84 return std::tie(parentId, methodKey) < std::tie(nodeKey.parentId, nodeKey.methodKey); 85 } 86 }; 87 88 class SamplesRecord { 89 public: 90 SamplesRecord() = default; 91 PANDA_PUBLIC_API std::unique_ptr<std::vector<std::unique_ptr<ProfileInfo>>> GetAllThreadsProfileInfos(); SetThreadStartTime(uint64_t threadStartTime)92 void SetThreadStartTime(uint64_t threadStartTime) 93 { 94 threadStartTime_ = threadStartTime; 95 }; AddSampleInfo(uint32_t threadId,std::unique_ptr<SampleInfo> sampleInfo)96 void AddSampleInfo(uint32_t threadId, std::unique_ptr<SampleInfo> sampleInfo) 97 { 98 os::memory::LockHolder holder(addSamplInfoLock_); 99 tidToSampleInfosMap_[threadId].emplace_back(std::move(sampleInfo)); 100 }; 101 102 private: 103 void NodeInit(ProfileInfo &profileInfo); 104 using SampleInfoVector = std::vector<std::unique_ptr<SampleInfo>>; 105 std::unique_ptr<ProfileInfo> GetSingleThreadProfileInfo(const SampleInfoVector &sampleInfos); 106 void BuildStackInfoMap(const SampleInfo &sampleInfo); 107 FrameInfo *GetFrameInfoByFrameId(const SampleInfo::ManagedStackFrameId &frameId); 108 void ProcessSingleCallStackData(const SampleInfo &sampleInfo, ProfileInfo &profileInfo, uint64_t &prevTimeStamp); 109 110 uint64_t threadStartTime_ = 0; 111 os::memory::Mutex addSamplInfoLock_; 112 std::map<NodeKey, int> nodesMap_ = {}; 113 std::map<std::string, size_t> scriptIdMap_ = {}; 114 std::map<SampleInfo::ManagedStackFrameId, FrameInfo> stackInfoMap_; 115 std::unordered_map<uint32_t, SampleInfoVector> tidToSampleInfosMap_ = {}; 116 }; 117 } // namespace ark::tooling::sampler 118 119 #endif // PANDA_RUNTIME_TOOLING_SAMPLER_SAMPLE_RECORD_H 120