-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathPageAllocatorPool.cpp
144 lines (126 loc) · 4.13 KB
/
PageAllocatorPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
#if ENABLE_OOP_NATIVE_CODEGEN
#include "JITServer/JITServer.h"
#include "PageAllocatorPool.h"
CriticalSection PageAllocatorPool::cs;
PageAllocatorPool* PageAllocatorPool::Instance = nullptr;
PageAllocatorPool::PageAllocatorPool()
:pageAllocators(&NoThrowHeapAllocator::Instance),
idleCleanupTimer(NULL),
activePageAllocatorCount(0)
{
idleCleanupTimer = CreateThreadpoolTimer(IdleCleanupRoutine, NULL, NULL);
}
PageAllocatorPool::~PageAllocatorPool()
{
AutoCriticalSection autoCS(&cs);
RemoveAll();
}
void PageAllocatorPool::Initialize()
{
Instance = HeapNewNoThrow(PageAllocatorPool);
if (Instance == nullptr)
{
Js::Throw::FatalInternalError();
}
}
void PageAllocatorPool::Shutdown()
{
AutoCriticalSection autoCS(&cs);
Assert(Instance);
if (Instance)
{
PageAllocatorPool* localInstance = Instance;
Instance = nullptr;
if (localInstance->idleCleanupTimer)
{
CloseThreadpoolTimer(localInstance->idleCleanupTimer);
}
HeapDelete(localInstance);
}
}
void PageAllocatorPool::RemoveAll()
{
Assert(cs.IsLocked());
while (!pageAllocators.Empty())
{
HeapDelete(pageAllocators.Pop());
}
}
unsigned int PageAllocatorPool::GetInactivePageAllocatorCount()
{
AutoCriticalSection autoCS(&cs);
return pageAllocators.Count();
}
PageAllocator* PageAllocatorPool::GetPageAllocator()
{
AutoCriticalSection autoCS(&cs);
PageAllocator* pageAllocator = nullptr;
if (pageAllocators.Count() > 0)
{
// TODO: OOP JIT, select the page allocator with right count of free pages
// base on some heuristic
pageAllocator = this->pageAllocators.Pop();
}
else
{
pageAllocator = HeapNew(PageAllocator, nullptr, Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
AutoSystemInfo::Data.IsLowMemoryProcess() ? PageAllocator::DefaultLowMaxFreePageCount : PageAllocator::DefaultMaxFreePageCount);
}
activePageAllocatorCount++;
return pageAllocator;
}
void PageAllocatorPool::ReturnPageAllocator(PageAllocator* pageAllocator)
{
AutoCriticalSection autoCS(&cs);
if (!this->pageAllocators.PrependNoThrow(&NoThrowHeapAllocator::Instance, pageAllocator))
{
HeapDelete(pageAllocator);
}
activePageAllocatorCount--;
if (activePageAllocatorCount == 0 || GetInactivePageAllocatorCount() > (uint)Js::Configuration::Global.flags.JITServerMaxInactivePageAllocatorCount)
{
PageAllocatorPool::IdleCleanup();
}
}
void PageAllocatorPool::IdleCleanup()
{
AutoCriticalSection autoCS(&cs);
if (Instance
#ifdef PROFILE_EXEC
// 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
&& !Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag)
#endif
)
{
LARGE_INTEGER liDueTime;
liDueTime.QuadPart = Js::Configuration::Global.flags.JITServerIdleTimeout * -10000LL; // wait for JITServerIdleTimeout milliseconds to do the cleanup
if (Instance->idleCleanupTimer)
{
// Setting the timer cancels the previous timer, if any.
SetThreadpoolTimer(Instance->idleCleanupTimer, (PFILETIME)&liDueTime, 0, 0);
}
else
{
Instance->RemoveAll();
}
}
}
VOID CALLBACK PageAllocatorPool::IdleCleanupRoutine(
_Inout_ PTP_CALLBACK_INSTANCE,
_Inout_opt_ PVOID,
_Inout_ PTP_TIMER)
{
AutoCriticalSection autoCS(&cs);
if (Instance)
{
// TODO: OOP JIT, use better stragtegy to do the cleanup, like do not remove all,
// instead keep couple inactivate page allocator for next calls
Instance->RemoveAll();
}
}
#endif