Skip to content

Commit b4ba1ef

Browse files
committed
fix codegen profiler under OOP JIT
1 parent 9cb7e8c commit b4ba1ef

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

34883454
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
@@ -430,11 +430,7 @@ ServerCloseScriptContext(
430430
return ServerCallWrapper(scriptContextInfo, [&]()->HRESULT
431431
{
432432
#ifdef PROFILE_EXEC
433-
auto profiler = scriptContextInfo->GetCodeGenProfiler();
434-
if (profiler && profiler->IsInitialized())
435-
{
436-
profiler->ProfilePrint(Js::Configuration::Global.flags.Profile.GetFirstPhase());
437-
}
433+
scriptContextInfo->GetFirstCodeGenProfiler()->ProfilePrint();
438434
#endif
439435
scriptContextInfo->Close();
440436
ServerContextManager::UnRegisterScriptContext(scriptContextInfo);
@@ -733,12 +729,10 @@ ServerRemoteCodeGen(
733729
Output::Flush();
734730
}
735731

736-
auto profiler = scriptContextInfo->GetCodeGenProfiler();
737732
#ifdef PROFILE_EXEC
738-
if (profiler && !profiler->IsInitialized())
739-
{
740-
profiler->Initialize(pageAllocator, nullptr);
741-
}
733+
Js::ScriptContextProfiler* profiler = scriptContextInfo->GetCodeGenProfiler(pageAllocator);
734+
#else
735+
Js::ScriptContextProfiler* profiler = nullptr;
742736
#endif
743737

744738
#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)