14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_OHOS_OHOS_PGO_PROCESSOR_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_OHOS_OHOS_PGO_PROCESSOR_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_compiler_preprocessor.h"
204514f5e3Sopenharmony_ci#include "ecmascript/log_wrapper.h"
214514f5e3Sopenharmony_ci#include "ecmascript/ohos/ohos_pkg_args.h"
224514f5e3Sopenharmony_ci#include "ecmascript/platform/os.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
254514f5e3Sopenharmony_ciclass OhosPgoProcessor {
264514f5e3Sopenharmony_cipublic:
274514f5e3Sopenharmony_ci    static bool MergeAndRemoveRuntimeAp(CompilationOptions &cOptions, const std::shared_ptr<OhosPkgArgs> &mainPkgArgs)
284514f5e3Sopenharmony_ci    {
294514f5e3Sopenharmony_ci        if (!cOptions.needMerge_) {
304514f5e3Sopenharmony_ci            // use baseline ap, do not need to merge and remove runtime ap(s)
314514f5e3Sopenharmony_ci            return true;
324514f5e3Sopenharmony_ci        }
334514f5e3Sopenharmony_ci        if (cOptions.profilerIn_.empty()) {
344514f5e3Sopenharmony_ci            LOG_COMPILER(WARN) << "No valid ap files found in : " << mainPkgArgs->GetPgoDir();
354514f5e3Sopenharmony_ci            return true;
364514f5e3Sopenharmony_ci        }
374514f5e3Sopenharmony_ci        std::string mergedAp(mainPkgArgs->GetMergedApPathWithoutCheck());
384514f5e3Sopenharmony_ci        bool isSingleAp = cOptions.profilerIn_.find(GetFileDelimiter()) == std::string::npos;
394514f5e3Sopenharmony_ci        if (isSingleAp) {
404514f5e3Sopenharmony_ci            if (!RenameSingleAp(cOptions.profilerIn_, mergedAp)) {
414514f5e3Sopenharmony_ci                return false;
424514f5e3Sopenharmony_ci            }
434514f5e3Sopenharmony_ci        } else {
444514f5e3Sopenharmony_ci            if (!ExportMergedAp(cOptions.profilerIn_, cOptions.hotnessThreshold_, mergedAp)) {
454514f5e3Sopenharmony_ci                return false;
464514f5e3Sopenharmony_ci            }
474514f5e3Sopenharmony_ci            if (!UnlinkRuntimeAp(mainPkgArgs->GetRuntimeApPath())) {
484514f5e3Sopenharmony_ci                return false;
494514f5e3Sopenharmony_ci            }
504514f5e3Sopenharmony_ci        }
514514f5e3Sopenharmony_ci        ASSERT(mainPkgArgs != nullptr);
524514f5e3Sopenharmony_ci        // all ohos ap(s) merged into the merged ap file.
534514f5e3Sopenharmony_ci        cOptions.profilerIn_ = mainPkgArgs->GetMergedApPath();
544514f5e3Sopenharmony_ci        SetSecurityLabel(cOptions.profilerIn_);
554514f5e3Sopenharmony_ci        ASSERT(!cOptions.profilerIn_.empty());
564514f5e3Sopenharmony_ci        return true;
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ciprivate:
604514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(OhosPgoProcessor);
614514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(OhosPgoProcessor);
624514f5e3Sopenharmony_ci    static bool RenameSingleAp(const std::string &apPath, const std::string &exportAp)
634514f5e3Sopenharmony_ci    {
644514f5e3Sopenharmony_ci        ASSERT(!exportAp.empty());
654514f5e3Sopenharmony_ci        if (apPath == exportAp) {
664514f5e3Sopenharmony_ci            // no need to merge self.
674514f5e3Sopenharmony_ci            return true;
684514f5e3Sopenharmony_ci        }
694514f5e3Sopenharmony_ci        std::string apRealPath;
704514f5e3Sopenharmony_ci        if (!RealPath(apPath, apRealPath)) {
714514f5e3Sopenharmony_ci            return false;
724514f5e3Sopenharmony_ci        }
734514f5e3Sopenharmony_ci        if (rename(apRealPath.c_str(), exportAp.c_str())) {
744514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "RenameSingleAp " << apRealPath << " --> " << exportAp << " failure!, errno: " << errno;
754514f5e3Sopenharmony_ci            return false;
764514f5e3Sopenharmony_ci        }
774514f5e3Sopenharmony_ci        return true;
784514f5e3Sopenharmony_ci    }
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    static bool UnlinkRuntimeAp(const std::string &runtimeAp)
814514f5e3Sopenharmony_ci    {
824514f5e3Sopenharmony_ci        ASSERT(!runtimeAp.empty());
834514f5e3Sopenharmony_ci        std::string runtimeApRealPath;
844514f5e3Sopenharmony_ci        if (!RealPath(runtimeAp, runtimeApRealPath)) {
854514f5e3Sopenharmony_ci            return false;
864514f5e3Sopenharmony_ci        }
874514f5e3Sopenharmony_ci        if (!runtimeApRealPath.empty() && (Unlink(runtimeApRealPath.c_str()) == -1)) {
884514f5e3Sopenharmony_ci            LOG_COMPILER(ERROR) << "Remove runtime ap failed. file: " << runtimeApRealPath << ", errno: " << errno;
894514f5e3Sopenharmony_ci            return false;
904514f5e3Sopenharmony_ci        }
914514f5e3Sopenharmony_ci        return true;
924514f5e3Sopenharmony_ci    }
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    static bool ExportMergedAp(const std::string &profilerIn, uint32_t hotnessThreshold, const std::string &exportAp)
954514f5e3Sopenharmony_ci    {
964514f5e3Sopenharmony_ci        ASSERT(!exportAp.empty());
974514f5e3Sopenharmony_ci        return pgo::PGOProfilerManager::MergeApFiles(profilerIn, exportAp, hotnessThreshold,
984514f5e3Sopenharmony_ci                                                     pgo::PGOProfilerEncoder::ApGenMode::MERGE);
994514f5e3Sopenharmony_ci    }
1004514f5e3Sopenharmony_ci};
1014514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::kungfu
1024514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_OHOS_OHOS_PGO_PROCESSOR_H