Disable PGO profile when an/ai file exists

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IAQODS

Signed-off-by: zhaoziming <zhaoziming9@huawei.com>
Change-Id: I934e0954dfc325084c0088b8ce48a2578a885a48
This commit is contained in:
zhaoziming 2024-09-11 23:22:22 +08:00
parent 12d83dd279
commit 183bd39dda
7 changed files with 57 additions and 1 deletions

View File

@ -70,6 +70,16 @@ JsAotReaderCallback AOTFileManager::GetJsAotReader()
}
#endif
bool AOTFileManager::AOTFileExist(const std::string &aotFileName, const std::string &extension)
{
std::string realPath;
std::string filename = aotFileName + extension;
if (!RealPath(filename, realPath, false)) {
return false;
}
return FileExist(realPath.c_str());
}
void AOTFileManager::DumpAOTInfo()
{
AnFileDataManager *m = AnFileDataManager::GetInstance();

View File

@ -164,6 +164,7 @@ public:
static bool TryReadLock();
static bool InsideStub(uintptr_t pc);
static bool InsideAOT(uintptr_t pc);
static bool AOTFileExist(const std::string &aotFileName, const std::string &extension);
bool IsEnableAOT() const;
void Iterate(const RootVisitor& v);

View File

@ -1115,6 +1115,7 @@ void EcmaContext::LoadStubFile()
bool EcmaContext::LoadAOTFilesInternal(const std::string& aotFileName)
{
#ifdef AOT_ESCAPE_ENABLE
vm_->DisablePGOProfilerWithAOTFile(aotFileName);
std::string bundleName = pgo::PGOProfilerManager::GetInstance()->GetBundleName();
if (AotCrashInfo::GetInstance().IsAotEscapedOrNotInEnableList(vm_, bundleName)) {
return false;

View File

@ -208,6 +208,16 @@ void EcmaVM::ResetPGOProfiler()
}
}
void EcmaVM::DisablePGOProfilerWithAOTFile(const std::string &aotFileName)
{
if (AOTFileManager::AOTFileExist(aotFileName, AOTFileManager::FILE_EXTENSION_AN) ||
AOTFileManager::AOTFileExist(aotFileName, AOTFileManager::FILE_EXTENSION_AI)) {
options_.SetEnablePGOProfiler(false);
PGOProfilerManager::GetInstance()->SetDisablePGO(true);
ResetPGOProfiler();
}
}
bool EcmaVM::IsEnablePGOProfiler() const
{
if (options_.IsWorker()) {

View File

@ -160,6 +160,7 @@ public:
void InitializePGOProfiler();
void ResetPGOProfiler();
void DisablePGOProfilerWithAOTFile(const std::string &aotFileName);
bool PUBLIC_API IsEnablePGOProfiler() const;
bool PUBLIC_API IsEnableElementsKind() const;

View File

@ -106,9 +106,10 @@ public:
return profiler;
}
// Return false if force disabled or never initialized
bool IsEnable() const
{
return encoder_ && encoder_->IsInitialized();
return !disablePGO_ && encoder_ && encoder_->IsInitialized();
}
void Destroy(std::shared_ptr<PGOProfiler> &profiler)
@ -199,6 +200,13 @@ public:
disableAot_ = state;
}
// Only set flag to ensure future actions will not trigger PGO path
// Caller should handle existing threads and PGO data properly
void SetDisablePGO(bool state)
{
disablePGO_ = state;
}
void ForceSave()
{
os::memory::LockHolder lock(*mutex_);
@ -285,6 +293,7 @@ private:
}
bool disableAot_ {false};
bool disablePGO_ {false};
std::unique_ptr<PGOProfilerEncoder> encoder_;
RequestAotCallback requestAotCallback_;
std::atomic_bool enableSignalSaving_ { false };

View File

@ -1358,4 +1358,28 @@ HWTEST_F_L0(PGOProfilerTest, PGOObjectInfoOperatorLessThanTest)
EXPECT_FALSE(objectInfoGreater < objectInfoLess);
}
HWTEST_F_L0(PGOProfilerTest, PGODisableWithAOTFileTest)
{
mkdir("ark-profiler27/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
std::ofstream fWriter("ark-profiler27/tmp.an", std::fstream::app);
fWriter.close();
RuntimeOption option;
EcmaVM *ecmaVM = JSNApi::CreateJSVM(option);
option.SetEnableProfile(true);
option.SetProfileDir("ark-profiler27/");
JSNApi::PreFork(ecmaVM);
JSNApi::PostFork(ecmaVM, option);
EXPECT_TRUE(ecmaVM->IsEnablePGOProfiler());
ecmaVM->DisablePGOProfilerWithAOTFile("ark-profiler27/tmp");
EXPECT_FALSE(ecmaVM->IsEnablePGOProfiler());
const char *targetRecordName = "typedarray_length";
std::string targetAbcPath = std::string(TARGET_ABC_PATH) + targetRecordName + ".abc";
auto result = JSNApi::Execute(ecmaVM, targetAbcPath, targetRecordName, false);
EXPECT_TRUE(result);
JSNApi::DestroyJSVM(ecmaVM);
EXPECT_FALSE(FileExist("ark-profiler27/modules.ap"));
unlink("ark-profiler27/tmp.an");
rmdir("ark-profiler27/");
}
} // namespace panda::test