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#include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
174514f5e3Sopenharmony_ci#include <fstream>
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/platform/file.h"
204514f5e3Sopenharmony_cinamespace panda::ecmascript::pgo {
214514f5e3Sopenharmony_cinamespace {
224514f5e3Sopenharmony_ci    constexpr int32_t PGO_SAVING_SIGNAL = 50;
234514f5e3Sopenharmony_ci} // namespace
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_ciPGOProfilerManager* PGOProfilerManager::GetInstance()
264514f5e3Sopenharmony_ci{
274514f5e3Sopenharmony_ci    static PGOProfilerManager* instance = new PGOProfilerManager();
284514f5e3Sopenharmony_ci    return instance;
294514f5e3Sopenharmony_ci}
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_cibool PGOProfilerManager::MergeApFiles(const std::string &inFiles, const std::string &outPath, uint32_t hotnessThreshold,
324514f5e3Sopenharmony_ci                                      ApGenMode mode)
334514f5e3Sopenharmony_ci{
344514f5e3Sopenharmony_ci    arg_list_t apFileNames = base::StringHelper::SplitString(inFiles, GetFileDelimiter());
354514f5e3Sopenharmony_ci    PGOProfilerEncoder merger(outPath, hotnessThreshold, mode);
364514f5e3Sopenharmony_ci    if (!merger.InitializeData()) {
374514f5e3Sopenharmony_ci        LOG_ECMA(ERROR) << "PGO Profiler encoder initialized failed. outPath: " << outPath
384514f5e3Sopenharmony_ci                        << " ,hotnessThreshold: " << hotnessThreshold;
394514f5e3Sopenharmony_ci        return false;
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    bool hasMerged = false;
424514f5e3Sopenharmony_ci    std::string firstApFileName;
434514f5e3Sopenharmony_ci    for (const auto &fileName : apFileNames) {
444514f5e3Sopenharmony_ci        if (!base::StringHelper::EndsWith(fileName, ".ap")) {
454514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "The file path (" << fileName << ") does not end with .ap";
464514f5e3Sopenharmony_ci            continue;
474514f5e3Sopenharmony_ci        }
484514f5e3Sopenharmony_ci        PGOProfilerDecoder decoder(fileName, hotnessThreshold);
494514f5e3Sopenharmony_ci        if (!decoder.LoadFull(merger.GetAbcFilePool())) {
504514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "Fail to load file path (" << fileName << "), skip it.";
514514f5e3Sopenharmony_ci            continue;
524514f5e3Sopenharmony_ci        }
534514f5e3Sopenharmony_ci        if (!hasMerged) {
544514f5e3Sopenharmony_ci            firstApFileName = fileName;
554514f5e3Sopenharmony_ci        } else {
564514f5e3Sopenharmony_ci            if (!merger.VerifyPandaFileMatched(decoder.GetPandaFileInfos(), firstApFileName, fileName)) {
574514f5e3Sopenharmony_ci                continue;
584514f5e3Sopenharmony_ci            }
594514f5e3Sopenharmony_ci        }
604514f5e3Sopenharmony_ci        if (!decoder.IsCompatibleWithAOTFile()) {
614514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "The ap file (" << fileName << ") is not compatible with AOT version. skip it";
624514f5e3Sopenharmony_ci            continue;
634514f5e3Sopenharmony_ci        }
644514f5e3Sopenharmony_ci        merger.Merge(decoder.GetRecordDetailInfos());
654514f5e3Sopenharmony_ci        merger.Merge(decoder.GetPandaFileInfos());
664514f5e3Sopenharmony_ci        hasMerged = true;
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci    if (!hasMerged) {
694514f5e3Sopenharmony_ci        LOG_ECMA(ERROR)
704514f5e3Sopenharmony_ci            << "No ap file pass verify, no ap file compatible an version, no ap file processed. Input files: "
714514f5e3Sopenharmony_ci            << inFiles;
724514f5e3Sopenharmony_ci        GetInstance()->SetIsApFileCompatible(false);
734514f5e3Sopenharmony_ci        return false;
744514f5e3Sopenharmony_ci    }
754514f5e3Sopenharmony_ci    GetInstance()->SetIsApFileCompatible(true);
764514f5e3Sopenharmony_ci    merger.Save();
774514f5e3Sopenharmony_ci    return true;
784514f5e3Sopenharmony_ci}
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_cibool PGOProfilerManager::MergeApFiles(uint32_t checksum, PGOProfilerDecoder &merger)
814514f5e3Sopenharmony_ci{
824514f5e3Sopenharmony_ci    uint32_t hotnessThreshold = merger.GetHotnessThreshold();
834514f5e3Sopenharmony_ci    std::string inFiles(merger.GetInPath());
844514f5e3Sopenharmony_ci    arg_list_t pandaFileNames = base::StringHelper::SplitString(inFiles, GetFileDelimiter());
854514f5e3Sopenharmony_ci    if (pandaFileNames.empty()) {
864514f5e3Sopenharmony_ci        return true;
874514f5e3Sopenharmony_ci    }
884514f5e3Sopenharmony_ci    merger.InitMergeData();
894514f5e3Sopenharmony_ci    bool hasMerged = false;
904514f5e3Sopenharmony_ci    std::string firstApFileName;
914514f5e3Sopenharmony_ci    for (const auto &fileName : pandaFileNames) {
924514f5e3Sopenharmony_ci        PGOProfilerDecoder decoder(fileName, hotnessThreshold);
934514f5e3Sopenharmony_ci        if (!decoder.LoadAndVerify(checksum, merger.GetAbcFilePool())) {
944514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "Load and verify file (" << fileName << ") failed, skip it.";
954514f5e3Sopenharmony_ci            continue;
964514f5e3Sopenharmony_ci        }
974514f5e3Sopenharmony_ci        if (!hasMerged) {
984514f5e3Sopenharmony_ci            firstApFileName = fileName;
994514f5e3Sopenharmony_ci        } else {
1004514f5e3Sopenharmony_ci            if (!merger.GetPandaFileInfos().VerifyChecksum(decoder.GetPandaFileInfos(), firstApFileName, fileName)) {
1014514f5e3Sopenharmony_ci                continue;
1024514f5e3Sopenharmony_ci            }
1034514f5e3Sopenharmony_ci        }
1044514f5e3Sopenharmony_ci        if (!decoder.IsCompatibleWithAOTFile()) {
1054514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "The ap file (" << fileName << ") is not compatible with AOT version. skip it";
1064514f5e3Sopenharmony_ci            continue;
1074514f5e3Sopenharmony_ci        }
1084514f5e3Sopenharmony_ci        merger.Merge(decoder);
1094514f5e3Sopenharmony_ci        hasMerged = true;
1104514f5e3Sopenharmony_ci    }
1114514f5e3Sopenharmony_ci    if (!hasMerged) {
1124514f5e3Sopenharmony_ci        LOG_ECMA(ERROR)
1134514f5e3Sopenharmony_ci            << "No ap file pass verify, no ap file compatible an version, no ap file processed. Input files: "
1144514f5e3Sopenharmony_ci            << inFiles;
1154514f5e3Sopenharmony_ci        GetInstance()->SetIsApFileCompatible(false);
1164514f5e3Sopenharmony_ci        return false;
1174514f5e3Sopenharmony_ci    }
1184514f5e3Sopenharmony_ci    GetInstance()->SetIsApFileCompatible(true);
1194514f5e3Sopenharmony_ci    return true;
1204514f5e3Sopenharmony_ci}
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_civoid PGOProfilerManager::RegisterSavingSignal()
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    LOG_ECMA(INFO) << "Register Pgo Saving Signal";
1254514f5e3Sopenharmony_ci    if (encoder_ == nullptr) {
1264514f5e3Sopenharmony_ci        LOG_ECMA(ERROR) << "Can not register pgo saving signal, because encoder is null.";
1274514f5e3Sopenharmony_ci        return;
1284514f5e3Sopenharmony_ci    }
1294514f5e3Sopenharmony_ci    if (!encoder_->IsInitialized()) {
1304514f5e3Sopenharmony_ci        LOG_ECMA(DEBUG) << "Can not register pgo saving signal, because encoder is initialized.";
1314514f5e3Sopenharmony_ci        return;
1324514f5e3Sopenharmony_ci    }
1334514f5e3Sopenharmony_ci    signal(PGO_SAVING_SIGNAL, SavingSignalHandler);
1344514f5e3Sopenharmony_ci    enableSignalSaving_ = true;
1354514f5e3Sopenharmony_ci}
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_civoid PGOProfilerManager::SavingSignalHandler(int signo)
1384514f5e3Sopenharmony_ci{
1394514f5e3Sopenharmony_ci    if (signo != PGO_SAVING_SIGNAL) {
1404514f5e3Sopenharmony_ci        return;
1414514f5e3Sopenharmony_ci    }
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ci    PGOProfilerManager::GetInstance()->ForceSave();
1444514f5e3Sopenharmony_ci}
1454514f5e3Sopenharmony_ci} // namespace panda::ecmascript::pgo
146