• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RUNTIME_STAT_H
17 #define ECMASCRIPT_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/vmstat/caller_stat.h"
23 
24 namespace panda::ecmascript {
25 class EcmaRuntimeStat {
26 public:
27     // NOLINTNEXTLINE(modernize-avoid-c-arrays)
28     explicit 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 
35     void StartCount(PandaRuntimeTimer *timer, int callerId);
36     void StopCount(const PandaRuntimeTimer *timer);
37     CString GetAllStats() const;
38     void ResetAllCount();
39     void Print() const;
40 
41 private:
42     PandaRuntimeTimer *currentTimer_ = nullptr;
43     CVector<PandaRuntimeCallerStat> callerStat_{};
44 };
45 
46 class RuntimeTimerScope {
47 public:
RuntimeTimerScope(JSThread * thread,int callerId,EcmaRuntimeStat * stat)48     explicit RuntimeTimerScope(JSThread *thread, int callerId, EcmaRuntimeStat *stat)
49     {
50         bool statEnabled = thread->GetEcmaVM()->IsRuntimeStatEnabled();
51         if (!statEnabled || stat == nullptr) {
52             return;
53         }
54         stats_ = stat;
55         stats_->StartCount(&timer_, callerId);
56     }
RuntimeTimerScope(const EcmaVM * vm,int callerId,EcmaRuntimeStat * stat)57     RuntimeTimerScope(const EcmaVM *vm, int callerId, EcmaRuntimeStat *stat)
58     {
59         bool statEnabled = vm->IsRuntimeStatEnabled();
60         if (!statEnabled || stat == nullptr) {
61             return;
62         }
63         stats_ = stat;
64         stats_->StartCount(&timer_, callerId);
65     }
~RuntimeTimerScope()66     ~RuntimeTimerScope()
67     {
68         if (stats_ != nullptr) {
69             stats_->StopCount(&timer_);
70         }
71     }
72     NO_COPY_SEMANTIC(RuntimeTimerScope);
73     NO_MOVE_SEMANTIC(RuntimeTimerScope);
74 
75 private:
76     PandaRuntimeTimer timer_{};
77     EcmaRuntimeStat *stats_{nullptr};
78 };
79 }  // namespace panda::ecmascript
80 #endif
81