1 /* 2 * Copyright (c) 2023 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_STATE_MGMT_PROFILER_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_STATE_MGMT_PROFILER_H 18 19 #include <memory> 20 21 #include "base/memory/ace_type.h" 22 #include "base/memory/referenced.h" 23 #include "frameworks/bridge/declarative_frontend/engine/bindings.h" 24 25 namespace OHOS::Ace::Framework { 26 class JSStateMgmtProfiler : public AceType { 27 DECLARE_ACE_TYPE(JSStateMgmtProfiler, AceType); 28 29 public: 30 explicit JSStateMgmtProfiler(std::string profilerPackage); 31 ~JSStateMgmtProfiler() = default; 32 33 static void JSBind(BindingTarget globalObj); 34 static void ConstructorCallback(const JSCallbackInfo& info); 35 void Begin(const std::string& blockName); 36 void End(); 37 void Report(); 38 void Clear(); 39 40 private: 41 class ProfileBlock { 42 public: ProfileBlock(std::string blockName)43 ProfileBlock(std::string blockName) : name_(blockName), startTime_(0ULL), totalTime_(0ULL), 44 ownLookupTime_(0ULL), numberOfCalls_(0u) 45 {} 46 ~ProfileBlock() = default; 47 GetOrCreateChild(const std::string name)48 std::shared_ptr<ProfileBlock> GetOrCreateChild(const std::string name) 49 { 50 for (const auto& child : childrenBlocks_) { 51 if (child->name_ == name) { 52 return child; 53 } 54 } 55 auto block = std::make_shared<ProfileBlock>(name); 56 childrenBlocks_.push_back(block); 57 return block; 58 } 59 60 void Report(int32_t depth) const; 61 StartTime()62 uint64_t StartTime() const 63 { 64 return startTime_; 65 } TotalTime()66 uint64_t TotalTime() const 67 { 68 return totalTime_; 69 } OwnLookupTime()70 uint64_t OwnLookupTime() const 71 { 72 return ownLookupTime_; 73 } Name()74 std::string Name() const 75 { 76 return name_; 77 } 78 SetOwnLookupTime(uint64_t lookupTime)79 void SetOwnLookupTime(uint64_t lookupTime) 80 { 81 ownLookupTime_ = lookupTime; 82 } SetStartTime(uint64_t startTime)83 void SetStartTime(uint64_t startTime) 84 { 85 startTime_ = startTime; 86 } SetTotalTime(uint64_t totalTime)87 void SetTotalTime(uint64_t totalTime) 88 { 89 totalTime_ = totalTime; 90 } IncreaseCalls()91 void IncreaseCalls() 92 { 93 numberOfCalls_++; 94 } 95 96 private: 97 std::vector<std::shared_ptr<ProfileBlock>> childrenBlocks_; 98 std::string name_; 99 uint64_t startTime_; 100 uint64_t totalTime_; 101 uint64_t ownLookupTime_; 102 uint32_t numberOfCalls_; 103 }; 104 std::vector<std::shared_ptr<ProfileBlock>> rootBlocks_; 105 std::stack<std::shared_ptr<ProfileBlock>> currentBlocks_; 106 std::string profilerPackage_; 107 }; 108 } // namespace OHOS::Ace::Framework 109 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_STATE_MGMT_PROFILER_H 110