• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 ECMASCRIPT_DFX_VMSTAT_RUNTIME_STAT_H
17 #define ECMASCRIPT_DFX_VMSTAT_RUNTIME_STAT_H
18 
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/js_thread.h"
21 #include "ecmascript/mem/c_containers.h"
22 #include "ecmascript/dfx/vmstat/caller_stat.h"
23 
24 namespace panda::ecmascript {
25 class EcmaRuntimeStat {
26 public:
27     // NOLINTNEXTLINE(modernize-avoid-c-arrays)
28     EcmaRuntimeStat(const char * const runtimeCallerNames[], int count);
29     EcmaRuntimeStat() = default;
30     virtual ~EcmaRuntimeStat() = default;
31 
32     DEFAULT_NOEXCEPT_MOVE_SEMANTIC(EcmaRuntimeStat);
33     DEFAULT_COPY_SEMANTIC(EcmaRuntimeStat);
34 
SetRuntimeStatEnabled(bool enable)35     void SetRuntimeStatEnabled(bool enable)
36     {
37         runtimeStatEnabled_ = enable;
38     }
IsRuntimeStatEnabled()39     bool IsRuntimeStatEnabled()
40     {
41         return runtimeStatEnabled_;
42     }
43     void StartCount(PandaRuntimeTimer *timer, int callerId);
44     void StopCount(const PandaRuntimeTimer *timer);
45     void PrintAllStats() const;
46     void ResetAllCount();
47     void Print() const;
48 
49 private:
50     bool runtimeStatEnabled_ {false};
51     PandaRuntimeTimer *currentTimer_ = nullptr;
52     CVector<PandaRuntimeCallerStat> callerStat_ {};
53 };
54 
55 class EcmaRuntimeStatScope {
56 public:
57     explicit EcmaRuntimeStatScope(EcmaVM *vm);
58     virtual ~EcmaRuntimeStatScope();
59 
60 private:
61     EcmaVM *vm_ = nullptr;
62 };
63 
64 class RuntimeTimerScope {
65 public:
RuntimeTimerScope(int callerId,EcmaRuntimeStat * stat)66     RuntimeTimerScope(int callerId, EcmaRuntimeStat *stat)
67     {
68         if (stat == nullptr || !stat->IsRuntimeStatEnabled()) {
69             return;
70         }
71         stats_ = stat;
72         stats_->StartCount(&timer_, callerId);
73     }
~RuntimeTimerScope()74     ~RuntimeTimerScope()
75     {
76         if (stats_ != nullptr) {
77             stats_->StopCount(&timer_);
78         }
79     }
80     NO_COPY_SEMANTIC(RuntimeTimerScope);
81     NO_MOVE_SEMANTIC(RuntimeTimerScope);
82 
83 private:
84     PandaRuntimeTimer timer_ {};
85     EcmaRuntimeStat *stats_ {nullptr};
86 };
87 }  // namespace panda::ecmascript
88 #endif // ECMASCRIPT_DFX_VMSTAT_RUNTIME_STAT_H
89