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 #ifndef ECMASCRIPT_DFX_VMSTAT_OPT_CODE_PROFILER_H 16 #define ECMASCRIPT_DFX_VMSTAT_OPT_CODE_PROFILER_H 17 18 #include "ecmascript/compiler/bytecodes.h" 19 #include "ecmascript/compiler/ecma_opcode_des.h" 20 21 namespace panda::ecmascript { 22 class OptCodeProfiler { 23 public: 24 using EcmaOpcode = kungfu::EcmaOpcode; 25 26 enum Mode { 27 TYPED_PATH, 28 SLOW_PATH, 29 }; 30 31 struct Value { ValueValue32 Value() : typedPathValue(0), slowPathValue(0) {} 33 TypedPathCountValue34 uint64_t TypedPathCount() const 35 { 36 return typedPathValue; 37 } 38 SlowPathCountValue39 uint64_t SlowPathCount() const 40 { 41 return slowPathValue; 42 } 43 CountValue44 uint64_t Count() const 45 { 46 return typedPathValue + slowPathValue; 47 } 48 ResetStatValue49 void ResetStat() 50 { 51 typedPathValue = 0; 52 slowPathValue = 0; 53 } 54 55 uint64_t typedPathValue; 56 uint64_t slowPathValue; 57 }; 58 OptCodeProfiler()59 OptCodeProfiler() 60 { 61 #if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER 62 profMap_ = { 63 #define BYTECODE_PROF_MAP(name) { kungfu::EcmaOpcode::name, OptCodeProfiler::Value() }, 64 ECMA_OPCODE_LIST(BYTECODE_PROF_MAP) 65 #undef BYTECODE_PROF_MAP 66 }; 67 #endif 68 } 69 70 ~OptCodeProfiler(); 71 Update(EcmaOpcode opcode,Mode mode)72 void Update(EcmaOpcode opcode, Mode mode) 73 { 74 auto it = profMap_.find(opcode); 75 if (it != profMap_.end()) { 76 (mode == Mode::TYPED_PATH) ? (it->second.typedPathValue++) : (it->second.slowPathValue++); 77 } 78 } 79 80 void PrintAndReset(); 81 82 private: 83 std::map<EcmaOpcode, Value> profMap_; 84 }; 85 } // namespace panda::ecmascript 86 #endif // ECMASCRIPT_DFX_VMSTAT_OPT_CODE_PROFILER_H 87