14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022 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 <cstdint> 174514f5e3Sopenharmony_ci#include <fstream> 184514f5e3Sopenharmony_ci#include <memory> 194514f5e3Sopenharmony_ci#include <sstream> 204514f5e3Sopenharmony_ci#include <string> 214514f5e3Sopenharmony_ci#include <unordered_map> 224514f5e3Sopenharmony_ci#include <utility> 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_ci#include "ecmascript/base/file_header.h" 254514f5e3Sopenharmony_ci#include "ecmascript/elements.h" 264514f5e3Sopenharmony_ci#include "ecmascript/js_symbol.h" 274514f5e3Sopenharmony_ci#include "ecmascript/log_wrapper.h" 284514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 294514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/ap_file/pgo_file_info.h" 304514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_context.h" 314514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_encoder.h" 324514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/types/pgo_profile_type.h" 334514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/types/pgo_profiler_type.h" 344514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_utils.h" 354514f5e3Sopenharmony_ci#include "ecmascript/platform/file.h" 364514f5e3Sopenharmony_ci#include "gtest/gtest.h" 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_ci#include "assembler/assembly-emitter.h" 394514f5e3Sopenharmony_ci#include "assembler/assembly-parser.h" 404514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 414514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 424514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile.h" 434514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile_manager.h" 444514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/method_literal.h" 454514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/program_object.h" 464514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h" 474514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/ap_file/pgo_method_type_set.h" 484514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_decoder.h" 494514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_info.h" 504514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_manager.h" 514514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/tests/pgo_context_mock.h" 524514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/tests/pgo_encoder_mock.h" 534514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 544514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi_expo.h" 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_ciusing namespace panda; 574514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 584514f5e3Sopenharmony_ciusing namespace panda::ecmascript::pgo; 594514f5e3Sopenharmony_ciusing namespace panda::panda_file; 604514f5e3Sopenharmony_ciusing namespace panda::pandasm; 614514f5e3Sopenharmony_ci 624514f5e3Sopenharmony_cinamespace panda::test { 634514f5e3Sopenharmony_ciclass PGOProfilerTest : public testing::Test { 644514f5e3Sopenharmony_cipublic: 654514f5e3Sopenharmony_ci using ApGenMode = PGOProfilerEncoder::ApGenMode; 664514f5e3Sopenharmony_ci static void SetUpTestCase() 674514f5e3Sopenharmony_ci { 684514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "SetUpTestCase"; 694514f5e3Sopenharmony_ci } 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci static void TearDownTestCase() 724514f5e3Sopenharmony_ci { 734514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "TearDownCase"; 744514f5e3Sopenharmony_ci } 754514f5e3Sopenharmony_ci 764514f5e3Sopenharmony_ci void SetUp() override 774514f5e3Sopenharmony_ci { 784514f5e3Sopenharmony_ci strictMatch_ = PGOProfilerHeader::IsStrictMatch(); 794514f5e3Sopenharmony_ci } 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_ci void TearDown() override 824514f5e3Sopenharmony_ci { 834514f5e3Sopenharmony_ci JSPandaFileManager::GetInstance()->RemoveJSPandaFile(pf_.get()); 844514f5e3Sopenharmony_ci vm_ = nullptr; 854514f5e3Sopenharmony_ci pf_.reset(); 864514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SetDisablePGO(false); 874514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->Destroy(); 884514f5e3Sopenharmony_ci PGOProfilerHeader::SetStrictMatch(strictMatch_); 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci 914514f5e3Sopenharmony_ciprotected: 924514f5e3Sopenharmony_ci void CreateJSPandaFile(const CString filename, std::vector<MethodLiteral *> &methodLiterals) 934514f5e3Sopenharmony_ci { 944514f5e3Sopenharmony_ci std::string targetAbcPath = std::string(TARGET_ABC_PATH) + filename.c_str(); 954514f5e3Sopenharmony_ci auto pfPtr = panda_file::OpenPandaFileOrZip(targetAbcPath, panda_file::File::READ_WRITE); 964514f5e3Sopenharmony_ci JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance(); 974514f5e3Sopenharmony_ci pf_ = pfManager->NewJSPandaFile(pfPtr.release(), filename); 984514f5e3Sopenharmony_ci 994514f5e3Sopenharmony_ci const File *file = pf_->GetPandaFile(); 1004514f5e3Sopenharmony_ci auto classes = pf_->GetClasses(); 1014514f5e3Sopenharmony_ci 1024514f5e3Sopenharmony_ci for (size_t i = 0; i < classes.Size(); i++) { 1034514f5e3Sopenharmony_ci panda_file::File::EntityId classId(classes[i]); 1044514f5e3Sopenharmony_ci if (!classId.IsValid() || pf_->IsExternal(classId)) { 1054514f5e3Sopenharmony_ci continue; 1064514f5e3Sopenharmony_ci } 1074514f5e3Sopenharmony_ci ClassDataAccessor cda(*file, classId); 1084514f5e3Sopenharmony_ci cda.EnumerateMethods([&](panda_file::MethodDataAccessor &mda) { 1094514f5e3Sopenharmony_ci auto *methodLiteral = new MethodLiteral(mda.GetMethodId()); 1104514f5e3Sopenharmony_ci methodLiteral->Initialize(pf_.get()); 1114514f5e3Sopenharmony_ci pf_->SetMethodLiteralToMap(methodLiteral); 1124514f5e3Sopenharmony_ci methodLiterals.push_back(methodLiteral); 1134514f5e3Sopenharmony_ci }); 1144514f5e3Sopenharmony_ci } 1154514f5e3Sopenharmony_ci } 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci void CreateJSPandaFile(const char *source, const CString filename, std::vector<MethodLiteral *> &methodLiterals) 1184514f5e3Sopenharmony_ci { 1194514f5e3Sopenharmony_ci Parser parser; 1204514f5e3Sopenharmony_ci const std::string fn = "SRC.abc"; // test file name : "SRC.abc" 1214514f5e3Sopenharmony_ci auto res = parser.Parse(source, fn); 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value()); 1244514f5e3Sopenharmony_ci JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance(); 1254514f5e3Sopenharmony_ci pf_ = pfManager->NewJSPandaFile(pfPtr.release(), filename); 1264514f5e3Sopenharmony_ci 1274514f5e3Sopenharmony_ci const File *file = pf_->GetPandaFile(); 1284514f5e3Sopenharmony_ci const uint8_t *typeDesc = utf::CStringAsMutf8("L_GLOBAL;"); 1294514f5e3Sopenharmony_ci File::EntityId classId = file->GetClassId(typeDesc); 1304514f5e3Sopenharmony_ci EXPECT_TRUE(classId.IsValid()); 1314514f5e3Sopenharmony_ci 1324514f5e3Sopenharmony_ci ClassDataAccessor cda(*file, classId); 1334514f5e3Sopenharmony_ci cda.EnumerateMethods([&](panda_file::MethodDataAccessor &mda) { 1344514f5e3Sopenharmony_ci auto *methodLiteral = new MethodLiteral(mda.GetMethodId()); 1354514f5e3Sopenharmony_ci methodLiteral->Initialize(pf_.get()); 1364514f5e3Sopenharmony_ci pf_->SetMethodLiteralToMap(methodLiteral); 1374514f5e3Sopenharmony_ci methodLiterals.push_back(methodLiteral); 1384514f5e3Sopenharmony_ci }); 1394514f5e3Sopenharmony_ci } 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci void ExecuteAndLoadJSPandaFile(std::string profDir, std::string recordName) 1424514f5e3Sopenharmony_ci { 1434514f5e3Sopenharmony_ci RuntimeOption option; 1444514f5e3Sopenharmony_ci option.SetLogLevel(LOG_LEVEL::INFO); 1454514f5e3Sopenharmony_ci option.SetEnableProfile(true); 1464514f5e3Sopenharmony_ci option.SetProfileDir(profDir); 1474514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 1484514f5e3Sopenharmony_ci JSNApi::EnableUserUncaughtErrorHandler(vm_); 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ci std::string targetAbcPath = TARGET_ABC_PATH + recordName + ".abc"; 1514514f5e3Sopenharmony_ci auto result = JSNApi::Execute(vm_, targetAbcPath, recordName, false); 1524514f5e3Sopenharmony_ci EXPECT_TRUE(result); 1534514f5e3Sopenharmony_ci 1544514f5e3Sopenharmony_ci pf_ = JSPandaFileManager::GetInstance()->FindJSPandaFile(CString(targetAbcPath)); 1554514f5e3Sopenharmony_ci 1564514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 1574514f5e3Sopenharmony_ci } 1584514f5e3Sopenharmony_ci 1594514f5e3Sopenharmony_ci void ParseRelatedPandaFileMethods( 1604514f5e3Sopenharmony_ci PGOProfilerDecoder &loader, 1614514f5e3Sopenharmony_ci std::unordered_map<std::string, std::unordered_map<std::string, std::vector<PGOMethodId>>> &methodIdInAp) 1624514f5e3Sopenharmony_ci { 1634514f5e3Sopenharmony_ci std::shared_ptr<PGOAbcFilePool> abcFilePool = std::make_shared<PGOAbcFilePool>(); 1644514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadFull(abcFilePool)); 1654514f5e3Sopenharmony_ci for (const auto &recordInfo : loader.GetRecordDetailInfos().GetRecordInfos()) { 1664514f5e3Sopenharmony_ci auto recordProfile = recordInfo.first; 1674514f5e3Sopenharmony_ci ASSERT_EQ(recordProfile.GetKind(), ProfileType::Kind::RecordClassId); 1684514f5e3Sopenharmony_ci if (recordProfile.IsNone()) { 1694514f5e3Sopenharmony_ci continue; 1704514f5e3Sopenharmony_ci } 1714514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "recordProfile: " << recordProfile.GetTypeString(); 1724514f5e3Sopenharmony_ci const auto *recordName = 1734514f5e3Sopenharmony_ci loader.GetRecordDetailInfos().GetRecordPool()->GetName(recordProfile); 1744514f5e3Sopenharmony_ci ASSERT(recordName != nullptr); 1754514f5e3Sopenharmony_ci const auto abcNormalizedDesc = 1764514f5e3Sopenharmony_ci JSPandaFile::GetNormalizedFileDesc(abcFilePool->GetEntry(recordProfile.GetAbcId())->GetData()); 1774514f5e3Sopenharmony_ci if (abcNormalizedDesc.empty()) { 1784514f5e3Sopenharmony_ci continue; 1794514f5e3Sopenharmony_ci } 1804514f5e3Sopenharmony_ci 1814514f5e3Sopenharmony_ci const auto *info = recordInfo.second; 1824514f5e3Sopenharmony_ci for (const auto &method : info->GetMethodInfos()) { 1834514f5e3Sopenharmony_ci // add ap entry info 1844514f5e3Sopenharmony_ci methodIdInAp[abcNormalizedDesc.c_str()][recordName].emplace_back(method.first); 1854514f5e3Sopenharmony_ci } 1864514f5e3Sopenharmony_ci }; 1874514f5e3Sopenharmony_ci } 1884514f5e3Sopenharmony_ci 1894514f5e3Sopenharmony_ci void CheckApMethods( 1904514f5e3Sopenharmony_ci std::unordered_map<std::string, std::unordered_map<std::string, std::vector<PGOMethodId>>> &methodIdInAp) 1914514f5e3Sopenharmony_ci { 1924514f5e3Sopenharmony_ci for (auto abcIter = methodIdInAp.begin(); abcIter != methodIdInAp.end();) { 1934514f5e3Sopenharmony_ci std::string fileName(abcIter->first.c_str()); 1944514f5e3Sopenharmony_ci auto lastDirToken = fileName.find_last_of('/'); 1954514f5e3Sopenharmony_ci if (lastDirToken != std::string::npos) { 1964514f5e3Sopenharmony_ci fileName = fileName.substr(lastDirToken + 1); 1974514f5e3Sopenharmony_ci } 1984514f5e3Sopenharmony_ci std::unordered_map<std::string, std::vector<PGOMethodId>> &recordMethodList = abcIter->second; 1994514f5e3Sopenharmony_ci CheckApMethodsInApFiles(fileName, recordMethodList); 2004514f5e3Sopenharmony_ci if (recordMethodList.empty()) { 2014514f5e3Sopenharmony_ci abcIter = methodIdInAp.erase(abcIter); 2024514f5e3Sopenharmony_ci } else { 2034514f5e3Sopenharmony_ci abcIter++; 2044514f5e3Sopenharmony_ci } 2054514f5e3Sopenharmony_ci } 2064514f5e3Sopenharmony_ci ASSERT_TRUE(methodIdInAp.empty()); 2074514f5e3Sopenharmony_ci } 2084514f5e3Sopenharmony_ci 2094514f5e3Sopenharmony_ci void CheckApMethodsInApFiles(const std::string &fileName, 2104514f5e3Sopenharmony_ci std::unordered_map<std::string, std::vector<PGOMethodId>> &recordMethodList) 2114514f5e3Sopenharmony_ci { 2124514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 2134514f5e3Sopenharmony_ci CreateJSPandaFile(fileName.c_str(), methodLiterals); 2144514f5e3Sopenharmony_ci for (auto &methodLiteral : methodLiterals) { 2154514f5e3Sopenharmony_ci auto methodName = MethodLiteral::GetRecordName(pf_.get(), methodLiteral->GetMethodId()); 2164514f5e3Sopenharmony_ci auto recordEntry = recordMethodList.find(methodName.c_str()); 2174514f5e3Sopenharmony_ci if (recordEntry == recordMethodList.end()) { 2184514f5e3Sopenharmony_ci continue; 2194514f5e3Sopenharmony_ci } 2204514f5e3Sopenharmony_ci for (size_t index = 0; index < recordEntry->second.size(); ++index) { 2214514f5e3Sopenharmony_ci if (!(recordEntry->second.at(index) == methodLiteral->GetMethodId())) { 2224514f5e3Sopenharmony_ci continue; 2234514f5e3Sopenharmony_ci } 2244514f5e3Sopenharmony_ci // Remove matched entry 2254514f5e3Sopenharmony_ci recordEntry->second.erase(recordEntry->second.begin() + index); 2264514f5e3Sopenharmony_ci if (recordEntry->second.empty()) { 2274514f5e3Sopenharmony_ci recordEntry = recordMethodList.erase(recordEntry); 2284514f5e3Sopenharmony_ci } 2294514f5e3Sopenharmony_ci break; 2304514f5e3Sopenharmony_ci } 2314514f5e3Sopenharmony_ci } 2324514f5e3Sopenharmony_ci } 2334514f5e3Sopenharmony_ci 2344514f5e3Sopenharmony_ci static constexpr uint32_t DECODER_THRESHOLD = 2; 2354514f5e3Sopenharmony_ci EcmaVM *vm_ = nullptr; 2364514f5e3Sopenharmony_ci bool strictMatch_ = false; 2374514f5e3Sopenharmony_ci std::shared_ptr<JSPandaFile> pf_; 2384514f5e3Sopenharmony_ci}; 2394514f5e3Sopenharmony_ci 2404514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, Sample) 2414514f5e3Sopenharmony_ci{ 2424514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 2434514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 2444514f5e3Sopenharmony_ci 2454514f5e3Sopenharmony_ci mkdir("ark-profiler/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 2464514f5e3Sopenharmony_ci RuntimeOption option; 2474514f5e3Sopenharmony_ci option.SetEnableProfile(true); 2484514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler/"); 2494514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 2504514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 2514514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 2524514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 2534514f5e3Sopenharmony_ci uint32_t checksum = 304293; 2544514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "ark-profiler.abc"); 2554514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 2564514f5e3Sopenharmony_ci 2574514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 2584514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 2594514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test")); 2604514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 2614514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 2624514f5e3Sopenharmony_ci vm_->GetPGOProfiler()->SetSaveTimestamp(std::chrono::system_clock::now()); 2634514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 2644514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 2654514f5e3Sopenharmony_ci // Loader 2664514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler/modules.ap", DECODER_THRESHOLD); 2674514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 2684514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 2694514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(checksum)); 2704514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 2714514f5e3Sopenharmony_ci#else 2724514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 2734514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 2744514f5e3Sopenharmony_ci#endif 2754514f5e3Sopenharmony_ci unlink("ark-profiler/modules.ap"); 2764514f5e3Sopenharmony_ci rmdir("ark-profiler/"); 2774514f5e3Sopenharmony_ci} 2784514f5e3Sopenharmony_ci 2794514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, Sample1) 2804514f5e3Sopenharmony_ci{ 2814514f5e3Sopenharmony_ci mkdir("ark-profiler1/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 2824514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 2834514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 2844514f5e3Sopenharmony_ci 2854514f5e3Sopenharmony_ci mkdir("ark-profiler1/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 2864514f5e3Sopenharmony_ci RuntimeOption option; 2874514f5e3Sopenharmony_ci option.SetEnableProfile(true); 2884514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler1/"); 2894514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 2904514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 2914514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 2924514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 2934514f5e3Sopenharmony_ci uint32_t checksum = 304293; 2944514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "ark-profiler1.abc"); 2954514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 2964514f5e3Sopenharmony_ci 2974514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 2984514f5e3Sopenharmony_ci JSHandle<Method> method1 = vm_->GetFactory()->NewSMethod(methodLiterals[1]); 2994514f5e3Sopenharmony_ci JSHandle<Method> method2 = vm_->GetFactory()->NewSMethod(methodLiterals[2]); 3004514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 3014514f5e3Sopenharmony_ci method1->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 3024514f5e3Sopenharmony_ci method2->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 3034514f5e3Sopenharmony_ci 3044514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 3054514f5e3Sopenharmony_ci JSHandle<JSFunction> func1 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method1); 3064514f5e3Sopenharmony_ci JSHandle<JSFunction> func2 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method2); 3074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test")); 3084514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 3094514f5e3Sopenharmony_ci func1->SetModule(vm_->GetJSThread(), recordName); 3104514f5e3Sopenharmony_ci func2->SetModule(vm_->GetJSThread(), recordName); 3114514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 3124514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 3134514f5e3Sopenharmony_ci 3144514f5e3Sopenharmony_ci // Loader 3154514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler1/modules.ap", DECODER_THRESHOLD); 3164514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 3174514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 3184514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(checksum)); 3194514f5e3Sopenharmony_ci for (uint32_t idx = 0; idx < 3; idx++) { 3204514f5e3Sopenharmony_ci loader.MatchAndMarkMethod(pf_.get(), expectRecordName, 3214514f5e3Sopenharmony_ci methodLiterals[idx]->GetMethodName(pf_.get(), methodLiterals[idx]->GetMethodId()), 3224514f5e3Sopenharmony_ci methodLiterals[idx]->GetMethodId()); 3234514f5e3Sopenharmony_ci } 3244514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 3254514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[2]->GetMethodId())); 3264514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[1]->GetMethodId())); 3274514f5e3Sopenharmony_ci#else 3284514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 3294514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[1]->GetMethodId())); 3304514f5e3Sopenharmony_ci#endif 3314514f5e3Sopenharmony_ci unlink("ark-profiler1/modules.ap"); 3324514f5e3Sopenharmony_ci rmdir("ark-profiler1/"); 3334514f5e3Sopenharmony_ci} 3344514f5e3Sopenharmony_ci 3354514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, Sample2) 3364514f5e3Sopenharmony_ci{ 3374514f5e3Sopenharmony_ci mkdir("ark-profiler2/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 3384514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 3394514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 3404514f5e3Sopenharmony_ci 3414514f5e3Sopenharmony_ci mkdir("ark-profiler2/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 3424514f5e3Sopenharmony_ci RuntimeOption option; 3434514f5e3Sopenharmony_ci option.SetEnableProfile(true); 3444514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler2/"); 3454514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 3464514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 3474514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 3484514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 3494514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 3504514f5e3Sopenharmony_ci uint32_t checksum = 304293; 3514514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "ark-profiler2.abc"); 3524514f5e3Sopenharmony_ci 3534514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 3544514f5e3Sopenharmony_ci JSHandle<Method> method1 = vm_->GetFactory()->NewSMethod(methodLiterals[1]); 3554514f5e3Sopenharmony_ci 3564514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 3574514f5e3Sopenharmony_ci method1->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 3584514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 3594514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test")); 3604514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 3614514f5e3Sopenharmony_ci JSHandle<JSFunction> func1 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method1); 3624514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName1(vm_->GetFactory()->NewFromStdString("test1")); 3634514f5e3Sopenharmony_ci func1->SetModule(vm_->GetJSThread(), recordName1); 3644514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 3654514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 3664514f5e3Sopenharmony_ci 3674514f5e3Sopenharmony_ci // Loader 3684514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler2/modules.ap", DECODER_THRESHOLD); 3694514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 3704514f5e3Sopenharmony_ci CString expectRecordName1 = "sample_test"; 3714514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 3724514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(checksum)); 3734514f5e3Sopenharmony_ci for (uint32_t idx = 0; idx < 2; idx++) { 3744514f5e3Sopenharmony_ci loader.MatchAndMarkMethod(pf_.get(), expectRecordName, 3754514f5e3Sopenharmony_ci methodLiterals[idx]->GetMethodName(pf_.get(), methodLiterals[idx]->GetMethodId()), 3764514f5e3Sopenharmony_ci methodLiterals[idx]->GetMethodId()); 3774514f5e3Sopenharmony_ci loader.MatchAndMarkMethod(pf_.get(), expectRecordName1, 3784514f5e3Sopenharmony_ci methodLiterals[idx]->GetMethodName(pf_.get(), methodLiterals[idx]->GetMethodId()), 3794514f5e3Sopenharmony_ci methodLiterals[idx]->GetMethodId()); 3804514f5e3Sopenharmony_ci } 3814514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 3824514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName1, methodLiterals[1]->GetMethodId())); 3834514f5e3Sopenharmony_ci#else 3844514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 3854514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 3864514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName1, methodLiterals[1]->GetMethodId())); 3874514f5e3Sopenharmony_ci#endif 3884514f5e3Sopenharmony_ci unlink("ark-profiler2/modules.ap"); 3894514f5e3Sopenharmony_ci rmdir("ark-profiler2/"); 3904514f5e3Sopenharmony_ci} 3914514f5e3Sopenharmony_ci 3924514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, DisEnableSample) 3934514f5e3Sopenharmony_ci{ 3944514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 3954514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 3964514f5e3Sopenharmony_ci EXPECT_GE(methodLiterals.size(), 1); 3974514f5e3Sopenharmony_ci mkdir("ark-profiler3/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 3984514f5e3Sopenharmony_ci RuntimeOption option; 3994514f5e3Sopenharmony_ci option.SetEnableProfile(false); 4004514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler3/"); 4014514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 4024514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 4034514f5e3Sopenharmony_ci JSPandaFileManager::GetInstance()->AddJSPandaFile(pf_); 4044514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 4054514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 4064514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 4074514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "sample_test.abc"); 4084514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 4094514f5e3Sopenharmony_ci 4104514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 4114514f5e3Sopenharmony_ci 4124514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 4134514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 4144514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("sample_test")); 4154514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 4164514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 4174514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 4184514f5e3Sopenharmony_ci 4194514f5e3Sopenharmony_ci // Loader 4204514f5e3Sopenharmony_ci ASSERT_FALSE(FileExist("ark-profiler3/modules.ap")); 4214514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler3/modules.ap", DECODER_THRESHOLD); 4224514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 4234514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 4244514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 4254514f5e3Sopenharmony_ci rmdir("ark-profiler3/"); 4264514f5e3Sopenharmony_ci} 4274514f5e3Sopenharmony_ci 4284514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOProfilerManagerInvalidPath) 4294514f5e3Sopenharmony_ci{ 4304514f5e3Sopenharmony_ci RuntimeOption option; 4314514f5e3Sopenharmony_ci option.SetEnableProfile(true); 4324514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler4"); 4334514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 4344514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 4354514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 4364514f5e3Sopenharmony_ci} 4374514f5e3Sopenharmony_ci 4384514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOProfilerManagerInitialize) 4394514f5e3Sopenharmony_ci{ 4404514f5e3Sopenharmony_ci RuntimeOption option; 4414514f5e3Sopenharmony_ci option.SetEnableProfile(true); 4424514f5e3Sopenharmony_ci // outDir is empty 4434514f5e3Sopenharmony_ci option.SetProfileDir(""); 4444514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 4454514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 4464514f5e3Sopenharmony_ci 4474514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 4484514f5e3Sopenharmony_ci} 4494514f5e3Sopenharmony_ci 4504514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOProfilerManagerSample) 4514514f5e3Sopenharmony_ci{ 4524514f5e3Sopenharmony_ci RuntimeOption option; 4534514f5e3Sopenharmony_ci option.SetEnableProfile(true); 4544514f5e3Sopenharmony_ci char currentPath[PATH_MAX + 2]; 4554514f5e3Sopenharmony_ci if (memset_s(currentPath, PATH_MAX, 1, PATH_MAX) != EOK) { 4564514f5e3Sopenharmony_ci ASSERT_TRUE(false); 4574514f5e3Sopenharmony_ci } 4584514f5e3Sopenharmony_ci currentPath[PATH_MAX + 1] = '\0'; 4594514f5e3Sopenharmony_ci option.SetProfileDir(currentPath); 4604514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 4614514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 4624514f5e3Sopenharmony_ci uint32_t checksum = 304293; 4634514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, ""); 4644514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 4654514f5e3Sopenharmony_ci 4664514f5e3Sopenharmony_ci // RecordName is hole 4674514f5e3Sopenharmony_ci MethodLiteral *methodLiteral = new MethodLiteral(EntityId(61)); 4684514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiteral); 4694514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 4704514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), JSTaggedValue::Hole()); 4714514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 4724514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 4734514f5e3Sopenharmony_ci 4744514f5e3Sopenharmony_ci PGOProfilerDecoder loader("", DECODER_THRESHOLD); 4754514f5e3Sopenharmony_ci // path is empty() 4764514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(checksum)); 4774514f5e3Sopenharmony_ci // path size greater than PATH_MAX 4784514f5e3Sopenharmony_ci char path[PATH_MAX + 1] = {'0'}; 4794514f5e3Sopenharmony_ci PGOProfilerDecoder loader1(path, 4); 4804514f5e3Sopenharmony_ci ASSERT_TRUE(!loader1.LoadAndVerify(checksum)); 4814514f5e3Sopenharmony_ci} 4824514f5e3Sopenharmony_ci 4834514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOProfilerDoubleVM) 4844514f5e3Sopenharmony_ci{ 4854514f5e3Sopenharmony_ci mkdir("ark-profiler5/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 4864514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 4874514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 4884514f5e3Sopenharmony_ci EXPECT_GE(methodLiterals.size(), 2); // number of methods 4894514f5e3Sopenharmony_ci mkdir("ark-profiler5/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 4904514f5e3Sopenharmony_ci RuntimeOption option; 4914514f5e3Sopenharmony_ci option.SetEnableProfile(true); 4924514f5e3Sopenharmony_ci // outDir is empty 4934514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler5/"); 4944514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 4954514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 4964514f5e3Sopenharmony_ci JSPandaFileManager::GetInstance()->AddJSPandaFile(pf_); 4974514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 4984514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 4994514f5e3Sopenharmony_ci uint32_t checksum = 304293; 5004514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "sample_test.abc"); 5014514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 5024514f5e3Sopenharmony_ci // worker vm read profile enable from PGOProfilerManager singleton 5034514f5e3Sopenharmony_ci option.SetEnableProfile(false); 5044514f5e3Sopenharmony_ci 5054514f5e3Sopenharmony_ci std::thread t1([&]() { 5064514f5e3Sopenharmony_ci auto vm2 = JSNApi::CreateJSVM(option); 5074514f5e3Sopenharmony_ci vm2->GetJSThread()->ManagedCodeBegin(); 5084514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool2 = vm2->GetFactory()->NewSConstantPool(4); 5094514f5e3Sopenharmony_ci constPool2->SetJSPandaFile(pf_.get()); 5104514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "sample_test.abc"); 5114514f5e3Sopenharmony_ci ASSERT_TRUE(vm2 != nullptr) << "Cannot create Runtime"; 5124514f5e3Sopenharmony_ci 5134514f5e3Sopenharmony_ci JSHandle<Method> method = vm2->GetFactory()->NewSMethod(methodLiterals[0]); 5144514f5e3Sopenharmony_ci method->SetConstantPool(vm2->GetJSThread(), constPool2.GetTaggedValue()); 5154514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm2->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 5164514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm2->GetFactory()->NewFromStdString("sample_test")); 5174514f5e3Sopenharmony_ci func->SetModule(vm2->GetJSThread(), recordName); 5184514f5e3Sopenharmony_ci vm2->GetJSThread()->ManagedCodeEnd(); 5194514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm2); 5204514f5e3Sopenharmony_ci }); 5214514f5e3Sopenharmony_ci { 5224514f5e3Sopenharmony_ci ThreadSuspensionScope scope(vm_->GetJSThread()); 5234514f5e3Sopenharmony_ci t1.join(); 5244514f5e3Sopenharmony_ci } 5254514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("sample_test")); 5264514f5e3Sopenharmony_ci JSHandle<Method> method1 = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 5274514f5e3Sopenharmony_ci JSHandle<Method> method2 = vm_->GetFactory()->NewSMethod(methodLiterals[1]); 5284514f5e3Sopenharmony_ci method1->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 5294514f5e3Sopenharmony_ci method2->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 5304514f5e3Sopenharmony_ci JSHandle<JSFunction> func1 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method1); 5314514f5e3Sopenharmony_ci JSHandle<JSFunction> func2 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method2); 5324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName1(vm_->GetFactory()->NewFromStdString("sample_test")); 5334514f5e3Sopenharmony_ci func1->SetModule(vm_->GetJSThread(), recordName); 5344514f5e3Sopenharmony_ci func2->SetModule(vm_->GetJSThread(), recordName); 5354514f5e3Sopenharmony_ci 5364514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 5374514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 5384514f5e3Sopenharmony_ci 5394514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler5/profiler", DECODER_THRESHOLD); 5404514f5e3Sopenharmony_ci mkdir("ark-profiler5/profiler", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 5414514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 5424514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 5434514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[1]->GetMethodId())); 5444514f5e3Sopenharmony_ci 5454514f5e3Sopenharmony_ci PGOProfilerDecoder loader1("ark-profiler5/modules.ap", DECODER_THRESHOLD); 5464514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 5474514f5e3Sopenharmony_ci ASSERT_TRUE(loader1.LoadAndVerify(checksum)); 5484514f5e3Sopenharmony_ci ASSERT_TRUE(!loader1.Match(pf_.get(), expectRecordName, methodLiterals[1]->GetMethodId())); 5494514f5e3Sopenharmony_ci#else 5504514f5e3Sopenharmony_ci ASSERT_TRUE(!loader1.LoadAndVerify(checksum)); 5514514f5e3Sopenharmony_ci ASSERT_TRUE(loader1.Match(pf_.get(), expectRecordName, methodLiterals[1]->GetMethodId())); 5524514f5e3Sopenharmony_ci#endif 5534514f5e3Sopenharmony_ci 5544514f5e3Sopenharmony_ci unlink("ark-profiler5/modules.ap"); 5554514f5e3Sopenharmony_ci rmdir("ark-profiler5/profiler"); 5564514f5e3Sopenharmony_ci rmdir("ark-profiler5/"); 5574514f5e3Sopenharmony_ci} 5584514f5e3Sopenharmony_ci 5594514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOProfilerDecoderNoHotMethod) 5604514f5e3Sopenharmony_ci{ 5614514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 5624514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 5634514f5e3Sopenharmony_ci EXPECT_GE(methodLiterals.size(), 1); // number of methods 5644514f5e3Sopenharmony_ci mkdir("ark-profiler8/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 5654514f5e3Sopenharmony_ci RuntimeOption option; 5664514f5e3Sopenharmony_ci option.SetEnableProfile(true); 5674514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler8/"); 5684514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 5694514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 5704514f5e3Sopenharmony_ci JSPandaFileManager::GetInstance()->AddJSPandaFile(pf_); 5714514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 5724514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 5734514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 5744514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "sample_test.abc"); 5754514f5e3Sopenharmony_ci 5764514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 5774514f5e3Sopenharmony_ci 5784514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 5794514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 5804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("sample_test")); 5814514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 5824514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 5834514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 5844514f5e3Sopenharmony_ci 5854514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler8/modules.ap", DECODER_THRESHOLD); 5864514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 5874514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 5884514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(checksum)); 5894514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 5904514f5e3Sopenharmony_ci#else 5914514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 5924514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 5934514f5e3Sopenharmony_ci#endif 5944514f5e3Sopenharmony_ci 5954514f5e3Sopenharmony_ci unlink("ark-profiler8/modules.ap"); 5964514f5e3Sopenharmony_ci rmdir("ark-profiler8/"); 5974514f5e3Sopenharmony_ci} 5984514f5e3Sopenharmony_ci 5994514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOProfilerPostTask) 6004514f5e3Sopenharmony_ci{ 6014514f5e3Sopenharmony_ci std::stringstream sourceStream; 6024514f5e3Sopenharmony_ci sourceStream << " .language ECMAScript" << std::endl; 6034514f5e3Sopenharmony_ci for (uint32_t funcIdx = 0; funcIdx < 100; funcIdx++) { 6044514f5e3Sopenharmony_ci sourceStream << " .function void foo" << std::to_string(funcIdx) << "(any a0, any a1, any a2) {}" << std::endl; 6054514f5e3Sopenharmony_ci } 6064514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 6074514f5e3Sopenharmony_ci CreateJSPandaFile(sourceStream.str().c_str(), "ark-profiler9.abc", methodLiterals); 6084514f5e3Sopenharmony_ci EXPECT_EQ(methodLiterals.size(), 100); // number of methods 6094514f5e3Sopenharmony_ci mkdir("ark-profiler9/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 6104514f5e3Sopenharmony_ci RuntimeOption option; 6114514f5e3Sopenharmony_ci option.SetEnableProfile(true); 6124514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler9/"); 6134514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 6144514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 6154514f5e3Sopenharmony_ci JSPandaFileManager::GetInstance()->AddJSPandaFile(pf_); 6164514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 6174514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 6184514f5e3Sopenharmony_ci uint32_t checksum = 304293; 6194514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SetApGenMode(ApGenMode::OVERWRITE); 6204514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "ark-profiler9.abc"); 6214514f5e3Sopenharmony_ci 6224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("ark-profiler9.abc")); 6234514f5e3Sopenharmony_ci for (int i = 61; i < 91; i++) { 6244514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[i]); 6254514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 6264514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 6274514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 6284514f5e3Sopenharmony_ci } 6294514f5e3Sopenharmony_ci 6304514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 6314514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 6324514f5e3Sopenharmony_ci 6334514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler9/modules.ap", DECODER_THRESHOLD); 6344514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 6354514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(checksum)); 6364514f5e3Sopenharmony_ci#else 6374514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 6384514f5e3Sopenharmony_ci#endif 6394514f5e3Sopenharmony_ci CString expectRecordName = "ark-profiler9.abc"; 6404514f5e3Sopenharmony_ci for (int i = 0; i < 100; i++) { 6414514f5e3Sopenharmony_ci EntityId methodId = methodLiterals[i]->GetMethodId(); 6424514f5e3Sopenharmony_ci loader.MatchAndMarkMethod(pf_.get(), expectRecordName, 6434514f5e3Sopenharmony_ci methodLiterals[i]->GetMethodName(pf_.get(), methodId), methodId); 6444514f5e3Sopenharmony_ci } 6454514f5e3Sopenharmony_ci for (int i = 61; i < 91; i++) { 6464514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 6474514f5e3Sopenharmony_ci if (i % 3 == 0) { 6484514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[i]->GetMethodId())); 6494514f5e3Sopenharmony_ci } else { 6504514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.Match(pf_.get(), expectRecordName, methodLiterals[i]->GetMethodId())); 6514514f5e3Sopenharmony_ci#else 6524514f5e3Sopenharmony_ci if (i % 3 == 0) { 6534514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[i]->GetMethodId())); 6544514f5e3Sopenharmony_ci } else { 6554514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[i]->GetMethodId())); 6564514f5e3Sopenharmony_ci#endif 6574514f5e3Sopenharmony_ci } 6584514f5e3Sopenharmony_ci } 6594514f5e3Sopenharmony_ci 6604514f5e3Sopenharmony_ci unlink("ark-profiler9/modules.ap"); 6614514f5e3Sopenharmony_ci rmdir("ark-profiler9/"); 6624514f5e3Sopenharmony_ci} 6634514f5e3Sopenharmony_ci 6644514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, TextToBinary) 6654514f5e3Sopenharmony_ci{ 6664514f5e3Sopenharmony_ci mkdir("ark-profiler10/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 6674514f5e3Sopenharmony_ci 6684514f5e3Sopenharmony_ci std::ofstream file("ark-profiler10/modules.text"); 6694514f5e3Sopenharmony_ci std::string result = "Profiler Version: 0.0.0.1\n"; 6704514f5e3Sopenharmony_ci file.write(result.c_str(), result.size()); 6714514f5e3Sopenharmony_ci result = "\nPanda file sumcheck list: [ 413775942 ]\n"; 6724514f5e3Sopenharmony_ci file.write(result.c_str(), result.size()); 6734514f5e3Sopenharmony_ci result = "\nrecordName: [ 1232/3/CALL_MODE/hello, 234/100/HOTNESS_MODE/h#ello1 ]\n"; 6744514f5e3Sopenharmony_ci file.write(result.c_str(), result.size()); 6754514f5e3Sopenharmony_ci file.close(); 6764514f5e3Sopenharmony_ci 6774514f5e3Sopenharmony_ci PGOProfilerHeader::SetStrictMatch(false); 6784514f5e3Sopenharmony_ci ASSERT_TRUE(PGOProfilerManager::GetInstance()->TextToBinary("ark-profiler10/modules.text", "ark-profiler10/", 2, 6794514f5e3Sopenharmony_ci ApGenMode::OVERWRITE)); 6804514f5e3Sopenharmony_ci 6814514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler10/modules.ap", DECODER_THRESHOLD); 6824514f5e3Sopenharmony_ci ASSERT_TRUE(loader.LoadAndVerify(413775942)); 6834514f5e3Sopenharmony_ci 6844514f5e3Sopenharmony_ci unlink("ark-profiler10/modules.ap"); 6854514f5e3Sopenharmony_ci unlink("ark-profiler10/modules.text"); 6864514f5e3Sopenharmony_ci rmdir("ark-profiler10"); 6874514f5e3Sopenharmony_ci} 6884514f5e3Sopenharmony_ci 6894514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, FailResetProfilerInWorker) 6904514f5e3Sopenharmony_ci{ 6914514f5e3Sopenharmony_ci std::vector<MethodLiteral *> methodLiterals {}; 6924514f5e3Sopenharmony_ci CreateJSPandaFile("sample_test.abc", methodLiterals); 6934514f5e3Sopenharmony_ci EXPECT_GE(methodLiterals.size(), 1); // number of methods 6944514f5e3Sopenharmony_ci mkdir("ark-profiler12/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 6954514f5e3Sopenharmony_ci RuntimeOption option; 6964514f5e3Sopenharmony_ci // Although enableProfile is set in option, but it will not work when isWorker is set. 6974514f5e3Sopenharmony_ci option.SetEnableProfile(true); 6984514f5e3Sopenharmony_ci option.SetIsWorker(); 6994514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler12/"); 7004514f5e3Sopenharmony_ci // PgoProfiler is disabled as default. 7014514f5e3Sopenharmony_ci vm_ = JSNApi::CreateJSVM(option); 7024514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeBegin(); 7034514f5e3Sopenharmony_ci JSPandaFileManager::GetInstance()->AddJSPandaFile(pf_); 7044514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 7054514f5e3Sopenharmony_ci PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum, "sample_test.abc"); 7064514f5e3Sopenharmony_ci ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; 7074514f5e3Sopenharmony_ci 7084514f5e3Sopenharmony_ci JSHandle<Method> method = vm_->GetFactory()->NewSMethod(methodLiterals[0]); 7094514f5e3Sopenharmony_ci 7104514f5e3Sopenharmony_ci JSHandle<ConstantPool> constPool = vm_->GetFactory()->NewSConstantPool(4); 7114514f5e3Sopenharmony_ci constPool->SetJSPandaFile(pf_.get()); 7124514f5e3Sopenharmony_ci method->SetConstantPool(vm_->GetJSThread(), constPool.GetTaggedValue()); 7134514f5e3Sopenharmony_ci JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method); 7144514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("sample_test")); 7154514f5e3Sopenharmony_ci func->SetModule(vm_->GetJSThread(), recordName); 7164514f5e3Sopenharmony_ci vm_->GetJSThread()->ManagedCodeEnd(); 7174514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm_); 7184514f5e3Sopenharmony_ci 7194514f5e3Sopenharmony_ci // Loader 7204514f5e3Sopenharmony_ci ASSERT_FALSE(FileExist("ark-profiler12/modules.ap")); 7214514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler12/modules.ap", DECODER_THRESHOLD); 7224514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 7234514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 7244514f5e3Sopenharmony_ci ASSERT_TRUE(loader.Match(pf_.get(), expectRecordName, methodLiterals[0]->GetMethodId())); 7254514f5e3Sopenharmony_ci rmdir("ark-profiler12/"); 7264514f5e3Sopenharmony_ci} 7274514f5e3Sopenharmony_ci 7284514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 7294514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ProfileCallTest) 7304514f5e3Sopenharmony_ci{ 7314514f5e3Sopenharmony_ci mkdir("ark-profiler13/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 7324514f5e3Sopenharmony_ci const char *targetRecordName = "call_test"; 7334514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler13/", targetRecordName); 7344514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 7354514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 7364514f5e3Sopenharmony_ci 7374514f5e3Sopenharmony_ci // Loader 7384514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler13/modules.ap", 1); 7394514f5e3Sopenharmony_ci PGOProfilerDecoder decoder1("ark-profiler13/modules.ap", 10); 7404514f5e3Sopenharmony_ci PGOProfilerDecoder decoder2("ark-profiler13/modules.ap", 11000); 7414514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 7424514f5e3Sopenharmony_ci ASSERT_TRUE(decoder1.LoadAndVerify(checksum)); 7434514f5e3Sopenharmony_ci ASSERT_TRUE(decoder2.LoadAndVerify(checksum)); 7444514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 7454514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 7464514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 7474514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 7484514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 7494514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 7504514f5e3Sopenharmony_ci decoder1.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 7514514f5e3Sopenharmony_ci decoder2.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 7524514f5e3Sopenharmony_ci // ASSERT_TRUE(decoder.Match(pf_.get(), targetRecordName, methodId)) 7534514f5e3Sopenharmony_ci if (std::string(methodName) == "Test") { 7544514f5e3Sopenharmony_ci ASSERT_TRUE(decoder1.Match(pf_.get(), targetRecordName, methodId)); 7554514f5e3Sopenharmony_ci ASSERT_TRUE(decoder2.Match(pf_.get(), targetRecordName, methodId)); 7564514f5e3Sopenharmony_ci } else { 7574514f5e3Sopenharmony_ci // ASSERT_TRUE(decoder1.Match(pf_.get(), targetRecordName, methodId)) 7584514f5e3Sopenharmony_ci // ASSERT_TRUE(decoder2.Match(pf_.get(), targetRecordName, methodId)) 7594514f5e3Sopenharmony_ci } 7604514f5e3Sopenharmony_ci } 7614514f5e3Sopenharmony_ci unlink("ark-profiler13/modules.ap"); 7624514f5e3Sopenharmony_ci rmdir("ark-profiler13/"); 7634514f5e3Sopenharmony_ci} 7644514f5e3Sopenharmony_ci 7654514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, UseClassTypeTest) 7664514f5e3Sopenharmony_ci{ 7674514f5e3Sopenharmony_ci mkdir("ark-profiler14/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 7684514f5e3Sopenharmony_ci const char *targetRecordName = "class_test"; 7694514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler14/", targetRecordName); 7704514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 7714514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 7724514f5e3Sopenharmony_ci 7734514f5e3Sopenharmony_ci // Loader 7744514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler14/modules.ap", 1); 7754514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 7764514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 7774514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 7784514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 7794514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 7804514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 7814514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 7824514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 7834514f5e3Sopenharmony_ci auto callback = [methodName](uint32_t offset, const PGOType *type) { 7844514f5e3Sopenharmony_ci ASSERT_NE(offset, 0); 7854514f5e3Sopenharmony_ci if (type->IsScalarOpType()) { 7864514f5e3Sopenharmony_ci } else if (type->IsRwOpType()) { 7874514f5e3Sopenharmony_ci auto pgoRWOpType = *reinterpret_cast<const PGORWOpType *>(type); 7884514f5e3Sopenharmony_ci if (std::string(methodName) == "Foot" || std::string(methodName) == "Arm") { 7894514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 1); 7904514f5e3Sopenharmony_ci } else if (std::string(methodName) == "foo" || std::string(methodName) == "Body") { 7914514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 3); 7924514f5e3Sopenharmony_ci } 7934514f5e3Sopenharmony_ci } else { 7944514f5e3Sopenharmony_ci ASSERT_TRUE(true); 7954514f5e3Sopenharmony_ci } 7964514f5e3Sopenharmony_ci }; 7974514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 7984514f5e3Sopenharmony_ci callback); 7994514f5e3Sopenharmony_ci } 8004514f5e3Sopenharmony_ci unlink("ark-profiler14/modules.ap"); 8014514f5e3Sopenharmony_ci rmdir("ark-profiler14/"); 8024514f5e3Sopenharmony_ci} 8034514f5e3Sopenharmony_ci 8044514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, DefineClassTypeTest) 8054514f5e3Sopenharmony_ci{ 8064514f5e3Sopenharmony_ci mkdir("ark-profiler15/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 8074514f5e3Sopenharmony_ci const char *targetRecordName = "class_test"; 8084514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler15/", targetRecordName); 8094514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 8104514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 8114514f5e3Sopenharmony_ci 8124514f5e3Sopenharmony_ci // Loader 8134514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler15/modules.ap", 1); 8144514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 8154514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 8164514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 8174514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 8184514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 8194514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 8204514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 8214514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 8224514f5e3Sopenharmony_ci auto callback = [methodName, &decoder, jsPandaFile = pf_](uint32_t offset, const PGOType *type) { 8234514f5e3Sopenharmony_ci ASSERT_NE(offset, 0); 8244514f5e3Sopenharmony_ci if (type->IsScalarOpType()) { 8254514f5e3Sopenharmony_ci auto sampleType = *reinterpret_cast<const PGOSampleType *>(type); 8264514f5e3Sopenharmony_ci if (sampleType.IsProfileType()) { 8274514f5e3Sopenharmony_ci ASSERT_EQ(std::string(methodName), "func_main_0"); 8284514f5e3Sopenharmony_ci PGOHClassTreeDesc *desc; 8294514f5e3Sopenharmony_ci if (!decoder.GetHClassTreeDesc(sampleType, &desc)) { 8304514f5e3Sopenharmony_ci return; 8314514f5e3Sopenharmony_ci } 8324514f5e3Sopenharmony_ci return; 8334514f5e3Sopenharmony_ci auto classId = EntityId(sampleType.GetProfileType().GetId()); 8344514f5e3Sopenharmony_ci auto className = MethodLiteral::GetMethodName(jsPandaFile.get(), classId); 8354514f5e3Sopenharmony_ci if (std::string(className) == "Arm") { 8364514f5e3Sopenharmony_ci auto superClassId = EntityId(desc->GetProfileType().GetId()); 8374514f5e3Sopenharmony_ci auto superClassName = MethodLiteral::GetMethodName(jsPandaFile.get(), superClassId); 8384514f5e3Sopenharmony_ci ASSERT_EQ(std::string(superClassName), "Body"); 8394514f5e3Sopenharmony_ci } else if (std::string(className) == "Foot") { 8404514f5e3Sopenharmony_ci auto superClassId = EntityId(desc->GetProfileType().GetId()); 8414514f5e3Sopenharmony_ci auto superClassName = MethodLiteral::GetMethodName(jsPandaFile.get(), superClassId); 8424514f5e3Sopenharmony_ci ASSERT_EQ(std::string(superClassName), "Body"); 8434514f5e3Sopenharmony_ci } else { 8444514f5e3Sopenharmony_ci ASSERT_EQ(desc->GetProfileType().GetRaw(), 0); 8454514f5e3Sopenharmony_ci } 8464514f5e3Sopenharmony_ci } 8474514f5e3Sopenharmony_ci } 8484514f5e3Sopenharmony_ci }; 8494514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 8504514f5e3Sopenharmony_ci callback); 8514514f5e3Sopenharmony_ci } 8524514f5e3Sopenharmony_ci unlink("ark-profiler15/modules.ap"); 8534514f5e3Sopenharmony_ci rmdir("ark-profiler15/"); 8544514f5e3Sopenharmony_ci} 8554514f5e3Sopenharmony_ci 8564514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, OpTypeTest) 8574514f5e3Sopenharmony_ci{ 8584514f5e3Sopenharmony_ci mkdir("ark-profiler16/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 8594514f5e3Sopenharmony_ci const char *targetRecordName = "op_type_test"; 8604514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler16/", targetRecordName); 8614514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 8624514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 8634514f5e3Sopenharmony_ci 8644514f5e3Sopenharmony_ci // Loader 8654514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler16/modules.ap", 1); 8664514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 8674514f5e3Sopenharmony_ci std::string types[17] = 8684514f5e3Sopenharmony_ci { "1", "5", "4", "4", "4", "4", "4", "4", "5", "4", "4", "1", "1", "4", "5", "1", "1" }; 8694514f5e3Sopenharmony_ci int index = 0; 8704514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 8714514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 8724514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 8734514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 8744514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 8754514f5e3Sopenharmony_ci if (std::string(methodName) != "sun" && std::string(methodName) != "sun1") { 8764514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 8774514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 8784514f5e3Sopenharmony_ci } 8794514f5e3Sopenharmony_ci auto callback = [methodName, types, &index](uint32_t offset, const PGOType *type) { 8804514f5e3Sopenharmony_ci ASSERT_NE(offset, 0); 8814514f5e3Sopenharmony_ci if (type->IsScalarOpType()) { 8824514f5e3Sopenharmony_ci auto sampleType = *reinterpret_cast<const PGOSampleType *>(type); 8834514f5e3Sopenharmony_ci if (sampleType.IsProfileType()) { 8844514f5e3Sopenharmony_ci return; 8854514f5e3Sopenharmony_ci } 8864514f5e3Sopenharmony_ci if (std::string(methodName) == "advance") { 8874514f5e3Sopenharmony_ci if (sampleType.GetWeight() > 0) { 8884514f5e3Sopenharmony_ci auto trueWeight = sampleType.GetWeight() >> 10; 8894514f5e3Sopenharmony_ci auto falseWeight = sampleType.GetWeight() & 0x7FF; 8904514f5e3Sopenharmony_ci auto primitiveType = sampleType.GetPrimitiveType(); 8914514f5e3Sopenharmony_ci ASSERT_GT(trueWeight, falseWeight); 8924514f5e3Sopenharmony_ci ASSERT_EQ(static_cast<uint32_t>(primitiveType), PGOSampleType::IntType()); 8934514f5e3Sopenharmony_ci } else { 8944514f5e3Sopenharmony_ci ASSERT_EQ(sampleType.GetTypeString(), types[index++]); 8954514f5e3Sopenharmony_ci } 8964514f5e3Sopenharmony_ci } 8974514f5e3Sopenharmony_ci } 8984514f5e3Sopenharmony_ci }; 8994514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 9004514f5e3Sopenharmony_ci callback); 9014514f5e3Sopenharmony_ci } 9024514f5e3Sopenharmony_ci unlink("ark-profiler16/modules.ap"); 9034514f5e3Sopenharmony_ci rmdir("ark-profiler16/"); 9044514f5e3Sopenharmony_ci} 9054514f5e3Sopenharmony_ci 9064514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ArrayProfileTest) 9074514f5e3Sopenharmony_ci{ 9084514f5e3Sopenharmony_ci mkdir("ark-profiler18/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 9094514f5e3Sopenharmony_ci const char *targetRecordName = "array_test"; 9104514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler18/", targetRecordName); 9114514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 9124514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 9134514f5e3Sopenharmony_ci 9144514f5e3Sopenharmony_ci // Loader 9154514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler18/modules.ap", 1); 9164514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 9174514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 9184514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 9194514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 9204514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 9214514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 9224514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 9234514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 9244514f5e3Sopenharmony_ci auto callback = [methodName, &decoder, jsPandaFile = pf_](uint32_t offset, const PGOType *type) { 9254514f5e3Sopenharmony_ci if (type->IsScalarOpType()) { 9264514f5e3Sopenharmony_ci auto sampleType = *reinterpret_cast<const PGOSampleType *>(type); 9274514f5e3Sopenharmony_ci if (sampleType.IsProfileType()) { 9284514f5e3Sopenharmony_ci ASSERT_EQ(std::string(methodName), "func_main_0"); 9294514f5e3Sopenharmony_ci PGOHClassTreeDesc *desc; 9304514f5e3Sopenharmony_ci if (!decoder.GetHClassTreeDesc(sampleType, &desc)) { 9314514f5e3Sopenharmony_ci return; 9324514f5e3Sopenharmony_ci } 9334514f5e3Sopenharmony_ci } 9344514f5e3Sopenharmony_ci } else if (type->IsRwOpType()) { 9354514f5e3Sopenharmony_ci auto pgoRWOpType = *reinterpret_cast<const PGORWOpType *>(type); 9364514f5e3Sopenharmony_ci if (std::string(methodName) == "foo") { 9374514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 3); 9384514f5e3Sopenharmony_ci auto classType = pgoRWOpType.GetObjectInfo(0).GetProfileType(); 9394514f5e3Sopenharmony_ci ASSERT_TRUE(classType.IsBuiltinsArray()); 9404514f5e3Sopenharmony_ci ASSERT_EQ(classType.GetElementsKindBeforeTransition(), ElementsKind::NUMBER); 9414514f5e3Sopenharmony_ci 9424514f5e3Sopenharmony_ci classType = pgoRWOpType.GetObjectInfo(1).GetProfileType(); 9434514f5e3Sopenharmony_ci ASSERT_TRUE(classType.IsBuiltinsArray()); 9444514f5e3Sopenharmony_ci ASSERT_EQ(classType.GetElementsKindBeforeTransition(), ElementsKind::HOLE_INT); 9454514f5e3Sopenharmony_ci 9464514f5e3Sopenharmony_ci classType = pgoRWOpType.GetObjectInfo(2).GetProfileType(); 9474514f5e3Sopenharmony_ci ASSERT_TRUE(classType.IsBuiltinsArray()); 9484514f5e3Sopenharmony_ci ASSERT_EQ(classType.GetElementsKindBeforeTransition(), ElementsKind::TAGGED); 9494514f5e3Sopenharmony_ci } else if (std::string(methodName) == "foo1") { 9504514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 2); 9514514f5e3Sopenharmony_ci auto classType = pgoRWOpType.GetObjectInfo(0).GetProfileType(); 9524514f5e3Sopenharmony_ci ASSERT_TRUE(classType.IsBuiltinsArray()); 9534514f5e3Sopenharmony_ci ASSERT_EQ(classType.GetElementsKindBeforeTransition(), ElementsKind::NUMBER); 9544514f5e3Sopenharmony_ci 9554514f5e3Sopenharmony_ci classType = pgoRWOpType.GetObjectInfo(1).GetProfileType(); 9564514f5e3Sopenharmony_ci ASSERT_TRUE(classType.IsBuiltinsArray()); 9574514f5e3Sopenharmony_ci ASSERT_EQ(classType.GetElementsKindBeforeTransition(), ElementsKind::TAGGED); 9584514f5e3Sopenharmony_ci } else if (std::string(methodName) == "foo2") { 9594514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 1); 9604514f5e3Sopenharmony_ci auto classType = pgoRWOpType.GetObjectInfo(0).GetProfileType(); 9614514f5e3Sopenharmony_ci ASSERT_TRUE(classType.IsBuiltinsArray()); 9624514f5e3Sopenharmony_ci ASSERT_EQ(classType.GetElementsKindBeforeTransition(), ElementsKind::HOLE_TAGGED); 9634514f5e3Sopenharmony_ci } 9644514f5e3Sopenharmony_ci } 9654514f5e3Sopenharmony_ci }; 9664514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 9674514f5e3Sopenharmony_ci callback); 9684514f5e3Sopenharmony_ci } 9694514f5e3Sopenharmony_ci unlink("ark-profiler18/modules.ap"); 9704514f5e3Sopenharmony_ci rmdir("ark-profiler18/"); 9714514f5e3Sopenharmony_ci} 9724514f5e3Sopenharmony_ci 9734514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ObjectLiteralProfileTest) 9744514f5e3Sopenharmony_ci{ 9754514f5e3Sopenharmony_ci mkdir("ark-profiler20/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 9764514f5e3Sopenharmony_ci const char *targetRecordName = "object_literal"; 9774514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler20/", targetRecordName); 9784514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 9794514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 9804514f5e3Sopenharmony_ci 9814514f5e3Sopenharmony_ci // Loader 9824514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler20/modules.ap", 1); 9834514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 9844514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 9854514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 9864514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 9874514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 9884514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 9894514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 9904514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 9914514f5e3Sopenharmony_ci auto callback = [methodName, &decoder, jsPandaFile = pf_](uint32_t offset, const PGOType *type) { 9924514f5e3Sopenharmony_ci if (type->IsScalarOpType()) { 9934514f5e3Sopenharmony_ci auto sampleType = *reinterpret_cast<const PGOSampleType *>(type); 9944514f5e3Sopenharmony_ci if (sampleType.IsProfileType()) { 9954514f5e3Sopenharmony_ci ASSERT_EQ(std::string(methodName), "func_main_0"); 9964514f5e3Sopenharmony_ci PGOHClassTreeDesc *desc; 9974514f5e3Sopenharmony_ci if (!decoder.GetHClassTreeDesc(sampleType, &desc)) { 9984514f5e3Sopenharmony_ci return; 9994514f5e3Sopenharmony_ci } 10004514f5e3Sopenharmony_ci } 10014514f5e3Sopenharmony_ci } else if (type->IsRwOpType()) { 10024514f5e3Sopenharmony_ci auto pgoRWOpType = *reinterpret_cast<const PGORWOpType *>(type); 10034514f5e3Sopenharmony_ci if (std::string(methodName) == "foo") { 10044514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 2); 10054514f5e3Sopenharmony_ci auto classType = PGOSampleType(pgoRWOpType.GetObjectInfo(0).GetProfileType()); 10064514f5e3Sopenharmony_ci PGOHClassTreeDesc *desc; 10074514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.GetHClassTreeDesc(classType, &desc)); 10084514f5e3Sopenharmony_ci 10094514f5e3Sopenharmony_ci classType = PGOSampleType(pgoRWOpType.GetObjectInfo(1).GetProfileType()); 10104514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.GetHClassTreeDesc(classType, &desc)); 10114514f5e3Sopenharmony_ci } 10124514f5e3Sopenharmony_ci } 10134514f5e3Sopenharmony_ci }; 10144514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 10154514f5e3Sopenharmony_ci callback); 10164514f5e3Sopenharmony_ci } 10174514f5e3Sopenharmony_ci unlink("ark-profiler20/modules.ap"); 10184514f5e3Sopenharmony_ci rmdir("ark-profiler20/"); 10194514f5e3Sopenharmony_ci} 10204514f5e3Sopenharmony_ci 10214514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ArraySizeProfileTest) 10224514f5e3Sopenharmony_ci{ 10234514f5e3Sopenharmony_ci mkdir("ark-profiler21/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 10244514f5e3Sopenharmony_ci const char *targetRecordName = "array_size_test"; 10254514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler21/", targetRecordName); 10264514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 10274514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 10284514f5e3Sopenharmony_ci 10294514f5e3Sopenharmony_ci // Loader 10304514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler21/modules.ap", 1); 10314514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 10324514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 10334514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 10344514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 10354514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 10364514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 10374514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 10384514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 10394514f5e3Sopenharmony_ci auto callback = [methodName, jsPandaFile = pf_](uint32_t offset, const PGOType *type) { 10404514f5e3Sopenharmony_ci if (type->IsDefineOpType()) { 10414514f5e3Sopenharmony_ci auto defineOptype = reinterpret_cast<const PGODefineOpType *>(type); 10424514f5e3Sopenharmony_ci if (std::string(methodName) == "foo") { 10434514f5e3Sopenharmony_ci ASSERT_EQ(defineOptype->GetElementsLength(), 4); 10444514f5e3Sopenharmony_ci } else if (std::string(methodName) == "foo1") { 10454514f5e3Sopenharmony_ci ASSERT_EQ(defineOptype->GetElementsLength(), 12); 10464514f5e3Sopenharmony_ci } else if (std::string(methodName) == "foo2") { 10474514f5e3Sopenharmony_ci ASSERT_EQ(defineOptype->GetElementsLength(), 12); 10484514f5e3Sopenharmony_ci } 10494514f5e3Sopenharmony_ci } 10504514f5e3Sopenharmony_ci }; 10514514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 10524514f5e3Sopenharmony_ci callback); 10534514f5e3Sopenharmony_ci } 10544514f5e3Sopenharmony_ci unlink("ark-profiler21/modules.ap"); 10554514f5e3Sopenharmony_ci rmdir("ark-profiler21/"); 10564514f5e3Sopenharmony_ci} 10574514f5e3Sopenharmony_ci 10584514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, StringEqualProfileTest) 10594514f5e3Sopenharmony_ci{ 10604514f5e3Sopenharmony_ci mkdir("ark-profiler22/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 10614514f5e3Sopenharmony_ci const char *targetRecordName = "string_equal"; 10624514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler22/", targetRecordName); 10634514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 10644514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 10654514f5e3Sopenharmony_ci 10664514f5e3Sopenharmony_ci // Loader 10674514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler22/modules.ap", 1); 10684514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 10694514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 10704514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 10714514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 10724514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 10734514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 10744514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 10754514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 10764514f5e3Sopenharmony_ci auto callback = [methodName, jsPandaFile = pf_](uint32_t offset, const PGOType *type) { 10774514f5e3Sopenharmony_ci if (type->IsScalarOpType()) { 10784514f5e3Sopenharmony_ci auto sampleType = *reinterpret_cast<const PGOSampleType *>(type); 10794514f5e3Sopenharmony_ci if (sampleType.IsProfileType()) { 10804514f5e3Sopenharmony_ci return; 10814514f5e3Sopenharmony_ci } 10824514f5e3Sopenharmony_ci if (std::string(methodName) == "foo1" || 10834514f5e3Sopenharmony_ci std::string(methodName) == "foo2") { 10844514f5e3Sopenharmony_ci auto primitiveType = sampleType.GetPrimitiveType(); 10854514f5e3Sopenharmony_ci ASSERT_EQ(static_cast<uint32_t>(primitiveType), PGOSampleType::StringType()); 10864514f5e3Sopenharmony_ci } 10874514f5e3Sopenharmony_ci } 10884514f5e3Sopenharmony_ci }; 10894514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 10904514f5e3Sopenharmony_ci callback); 10914514f5e3Sopenharmony_ci } 10924514f5e3Sopenharmony_ci unlink("ark-profiler22/modules.ap"); 10934514f5e3Sopenharmony_ci rmdir("ark-profiler22/"); 10944514f5e3Sopenharmony_ci} 10954514f5e3Sopenharmony_ci 10964514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, BuiltinsTest) 10974514f5e3Sopenharmony_ci{ 10984514f5e3Sopenharmony_ci mkdir("ark-profiler23/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 10994514f5e3Sopenharmony_ci const char *targetRecordName = "builtins_test"; 11004514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler23/", targetRecordName); 11014514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 11024514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 11034514f5e3Sopenharmony_ci 11044514f5e3Sopenharmony_ci // Loader 11054514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler23/modules.ap", 1); 11064514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 11074514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 11084514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 11094514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 11104514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 11114514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 11124514f5e3Sopenharmony_ci if (std::string(methodName) != "ArrayList") { 11134514f5e3Sopenharmony_ci decoder.MatchAndMarkMethod(pf_.get(), targetRecordName, methodName, methodId); 11144514f5e3Sopenharmony_ci ASSERT_TRUE(!decoder.Match(pf_.get(), targetRecordName, methodId)); 11154514f5e3Sopenharmony_ci } 11164514f5e3Sopenharmony_ci auto callback = [methodName](uint32_t offset, const PGOType *type) { 11174514f5e3Sopenharmony_ci ASSERT_NE(offset, 0); 11184514f5e3Sopenharmony_ci if (type->IsRwOpType() && std::string(methodName) == "A") { 11194514f5e3Sopenharmony_ci auto pgoRWOpType = *reinterpret_cast<const PGORWOpType *>(type); 11204514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 1); 11214514f5e3Sopenharmony_ci } 11224514f5e3Sopenharmony_ci }; 11234514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 11244514f5e3Sopenharmony_ci callback); 11254514f5e3Sopenharmony_ci } 11264514f5e3Sopenharmony_ci unlink("ark-profiler23/modules.ap"); 11274514f5e3Sopenharmony_ci rmdir("ark-profiler23/"); 11284514f5e3Sopenharmony_ci} 11294514f5e3Sopenharmony_ci 11304514f5e3Sopenharmony_ci#endif 11314514f5e3Sopenharmony_ci 11324514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 11334514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, FileConsistencyCheck) 11344514f5e3Sopenharmony_ci{ 11354514f5e3Sopenharmony_ci mkdir("ark-profiler17/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 11364514f5e3Sopenharmony_ci const char *targetRecordName = "sample_test"; 11374514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler17/", targetRecordName); 11384514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 11394514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 11404514f5e3Sopenharmony_ci 11414514f5e3Sopenharmony_ci // write to corrupt the ap file's consistency 11424514f5e3Sopenharmony_ci std::ofstream fWriter("ark-profiler17/modules.ap", std::fstream::app); 11434514f5e3Sopenharmony_ci 11444514f5e3Sopenharmony_ci fWriter.write(reinterpret_cast<char *>(&checksum), sizeof(checksum)); 11454514f5e3Sopenharmony_ci fWriter.seekp(100); 11464514f5e3Sopenharmony_ci fWriter.write(reinterpret_cast<char *>(&checksum), sizeof(checksum)); 11474514f5e3Sopenharmony_ci fWriter.close(); 11484514f5e3Sopenharmony_ci 11494514f5e3Sopenharmony_ci // Loader 11504514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler17/modules.ap", DECODER_THRESHOLD); 11514514f5e3Sopenharmony_ci ASSERT_FALSE(loader.LoadAndVerify(checksum)); 11524514f5e3Sopenharmony_ci unlink("ark-profiler17/modules.ap"); 11534514f5e3Sopenharmony_ci rmdir("ark-profiler17/"); 11544514f5e3Sopenharmony_ci} 11554514f5e3Sopenharmony_ci 11564514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, MergeApSelfTwice) 11574514f5e3Sopenharmony_ci{ 11584514f5e3Sopenharmony_ci mkdir("ark-profiler18/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 11594514f5e3Sopenharmony_ci const char *targetRecordName = "op_type_test"; 11604514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler18/", targetRecordName); 11614514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 11624514f5e3Sopenharmony_ci 11634514f5e3Sopenharmony_ci // Loader 11644514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler18/modules_merge.ap", 1); 11654514f5e3Sopenharmony_ci PGOProfilerDecoder decoderSingle("ark-profiler18/modules.ap", 1); 11664514f5e3Sopenharmony_ci ASSERT_TRUE(PGOProfilerManager::MergeApFiles("ark-profiler18/modules.ap:ark-profiler18/modules.ap", 11674514f5e3Sopenharmony_ci "ark-profiler18/modules_merge.ap", 1, ApGenMode::OVERWRITE)); 11684514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadFull()); 11694514f5e3Sopenharmony_ci ASSERT_TRUE(decoderSingle.LoadFull()); 11704514f5e3Sopenharmony_ci 11714514f5e3Sopenharmony_ci auto doubleCount = 11724514f5e3Sopenharmony_ci decoder.GetRecordDetailInfos().GetRecordInfos().begin()->second->GetMethodInfos().begin()->second->GetCount(); 11734514f5e3Sopenharmony_ci auto singleCount = decoderSingle.GetRecordDetailInfos() 11744514f5e3Sopenharmony_ci .GetRecordInfos() 11754514f5e3Sopenharmony_ci .begin() 11764514f5e3Sopenharmony_ci ->second->GetMethodInfos() 11774514f5e3Sopenharmony_ci .begin() 11784514f5e3Sopenharmony_ci ->second->GetCount(); 11794514f5e3Sopenharmony_ci ASSERT_EQ(doubleCount, singleCount + singleCount); 11804514f5e3Sopenharmony_ci 11814514f5e3Sopenharmony_ci unlink("ark-profiler18/modules.ap"); 11824514f5e3Sopenharmony_ci unlink("ark-profiler18/modules_merge.ap"); 11834514f5e3Sopenharmony_ci rmdir("ark-profiler18/"); 11844514f5e3Sopenharmony_ci} 11854514f5e3Sopenharmony_ci#endif 11864514f5e3Sopenharmony_ci 11874514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, RuntimeMerge) 11884514f5e3Sopenharmony_ci{ 11894514f5e3Sopenharmony_ci mkdir("ark-profiler19/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 11904514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler19/", "truck"); 11914514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler19/", "call_test"); 11924514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler19/", "truck"); 11934514f5e3Sopenharmony_ci 11944514f5e3Sopenharmony_ci // Loader 11954514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler19/modules.ap", DECODER_THRESHOLD); 11964514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 11974514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 11984514f5e3Sopenharmony_ci std::unordered_map<std::string, std::unordered_map<std::string, std::vector<PGOMethodId>>> methodIdInAp; 11994514f5e3Sopenharmony_ci ParseRelatedPandaFileMethods(loader, methodIdInAp); 12004514f5e3Sopenharmony_ci ASSERT_EQ(methodIdInAp.size(), 3); 12014514f5e3Sopenharmony_ci CheckApMethods(methodIdInAp); 12024514f5e3Sopenharmony_ci#else 12034514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 12044514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 12054514f5e3Sopenharmony_ci#endif 12064514f5e3Sopenharmony_ci unlink("ark-profiler19/modules.ap"); 12074514f5e3Sopenharmony_ci rmdir("ark-profiler19/"); 12084514f5e3Sopenharmony_ci} 12094514f5e3Sopenharmony_ci 12104514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ProfdumpMerge) 12114514f5e3Sopenharmony_ci{ 12124514f5e3Sopenharmony_ci mkdir("ark-profiler20/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 12134514f5e3Sopenharmony_ci 12144514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler20/merge_file_1.ap", "merge_file_1"); 12154514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler20/merge_file_2.ap", "merge_file_2"); 12164514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler20/merge_file_3.ap", "merge_file_3"); 12174514f5e3Sopenharmony_ci 12184514f5e3Sopenharmony_ci // Loader 12194514f5e3Sopenharmony_ci PGOProfilerDecoder loader("ark-profiler20/merged.ap", DECODER_THRESHOLD); 12204514f5e3Sopenharmony_ci ASSERT_TRUE(PGOProfilerManager::MergeApFiles( 12214514f5e3Sopenharmony_ci "ark-profiler20/merge_file_1.ap:ark-profiler20/merge_file_2.ap:ark-profiler20/merge_file_3.ap", 12224514f5e3Sopenharmony_ci "ark-profiler20/merged.ap", 1, ApGenMode::OVERWRITE)); 12234514f5e3Sopenharmony_ci 12244514f5e3Sopenharmony_ci CString expectRecordName = "sample_test"; 12254514f5e3Sopenharmony_ci#if defined(SUPPORT_ENABLE_ASM_INTERP) 12264514f5e3Sopenharmony_ci std::unordered_map<std::string, std::unordered_map<std::string, std::vector<PGOMethodId>>> methodIdInAp; 12274514f5e3Sopenharmony_ci ParseRelatedPandaFileMethods(loader, methodIdInAp); 12284514f5e3Sopenharmony_ci ASSERT_EQ(methodIdInAp.size(), 3); 12294514f5e3Sopenharmony_ci CheckApMethods(methodIdInAp); 12304514f5e3Sopenharmony_ci#else 12314514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 12324514f5e3Sopenharmony_ci ASSERT_TRUE(!loader.LoadAndVerify(checksum)); 12334514f5e3Sopenharmony_ci#endif 12344514f5e3Sopenharmony_ci unlink("ark-profiler20/merge_file_1.ap"); 12354514f5e3Sopenharmony_ci unlink("ark-profiler20/merge_file_2.ap"); 12364514f5e3Sopenharmony_ci unlink("ark-profiler20/merge_file_3.ap"); 12374514f5e3Sopenharmony_ci unlink("ark-profiler20/merged.ap"); 12384514f5e3Sopenharmony_ci rmdir("ark-profiler20/"); 12394514f5e3Sopenharmony_ci} 12404514f5e3Sopenharmony_ci 12414514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ApVersionMatchCheck) 12424514f5e3Sopenharmony_ci{ 12434514f5e3Sopenharmony_ci mkdir("ark-ApVersionMatchCheck/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 12444514f5e3Sopenharmony_ci const char *targetRecordName = "sample_test"; 12454514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-ApVersionMatchCheck/", targetRecordName); 12464514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 12474514f5e3Sopenharmony_ci 12484514f5e3Sopenharmony_ci PGOProfilerEncoderMock encoder("ark-ApVersionMatchCheck/modules.ap", DECODER_THRESHOLD, 12494514f5e3Sopenharmony_ci PGOProfilerEncoder::ApGenMode::MERGE); 12504514f5e3Sopenharmony_ci encoder.InitializeData(); 12514514f5e3Sopenharmony_ci encoder.SetVersion(PGOProfilerHeader::PROFILE_TYPE_WITH_ABC_ID_MINI_VERSION); 12524514f5e3Sopenharmony_ci encoder.Save(); 12534514f5e3Sopenharmony_ci 12544514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-ApVersionMatchCheck/modules.ap", DECODER_THRESHOLD); 12554514f5e3Sopenharmony_ci PGOProfilerHeader::SetStrictMatch(true); 12564514f5e3Sopenharmony_ci ASSERT_FALSE(decoder.LoadFull()); 12574514f5e3Sopenharmony_ci PGOProfilerHeader::SetStrictMatch(false); 12584514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadFull()); 12594514f5e3Sopenharmony_ci 12604514f5e3Sopenharmony_ci unlink("ark-ApVersionMatchCheck/modules.ap"); 12614514f5e3Sopenharmony_ci unlink("ark-ApVersionMatchCheck/"); 12624514f5e3Sopenharmony_ci} 12634514f5e3Sopenharmony_ci 12644514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, TypedArrayOnHeap) 12654514f5e3Sopenharmony_ci{ 12664514f5e3Sopenharmony_ci mkdir("ark-profiler24/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 12674514f5e3Sopenharmony_ci const char *targetRecordName = "typedarray_length"; 12684514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler24/", targetRecordName); 12694514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 12704514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 12714514f5e3Sopenharmony_ci 12724514f5e3Sopenharmony_ci // Loader 12734514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler24/modules.ap", 1); 12744514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 12754514f5e3Sopenharmony_ci auto methodLiterals = pf_->GetMethodLiteralMap(); 12764514f5e3Sopenharmony_ci for (auto iter : methodLiterals) { 12774514f5e3Sopenharmony_ci auto methodLiteral = iter.second; 12784514f5e3Sopenharmony_ci auto methodId = methodLiteral->GetMethodId(); 12794514f5e3Sopenharmony_ci auto methodName = methodLiteral->GetMethodName(pf_.get(), methodId); 12804514f5e3Sopenharmony_ci auto callback = [methodName](uint32_t offset, const PGOType *type) { 12814514f5e3Sopenharmony_ci ASSERT_NE(offset, 0); 12824514f5e3Sopenharmony_ci if (type->IsRwOpType() && std::string(methodName) == "test") { 12834514f5e3Sopenharmony_ci auto pgoRWOpType = *reinterpret_cast<const PGORWOpType *>(type); 12844514f5e3Sopenharmony_ci ASSERT_TRUE(pgoRWOpType.GetCount() == 1); 12854514f5e3Sopenharmony_ci } 12864514f5e3Sopenharmony_ci }; 12874514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, methodLiteral, 12884514f5e3Sopenharmony_ci callback); 12894514f5e3Sopenharmony_ci } 12904514f5e3Sopenharmony_ci unlink("ark-profiler24/modules.ap"); 12914514f5e3Sopenharmony_ci rmdir("ark-profiler24/"); 12924514f5e3Sopenharmony_ci} 12934514f5e3Sopenharmony_ci 12944514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ProfileTypeConstructor) 12954514f5e3Sopenharmony_ci{ 12964514f5e3Sopenharmony_ci mkdir("ark-profiler25/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 12974514f5e3Sopenharmony_ci const char *targetRecordName = "typedarray_length"; 12984514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler25/", targetRecordName); 12994514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 13004514f5e3Sopenharmony_ci ApEntityId inValidId = 555; 13014514f5e3Sopenharmony_ci ApEntityId validId = 64; 13024514f5e3Sopenharmony_ci // Loader 13034514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler25/modules.ap", 1); 13044514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadFull()); 13054514f5e3Sopenharmony_ci 13064514f5e3Sopenharmony_ci auto invalidRes = ProfileTypeRef(inValidId); 13074514f5e3Sopenharmony_ci auto expectFalse = ProfileType::CreateFromProfileTypeRef(decoder.GetRecordDetailInfos(), invalidRes); 13084514f5e3Sopenharmony_ci EXPECT_FALSE(expectFalse.has_value()); 13094514f5e3Sopenharmony_ci 13104514f5e3Sopenharmony_ci auto validRes = ProfileTypeRef(validId); 13114514f5e3Sopenharmony_ci auto expectTrue = ProfileType::CreateFromProfileTypeRef(decoder.GetRecordDetailInfos(), validRes); 13124514f5e3Sopenharmony_ci EXPECT_TRUE(expectTrue.has_value()); 13134514f5e3Sopenharmony_ci unlink("ark-profiler25/modules.ap"); 13144514f5e3Sopenharmony_ci rmdir("ark-profiler25/"); 13154514f5e3Sopenharmony_ci} 13164514f5e3Sopenharmony_ci 13174514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, CompatibleWithAOTFileTest) 13184514f5e3Sopenharmony_ci{ 13194514f5e3Sopenharmony_ci constexpr uint32_t CHECKSUM = 1; 13204514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("", DECODER_THRESHOLD); 13214514f5e3Sopenharmony_ci EXPECT_TRUE(decoder.LoadAndVerify(CHECKSUM)); 13224514f5e3Sopenharmony_ci EXPECT_FALSE(decoder.IsCompatibleWithAOTFile()); 13234514f5e3Sopenharmony_ci} 13244514f5e3Sopenharmony_ci 13254514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, ExternalMethodLiteralTest) 13264514f5e3Sopenharmony_ci{ 13274514f5e3Sopenharmony_ci mkdir("ark-profiler26/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 13284514f5e3Sopenharmony_ci const char *targetRecordName = "typedarray_length"; 13294514f5e3Sopenharmony_ci ExecuteAndLoadJSPandaFile("ark-profiler26/", targetRecordName); 13304514f5e3Sopenharmony_ci ASSERT_NE(pf_, nullptr); 13314514f5e3Sopenharmony_ci uint32_t checksum = pf_->GetChecksum(); 13324514f5e3Sopenharmony_ci 13334514f5e3Sopenharmony_ci PGOProfilerDecoder decoder("ark-profiler26/modules.ap", DECODER_THRESHOLD); 13344514f5e3Sopenharmony_ci ASSERT_TRUE(decoder.LoadAndVerify(checksum)); 13354514f5e3Sopenharmony_ci auto callback = []([[maybe_unused]] uint32_t offset, [[maybe_unused]] const PGOType *type) { 13364514f5e3Sopenharmony_ci EXPECT_TRUE(false); 13374514f5e3Sopenharmony_ci }; 13384514f5e3Sopenharmony_ci decoder.GetTypeInfo(pf_.get(), targetRecordName, nullptr, 13394514f5e3Sopenharmony_ci callback); 13404514f5e3Sopenharmony_ci unlink("ark-profiler26/modules.ap"); 13414514f5e3Sopenharmony_ci rmdir("ark-profiler26/"); 13424514f5e3Sopenharmony_ci} 13434514f5e3Sopenharmony_ci 13444514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGOObjectInfoOperatorLessThanTest) 13454514f5e3Sopenharmony_ci{ 13464514f5e3Sopenharmony_ci constexpr uint64_t rawTypeLess = 0; 13474514f5e3Sopenharmony_ci constexpr uint64_t rawTypeGreater = 1; 13484514f5e3Sopenharmony_ci 13494514f5e3Sopenharmony_ci ProfileType profileTypeLess(rawTypeLess); 13504514f5e3Sopenharmony_ci ProfileType profileTypeGreater(rawTypeGreater); 13514514f5e3Sopenharmony_ci 13524514f5e3Sopenharmony_ci PGOObjectInfo objectInfoLess(profileTypeLess, profileTypeGreater, profileTypeGreater, 13534514f5e3Sopenharmony_ci profileTypeGreater, profileTypeGreater, profileTypeGreater, PGOSampleType()); 13544514f5e3Sopenharmony_ci PGOObjectInfo objectInfoGreater(profileTypeGreater, profileTypeLess, profileTypeGreater, 13554514f5e3Sopenharmony_ci profileTypeGreater, profileTypeGreater, profileTypeGreater, PGOSampleType()); 13564514f5e3Sopenharmony_ci 13574514f5e3Sopenharmony_ci EXPECT_TRUE(objectInfoLess < objectInfoGreater); 13584514f5e3Sopenharmony_ci EXPECT_FALSE(objectInfoGreater < objectInfoLess); 13594514f5e3Sopenharmony_ci} 13604514f5e3Sopenharmony_ci 13614514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGODisableWithAOTFileWorkloadTest) 13624514f5e3Sopenharmony_ci{ 13634514f5e3Sopenharmony_ci mkdir("ark-profiler27/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 13644514f5e3Sopenharmony_ci std::ofstream fWriter("ark-profiler27/tmp.an", std::fstream::app); 13654514f5e3Sopenharmony_ci fWriter.close(); 13664514f5e3Sopenharmony_ci const char *targetRecordName = "typedarray_length"; 13674514f5e3Sopenharmony_ci std::string targetAbcPath = std::string(TARGET_ABC_PATH) + targetRecordName + ".abc"; 13684514f5e3Sopenharmony_ci // Force set in advance to simulate AOT/JIT enable list check is passed in UT 13694514f5e3Sopenharmony_ci ecmascript::AnFileDataManager::GetInstance()->SetEnable(true); 13704514f5e3Sopenharmony_ci ecmascript::AnFileDataManager::GetInstance()->SetDir("ark-profiler27/tmp"); 13714514f5e3Sopenharmony_ci RuntimeOption option; 13724514f5e3Sopenharmony_ci option.SetEnableProfile(true); 13734514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler27/"); 13744514f5e3Sopenharmony_ci EcmaVM *ecmaVM = JSNApi::CreateJSVM(option); 13754514f5e3Sopenharmony_ci JSNApi::LoadAotFile(ecmaVM, ""); 13764514f5e3Sopenharmony_ci EXPECT_TRUE(ecmaVM->IsEnablePGOProfiler()); 13774514f5e3Sopenharmony_ci auto result = JSNApi::Execute(ecmaVM, targetAbcPath, targetRecordName, false); 13784514f5e3Sopenharmony_ci EXPECT_TRUE(result); 13794514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(ecmaVM); 13804514f5e3Sopenharmony_ci EXPECT_TRUE(FileExist("ark-profiler27/modules.ap")); 13814514f5e3Sopenharmony_ci unlink("ark-profiler27/modules.ap"); 13824514f5e3Sopenharmony_ci unlink("ark-profiler27/tmp.an"); 13834514f5e3Sopenharmony_ci rmdir("ark-profiler27/"); 13844514f5e3Sopenharmony_ci} 13854514f5e3Sopenharmony_ci 13864514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGODisableWithAOTFileAppTest) 13874514f5e3Sopenharmony_ci{ 13884514f5e3Sopenharmony_ci mkdir("ark-profiler27/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 13894514f5e3Sopenharmony_ci std::ofstream fWriter("ark-profiler27/tmp.an", std::fstream::app); 13904514f5e3Sopenharmony_ci fWriter.close(); 13914514f5e3Sopenharmony_ci const char *targetRecordName = "typedarray_length"; 13924514f5e3Sopenharmony_ci std::string targetAbcPath = std::string(TARGET_ABC_PATH) + targetRecordName + ".abc"; 13934514f5e3Sopenharmony_ci // Force set in advance to simulate AOT/JIT enable list check is passed in UT 13944514f5e3Sopenharmony_ci ecmascript::AnFileDataManager::GetInstance()->SetEnable(true); 13954514f5e3Sopenharmony_ci ecmascript::AnFileDataManager::GetInstance()->SetDir("ark-profiler27/tmp"); 13964514f5e3Sopenharmony_ci RuntimeOption option; 13974514f5e3Sopenharmony_ci EcmaVM *ecmaVM = JSNApi::CreateJSVM(option); 13984514f5e3Sopenharmony_ci option.SetEnableProfile(true); 13994514f5e3Sopenharmony_ci option.SetProfileDir("ark-profiler27/"); 14004514f5e3Sopenharmony_ci JSNApi::PreFork(ecmaVM); 14014514f5e3Sopenharmony_ci JSNApi::PostFork(ecmaVM, option); 14024514f5e3Sopenharmony_ci EXPECT_TRUE(ecmaVM->IsEnablePGOProfiler()); 14034514f5e3Sopenharmony_ci JSNApi::LoadAotFile(ecmaVM, ""); 14044514f5e3Sopenharmony_ci EXPECT_FALSE(ecmaVM->IsEnablePGOProfiler()); 14054514f5e3Sopenharmony_ci auto result = JSNApi::Execute(ecmaVM, targetAbcPath, targetRecordName, false); 14064514f5e3Sopenharmony_ci EXPECT_TRUE(result); 14074514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(ecmaVM); 14084514f5e3Sopenharmony_ci EXPECT_FALSE(FileExist("ark-profiler27/modules.ap")); 14094514f5e3Sopenharmony_ci unlink("ark-profiler27/tmp.an"); 14104514f5e3Sopenharmony_ci rmdir("ark-profiler27/"); 14114514f5e3Sopenharmony_ci} 14124514f5e3Sopenharmony_ci 14134514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, PGODisableUnderAOTFailTest) 14144514f5e3Sopenharmony_ci{ 14154514f5e3Sopenharmony_ci std::map<std::string, int32_t> mockAOTCompileStatusMap; 14164514f5e3Sopenharmony_ci mockAOTCompileStatusMap["module1"] = 0; 14174514f5e3Sopenharmony_ci mockAOTCompileStatusMap["module2"] = 1; 14184514f5e3Sopenharmony_ci mockAOTCompileStatusMap["module3"] = 4; 14194514f5e3Sopenharmony_ci RuntimeOption option; 14204514f5e3Sopenharmony_ci { 14214514f5e3Sopenharmony_ci // Not update existing setting when AOT compilation uninitialized or succeed 14224514f5e3Sopenharmony_ci EcmaVM *ecmaVM = JSNApi::CreateJSVM(option); 14234514f5e3Sopenharmony_ci RuntimeOption localOption = option; 14244514f5e3Sopenharmony_ci localOption.SetEnableProfile(true); 14254514f5e3Sopenharmony_ci localOption.SetAOTCompileStatusMap(mockAOTCompileStatusMap); 14264514f5e3Sopenharmony_ci JSNApi::PreFork(ecmaVM); 14274514f5e3Sopenharmony_ci JSNApi::PostFork(ecmaVM, localOption); 14284514f5e3Sopenharmony_ci EXPECT_FALSE(ecmaVM->GetJSOptions().GetAOTHasException()); 14294514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(ecmaVM); 14304514f5e3Sopenharmony_ci } 14314514f5e3Sopenharmony_ci { 14324514f5e3Sopenharmony_ci // Disable existing setting when AOT compilation failed 14334514f5e3Sopenharmony_ci EcmaVM *ecmaVM = JSNApi::CreateJSVM(option); 14344514f5e3Sopenharmony_ci RuntimeOption localOption = option; 14354514f5e3Sopenharmony_ci mockAOTCompileStatusMap["module4"] = 2; 14364514f5e3Sopenharmony_ci localOption.SetAOTCompileStatusMap(mockAOTCompileStatusMap); 14374514f5e3Sopenharmony_ci JSNApi::PreFork(ecmaVM); 14384514f5e3Sopenharmony_ci JSNApi::PostFork(ecmaVM, localOption); 14394514f5e3Sopenharmony_ci EXPECT_TRUE(ecmaVM->GetJSOptions().GetAOTHasException()); 14404514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(ecmaVM); 14414514f5e3Sopenharmony_ci } 14424514f5e3Sopenharmony_ci { 14434514f5e3Sopenharmony_ci // Disable existing setting when AOT compilation crashed 14444514f5e3Sopenharmony_ci EcmaVM *ecmaVM = JSNApi::CreateJSVM(option); 14454514f5e3Sopenharmony_ci RuntimeOption localOption = option; 14464514f5e3Sopenharmony_ci mockAOTCompileStatusMap["module4"] = 3; 14474514f5e3Sopenharmony_ci localOption.SetAOTCompileStatusMap(mockAOTCompileStatusMap); 14484514f5e3Sopenharmony_ci JSNApi::PreFork(ecmaVM); 14494514f5e3Sopenharmony_ci JSNApi::PostFork(ecmaVM, localOption); 14504514f5e3Sopenharmony_ci EXPECT_TRUE(ecmaVM->GetJSOptions().GetAOTHasException()); 14514514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(ecmaVM); 14524514f5e3Sopenharmony_ci } 14534514f5e3Sopenharmony_ci} 14544514f5e3Sopenharmony_ci 14554514f5e3Sopenharmony_ciHWTEST_F_L0(PGOProfilerTest, EnableForceICTest) 14564514f5e3Sopenharmony_ci{ 14574514f5e3Sopenharmony_ci RuntimeOption option; 14584514f5e3Sopenharmony_ci EcmaVM* ecmaVM = JSNApi::CreateJSVM(option); 14594514f5e3Sopenharmony_ci JSThread* jsThread = ecmaVM->GetJSThread(); 14604514f5e3Sopenharmony_ci JSRuntimeOptions& jsOption = ecmaVM->GetJSOptions(); 14614514f5e3Sopenharmony_ci EXPECT_TRUE(jsThread->IsEnableForceIC()); 14624514f5e3Sopenharmony_ci EXPECT_TRUE(jsOption.IsEnableForceIC()); 14634514f5e3Sopenharmony_ci EXPECT_TRUE(ecmascript::pgo::PGOProfilerManager::GetInstance()->IsEnableForceIC()); 14644514f5e3Sopenharmony_ci 14654514f5e3Sopenharmony_ci JSNApi::PreFork(ecmaVM); 14664514f5e3Sopenharmony_ci JSNApi::PostFork(ecmaVM, option); 14674514f5e3Sopenharmony_ci EXPECT_FALSE(jsThread->IsEnableForceIC()); 14684514f5e3Sopenharmony_ci EXPECT_FALSE(jsOption.IsEnableForceIC()); 14694514f5e3Sopenharmony_ci EXPECT_FALSE(ecmascript::pgo::PGOProfilerManager::GetInstance()->IsEnableForceIC()); 14704514f5e3Sopenharmony_ci 14714514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(ecmaVM); 14724514f5e3Sopenharmony_ci} 14734514f5e3Sopenharmony_ci} // namespace panda::test 1474