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