forked from dolphinsmalltalk/DolphinVM
/
zct.cpp
233 lines (192 loc) · 6.32 KB
/
zct.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/******************************************************************************
File: Alloc.cpp
Description:
Object Memory management allocation/coping routines
******************************************************************************/
#include "Ist.h"
#pragma code_seg(MEM_SEG)
#include "ObjMem.h"
#include "ObjMemPriv.inl"
#include "Interprt.h"
#include "rc_vm.h"
#include "RegKey.h"
// Smalltalk classes
#include "STVirtualObject.h"
enum {
ZCTMINSIZE = 64, // Minimum size, and initial high water mark, of the ZCT.
ZCTINITIALSIZE = 2048, // Benchmarking shows this to be the best size at present.
ZCTMINRESERVE = 16*1024,
ZCTRESERVE = 512*1024 // Allow up to 0.5 million objects to be ref'd only from stack of active process
};
OTE** ObjectMemory::m_pZct;
int ObjectMemory::m_nZctEntries;
int ObjectMemory::m_nZctHighWater;
static DWORD ZctMinSize;
static DWORD ZctReserve;
bool ObjectMemory::m_bIsReconcilingZct;
#ifdef _DEBUG
static int nDeleted;
static DWORD dwLastReconcileTicks;
bool alwaysReconcileOnAdd = true;
bool ObjectMemory::IsInZct(OTE* ote)
{
// Expensive serial search needed because we don't have a flag free in the OTE at present
for (int i=0;i<m_nZctEntries;i++)
if (m_pZct[i] == ote) return true;
return false;
}
#endif
HRESULT ObjectMemory::InitializeZct()
{
CRegKey rkDump;
ZctReserve = ZCTRESERVE;
ZctMinSize = ZCTINITIALSIZE;
if (OpenDolphinKey(rkDump, "ObjMem", KEY_READ)==ERROR_SUCCESS)
{
DWORD dwValue;
if (rkDump.QueryDWORDValue("ZMax", dwValue) == ERROR_SUCCESS && dwValue > ZCTMINRESERVE)
ZctReserve = dwValue;
if (rkDump.QueryDWORDValue("ZMin", ZctMinSize) == ERROR_SUCCESS && dwValue > ZCTMINSIZE &&
dwValue < ZctReserve)
ZctMinSize = dwValue;
}
//trace("ZctMinSize = %u, ZctReserve = %u\n", ZctMinSize, ZctReserve);
m_nZctEntries = 0;
m_bIsReconcilingZct = false;
m_pZct = static_cast<OTE**>(::VirtualAlloc(NULL, ZctReserve*sizeof(OTE*), MEM_RESERVE, PAGE_NOACCESS));
if (!m_pZct)
return ReportError(IDP_ZCTRESERVEFAIL, ZctReserve, 0);
m_nZctHighWater = ZctMinSize >> 1;
GrowZct();
#ifdef _DEBUG
dwLastReconcileTicks = timeGetTime();
#endif
return S_OK;
}
void ObjectMemory::GrowZct()
{
TRACE("ZCT overflow at %d entries (%d in use), growing to %d\n", m_nZctHighWater, m_nZctEntries, m_nZctHighWater*2);
m_nZctHighWater <<= 1;
// Reserve and high water must be powers of 2, so this should be unless about to overflow reserve
HARDASSERT((DWORD)m_nZctHighWater <= ZctReserve);
m_pZct = static_cast<OTE**>(::VirtualAlloc(m_pZct, m_nZctHighWater*sizeof(OTE*), MEM_COMMIT, PAGE_READWRITE));
if (!m_pZct)
RaiseFatalError(IDP_ZCTRESERVEFAIL, 2, m_nZctHighWater, ZctReserve);
}
void ObjectMemory::ShrinkZct()
{
TRACE("Shrink ZCT from %d entries (%d in use) to %d\n", m_nZctHighWater, m_nZctEntries, m_nZctHighWater/2);
m_nZctHighWater >>= 1;
// Can't shrink below one page
if (m_nZctHighWater >= (dwPageSize/sizeof(OTE*)))
{
::VirtualFree(m_pZct+m_nZctHighWater, m_nZctHighWater*sizeof(OTE*), MEM_DECOMMIT);
}
}
OTE* ObjectMemory::ReconcileZct(OTE* ote)
{
#ifdef _DEBUG
DWORD dwTicksNow = timeGetTime();
if (!alwaysReconcileOnAdd)
{
TRACELOCK();
TRACESTREAM << "Reconciling Zct after " << dec << dwTicksNow - dwLastReconcileTicks << " mS: ";
//Interpreter::DumpOTEPoolStats();
TRACESTREAM << "..." << endl;
}
dwLastReconcileTicks = dwTicksNow;
const int nOldZctEntries = m_nZctEntries;
#endif
Interpreter::flushAtCaches();
EmptyZct();
PopulateZct();
#ifdef _DEBUG
if (!alwaysReconcileOnAdd)
{
DWORD dwTicksAfter = timeGetTime();
trace("Zct reconciled in %dmS: %d objects were deleted, %d slots free'd, %d still in use\n", dwTicksAfter - dwTicksNow, nDeleted, nOldZctEntries - m_nZctEntries, m_nZctEntries);
//Interpreter::DumpOTEPoolStats();
if (m_nZctEntries > 0 && Interpreter::executionTrace != 0)
DumpZct();
}
#endif
return ote;
}
void ObjectMemory::EmptyZct()
{
if (m_bIsReconcilingZct)
__debugbreak();
#ifdef _DEBUG
nDeleted = 0;
if (!alwaysReconcileOnAdd || Interpreter::executionTrace)
CHECKREFSNOFIX
else
checkStackRefs();
#endif
// Bump the refs from the stack. Any objects remaining in the ZCT with zero counts
// are truly garbage.
Interpreter::IncStackRefs();
OTE** pZct = m_pZct;
// This tells us that we are in the process of reconcilation
m_bIsReconcilingZct = true;
const int nOldZctEntries = m_nZctEntries;
m_nZctEntries = -1;
for (int i=0;i<nOldZctEntries;i++)
{
OTE* ote = pZct[i];
if (!ote->isFree() && ote->m_flags.m_count == 0)
{
// Note that deallocate cannot make new Zct entries
// Because we have bumped the ref. counts of all stack ref'd objects, only true
// garbage objects can ever have a ref. count of zero. Therefore if recursively
// counting down throws up new zero ref. counts, these should not be added to
// the Zct, but deallocated. To achieve this we set a global flag to indicate
// that we are reconciling, see AddToZct() above.
#ifdef _DEBUG
nDeleted++;
#endif
recursiveFree(ote);
}
}
// CHECKREFSNOFIX
}
void ObjectMemory::PopulateZct()
{
HARDASSERT(m_nZctEntries <= 0);
// To build the new Zct all we need do is countDown() everything in the current process
// stack. Any counts that dropped to zero, will get put back in the new Zct.
m_nZctEntries = 0;
#ifdef _DEBUG
bool bLast = alwaysReconcileOnAdd;
alwaysReconcileOnAdd = false;
#endif
// This will populate the Zct with all objects ref'd only from the active process stack
Interpreter::DecStackRefs();
#ifdef _DEBUG
alwaysReconcileOnAdd = bLast;
//CHECKREFSNOFIX
#endif
if (m_nZctEntries > (m_nZctHighWater - m_nZctHighWater/4))
{
// More than 75% full, then grow it
GrowZct();
}
else if ((m_nZctHighWater > (int)ZctMinSize) && (m_nZctEntries < m_nZctHighWater/4))
{
// Less than 25% full, so shrink it
ShrinkZct();
}
// Reconciliation complete
m_bIsReconcilingZct = false;
}
#ifdef _DEBUG
void ObjectMemory::DumpZct()
{
TRACESTREAM << "===========================================================" << endl;
TRACESTREAM << "ZCT @ " << hex << m_pZct << ", size: " << dec << m_nZctEntries << endl;
TRACESTREAM << "===========================================================" << endl;
for (int i=0;i<m_nZctEntries;i++)
TRACESTREAM << dec << i << ": " << hex << (DWORD)m_pZct[i] << ": " << m_pZct[i] << endl;
TRACESTREAM << "===========================================================" << endl;
}
#endif