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 49 uint64_t typedPathValue; 50 uint64_t slowPathValue; 51 }; 52 OptCodeProfiler()53 OptCodeProfiler() 54 { 55 #if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER 56 profMap_ = { 57 #define BYTECODE_PROF_MAP(name) { kungfu::EcmaOpcode::name, OptCodeProfiler::Value() }, 58 ECMA_OPCODE_LIST(BYTECODE_PROF_MAP) 59 #undef BYTECODE_PROF_MAP 60 }; 61 #endif 62 } 63 64 ~OptCodeProfiler(); 65 Update(EcmaOpcode opcode,Mode mode)66 void Update(EcmaOpcode opcode, Mode mode) 67 { 68 auto it = profMap_.find(opcode); 69 if (it != profMap_.end()) { 70 (mode == Mode::TYPED_PATH) ? (it->second.typedPathValue++) : (it->second.slowPathValue++); 71 } 72 } 73 74 private: 75 std::map<EcmaOpcode, Value> profMap_; 76 }; 77 } // namespace panda::ecmascript 78 #endif // ECMASCRIPT_DFX_VMSTAT_OPT_CODE_PROFILER_H 79