Skip to content

Commit b75a780

Browse files
committed
[MERGE #5460 @MikeHolman] fix codegen profiler under OOP JIT
Merge pull request #5460 from MikeHolman:users/michhol/oopprofilerfix Codegen profiling was previously broken for OOP JIT (except with -bgjit-). This fixes it. OS: 17413924
2 parents 9fdb174 + b4ba1ef commit b75a780

File tree

7 files changed

+104
-65
lines changed

7 files changed

+104
-65
lines changed

lib/Backend/NativeCodeGenerator.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,41 +3447,7 @@ NativeCodeGenerator::SetProfilerFromNativeCodeGen(NativeCodeGenerator * nativeCo
34473447
void
34483448
NativeCodeGenerator::ProfilePrint()
34493449
{
3450-
Js::ScriptContextProfiler *codegenProfiler = this->backgroundCodeGenProfiler;
3451-
if (Js::Configuration::Global.flags.Verbose)
3452-
{
3453-
//Print individual CodegenProfiler information in verbose mode
3454-
while (codegenProfiler)
3455-
{
3456-
codegenProfiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
3457-
codegenProfiler = codegenProfiler->next;
3458-
}
3459-
}
3460-
else
3461-
{
3462-
//Merge all the codegenProfiler for single snapshot.
3463-
Js::ScriptContextProfiler* mergeToProfiler = codegenProfiler;
3464-
3465-
// find the first initialized profiler
3466-
while (mergeToProfiler != nullptr && !mergeToProfiler->IsInitialized())
3467-
{
3468-
mergeToProfiler = mergeToProfiler->next;
3469-
}
3470-
if (mergeToProfiler != nullptr)
3471-
{
3472-
// merge the rest profiler to the above initialized profiler
3473-
codegenProfiler = mergeToProfiler->next;
3474-
while (codegenProfiler)
3475-
{
3476-
if (codegenProfiler->IsInitialized())
3477-
{
3478-
mergeToProfiler->ProfileMerge(codegenProfiler);
3479-
}
3480-
codegenProfiler = codegenProfiler->next;
3481-
}
3482-
mergeToProfiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
3483-
}
3484-
}
3450+
this->backgroundCodeGenProfiler->ProfilePrint();
34853451
}
34863452

34873453
void

lib/Backend/PageAllocatorPool.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ void PageAllocatorPool::ReturnPageAllocator(PageAllocator* pageAllocator)
106106
void PageAllocatorPool::IdleCleanup()
107107
{
108108
AutoCriticalSection autoCS(&cs);
109-
if (Instance)
109+
if (Instance
110+
#ifdef PROFILE_EXEC
111+
// profiler keeps persistent reference to page allocators, so we can't cleanup on idle even if the allocators aren't currently being used for JIT
112+
&& !Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag)
113+
#endif
114+
)
110115
{
111116
LARGE_INTEGER liDueTime;
112117
liDueTime.QuadPart = Js::Configuration::Global.flags.JITServerIdleTimeout * -10000LL; // wait for JITServerIdleTimeout milliseconds to do the cleanup

lib/Backend/ServerScriptContext.cpp

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ServerScriptContext::ServerScriptContext(ScriptContextDataIDL * contextData, Ser
2929
m_codeGenAlloc(nullptr, nullptr, threadContextInfo, threadContextInfo->GetCodePageAllocators(), threadContextInfo->GetProcessHandle()),
3030
m_globalThisAddr(0),
3131
#ifdef PROFILE_EXEC
32-
m_codeGenProfiler(nullptr),
32+
codeGenProfiler(nullptr),
3333
#endif
3434
m_refCount(0),
3535
m_isClosed(false)
@@ -39,12 +39,6 @@ ServerScriptContext::ServerScriptContext(ScriptContextDataIDL * contextData, Ser
3939
m_codeGenAlloc.canCreatePreReservedSegment = threadContextInfo->CanCreatePreReservedSegment();
4040
#endif
4141

42-
#ifdef PROFILE_EXEC
43-
if (Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag))
44-
{
45-
m_codeGenProfiler = HeapNew(Js::ScriptContextProfiler);
46-
}
47-
#endif
4842
m_domFastPathHelperMap = HeapNew(JITDOMFastPathHelperMap, &HeapAllocator::Instance, 17);
4943
}
5044

@@ -57,9 +51,11 @@ ServerScriptContext::~ServerScriptContext()
5751
});
5852

5953
#ifdef PROFILE_EXEC
60-
if (m_codeGenProfiler)
54+
while (this->codeGenProfiler)
6155
{
62-
HeapDelete(m_codeGenProfiler);
56+
Js::ScriptContextProfiler* profiler = this->codeGenProfiler;
57+
this->codeGenProfiler = this->codeGenProfiler->next;
58+
HeapDelete(profiler);
6359
}
6460
#endif
6561
}
@@ -416,14 +412,46 @@ ServerScriptContext::AddModuleRecordInfo(unsigned int moduleId, __int64 localExp
416412
m_moduleRecords.Add(moduleId, record);
417413
}
418414

