• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace panda::ecmascript {
21 using EcmaOpcode = kungfu::EcmaOpcode;
22 
~OptCodeProfiler()23 OptCodeProfiler::~OptCodeProfiler()
24 {
25 #if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER
26     std::vector<std::pair<EcmaOpcode, Value>> profVec;
27     for (auto it = profMap_.begin(); it != profMap_.end(); it++) {
28         profVec.emplace_back(std::make_pair(it->first, it->second));
29     }
30     std::sort(profVec.begin(), profVec.end(),
31               [](std::pair<EcmaOpcode, Value> &x, std::pair<EcmaOpcode, Value> &y) -> bool {
32                   return x.second.Count() > y.second.Count();
33               });
34 
35     LOG_ECMA(INFO) << "Runtime Statistics of optimized code path:";
36     static constexpr int nameRightAdjustment = 46;
37     static constexpr int numberRightAdjustment = 15;
38     static constexpr int hundred = 100;
39     LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << "Bytecode"
40                    << std::setw(numberRightAdjustment) << "Count"
41                    << std::setw(numberRightAdjustment) << "TypedPathCount"
42                    << std::setw(numberRightAdjustment) << "SlowPathCount"
43                    << std::setw(numberRightAdjustment + 1) << "TypedPathRate";
44     LOG_ECMA(INFO) << "============================================================"
45                    << "=========================================================";
46 
47     uint64_t totalCount = 0;
48     uint64_t totalTypedPathCount = 0;
49     uint64_t totalSlowPathCount = 0;
50 
51     for (auto it = profVec.begin(); it != profVec.end(); it++) {
52         Value val = it->second;
53         if (val.Count() == 0) {
54             break;
55         }
56 
57         LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << kungfu::GetEcmaOpcodeStr(it->first)
58                        << std::setw(numberRightAdjustment) << val.Count()
59                        << std::setw(numberRightAdjustment) << val.TypedPathCount()
60                        << std::setw(numberRightAdjustment) << val.SlowPathCount()
61                        << std::setw(numberRightAdjustment) << val.TypedPathCount() * hundred / val.Count() << "%";
62 
63         totalCount += val.Count();
64         totalTypedPathCount += val.TypedPathCount();
65         totalSlowPathCount += val.SlowPathCount();
66     }
67 
68     if (totalCount != 0) {
69         LOG_ECMA(INFO) << "------------------------------------------------------------"
70                        << "---------------------------------------------------------";
71         LOG_ECMA(INFO) << std::right << std::setw(nameRightAdjustment) << "Total"
72                        << std::setw(numberRightAdjustment) << totalCount
73                        << std::setw(numberRightAdjustment) << totalTypedPathCount
74                        << std::setw(numberRightAdjustment) << totalSlowPathCount
75                        << std::setw(numberRightAdjustment) << totalTypedPathCount * hundred / totalCount << "%";
76     }
77 #endif
78 }
79 } // namespace panda::ecmascript