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 #ifndef ECMASCRIPT_PGO_PROFILE_LOADER_H 17 #define ECMASCRIPT_PGO_PROFILE_LOADER_H 18 19 #include "ecmascript/pgo_profiler/pgo_profiler_info.h" 20 #include "ecmascript/platform/map.h" 21 22 namespace panda::ecmascript { 23 class PGOProfilerLoader { 24 public: 25 PGOProfilerLoader() = default; PGOProfilerLoader(const std::string & inPath,uint32_t hotnessThreshold)26 PGOProfilerLoader(const std::string &inPath, uint32_t hotnessThreshold) 27 : inPath_(inPath), hotnessThreshold_(hotnessThreshold) {} 28 29 virtual ~PGOProfilerLoader() = default; 30 31 NO_COPY_SEMANTIC(PGOProfilerLoader); 32 NO_MOVE_SEMANTIC(PGOProfilerLoader); 33 34 bool PUBLIC_API Match(const CString &recordName, EntityId methodId); 35 36 bool PUBLIC_API LoadAndVerify(uint32_t checksum); 37 bool PUBLIC_API LoadFull(); 38 void PUBLIC_API Clear(); 39 40 bool PUBLIC_API SaveAPTextFile(const std::string &outPath); 41 42 template <typename Callback> Update(Callback callback)43 void Update(Callback callback) 44 { 45 if (!isLoaded_ || !isVerifySuccess_) { 46 return; 47 } 48 recordSimpleInfos_->Update(callback); 49 } 50 51 template <typename Callback> Update(const CString & recordName,Callback callback)52 void Update(const CString &recordName, Callback callback) 53 { 54 if (!isLoaded_ || !isVerifySuccess_) { 55 return; 56 } 57 recordSimpleInfos_->Update(recordName, callback); 58 } 59 IsLoaded()60 bool IsLoaded() const 61 { 62 return isLoaded_; 63 } 64 65 private: 66 bool Load(); 67 bool Verify(uint32_t checksum); 68 69 bool LoadAPBinaryFile(); 70 void UnLoadAPBinaryFile(); 71 72 bool isLoaded_ {false}; 73 bool isVerifySuccess_ {false}; 74 std::string inPath_; 75 uint32_t hotnessThreshold_ {0}; 76 PGOProfilerHeader *header_ {nullptr}; 77 PGOPandaFileInfos pandaFileInfos_; 78 std::unique_ptr<PGORecordDetailInfos> recordDetailInfos_; 79 std::unique_ptr<PGORecordSimpleInfos> recordSimpleInfos_; 80 MemMap fileMapAddr_; 81 }; 82 } // namespace panda::ecmascript 83 #endif // ECMASCRIPT_PGO_PROFILE_LOADER_H 84