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_ENCODER_H 17 #define ECMASCRIPT_PGO_PROFILER_ENCODER_H 18 19 #include <atomic> 20 #include <memory> 21 #include <utility> 22 23 #include "libpandabase/macros.h" 24 25 #include "ecmascript/pgo_profiler/pgo_info.h" 26 #include "ecmascript/pgo_profiler/pgo_profiler_info.h" 27 28 namespace panda::ecmascript::pgo { 29 class PGOProfilerDecoder; 30 class PGOProfilerEncoder { 31 public: 32 static constexpr size_t MIN_DISK_SPACE = 300_MB; 33 static constexpr size_t MAX_AP_FILE_SIZE = 100_MB; 34 static constexpr size_t CONVERT_FACTOR = 1_MB; 35 36 enum ApGenMode { OVERWRITE, MERGE }; 37 PGOProfilerEncoder(const std::string & path,ApGenMode mode)38 PGOProfilerEncoder(const std::string& path, ApGenMode mode): path_(path), mode_(mode) {} 39 ~PGOProfilerEncoder() = default; 40 41 NO_COPY_SEMANTIC(PGOProfilerEncoder); 42 NO_MOVE_SEMANTIC(PGOProfilerEncoder); 43 44 void SetApGenMode(ApGenMode mode); 45 ApGenMode GetApGenMode() const; 46 bool PUBLIC_API Save(const std::shared_ptr<PGOInfo> pgoInfo); 47 48 private: 49 bool InternalSave(const std::shared_ptr<PGOInfo> info); 50 bool SaveAndRename(const std::shared_ptr<PGOInfo> info); 51 std::string GetDirectoryPath(const std::string& path) const; 52 bool WriteProfilerFile(const std::shared_ptr<PGOInfo> info, const std::string& tmpOutPath); 53 bool ValidateAndRename(const std::string& tmpOutPath); 54 void AddChecksum(std::fstream& fileStream); 55 56 std::string path_; 57 ApGenMode mode_ {OVERWRITE}; 58 Mutex mutex_; 59 }; 60 } // namespace panda::ecmascript::pgo 61 #endif // ECMASCRIPT_PGO_PROFILER_ENCODER_H 62