-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathCopyScanCacheList.cpp
355 lines (299 loc) · 10.4 KB
/
CopyScanCacheList.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*******************************************************************************
* Copyright (c) 1991, 2015 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <new>
#include "omrcfg.h"
#include "omrport.h"
#include "CopyScanCacheList.hpp"
#include "CopyScanCacheChunk.hpp"
#include "CopyScanCacheChunkInHeap.hpp"
#include "CopyScanCacheStandard.hpp"
#include "EnvironmentStandard.hpp"
#include "GCExtensionsBase.hpp"
#include "ParallelDispatcher.hpp"
#if defined(OMR_GC_MODRON_SCAVENGER)
bool
MM_CopyScanCacheList::initialize(MM_EnvironmentBase *env, volatile uintptr_t *cachedEntryCount)
{
MM_GCExtensionsBase *extensions = env->getExtensions();
bool result = true;
_sublistCount = extensions->cacheListSplit;
Assert_MM_true(0 < _sublistCount);
_sublists = (struct CopyScanCacheSublist *)extensions->getForge()->allocate(sizeof(struct CopyScanCacheSublist) * _sublistCount, OMR::GC::AllocationCategory::FIXED, OMR_GET_CALLSITE());
if (NULL == _sublists) {
result = false;
} else {
for (uintptr_t i = 0; i < _sublistCount; i++) {
new (&_sublists[i]) CopyScanCacheSublist();
if(_sublists[i].initialize(env)) {
result = false;
break;
}
}
}
_cachedEntryCount = cachedEntryCount;
return result;
}
void
MM_CopyScanCacheList::tearDown(MM_EnvironmentBase *env)
{
MM_GCExtensionsBase *extensions = env->getExtensions();
/* Free the memory allocated for the caches */
while (NULL != _chunkHead) {
MM_CopyScanCacheChunk *_next = _chunkHead->getNext();
_chunkHead->kill(env);
_chunkHead = _next;
}
if (NULL != _sublists) {
for (uintptr_t i = 0; i < _sublistCount; i++) {
_sublists[i]._cacheLock.tearDown();
}
extensions->getForge()->free(_sublists);
_sublists = NULL;
}
}
bool
MM_CopyScanCacheList::resizeCacheEntries(MM_EnvironmentBase *env, uintptr_t allocateCacheEntryCount, uintptr_t incrementCacheEntryCount)
{
MM_GCExtensionsBase *ext = env->getExtensions();
/* 0 has special meaning of 'do not change' */
if (0 == allocateCacheEntryCount) {
allocateCacheEntryCount = _totalAllocatedEntryCount;
}
if (0 != incrementCacheEntryCount) {
_incrementEntryCount = incrementCacheEntryCount;
}
/* If -Xgc:fvtest=scanCacheCountn has been specified, then restrict the number of scan caches to n.
* Stop all future resizes from having any effect. */
if (0 != ext->fvtest_scanCacheCount) {
if (0 == _totalAllocatedEntryCount) {
allocateCacheEntryCount = ext->fvtest_scanCacheCount;
_incrementEntryCount = 0;
return appendCacheEntries(env, allocateCacheEntryCount);
} else {
return true;
}
}
if ( allocateCacheEntryCount > _totalAllocatedEntryCount) {
/* Increase cacheEntries by incrementEntryCount */
return appendCacheEntries(env, _incrementEntryCount);
}
/* downsizing is non-trivial with current list/chunk implementation since
* the free caches are scattered across the chunks and cross reference themselves */
return true;
}
void
MM_CopyScanCacheList::removeAllHeapAllocatedChunks(MM_EnvironmentStandard *env)
{
if (_allocationInHeap) {
uintptr_t reservedInHeap = 0;
/* execute if allocation in heap occur this Local GC */
/*
* Walk caches list first to remove all references to heap allocated caches
*/
for (uintptr_t index = 0; index < _sublistCount; index++) {
MM_CopyScanCacheStandard *previousCache = NULL;
MM_CopyScanCacheStandard *cache = _sublists[index]._cacheHead;
while(cache != NULL) {
if (0 != (cache->flags & OMR_SCAVENGER_CACHE_TYPE_HEAP)) {
/* this cache is heap allocated - remove it from list */
if (NULL == previousCache) {
/* still be a first element */
_sublists[index]._cacheHead = (MM_CopyScanCacheStandard *)cache->next;
} else {
/* remove middle element */
previousCache->next = cache->next;
}
/* count number of removed */
reservedInHeap += 1;
Assert_MM_true(_sublists[index]._entryCount >= 1);
_sublists[index]._entryCount -= 1;
} else {
/* not heap allocated - just skip */
previousCache = cache;
}
cache = (MM_CopyScanCacheStandard *)cache->next;
}
}
/*
* Walk caches chunks list and release all heap allocated
*/
MM_CopyScanCacheChunk *previousChunk = NULL;
MM_CopyScanCacheChunk *chunk = _chunkHead;
while(chunk != NULL) {
MM_CopyScanCacheChunk *nextChunk = chunk->getNext();
if (0 != (chunk->getBase()->flags & OMR_SCAVENGER_CACHE_TYPE_HEAP)) {
/* this chunk is heap allocated - remove it from list */
if (NULL == previousChunk) {
/* still be a first element */
_chunkHead = nextChunk;
} else {
/* remove middle element */
previousChunk->setNext(nextChunk);
}
/* release heap allocated chunk */
chunk->kill(env);
} else {
/* not heap allocated - just skip */
previousChunk = chunk;
}
chunk = nextChunk;
}
/* clear flag - no more heap allocated caches */
_allocationInHeap = false;
Assert_MM_true(0 < reservedInHeap);
/* increase number of permanent scan caches by number of incrementEntryCount */
appendCacheEntries(env, _incrementEntryCount);
}
}
bool
MM_CopyScanCacheList::appendCacheEntries(MM_EnvironmentBase *env, uintptr_t cacheEntryCount)
{
bool result = false;
MM_CopyScanCacheStandard *sublistTail = NULL;
MM_CopyScanCacheChunk *chunk = MM_CopyScanCacheChunk::newInstance(env, cacheEntryCount, _chunkHead, &sublistTail);
if(NULL != chunk) {
uintptr_t index = getSublistIndex(env);
Assert_MM_true(NULL != sublistTail);
Assert_MM_true(NULL == sublistTail->next);
_sublists[index]._cacheLock.acquire();
/* attach sublist of caches in chunk to main list */
sublistTail->next = _sublists[index]._cacheHead;
_sublists[index]._cacheHead = chunk->getBase();
_sublists[index]._entryCount += cacheEntryCount;
_sublists[index]._cacheLock.release();
_chunkHead = chunk;
_totalAllocatedEntryCount += cacheEntryCount;
result = true;
}
return result;
}
MM_CopyScanCacheStandard *
MM_CopyScanCacheList::appendCacheEntriesInHeap(MM_EnvironmentStandard *env, MM_MemorySubSpace *memorySubSpace, MM_Collector *requestCollector)
{
MM_CopyScanCacheStandard *result = NULL;
MM_CopyScanCacheStandard *sublistTail = NULL;
uintptr_t entries = 0;
MM_CopyScanCacheChunkInHeap *chunk = MM_CopyScanCacheChunkInHeap::newInstance(env, _chunkHead, memorySubSpace, requestCollector, &sublistTail, &entries);
if(NULL != chunk) {
uintptr_t index = getSublistIndex(env);
Assert_MM_true(0 <= entries);
Assert_MM_true(NULL != sublistTail);
Assert_MM_true(NULL == sublistTail->next);
_sublists[index]._cacheLock.acquire();
/* attach sublist of caches in chunk to main list */
sublistTail->next = _sublists[index]._cacheHead;
result = chunk->getBase();
_sublists[index]._cacheHead = (MM_CopyScanCacheStandard *)result->next;
_sublists[index]._entryCount += (entries - 1);
_sublists[index]._cacheLock.release();
_chunkHead = chunk;
_allocationInHeap = true;
}
return result;
}
uintptr_t
MM_CopyScanCacheList::getApproximateEntryCount()
{
uintptr_t entries = 0;
for (uintptr_t i = 0; i < _sublistCount; i++) {
entries += _sublists[i]._entryCount;
}
return entries;
}
bool
MM_CopyScanCacheList::areAllCachesReturned()
{
return (getApproximateEntryCount() == _totalAllocatedEntryCount);
}
MMINLINE void
MM_CopyScanCacheList::incrementCount(CopyScanCacheSublist *sublist, uintptr_t value)
{
if ((0 == sublist->_entryCount) && (NULL != _cachedEntryCount)) {
if (1 == _sublistCount) {
*_cachedEntryCount += 1;
} else {
/* use an atomic, as the locks have been split up */
MM_AtomicOperations::add(_cachedEntryCount, 1);
}
}
sublist->_entryCount += value;
}
MMINLINE void
MM_CopyScanCacheList::decrementCount(CopyScanCacheSublist *sublist, uintptr_t value)
{
Assert_MM_true(sublist->_entryCount >= value);
sublist->_entryCount -= value;
if ((0 == sublist->_entryCount) && (NULL != _cachedEntryCount)) {
Assert_MM_true(*_cachedEntryCount >= 1);
if (1 == _sublistCount) {
*_cachedEntryCount -= 1;
} else {
/* use an atomic, as the locks have been split up */
MM_AtomicOperations::subtract(_cachedEntryCount, 1);
}
}
}
void
MM_CopyScanCacheList::pushCache(MM_EnvironmentBase *env, MM_CopyScanCacheStandard *cacheEntry)
{
MM_CopyScanCacheList::CopyScanCacheSublist *list = &_sublists[getSublistIndex(env)];
/* This is a useful assertion to find who drop the same element to list twice
* It is fatal and caused hang right away.
* However it is too expensive to have this assertion here permanently
*/
/*
Assert_MM_true(newCacheEntry != _list->_cacheHead);
*/
list->_cacheLock.acquire();
cacheEntry->next = list->_cacheHead;
list->_cacheHead = cacheEntry;
incrementCount(list, 1);
list->_cacheLock.release();
}
MM_CopyScanCacheStandard *
MM_CopyScanCacheList::popCache(MM_EnvironmentBase *env)
{
uintptr_t index = getSublistIndex(env);
MM_CopyScanCacheStandard *cache = NULL;
for (uintptr_t i = 0; i < _sublistCount; i++) {
MM_CopyScanCacheList::CopyScanCacheSublist *list = &_sublists[index];
if (NULL != list->_cacheHead) {
env->_scavengerStats._acquireListLockCount += 1;
list->_cacheLock.acquire();
cache = list->_cacheHead;
if (NULL != cache) {
decrementCount(list, 1);
list->_cacheHead = (MM_CopyScanCacheStandard *)cache->next;
if (NULL == list->_cacheHead) {
Assert_MM_true(0 == list->_entryCount);
}
}
list->_cacheLock.release();
if (NULL != cache) {
break;
}
}
index = (index + 1) % _sublistCount;
}
return cache;
}
#endif /* OMR_GC_MODRON_SCAVENGER */