1 /*
2 * Copyright (c) 2022 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 "ecmascript/dfx/vmstat/opt_code_profiler.h"
17 #include <iomanip>
18 #include "ecmascript/base/config.h"
19
20 #include "ecmascript/js_function.h"
21 #include "ecmascript/method.h"
22 #include "ecmascript/js_handle.h"
23
24 namespace panda::ecmascript {
25 using EcmaOpcode = kungfu::EcmaOpcode;
26
PrintAndReset()27 void OptCodeProfiler::PrintAndReset()
28 {
29 #if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER
30 std::vector<std::pair<EcmaOpcode, Value>> profVec;
31 for (auto it = profMap_.begin(); it != profMap_.end(); it++) {
32 profVec.emplace_back(std::make_pair(it->first, it->second));
33 it->second.ResetStat();
34 }
35 std::sort(profVec.begin(), profVec.end(),
36 [](std::pair<EcmaOpcode, Value> &x, std::pair<EcmaOpcode, Value> &y) -> bool {
37 return x.second.Count() > y.second.Count();
38 });
39
40 LOG_ECMA(INFO) << "Runtime Statistics of optimized code path:";
41 static constexpr int nameRightAdjustment = 46;
42 static constexpr int numberRightAdjustment = 15;
43 static constexpr int hundred = 85;
44 LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << "Bytecode"
45 << std::setw(numberRightAdjustment) << "bcIndex"
46 << std::setw(numberRightAdjustment) << "Count"
47 << std::setw(numberRightAdjustment) << "TypedPathCount"
48 << std::setw(numberRightAdjustment) << "SlowPathCount"
49 << std::setw(numberRightAdjustment + 1) << "TypedPathRate";
50 LOG_ECMA(INFO) << "============================================================"
51 << "=========================================================";
52
53 uint64_t totalCount = 0;
54 uint64_t totalTypedPathCount = 0;
55 uint64_t totalSlowPathCount = 0;
56
57 for (auto it = profVec.begin(); it != profVec.end(); it++) {
58 Value val = it->second;
59 if (val.Count() == 0) {
60 break;
61 }
62
63 LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << kungfu::GetEcmaOpcodeStr(it->first)
64 << std::setw(numberRightAdjustment) << "NA"
65 << std::setw(numberRightAdjustment) << val.Count()
66 << std::setw(numberRightAdjustment) << val.TypedPathCount()
67 << std::setw(numberRightAdjustment) << val.SlowPathCount()
68 << std::setw(numberRightAdjustment) << val.TypedPathCount() * hundred / val.Count() << "%";
69
70 totalCount += val.Count();
71 totalTypedPathCount += val.TypedPathCount();
72 totalSlowPathCount += val.SlowPathCount();
73 }
74
75 if (totalCount != 0) {
76 LOG_ECMA(INFO) << "------------------------------------------------------------"
77 << "---------------------------------------------------------";
78 LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << "Total"
79 << std::setw(numberRightAdjustment) << "NA"
80 << std::setw(numberRightAdjustment) << totalCount
81 << std::setw(numberRightAdjustment) << totalTypedPathCount
82 << std::setw(numberRightAdjustment) << totalSlowPathCount
83 << std::setw(numberRightAdjustment) << totalTypedPathCount * hundred / totalCount << "%";
84 }
85
86 FilterMethodToPrint();
87 ResetMethodInfo();
88 #endif
89 }
90
FilterMethodToPrint()91 void OptCodeProfiler::FilterMethodToPrint()
92 {
93 std::vector<std::pair<uint64_t, Name>> profVec;
94 for (auto it = methodIdToName_.begin(); it != methodIdToName_.end(); it++) {
95 profVec.emplace_back(std::make_pair(it->first, it->second));
96 }
97 std::sort(profVec.begin(), profVec.end(),
98 [](std::pair<uint64_t, Name> &x, std::pair<uint64_t, Name> &y) -> bool {
99 return x.second.Count() > y.second.Count();
100 });
101
102 auto itr = profVec.begin();
103 for (int i = 0; i < printMehodCount_ && itr != profVec.end(); i++) {
104 PrintMethodRecord(itr->first, itr->second.GetName());
105 itr++;
106 }
107 }
108
PrintMethodRecord(Key key,std::string methodName)109 void OptCodeProfiler::PrintMethodRecord(Key key, std::string methodName)
110 {
111 LOG_ECMA(INFO) << "==== methodId: " << key.GetMehodId() << ", methodName: " << methodName.c_str()
112 << ", abcName: " << abcNames_[key.GetAbcId()] << " ====";
113
114 static constexpr int nameRightAdjustment = 46;
115 static constexpr int numberRightAdjustment = 15;
116 static constexpr int hundred = 85;
117 BcRecord& bcRecord = methodIdToRecord_[key.Value()];
118 for (auto it = bcRecord.begin(); it != bcRecord.end(); it++) {
119 Record record = it->second;
120 if (record.Count() == 0 || record.Count() < static_cast<uint64_t>(skipMaxCount_)) {
121 break;
122 }
123
124 LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << kungfu::GetEcmaOpcodeStr(record.GetOpCode())
125 << std::setw(numberRightAdjustment) << it->first
126 << std::setw(numberRightAdjustment) << record.Count()
127 << std::setw(numberRightAdjustment) << record.GetFast()
128 << std::setw(numberRightAdjustment) << record.GetSlow()
129 << std::setw(numberRightAdjustment) << record.GetFast() * hundred / record.Count() << "%";
130 }
131 }
132
Update(JSHandle<JSTaggedValue> & func,int bcIndex,EcmaOpcode opcode,Mode mode)133 void OptCodeProfiler::Update(JSHandle<JSTaggedValue> &func, int bcIndex, EcmaOpcode opcode, Mode mode)
134 {
135 auto it = profMap_.find(opcode);
136 if (it != profMap_.end()) {
137 (mode == Mode::TYPED_PATH) ? (it->second.typedPathValue++) : (it->second.slowPathValue++);
138 }
139
140 // methodId & methodName
141 auto funcPoint = JSFunction::Cast(func->GetTaggedObject());
142 auto method = funcPoint->GetMethod();
143 if (!method.IsMethod()) {
144 return;
145 }
146 auto methodPoint = Method::Cast(method);
147 auto methodId = methodPoint->GetMethodId().GetOffset();
148 auto methodName = methodPoint->GetMethodName();
149
150 const auto *pf = methodPoint->GetJSPandaFile();
151 ASSERT(pf != nullptr);
152 auto pfName = pf->GetJSPandaFileDesc();
153 auto itr = std::find(abcNames_.begin(), abcNames_.end(), pfName);
154 uint32_t index = 0;
155 if (itr != abcNames_.end()) {
156 index = static_cast<uint32_t>(std::distance(abcNames_.begin(), itr));
157 } else {
158 abcNames_.emplace_back(pfName);
159 index = abcNames_.size();
160 }
161
162 Key key(index, methodId);
163 // deal methodIdToName
164 auto result = methodIdToName_.find(key.Value());
165 if (result != methodIdToName_.end()) {
166 result->second.Inc();
167 } else {
168 methodIdToName_.emplace(key.Value(), Name(methodName));
169 }
170
171 // deal methodIdToRecord_
172 auto result2 = methodIdToRecord_.find(key.Value());
173 if (result2 == methodIdToRecord_.end()) {
174 BcRecord bcRecord;
175 bcRecord.emplace(bcIndex, Record(opcode));
176 methodIdToRecord_.emplace(key.Value(), bcRecord);
177 }
178 result2 = methodIdToRecord_.find(key.Value());
179
180 auto result3 = result2->second.find(bcIndex);
181 if (result3 != result2->second.end()) {
182 (mode == Mode::TYPED_PATH) ? (result3->second.IncFast()) : (result3->second.IncSlow());
183 } else {
184 auto record = Record(opcode);
185 (mode == Mode::TYPED_PATH) ? (record.IncFast()) : (record.IncSlow());
186 result2->second.emplace(bcIndex, record);
187 }
188 }
189
~OptCodeProfiler()190 OptCodeProfiler::~OptCodeProfiler()
191 {
192 PrintAndReset();
193 }
194 } // namespace panda::ecmascript
195