419-
Js::ScriptContextProfiler *
420-
ServerScriptContext::GetCodeGenProfiler() const
421-
{
422415
#ifdef PROFILE_EXEC
423-
return m_codeGenProfiler;
424-
#else
416+
Js::ScriptContextProfiler*
417+
ServerScriptContext::GetCodeGenProfiler(_In_ PageAllocator* pageAllocator)
418+
{
419+
if (Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag))
420+
{
421+
AutoCriticalSection cs(&this->profilerCS);
422+
423+
Js::ScriptContextProfiler* profiler = this->codeGenProfiler;
424+
while (profiler)
425+
{
426+
if (profiler->pageAllocator == pageAllocator)
427+
{
428+
if (!profiler->IsInitialized())
429+
{
430+
profiler->Initialize(pageAllocator, nullptr);
431+
}
432+
return profiler;
433+
}
434+
profiler = profiler->next;
435+
}
436+
437+
// If we didn't find a profiler, allocate a new one
438+
439+
profiler = HeapNew(Js::ScriptContextProfiler);
440+
profiler->Initialize(pageAllocator, nullptr);
441+
profiler->next = this->codeGenProfiler;
442+
443+
this->codeGenProfiler = profiler;
444+
445+
return profiler;
446+
}
425447
return nullptr;
426-
#endif
427448
}
428449

429-
#endif
450+
Js::ScriptContextProfiler*
451+
ServerScriptContext::GetFirstCodeGenProfiler() const
452+
{
453+
return this->codeGenProfiler;
454+
}
455+
#endif // PROFILE_EXEC
456+
457+
#endif // ENABLE_OOP_NATIVE_CODEGEN

lib/Backend/ServerScriptContext.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ class ServerScriptContext : public ScriptContextInfo
8282
void UpdateGlobalObjectThisAddr(intptr_t globalThis);
8383
OOPEmitBufferManager * GetEmitBufferManager(bool asmJsManager);
8484
void DecommitEmitBufferManager(bool asmJsManager);
85-
Js::ScriptContextProfiler * GetCodeGenProfiler() const;
85+
#ifdef PROFILE_EXEC
86+
Js::ScriptContextProfiler* GetCodeGenProfiler(_In_ PageAllocator* pageAllocator);
87+
Js::ScriptContextProfiler* GetFirstCodeGenProfiler() const;
88+
#endif
8689
ServerThreadContext* GetThreadContext() { return threadContextHolder.threadContextInfo; }
8790

8891
OOPCodeGenAllocators * GetCodeGenAllocators();
@@ -94,7 +97,8 @@ class ServerScriptContext : public ScriptContextInfo
9497
private:
9598
JITDOMFastPathHelperMap * m_domFastPathHelperMap;
9699
#ifdef PROFILE_EXEC
97-
Js::ScriptContextProfiler * m_codeGenProfiler;
100+
Js::ScriptContextProfiler * codeGenProfiler;
101+
CriticalSection profilerCS;
98102
#endif
99103
ArenaAllocator m_sourceCodeArena;
100104

