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_MANAGER_H 17 #define ECMASCRIPT_PGO_PROFILER_MANAGER_H 18 19 #include <memory> 20 21 #include "ecmascript/pgo_profiler/pgo_profiler.h" 22 #include "ecmascript/pgo_profiler/pgo_profiler_loader.h" 23 #include "ecmascript/pgo_profiler/pgo_profiler_saver.h" 24 25 namespace panda::ecmascript { 26 class PGOProfilerManager { 27 public: GetInstance()28 static PGOProfilerManager *GetInstance() 29 { 30 static PGOProfilerManager instance; 31 return &instance; 32 } 33 34 PGOProfilerManager() = default; 35 ~PGOProfilerManager() = default; 36 37 NO_COPY_SEMANTIC(PGOProfilerManager); 38 NO_MOVE_SEMANTIC(PGOProfilerManager); 39 Initialize(const std::string & outDir,uint32_t hotnessThreshold)40 void Initialize(const std::string &outDir, uint32_t hotnessThreshold) 41 { 42 saver_ = std::make_unique<PGOProfilerSaver>(outDir, hotnessThreshold); 43 } 44 Destroy()45 void Destroy() 46 { 47 if (saver_) { 48 saver_->Save(); 49 saver_->Destroy(); 50 saver_.reset(); 51 } 52 } 53 54 // Factory Build(EcmaVM * vm,bool isEnable)55 PGOProfiler *Build(EcmaVM *vm, bool isEnable) 56 { 57 if (isEnable) { 58 isEnable = InitializeData(); 59 } 60 return new PGOProfiler(vm, isEnable); 61 } 62 IsEnable()63 bool IsEnable() const 64 { 65 return saver_ && saver_->IsInitialized(); 66 } 67 Destroy(PGOProfiler * profiler)68 void Destroy(PGOProfiler *profiler) 69 { 70 if (profiler != nullptr) { 71 Merge(profiler); 72 delete profiler; 73 } 74 } 75 Reset(PGOProfiler * profiler,bool isEnable)76 void Reset(PGOProfiler *profiler, bool isEnable) 77 { 78 if (isEnable) { 79 isEnable = InitializeData(); 80 } 81 if (profiler) { 82 profiler->Reset(isEnable); 83 } 84 } 85 SamplePandaFileInfo(uint32_t checksum)86 void SamplePandaFileInfo(uint32_t checksum) 87 { 88 if (saver_) { 89 saver_->SamplePandaFileInfo(checksum); 90 } 91 } 92 Merge(PGOProfiler * profiler)93 void Merge(PGOProfiler *profiler) 94 { 95 if (saver_ && profiler->isEnable_) { 96 saver_->TerminateSaveTask(); 97 saver_->Merge(*profiler->recordInfos_); 98 } 99 } 100 AsynSave()101 void AsynSave() 102 { 103 if (saver_) { 104 saver_->PostSaveTask(); 105 } 106 } 107 TextToBinary(const std::string & inPath,const std::string & outPath,uint32_t hotnessThreshold)108 bool PUBLIC_API TextToBinary(const std::string &inPath, const std::string &outPath, uint32_t hotnessThreshold) 109 { 110 PGOProfilerSaver saver(outPath, hotnessThreshold); 111 if (!saver.InitializeData()) { 112 LOG_ECMA(ERROR) << "PGO Profiler saver initialized failed"; 113 return false; 114 } 115 bool ret = saver.LoadAPTextFile(inPath); 116 if (ret) { 117 saver.Save(); 118 } 119 saver.Destroy(); 120 return ret; 121 } 122 BinaryToText(const std::string & inPath,const std::string & outPath,uint32_t hotnessThreshold)123 bool PUBLIC_API BinaryToText(const std::string &inPath, const std::string &outPath, uint32_t hotnessThreshold) 124 { 125 PGOProfilerLoader loader(inPath, hotnessThreshold); 126 if (!loader.LoadFull()) { 127 return false; 128 } 129 bool ret = loader.SaveAPTextFile(outPath); 130 loader.Clear(); 131 return ret; 132 } 133 134 private: InitializeData()135 bool InitializeData() 136 { 137 if (!saver_) { 138 return false; 139 } 140 return saver_->InitializeData(); 141 } 142 143 std::unique_ptr<PGOProfilerSaver> saver_; 144 }; 145 } // namespace panda::ecmascript 146 #endif // ECMASCRIPT_PGO_PROFILER_MANAGER_H 147