1 /* 2 * Copyright (c) 2023 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 #ifndef ECMASCRIPT_PGO_PROFILER_H 17 #define ECMASCRIPT_PGO_PROFILER_H 18 19 #include <chrono> 20 #include <memory> 21 22 #include "ecmascript/ecma_vm.h" 23 #include "ecmascript/pgo_profiler/pgo_profiler_info.h" 24 25 namespace panda::ecmascript { 26 class PGOProfiler { 27 public: 28 NO_COPY_SEMANTIC(PGOProfiler); 29 NO_MOVE_SEMANTIC(PGOProfiler); 30 31 void ProfileCall(JSTaggedType func, JSTaggedType callTarget, int32_t pcOffset = -1, 32 SampleMode mode = SampleMode::CALL_MODE, int32_t incCount = 1); 33 void ProfileOpType(JSTaggedType func, int32_t offset, uint32_t type); 34 void ProfileCreateObject(JSTaggedType func, int32_t offset, JSTaggedType newObj, int32_t traceId); 35 void ProfileDefineClass(JSThread *thread, JSTaggedType func, int32_t offset, JSTaggedType ctor); 36 void ProfileObjLayout(JSThread *thread, JSTaggedType func, int32_t offset, JSTaggedType object, bool store); SetSaveTimestamp(std::chrono::system_clock::time_point timestamp)37 void SetSaveTimestamp(std::chrono::system_clock::time_point timestamp) 38 { 39 saveTimestamp_ = timestamp; 40 } 41 42 void InsertLiteralId(JSTaggedType hclass, int32_t traceId); 43 void ProcessReferences(const WeakRootVisitor &visitor); 44 45 private: 46 static constexpr uint32_t MERGED_EVERY_COUNT = 20; 47 static constexpr auto MERGED_MIN_INTERVAL = std::chrono::milliseconds(15); 48 PGOProfiler(EcmaVM * vm,bool isEnable)49 PGOProfiler([[maybe_unused]] EcmaVM *vm, bool isEnable) : isEnable_(isEnable) 50 { 51 if (isEnable_) { 52 recordInfos_ = std::make_unique<PGORecordDetailInfos>(0); 53 } 54 }; 55 ~PGOProfiler()56 virtual ~PGOProfiler() 57 { 58 Reset(false); 59 } 60 Reset(bool isEnable)61 void Reset(bool isEnable) 62 { 63 isEnable_ = isEnable; 64 methodCount_ = 0; 65 if (recordInfos_) { 66 recordInfos_->Clear(); 67 } else { 68 if (isEnable_) { 69 recordInfos_ = std::make_unique<PGORecordDetailInfos>(0); 70 } 71 } 72 } 73 74 bool isEnable_ {false}; 75 uint32_t methodCount_ {0}; 76 std::chrono::system_clock::time_point saveTimestamp_; 77 CMap<JSTaggedType, int32_t> traceIds_; 78 std::unique_ptr<PGORecordDetailInfos> recordInfos_; 79 friend class PGOProfilerManager; 80 }; 81 } // namespace panda::ecmascript 82 #endif // ECMASCRIPT_PGO_PROFILER_H 83