lib/JITServer/JITServer.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,7 @@ ServerCloseScriptContext(
435435
return ServerCallWrapper(scriptContextInfo, [&]()->HRESULT
436436
{
437437
#ifdef PROFILE_EXEC
438-
auto profiler = scriptContextInfo->GetCodeGenProfiler();
439-
if (profiler && profiler->IsInitialized())
440-
{
441-
profiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
442-
}
438+
scriptContextInfo->GetFirstCodeGenProfiler()->ProfilePrint();
443439
#endif
444440
scriptContextInfo->Close();
445441
ServerContextManager::UnRegisterScriptContext(scriptContextInfo);
@@ -742,12 +738,10 @@ ServerRemoteCodeGen(
742738
Output::Flush();
743739
}
744740

745-
auto profiler = scriptContextInfo->GetCodeGenProfiler();
746741
#ifdef PROFILE_EXEC
747-
if (profiler && !profiler->IsInitialized())
748-
{
749-
profiler->Initialize(pageAllocator, nullptr);
750-
}
742+
Js::ScriptContextProfiler* profiler = scriptContextInfo->GetCodeGenProfiler(pageAllocator);
743+
#else
744+
Js::ScriptContextProfiler* profiler = nullptr;
751745
#endif
752746

753747
#if !FLOATVAR

lib/Runtime/Base/ScriptContextProfiler.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,46 @@ namespace Js
8080
profiler->Begin(Js::AllPhase);
8181
}
8282

83+
void
84+
ScriptContextProfiler::ProfilePrint()
85+
{
86+
Js::ScriptContextProfiler* profiler = this;
87+
if (Js::Configuration::Global.flags.Verbose)
88+
{
89+
//Print individual profiler information in verbose mode
90+
while (profiler)
91+
{
92+
profiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
93+
profiler = profiler->next;
94+
}
95+
}
96+
else
97+
{
98+
//Merge all the profiler for single snapshot.
99+
Js::ScriptContextProfiler* mergeToProfiler = profiler;
100+
101+
// find the first initialized profiler
102+
while (mergeToProfiler != nullptr && !mergeToProfiler->IsInitialized())
103+
{
104+
mergeToProfiler = mergeToProfiler->next;
105+
}
106+
if (mergeToProfiler != nullptr)
107+
{
108+
// merge the rest profiler to the above initialized profiler
109+
profiler = mergeToProfiler->next;
110+
while (profiler)
111+
{
112+
if (profiler->IsInitialized())
113+
{
114+
mergeToProfiler->ProfileMerge(profiler);
115+
}
116+
profiler = profiler->next;
117+
}
118+
mergeToProfiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
119+
}
120+
}
121+
}
122+
83123
ScriptContextProfiler::~ScriptContextProfiler()
84124
{
85125
if (profilerArena)

lib/Runtime/Base/ScriptContextProfiler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
//-------------------------------------------------------------------------------------------------------
55
#pragma once
66

7+
class ServerScriptContext;
78
namespace Js
89
{
910
class ScriptContextProfiler
1011
{
1112
#ifdef PROFILE_EXEC
1213
friend class NativeCodeGenerator;
14+
friend class ServerScriptContext;
1315

1416
public:
1517
ScriptContextProfiler();
@@ -29,7 +31,7 @@ namespace Js
2931
void ProfileResume(Js::Profiler::SuspendRecord * suspendRecord);
3032
void ProfilePrint(Js::Phase phase);
3133
void ProfileMerge(ScriptContextProfiler * profiler);
32-
34+
void ProfilePrint();
3335
private:
3436
ArenaAllocator * profilerArena;
3537
ArenaAllocator * backgroundRecyclerProfilerArena;

0 commit comments

Comments
 (0)