-
Notifications
You must be signed in to change notification settings - Fork 392
/
SweepHeapSectioningSegmented.cpp
223 lines (191 loc) · 9.23 KB
/
SweepHeapSectioningSegmented.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
/*******************************************************************************
* Copyright (c) 1991, 2021 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 "omrcfg.h"
#include "omrcomp.h"
#include "SweepHeapSectioningSegmented.hpp"
#include "SweepPoolManager.hpp"
#include "EnvironmentBase.hpp"
#include "Heap.hpp"
#include "HeapRegionIterator.hpp"
#include "HeapRegionDescriptor.hpp"
#include "NonVirtualMemory.hpp"
#include "MemoryPool.hpp"
#include "MemorySubSpace.hpp"
#include "ParallelDispatcher.hpp"
#include "ParallelSweepChunk.hpp"
/**
* Return the expected total sweep chunks that will be used in the system.
* Called during initialization, this routine looks at the maximum size of the heap and expected
* configuration (generations, regions, etc) and determines the approximate maximum number of chunks
* that will be required for a sweep at any given time. It is safe to underestimate the number of chunks,
* as the sweep sectioning mechnanism will compensate, but the the expectation is that by having all
* chunk memory allocated in one go will keep the data localized and fragment system memory less.
* @return estimated upper bound number of chunks that will be required by the system.
*/
uintptr_t
MM_SweepHeapSectioningSegmented::estimateTotalChunkCount(MM_EnvironmentBase *env)
{
uintptr_t totalChunkCountEstimate;
if(0 == _extensions->parSweepChunkSize) {
/* -Xgc:sweepchunksize= has NOT been specified, so we set it heuristically.
*
* maxheapsize
* chunksize = ---------------- (rounded up to the nearest 256k)
* threadcount * 32
*/
_extensions->parSweepChunkSize = MM_Math::roundToCeiling(256*1024, _extensions->heap->getMaximumMemorySize() / (_extensions->dispatcher->threadCountMaximum() * 32));
}
totalChunkCountEstimate = MM_Math::roundToCeiling(_extensions->parSweepChunkSize, _extensions->heap->getMaximumMemorySize()) / _extensions->parSweepChunkSize;
#if defined(OMR_GC_MODRON_SCAVENGER)
/* Because object memory segments have not been allocated yet, we cannot get the real numbers.
* Assume that if the scavenger is enabled, each of the semispaces will need an extra chunk */
/* TODO: Can we make an estimate based on the number of leaf memory subspaces allocated (if they are already allocated but not inflated)??? */
if(_extensions->scavengerEnabled) {
totalChunkCountEstimate += 2;
}
#endif /* OMR_GC_MODRON_SCAVENGER */
return totalChunkCountEstimate;
}
/**
* Walk all segments and calculate the maximum number of chunks needed to represent the current heap.
* The chunk calculation is done on a per segment basis (no segment can represent memory from more than 1 chunk),
* and partial sized chunks (ie: less than the chunk size) are reserved for any remaining space at the end of a
* segment.
* @return number of chunks required to represent the current heap memory.
*/
uintptr_t
MM_SweepHeapSectioningSegmented::calculateActualChunkNumbers() const
{
uintptr_t totalChunkCount = 0;
MM_HeapRegionDescriptor *region;
MM_Heap *heap = _extensions->heap;
MM_HeapRegionManager *regionManager = heap->getHeapRegionManager();
GC_HeapRegionIterator regionIterator(regionManager);
while((region = regionIterator.nextRegion()) != NULL) {
if ((region)->isCommitted()) {
/* TODO: this must be rethought for Tarok since it treats all regions identically but some might require different sweep logic */
MM_MemorySubSpace *subspace = region->getSubSpace();
/* if this is a committed region, it requires a non-NULL subspace */
Assert_MM_true(NULL != subspace);
uintptr_t poolCount = subspace->getMemoryPoolCount();
totalChunkCount += MM_Math::roundToCeiling(_extensions->parSweepChunkSize, region->getSize()) / _extensions->parSweepChunkSize;
/* Add extra chunks if more than one memory pool */
totalChunkCount += (poolCount - 1);
}
}
return totalChunkCount;
}
/**
* Reset and reassign each chunk to a range of heap memory.
* Given the current updated listed of chunks and the corresponding heap memory, walk the chunk
* list reassigning each chunk to an appropriate range of memory. This will clear each chunk
* structure and then assign its basic values that connect it to a range of memory (base/top,
* pool, segment, etc).
* @return the total number of chunks in the system.
*/
uintptr_t
MM_SweepHeapSectioningSegmented::reassignChunks(MM_EnvironmentBase *env)
{
MM_ParallelSweepChunk *chunk; /* Sweep table chunk (global) */
MM_ParallelSweepChunk *previousChunk;
uintptr_t totalChunkCount; /* Total chunks in system */
MM_SweepHeapSectioningIterator sectioningIterator(this);
totalChunkCount = 0;
previousChunk = NULL;
MM_HeapRegionManager *regionManager = _extensions->getHeap()->getHeapRegionManager();
GC_HeapRegionIterator regionIterator(regionManager);
MM_HeapRegionDescriptor *region = NULL;
while (NULL != (region = regionIterator.nextRegion())) {
if (region->isCommitted()) {
/* TODO: this must be rethought for Tarok since it treats all regions identically but some might require different sweep logic */
uintptr_t *heapChunkBase = (uintptr_t *)region->getLowAddress(); /* Heap chunk base pointer */
uintptr_t *regionHighAddress = (uintptr_t *)region->getHighAddress();
while (heapChunkBase < regionHighAddress) {
void *poolHighAddr;
uintptr_t *heapChunkTop;
MM_MemoryPool *pool;
chunk = sectioningIterator.nextChunk();
Assert_MM_true(chunk != NULL); /* Should never return NULL */
totalChunkCount += 1;
/* Clear all data in the chunk (including sweep implementation specific information) */
chunk->clear();
if(((uintptr_t)regionHighAddress - (uintptr_t)heapChunkBase) < _extensions->parSweepChunkSize) {
/* corner case - we will wrap our address range */
heapChunkTop = regionHighAddress;
} else {
/* normal case - just increment by the chunk size */
heapChunkTop = (uintptr_t *)((uintptr_t)heapChunkBase + _extensions->parSweepChunkSize);
}
/* Find out if the range of memory we are considering spans 2 different pools. If it does,
* the current chunk can only be attributed to one, so we limit the upper range of the chunk
* to the first pool and will continue the assignment at the upper address range.
*/
pool = region->getSubSpace()->getMemoryPool(env, heapChunkBase, heapChunkTop, poolHighAddr);
if (NULL == poolHighAddr) {
heapChunkTop = (heapChunkTop > regionHighAddress ? regionHighAddress : heapChunkTop);
} else {
/* Yes ..so adjust chunk boundaries */
assume0(poolHighAddr > heapChunkBase && poolHighAddr < heapChunkTop);
heapChunkTop = (uintptr_t *) poolHighAddr;
}
/* All values for the chunk have been calculated - assign them */
chunk->chunkBase = (void *)heapChunkBase;
chunk->chunkTop = (void *)heapChunkTop;
chunk->memoryPool = pool;
Assert_MM_true(NULL != pool);
/* Some memory pools, like the one in LOA, may have larger min free size then in the rest of the heap being swept */
chunk->_minFreeSize = OMR_MAX(pool->getMinimumFreeEntrySize(), pool->getSweepPoolManager()->getMinimumFreeSize());
chunk->_coalesceCandidate = (heapChunkBase != region->getLowAddress());
chunk->_previous= previousChunk;
if(NULL != previousChunk) {
previousChunk->_next = chunk;
}
/* Move to the next chunk */
heapChunkBase = heapChunkTop;
/* and remember address of previous chunk */
previousChunk = chunk;
assume0((uintptr_t)heapChunkBase == MM_Math::roundToCeiling(_extensions->heapAlignment,(uintptr_t)heapChunkBase));
}
}
}
if(NULL != previousChunk) {
previousChunk->_next = NULL;
}
return totalChunkCount;
}
/**
* Allocate and initialize a new instance of the receiver.
* @return pointer to the new instance.
*/
MM_SweepHeapSectioningSegmented *
MM_SweepHeapSectioningSegmented::newInstance(MM_EnvironmentBase *env)
{
MM_SweepHeapSectioningSegmented *sweepHeapSectioning = (MM_SweepHeapSectioningSegmented *)env->getForge()->allocate(sizeof(MM_SweepHeapSectioningSegmented), OMR::GC::AllocationCategory::FIXED, OMR_GET_CALLSITE());
if (sweepHeapSectioning) {
new(sweepHeapSectioning) MM_SweepHeapSectioningSegmented(env);
if (!sweepHeapSectioning->initialize(env)) {
sweepHeapSectioning->kill(env);
return NULL;
}
}
return sweepHeapSectioning;
}