Skip to content

Commit d720588

Browse files
author
Mike Kaufman
committed
[MERGE #5344 @mike-kaufman] Merged PR 129185: [MERGE #5297 @VSadov] Adding telemetry for pinned objects and closed script contexts
Merge pull request #5344 from mike-kaufman:build/mkaufman/cherry-pick-telemetry-changes [MERGE #5297 @VSadov] Adding telemetry for pinned objects and closed script contexts Cherry-picked from commit `ea9a1d6d`.
2 parents f31299d + 08b2356 commit d720588

File tree

9 files changed

+51
-7
lines changed

9 files changed

+51
-7
lines changed

lib/Common/Exceptions/ReportError.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,20 @@ _NOINLINE void MemGCSingleAllocationLimit_unrecoverable_error()
157157
ReportFatalException(NULL, E_OUTOFMEMORY, Fatal_OutOfMemory, scenario);
158158
}
159159

160+
// same as OutOfMemory_unrecoverable_error, but with a different `scenario`
161+
// - just to cause separate bucketing of these failures
162+
_NOINLINE void OutOfMemoryTooManyPinnedObjects_unrecoverable_error()
163+
{
164+
int scenario = 12;
165+
ReportFatalException(NULL, E_OUTOFMEMORY, Fatal_OutOfMemory, scenario);
166+
}
167+
168+
// same as OutOfMemory_unrecoverable_error, but with a different `scenario`
169+
// - just to cause separate bucketing of these failures
170+
_NOINLINE void OutOfMemoryTooManyClosedContexts_unrecoverable_error()
171+
{
172+
int scenario = 13;
173+
ReportFatalException(NULL, E_OUTOFMEMORY, Fatal_OutOfMemory, scenario);
174+
}
175+
160176
#pragma optimize("",on)

lib/Common/Exceptions/ReportError.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ void RpcFailure_unrecoverable_error(HRESULT hr);
7676
void OutOfMemory_unrecoverable_error();
7777
void RecyclerSingleAllocationLimit_unrecoverable_error();
7878
void MemGCSingleAllocationLimit_unrecoverable_error();
79+
void OutOfMemoryTooManyPinnedObjects_unrecoverable_error();
80+
void OutOfMemoryTooManyClosedContexts_unrecoverable_error();
7981

8082
#ifndef DISABLE_SEH
8183
// RtlReportException is available on Vista and up, but we cannot use it for OOB release.

lib/Common/Memory/AllocationPolicyManager.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,17 @@ typedef bool (__stdcall * PageAllocatorMemoryAllocationCallback)(__in LPVOID con
134134
if (newCurrentMemory < currentMemory ||
135135
newCurrentMemory > memoryLimit ||
136136
(memoryAllocationCallback != NULL && !memoryAllocationCallback(context, MemoryAllocateEvent::MemoryAllocate, byteCount)))
137-
{
138-
if (memoryAllocationCallback != NULL)
139-
{
140-
memoryAllocationCallback(context, MemoryAllocateEvent::MemoryFailure, byteCount);
141-
}
142-
137+
{
143138
// oopjit number allocator allocated pages, we can't stop it from allocating so just increase the usage number
144139
if (externalAlloc)
145140
{
146141
currentMemory = newCurrentMemory;
142+
return true;
143+
}
144+
145+
if (memoryAllocationCallback != NULL)
146+
{
147+
memoryAllocationCallback(context, MemoryAllocateEvent::MemoryFailure, byteCount);
147148
}
148149

149150
return false;

lib/Common/Memory/Recycler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,8 @@ class Recycler
11461146
#ifdef NTBUILD
11471147
void SetTelemetryBlock(RecyclerWatsonTelemetryBlock * telemetryBlock) { this->telemetryBlock = telemetryBlock; }
11481148
#endif
1149+
1150+
uint GetPinnedObjectCount() const { return this->pinnedObjectMap.Count(); }
11491151

11501152
void Prime();
11511153
void* GetOwnerContext() { return (void*) this->collectionWrapper; }

lib/Common/Memory/RecyclerTelemetryInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ namespace Memory
138138
{
139139
LPFILETIME ft = this->hostInterface->GetLastScriptExecutionEndTime();
140140
stats->lastScriptExecutionEndTime = *ft;
141+
142+
stats->closedContextCount = this->hostInterface->GetClosedContextCount();
141143
}
142144

143145
stats->processCommittedBytes_start = RecyclerTelemetryInfo::GetProcessCommittedBytes();
@@ -152,6 +154,8 @@ namespace Memory
152154
this->FillInSizeData(this->recycler->GetHeapInfo()->GetRecyclerWithBarrierPageAllocator(), &stats->recyclerWithBarrierPageAllocator_start);
153155
#endif
154156
stats->startPassProcessingElapsedTime = Js::Tick::Now() - start;
157+
158+
stats->pinnedObjectCount = this->recycler->pinnedObjectMap.Count();
155159
}
156160
}
157161
}

