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 #include "runtime_stat.h"
17
18 #include <iomanip>
19
20 #include "ecmascript/ecma_macros.h"
21 #include "ecmascript/runtime_call_id.h"
22
23 namespace panda::ecmascript {
24 // NOLINTNEXTLINE(modernize-avoid-c-arrays)
EcmaRuntimeStat(const char * const runtimeCallerNames[],int count)25 EcmaRuntimeStat::EcmaRuntimeStat(const char *const runtimeCallerNames[], int count)
26 {
27 for (int i = 0; i < count; i++) {
28 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
29 callerStat_.emplace_back(PandaRuntimeCallerStat(CString(runtimeCallerNames[i])));
30 }
31 }
32
StartCount(PandaRuntimeTimer * timer,int callerId)33 void EcmaRuntimeStat::StartCount(PandaRuntimeTimer *timer, int callerId)
34 {
35 if (currentTimer_ != nullptr) {
36 timer->SetParent(currentTimer_);
37 }
38 PandaRuntimeTimer *parent = currentTimer_;
39 currentTimer_ = timer;
40 PandaRuntimeCallerStat *callerStat = &callerStat_[callerId];
41 timer->Start(callerStat, parent);
42 }
43
StopCount(const PandaRuntimeTimer * nowTimer)44 void EcmaRuntimeStat::StopCount(const PandaRuntimeTimer *nowTimer)
45 {
46 if (nowTimer != currentTimer_) {
47 return;
48 }
49 PandaRuntimeTimer *parentTimer = currentTimer_->Stop();
50 currentTimer_ = parentTimer;
51 }
52
Print() const53 void EcmaRuntimeStat::Print() const
54 {
55 if (currentTimer_ != nullptr) {
56 currentTimer_->Snapshot();
57 }
58 LOG_ECMA(ERROR) << GetAllStats();
59 }
60
ResetAllCount()61 void EcmaRuntimeStat::ResetAllCount()
62 {
63 while (currentTimer_ != nullptr) {
64 StopCount(currentTimer_);
65 }
66 for (auto &runCallerStat : callerStat_) {
67 runCallerStat.Reset();
68 }
69 }
70
GetAllStats() const71 CString EcmaRuntimeStat::GetAllStats() const
72 {
73 CStringStream statistic;
74 statistic << "panda runtime stat:" << std::endl;
75 static constexpr int nameRightAdjustment = 50;
76 static constexpr int numberRightAdjustment = 20;
77 statistic << std::right << std::setw(nameRightAdjustment) << "InterPreter && GC && C++ Builtin Function"
78 << std::setw(numberRightAdjustment) << "Time(ns)" << std::setw(numberRightAdjustment) << "Count"
79 << std::setw(numberRightAdjustment) << "MaxTime(ns)"
80 << std::setw(numberRightAdjustment) << "AverageTime(ns)" << std::endl;
81
82 statistic << "==========================================================================================="
83 << "=======================================" << std::endl;
84 for (auto &runCallerStat : callerStat_) {
85 if (runCallerStat.TotalCount() != 0) {
86 statistic << std::right << std::setw(nameRightAdjustment) << runCallerStat.Name()
87 << std::setw(numberRightAdjustment) << runCallerStat.TotalTime()
88 << std::setw(numberRightAdjustment) << runCallerStat.TotalCount()
89 << std::setw(numberRightAdjustment) << runCallerStat.MaxTime()
90 << std::setw(numberRightAdjustment) << runCallerStat.TotalTime() / runCallerStat.TotalCount()
91 << std::endl;
92 }
93 }
94 return statistic.str();
95 }
96 } // namespace panda::ecmascript
97