1 /* 2 * Copyright (c) 2021 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 ECMASCRIPT_VMSTAT_CALLER_STAT_H 17 #define ECMASCRIPT_VMSTAT_CALLER_STAT_H 18 19 #include <cstdint> 20 #include <string> 21 #include <time.h> // NOLINTNEXTLINE(modernize-deprecated-headers) 22 23 #include "ecmascript/mem/c_string.h" 24 #include "libpandabase/macros.h" 25 26 namespace panda::ecmascript { 27 class EcmaRuntimeStat; 28 class PandaRuntimeCallerStat { 29 public: 30 // NOLINTNEXTLINE(modernize-pass-by-value) PandaRuntimeCallerStat(const CString & name)31 explicit PandaRuntimeCallerStat(const CString &name) : name_(name) {} 32 PandaRuntimeCallerStat() = default; 33 virtual ~PandaRuntimeCallerStat() = default; 34 35 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(PandaRuntimeCallerStat); 36 DEFAULT_COPY_SEMANTIC(PandaRuntimeCallerStat); 37 UpdateState(uint64_t elapsed)38 void UpdateState(uint64_t elapsed) 39 { 40 totalCount_++; 41 totalTime_ += elapsed; 42 maxTime_ = elapsed < maxTime_ ? maxTime_ : elapsed; 43 } Name()44 const char *Name() const 45 { 46 return name_.c_str(); 47 } TotalCount()48 uint64_t TotalCount() const 49 { 50 return totalCount_; 51 } TotalTime()52 uint64_t TotalTime() const 53 { 54 return totalTime_; 55 } MaxTime()56 uint64_t MaxTime() const 57 { 58 return maxTime_; 59 } 60 Reset()61 void Reset() 62 { 63 totalCount_ = 0; 64 totalTime_ = 0; 65 maxTime_ = 0; 66 } 67 68 private: 69 CString name_{}; 70 uint64_t totalCount_{0}; 71 uint64_t totalTime_{0}; 72 uint64_t maxTime_{0}; 73 }; 74 75 class PandaRuntimeTimer { 76 public: 77 void Start(PandaRuntimeCallerStat *callerStat, PandaRuntimeTimer *parent); Now()78 inline static uint64_t Now() 79 { 80 struct timespec timeNow = {0, 0}; 81 clock_gettime(CLOCK_REALTIME, &timeNow); 82 return timeNow.tv_sec * NANOSECONDSINSECOND + timeNow.tv_nsec; 83 } 84 Elapsed()85 uint64_t Elapsed() const 86 { 87 return elapsed_; 88 } 89 IsStarted()90 inline bool IsStarted() const 91 { 92 return start_ != 0; 93 } 94 SetParent(PandaRuntimeTimer * parent)95 inline void SetParent(PandaRuntimeTimer *parent) 96 { 97 parent_ = parent; 98 } 99 100 void Snapshot(); 101 UpdateCallerState()102 inline void UpdateCallerState() 103 { 104 callerStat_->UpdateState(elapsed_); 105 } 106 107 private: 108 static constexpr uint64_t NANOSECONDSINSECOND = 1000000000; 109 PandaRuntimeTimer *Stop(); 110 void Pause(uint64_t now); 111 void Resume(uint64_t now); 112 PandaRuntimeCallerStat *callerStat_{nullptr}; 113 PandaRuntimeTimer *parent_{nullptr}; 114 uint64_t start_{0}; 115 uint64_t elapsed_{0}; 116 117 friend class EcmaRuntimeStat; 118 }; 119 } // namespace panda::ecmascript 120 #endif 121