lib/Common/Memory/RecyclerTelemetryInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace Memory
3333
virtual bool IsTelemetryProviderEnabled() const = 0;
3434
virtual bool IsThreadBound() const = 0;
3535
virtual DWORD GetCurrentScriptThreadID() const = 0;
36+
virtual uint GetClosedContextCount() const = 0;
3637
};
3738

3839
#ifdef ENABLE_BASIC_TELEMETRY
@@ -61,6 +62,8 @@ namespace Memory
6162
bool isInScript;
6263
bool isScriptActive;
6364
bool isGCPassActive;
65+
uint closedContextCount;
66+
uint pinnedObjectCount;
6467

6568
size_t processAllocaterUsedBytes_start;
6669
size_t processAllocaterUsedBytes_end;

lib/Runtime/Base/ScriptContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ namespace Js
569569
}
570570
#endif
571571

572+
Assert(this->IsActuallyClosed());
573+
this->GetThreadContext()->closedScriptContextCount--;
574+
572575
PERF_COUNTER_DEC(Basic, ScriptContext);
573576
}
574577

@@ -617,6 +620,7 @@ namespace Js
617620
void ScriptContext::InternalClose()
618621
{
619622
isScriptContextActuallyClosed = true;
623+
this->GetThreadContext()->closedScriptContextCount++;
620624

621625
PERF_COUNTER_DEC(Basic, ScriptContextActive);
622626

lib/Runtime/Base/ThreadContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ ThreadContext::ThreadContext(AllocationPolicyManager * allocationPolicyManager,
212212
, recyclerTelemetryHostInterface(this)
213213
, reentrancySafeOrHandled(false)
214214
, isInReentrancySafeRegion(false)
215+
, closedScriptContextCount(0)
215216
{
216217
pendingProjectionContextCloseList = JsUtil::List<IProjectionContext*, ArenaAllocator>::New(GetThreadAlloc());
217218
hostScriptContextStack = Anew(GetThreadAlloc(), JsUtil::Stack<HostScriptContext*>, GetThreadAlloc());
@@ -720,6 +721,11 @@ DWORD ThreadContext::ThreadContextRecyclerTelemetryHostInterface::GetCurrentScri
720721
return this->tc->GetCurrentThreadId();
721722
}
722723

724+
uint ThreadContext::ThreadContextRecyclerTelemetryHostInterface::GetClosedContextCount() const
725+
{
726+
return this->tc->closedScriptContextCount;
727+
}
728+
723729
Recycler* ThreadContext::EnsureRecycler()
724730
{
725731
if (recycler == NULL)

lib/Runtime/Base/ThreadContext.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ class ThreadContext sealed :
622622
bool reentrancySafeOrHandled;
623623
bool isInReentrancySafeRegion;
624624

625-
AllocationPolicyManager * allocationPolicyManager;
625+
AllocationPolicyManager * allocationPolicyManager;
626626

627627
JsUtil::ThreadService threadService;
628628
#if ENABLE_NATIVE_CODEGEN
@@ -856,6 +856,11 @@ class ThreadContext sealed :
856856

857857
AllocationPolicyManager * GetAllocationPolicyManager() { return allocationPolicyManager; }
858858

859+
// used for diagnosing abnormally high number of closed, but still formally reachable script contexts
860+
// at the time of failfast due to allocation limits.
861+
// high number may indicate that context leaks have occured.
862+
uint closedScriptContextCount;
863+
859864
#if ENABLE_NATIVE_CODEGEN
860865
PreReservedVirtualAllocWrapper * GetPreReservedVirtualAllocator() { return &preReservedVirtualAllocator; }
861866
#if DYNAMIC_INTERPRETER_THUNK || defined(ASMJS_PLAT)
@@ -1879,6 +1884,7 @@ class ThreadContext sealed :
18791884
virtual bool ThreadContextRecyclerTelemetryHostInterface::IsThreadBound() const;
18801885
virtual DWORD ThreadContextRecyclerTelemetryHostInterface::GetCurrentScriptThreadID() const;
18811886
virtual bool IsTelemetryProviderEnabled() const;
1887+
virtual uint GetClosedContextCount() const;
18821888

18831889
private:
18841890
ThreadContext * tc;

0 commit comments

Comments
 (0)