50193
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
#include "precompiled.hpp"
|
|
25 |
|
|
26 |
#include "logging/log.hpp"
|
|
27 |
#include "logging/logStream.hpp"
|
|
28 |
#include "memory/binaryTreeDictionary.inline.hpp"
|
|
29 |
#include "memory/freeList.inline.hpp"
|
|
30 |
#include "memory/metaspace/chunkManager.hpp"
|
|
31 |
#include "memory/metaspace/metachunk.hpp"
|
|
32 |
#include "memory/metaspace/metaspaceCommon.hpp"
|
|
33 |
#include "memory/metaspace/metaspaceStatistics.hpp"
|
|
34 |
#include "memory/metaspace/occupancyMap.hpp"
|
|
35 |
#include "memory/metaspace/virtualSpaceNode.hpp"
|
|
36 |
#include "runtime/mutexLocker.hpp"
|
|
37 |
#include "utilities/debug.hpp"
|
|
38 |
#include "utilities/globalDefinitions.hpp"
|
|
39 |
#include "utilities/ostream.hpp"
|
|
40 |
|
|
41 |
namespace metaspace {
|
|
42 |
|
|
43 |
void ChunkManager::remove_chunk(Metachunk* chunk) {
|
|
44 |
size_t word_size = chunk->word_size();
|
|
45 |
ChunkIndex index = list_index(word_size);
|
|
46 |
if (index != HumongousIndex) {
|
|
47 |
free_chunks(index)->remove_chunk(chunk);
|
|
48 |
} else {
|
|
49 |
humongous_dictionary()->remove_chunk(chunk);
|
|
50 |
}
|
|
51 |
|
|
52 |
// Chunk has been removed from the chunks free list, update counters.
|
|
53 |
account_for_removed_chunk(chunk);
|
|
54 |
}
|
|
55 |
|
|
56 |
bool ChunkManager::attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type) {
|
|
57 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
58 |
assert(chunk != NULL, "invalid chunk pointer");
|
|
59 |
// Check for valid merge combinations.
|
|
60 |
assert((chunk->get_chunk_type() == SpecializedIndex &&
|
|
61 |
(target_chunk_type == SmallIndex || target_chunk_type == MediumIndex)) ||
|
|
62 |
(chunk->get_chunk_type() == SmallIndex && target_chunk_type == MediumIndex),
|
|
63 |
"Invalid chunk merge combination.");
|
|
64 |
|
|
65 |
const size_t target_chunk_word_size =
|
|
66 |
get_size_for_nonhumongous_chunktype(target_chunk_type, this->is_class());
|
|
67 |
|
|
68 |
// [ prospective merge region )
|
|
69 |
MetaWord* const p_merge_region_start =
|
|
70 |
(MetaWord*) align_down(chunk, target_chunk_word_size * sizeof(MetaWord));
|
|
71 |
MetaWord* const p_merge_region_end =
|
|
72 |
p_merge_region_start + target_chunk_word_size;
|
|
73 |
|
|
74 |
// We need the VirtualSpaceNode containing this chunk and its occupancy map.
|
|
75 |
VirtualSpaceNode* const vsn = chunk->container();
|
|
76 |
OccupancyMap* const ocmap = vsn->occupancy_map();
|
|
77 |
|
|
78 |
// The prospective chunk merge range must be completely contained by the
|
|
79 |
// committed range of the virtual space node.
|
|
80 |
if (p_merge_region_start < vsn->bottom() || p_merge_region_end > vsn->top()) {
|
|
81 |
return false;
|
|
82 |
}
|
|
83 |
|
|
84 |
// Only attempt to merge this range if at its start a chunk starts and at its end
|
|
85 |
// a chunk ends. If a chunk (can only be humongous) straddles either start or end
|
|
86 |
// of that range, we cannot merge.
|
|
87 |
if (!ocmap->chunk_starts_at_address(p_merge_region_start)) {
|
|
88 |
return false;
|
|
89 |
}
|
|
90 |
if (p_merge_region_end < vsn->top() &&
|
|
91 |
!ocmap->chunk_starts_at_address(p_merge_region_end)) {
|
|
92 |
return false;
|
|
93 |
}
|
|
94 |
|
|
95 |
// Now check if the prospective merge area contains live chunks. If it does we cannot merge.
|
|
96 |
if (ocmap->is_region_in_use(p_merge_region_start, target_chunk_word_size)) {
|
|
97 |
return false;
|
|
98 |
}
|
|
99 |
|
|
100 |
// Success! Remove all chunks in this region...
|
|
101 |
log_trace(gc, metaspace, freelist)("%s: coalescing chunks in area [%p-%p)...",
|
|
102 |
(is_class() ? "class space" : "metaspace"),
|
|
103 |
p_merge_region_start, p_merge_region_end);
|
|
104 |
|
|
105 |
const int num_chunks_removed =
|
|
106 |
remove_chunks_in_area(p_merge_region_start, target_chunk_word_size);
|
|
107 |
|
|
108 |
// ... and create a single new bigger chunk.
|
|
109 |
Metachunk* const p_new_chunk =
|
|
110 |
::new (p_merge_region_start) Metachunk(target_chunk_type, is_class(), target_chunk_word_size, vsn);
|
|
111 |
assert(p_new_chunk == (Metachunk*)p_merge_region_start, "Sanity");
|
|
112 |
p_new_chunk->set_origin(origin_merge);
|
|
113 |
|
|
114 |
log_trace(gc, metaspace, freelist)("%s: created coalesced chunk at %p, size " SIZE_FORMAT_HEX ".",
|
|
115 |
(is_class() ? "class space" : "metaspace"),
|
|
116 |
p_new_chunk, p_new_chunk->word_size() * sizeof(MetaWord));
|
|
117 |
|
|
118 |
// Fix occupancy map: remove old start bits of the small chunks and set new start bit.
|
|
119 |
ocmap->wipe_chunk_start_bits_in_region(p_merge_region_start, target_chunk_word_size);
|
|
120 |
ocmap->set_chunk_starts_at_address(p_merge_region_start, true);
|
|
121 |
|
|
122 |
// Mark chunk as free. Note: it is not necessary to update the occupancy
|
|
123 |
// map in-use map, because the old chunks were also free, so nothing
|
|
124 |
// should have changed.
|
|
125 |
p_new_chunk->set_is_tagged_free(true);
|
|
126 |
|
|
127 |
// Add new chunk to its freelist.
|
|
128 |
ChunkList* const list = free_chunks(target_chunk_type);
|
|
129 |
list->return_chunk_at_head(p_new_chunk);
|
|
130 |
|
|
131 |
// And adjust ChunkManager:: _free_chunks_count (_free_chunks_total
|
|
132 |
// should not have changed, because the size of the space should be the same)
|
|
133 |
_free_chunks_count -= num_chunks_removed;
|
|
134 |
_free_chunks_count ++;
|
|
135 |
|
|
136 |
// VirtualSpaceNode::container_count does not have to be modified:
|
|
137 |
// it means "number of active (non-free) chunks", so merging free chunks
|
|
138 |
// should not affect that count.
|
|
139 |
|
|
140 |
// At the end of a chunk merge, run verification tests.
|
|
141 |
if (VerifyMetaspace) {
|
|
142 |
DEBUG_ONLY(this->locked_verify());
|
|
143 |
DEBUG_ONLY(vsn->verify());
|
|
144 |
}
|
|
145 |
|
|
146 |
return true;
|
|
147 |
}
|
|
148 |
|
|
149 |
// Remove all chunks in the given area - the chunks are supposed to be free -
|
|
150 |
// from their corresponding freelists. Mark them as invalid.
|
|
151 |
// - This does not correct the occupancy map.
|
|
152 |
// - This does not adjust the counters in ChunkManager.
|
|
153 |
// - Does not adjust container count counter in containing VirtualSpaceNode
|
|
154 |
// Returns number of chunks removed.
|
|
155 |
int ChunkManager::remove_chunks_in_area(MetaWord* p, size_t word_size) {
|
|
156 |
assert(p != NULL && word_size > 0, "Invalid range.");
|
|
157 |
const size_t smallest_chunk_size = get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class());
|
|
158 |
assert_is_aligned(word_size, smallest_chunk_size);
|
|
159 |
|
|
160 |
Metachunk* const start = (Metachunk*) p;
|
|
161 |
const Metachunk* const end = (Metachunk*)(p + word_size);
|
|
162 |
Metachunk* cur = start;
|
|
163 |
int num_removed = 0;
|
|
164 |
while (cur < end) {
|
|
165 |
Metachunk* next = (Metachunk*)(((MetaWord*)cur) + cur->word_size());
|
|
166 |
DEBUG_ONLY(do_verify_chunk(cur));
|
|
167 |
assert(cur->get_chunk_type() != HumongousIndex, "Unexpected humongous chunk found at %p.", cur);
|
|
168 |
assert(cur->is_tagged_free(), "Chunk expected to be free (%p)", cur);
|
|
169 |
log_trace(gc, metaspace, freelist)("%s: removing chunk %p, size " SIZE_FORMAT_HEX ".",
|
|
170 |
(is_class() ? "class space" : "metaspace"),
|
|
171 |
cur, cur->word_size() * sizeof(MetaWord));
|
|
172 |
cur->remove_sentinel();
|
|
173 |
// Note: cannot call ChunkManager::remove_chunk, because that
|
|
174 |
// modifies the counters in ChunkManager, which we do not want. So
|
|
175 |
// we call remove_chunk on the freelist directly (see also the
|
|
176 |
// splitting function which does the same).
|
|
177 |
ChunkList* const list = free_chunks(list_index(cur->word_size()));
|
|
178 |
list->remove_chunk(cur);
|
|
179 |
num_removed ++;
|
|
180 |
cur = next;
|
|
181 |
}
|
|
182 |
return num_removed;
|
|
183 |
}
|
|
184 |
|
|
185 |
size_t ChunkManager::free_chunks_total_words() {
|
|
186 |
return _free_chunks_total;
|
|
187 |
}
|
|
188 |
|
|
189 |
size_t ChunkManager::free_chunks_total_bytes() {
|
|
190 |
return free_chunks_total_words() * BytesPerWord;
|
|
191 |
}
|
|
192 |
|
|
193 |
// Update internal accounting after a chunk was added
|
|
194 |
void ChunkManager::account_for_added_chunk(const Metachunk* c) {
|
|
195 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
196 |
_free_chunks_count ++;
|
|
197 |
_free_chunks_total += c->word_size();
|
|
198 |
}
|
|
199 |
|
|
200 |
// Update internal accounting after a chunk was removed
|
|
201 |
void ChunkManager::account_for_removed_chunk(const Metachunk* c) {
|
|
202 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
203 |
assert(_free_chunks_count >= 1,
|
|
204 |
"ChunkManager::_free_chunks_count: about to go negative (" SIZE_FORMAT ").", _free_chunks_count);
|
|
205 |
assert(_free_chunks_total >= c->word_size(),
|
|
206 |
"ChunkManager::_free_chunks_total: about to go negative"
|
|
207 |
"(now: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ").", _free_chunks_total, c->word_size());
|
|
208 |
_free_chunks_count --;
|
|
209 |
_free_chunks_total -= c->word_size();
|
|
210 |
}
|
|
211 |
|
|
212 |
size_t ChunkManager::free_chunks_count() {
|
|
213 |
#ifdef ASSERT
|
|
214 |
if (!UseConcMarkSweepGC && !MetaspaceExpand_lock->is_locked()) {
|
|
215 |
MutexLockerEx cl(MetaspaceExpand_lock,
|
|
216 |
Mutex::_no_safepoint_check_flag);
|
|
217 |
// This lock is only needed in debug because the verification
|
|
218 |
// of the _free_chunks_totals walks the list of free chunks
|
|
219 |
slow_locked_verify_free_chunks_count();
|
|
220 |
}
|
|
221 |
#endif
|
|
222 |
return _free_chunks_count;
|
|
223 |
}
|
|
224 |
|
|
225 |
ChunkIndex ChunkManager::list_index(size_t size) {
|
|
226 |
return get_chunk_type_by_size(size, is_class());
|
|
227 |
}
|
|
228 |
|
|
229 |
size_t ChunkManager::size_by_index(ChunkIndex index) const {
|
|
230 |
index_bounds_check(index);
|
|
231 |
assert(index != HumongousIndex, "Do not call for humongous chunks.");
|
|
232 |
return get_size_for_nonhumongous_chunktype(index, is_class());
|
|
233 |
}
|
|
234 |
|
|
235 |
void ChunkManager::locked_verify_free_chunks_total() {
|
|
236 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
237 |
assert(sum_free_chunks() == _free_chunks_total,
|
|
238 |
"_free_chunks_total " SIZE_FORMAT " is not the"
|
|
239 |
" same as sum " SIZE_FORMAT, _free_chunks_total,
|
|
240 |
sum_free_chunks());
|
|
241 |
}
|
|
242 |
|
|
243 |
void ChunkManager::locked_verify_free_chunks_count() {
|
|
244 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
245 |
assert(sum_free_chunks_count() == _free_chunks_count,
|
|
246 |
"_free_chunks_count " SIZE_FORMAT " is not the"
|
|
247 |
" same as sum " SIZE_FORMAT, _free_chunks_count,
|
|
248 |
sum_free_chunks_count());
|
|
249 |
}
|
|
250 |
|
|
251 |
void ChunkManager::verify() {
|
|
252 |
MutexLockerEx cl(MetaspaceExpand_lock,
|
|
253 |
Mutex::_no_safepoint_check_flag);
|
|
254 |
locked_verify();
|
|
255 |
}
|
|
256 |
|
|
257 |
void ChunkManager::locked_verify() {
|
|
258 |
locked_verify_free_chunks_count();
|
|
259 |
locked_verify_free_chunks_total();
|
|
260 |
for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
|
|
261 |
ChunkList* list = free_chunks(i);
|
|
262 |
if (list != NULL) {
|
|
263 |
Metachunk* chunk = list->head();
|
|
264 |
while (chunk) {
|
|
265 |
DEBUG_ONLY(do_verify_chunk(chunk);)
|
|
266 |
assert(chunk->is_tagged_free(), "Chunk should be tagged as free.");
|
|
267 |
chunk = chunk->next();
|
|
268 |
}
|
|
269 |
}
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
void ChunkManager::locked_print_free_chunks(outputStream* st) {
|
|
274 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
275 |
st->print_cr("Free chunk total " SIZE_FORMAT " count " SIZE_FORMAT,
|
|
276 |
_free_chunks_total, _free_chunks_count);
|
|
277 |
}
|
|
278 |
|
|
279 |
void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
|
|
280 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
281 |
st->print_cr("Sum free chunk total " SIZE_FORMAT " count " SIZE_FORMAT,
|
|
282 |
sum_free_chunks(), sum_free_chunks_count());
|
|
283 |
}
|
|
284 |
|
|
285 |
ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
|
|
286 |
assert(index == SpecializedIndex || index == SmallIndex || index == MediumIndex,
|
|
287 |
"Bad index: %d", (int)index);
|
|
288 |
|
|
289 |
return &_free_chunks[index];
|
|
290 |
}
|
|
291 |
|
|
292 |
// These methods that sum the free chunk lists are used in printing
|
|
293 |
// methods that are used in product builds.
|
|
294 |
size_t ChunkManager::sum_free_chunks() {
|
|
295 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
296 |
size_t result = 0;
|
|
297 |
for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
|
|
298 |
ChunkList* list = free_chunks(i);
|
|
299 |
|
|
300 |
if (list == NULL) {
|
|
301 |
continue;
|
|
302 |
}
|
|
303 |
|
|
304 |
result = result + list->count() * list->size();
|
|
305 |
}
|
|
306 |
result = result + humongous_dictionary()->total_size();
|
|
307 |
return result;
|
|
308 |
}
|
|
309 |
|
|
310 |
size_t ChunkManager::sum_free_chunks_count() {
|
|
311 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
312 |
size_t count = 0;
|
|
313 |
for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
|
|
314 |
ChunkList* list = free_chunks(i);
|
|
315 |
if (list == NULL) {
|
|
316 |
continue;
|
|
317 |
}
|
|
318 |
count = count + list->count();
|
|
319 |
}
|
|
320 |
count = count + humongous_dictionary()->total_free_blocks();
|
|
321 |
return count;
|
|
322 |
}
|
|
323 |
|
|
324 |
ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
|
|
325 |
ChunkIndex index = list_index(word_size);
|
|
326 |
assert(index < HumongousIndex, "No humongous list");
|
|
327 |
return free_chunks(index);
|
|
328 |
}
|
|
329 |
|
|
330 |
// Helper for chunk splitting: given a target chunk size and a larger free chunk,
|
|
331 |
// split up the larger chunk into n smaller chunks, at least one of which should be
|
|
332 |
// the target chunk of target chunk size. The smaller chunks, including the target
|
|
333 |
// chunk, are returned to the freelist. The pointer to the target chunk is returned.
|
|
334 |
// Note that this chunk is supposed to be removed from the freelist right away.
|
|
335 |
Metachunk* ChunkManager::split_chunk(size_t target_chunk_word_size, Metachunk* larger_chunk) {
|
|
336 |
assert(larger_chunk->word_size() > target_chunk_word_size, "Sanity");
|
|
337 |
|
|
338 |
const ChunkIndex larger_chunk_index = larger_chunk->get_chunk_type();
|
|
339 |
const ChunkIndex target_chunk_index = get_chunk_type_by_size(target_chunk_word_size, is_class());
|
|
340 |
|
|
341 |
MetaWord* const region_start = (MetaWord*)larger_chunk;
|
|
342 |
const size_t region_word_len = larger_chunk->word_size();
|
|
343 |
MetaWord* const region_end = region_start + region_word_len;
|
|
344 |
VirtualSpaceNode* const vsn = larger_chunk->container();
|
|
345 |
OccupancyMap* const ocmap = vsn->occupancy_map();
|
|
346 |
|
|
347 |
// Any larger non-humongous chunk size is a multiple of any smaller chunk size.
|
|
348 |
// Since non-humongous chunks are aligned to their chunk size, the larger chunk should start
|
|
349 |
// at an address suitable to place the smaller target chunk.
|
|
350 |
assert_is_aligned(region_start, target_chunk_word_size);
|
|
351 |
|
|
352 |
// Remove old chunk.
|
|
353 |
free_chunks(larger_chunk_index)->remove_chunk(larger_chunk);
|
|
354 |
larger_chunk->remove_sentinel();
|
|
355 |
|
|
356 |
// Prevent access to the old chunk from here on.
|
|
357 |
larger_chunk = NULL;
|
|
358 |
// ... and wipe it.
|
|
359 |
DEBUG_ONLY(memset(region_start, 0xfe, region_word_len * BytesPerWord));
|
|
360 |
|
|
361 |
// In its place create first the target chunk...
|
|
362 |
MetaWord* p = region_start;
|
|
363 |
Metachunk* target_chunk = ::new (p) Metachunk(target_chunk_index, is_class(), target_chunk_word_size, vsn);
|
|
364 |
assert(target_chunk == (Metachunk*)p, "Sanity");
|
|
365 |
target_chunk->set_origin(origin_split);
|
|
366 |
|
|
367 |
// Note: we do not need to mark its start in the occupancy map
|
|
368 |
// because it coincides with the old chunk start.
|
|
369 |
|
|
370 |
// Mark chunk as free and return to the freelist.
|
|
371 |
do_update_in_use_info_for_chunk(target_chunk, false);
|
|
372 |
free_chunks(target_chunk_index)->return_chunk_at_head(target_chunk);
|
|
373 |
|
|
374 |
// This chunk should now be valid and can be verified.
|
|
375 |
DEBUG_ONLY(do_verify_chunk(target_chunk));
|
|
376 |
|
|
377 |
// In the remaining space create the remainder chunks.
|
|
378 |
p += target_chunk->word_size();
|
|
379 |
assert(p < region_end, "Sanity");
|
|
380 |
|
|
381 |
while (p < region_end) {
|
|
382 |
|
|
383 |
// Find the largest chunk size which fits the alignment requirements at address p.
|
|
384 |
ChunkIndex this_chunk_index = prev_chunk_index(larger_chunk_index);
|
|
385 |
size_t this_chunk_word_size = 0;
|
|
386 |
for(;;) {
|
|
387 |
this_chunk_word_size = get_size_for_nonhumongous_chunktype(this_chunk_index, is_class());
|
|
388 |
if (is_aligned(p, this_chunk_word_size * BytesPerWord)) {
|
|
389 |
break;
|
|
390 |
} else {
|
|
391 |
this_chunk_index = prev_chunk_index(this_chunk_index);
|
|
392 |
assert(this_chunk_index >= target_chunk_index, "Sanity");
|
|
393 |
}
|
|
394 |
}
|
|
395 |
|
|
396 |
assert(this_chunk_word_size >= target_chunk_word_size, "Sanity");
|
|
397 |
assert(is_aligned(p, this_chunk_word_size * BytesPerWord), "Sanity");
|
|
398 |
assert(p + this_chunk_word_size <= region_end, "Sanity");
|
|
399 |
|
|
400 |
// Create splitting chunk.
|
|
401 |
Metachunk* this_chunk = ::new (p) Metachunk(this_chunk_index, is_class(), this_chunk_word_size, vsn);
|
|
402 |
assert(this_chunk == (Metachunk*)p, "Sanity");
|
|
403 |
this_chunk->set_origin(origin_split);
|
|
404 |
ocmap->set_chunk_starts_at_address(p, true);
|
|
405 |
do_update_in_use_info_for_chunk(this_chunk, false);
|
|
406 |
|
|
407 |
// This chunk should be valid and can be verified.
|
|
408 |
DEBUG_ONLY(do_verify_chunk(this_chunk));
|
|
409 |
|
|
410 |
// Return this chunk to freelist and correct counter.
|
|
411 |
free_chunks(this_chunk_index)->return_chunk_at_head(this_chunk);
|
|
412 |
_free_chunks_count ++;
|
|
413 |
|
|
414 |
log_trace(gc, metaspace, freelist)("Created chunk at " PTR_FORMAT ", word size "
|
|
415 |
SIZE_FORMAT_HEX " (%s), in split region [" PTR_FORMAT "..." PTR_FORMAT ").",
|
|
416 |
p2i(this_chunk), this_chunk->word_size(), chunk_size_name(this_chunk_index),
|
|
417 |
p2i(region_start), p2i(region_end));
|
|
418 |
|
|
419 |
p += this_chunk_word_size;
|
|
420 |
|
|
421 |
}
|
|
422 |
|
|
423 |
return target_chunk;
|
|
424 |
}
|
|
425 |
|
|
426 |
Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
|
|
427 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
428 |
|
|
429 |
slow_locked_verify();
|
|
430 |
|
|
431 |
Metachunk* chunk = NULL;
|
|
432 |
bool we_did_split_a_chunk = false;
|
|
433 |
|
|
434 |
if (list_index(word_size) != HumongousIndex) {
|
|
435 |
|
|
436 |
ChunkList* free_list = find_free_chunks_list(word_size);
|
|
437 |
assert(free_list != NULL, "Sanity check");
|
|
438 |
|
|
439 |
chunk = free_list->head();
|
|
440 |
|
|
441 |
if (chunk == NULL) {
|
|
442 |
// Split large chunks into smaller chunks if there are no smaller chunks, just large chunks.
|
|
443 |
// This is the counterpart of the coalescing-upon-chunk-return.
|
|
444 |
|
|
445 |
ChunkIndex target_chunk_index = get_chunk_type_by_size(word_size, is_class());
|
|
446 |
|
|
447 |
// Is there a larger chunk we could split?
|
|
448 |
Metachunk* larger_chunk = NULL;
|
|
449 |
ChunkIndex larger_chunk_index = next_chunk_index(target_chunk_index);
|
|
450 |
while (larger_chunk == NULL && larger_chunk_index < NumberOfFreeLists) {
|
|
451 |
larger_chunk = free_chunks(larger_chunk_index)->head();
|
|
452 |
if (larger_chunk == NULL) {
|
|
453 |
larger_chunk_index = next_chunk_index(larger_chunk_index);
|
|
454 |
}
|
|
455 |
}
|
|
456 |
|
|
457 |
if (larger_chunk != NULL) {
|
|
458 |
assert(larger_chunk->word_size() > word_size, "Sanity");
|
|
459 |
assert(larger_chunk->get_chunk_type() == larger_chunk_index, "Sanity");
|
|
460 |
|
|
461 |
// We found a larger chunk. Lets split it up:
|
|
462 |
// - remove old chunk
|
|
463 |
// - in its place, create new smaller chunks, with at least one chunk
|
|
464 |
// being of target size, the others sized as large as possible. This
|
|
465 |
// is to make sure the resulting chunks are "as coalesced as possible"
|
|
466 |
// (similar to VirtualSpaceNode::retire()).
|
|
467 |
// Note: during this operation both ChunkManager and VirtualSpaceNode
|
|
468 |
// are temporarily invalid, so be careful with asserts.
|
|
469 |
|
|
470 |
log_trace(gc, metaspace, freelist)("%s: splitting chunk " PTR_FORMAT
|
|
471 |
", word size " SIZE_FORMAT_HEX " (%s), to get a chunk of word size " SIZE_FORMAT_HEX " (%s)...",
|
|
472 |
(is_class() ? "class space" : "metaspace"), p2i(larger_chunk), larger_chunk->word_size(),
|
|
473 |
chunk_size_name(larger_chunk_index), word_size, chunk_size_name(target_chunk_index));
|
|
474 |
|
|
475 |
chunk = split_chunk(word_size, larger_chunk);
|
|
476 |
|
|
477 |
// This should have worked.
|
|
478 |
assert(chunk != NULL, "Sanity");
|
|
479 |
assert(chunk->word_size() == word_size, "Sanity");
|
|
480 |
assert(chunk->is_tagged_free(), "Sanity");
|
|
481 |
|
|
482 |
we_did_split_a_chunk = true;
|
|
483 |
|
|
484 |
}
|
|
485 |
}
|
|
486 |
|
|
487 |
if (chunk == NULL) {
|
|
488 |
return NULL;
|
|
489 |
}
|
|
490 |
|
|
491 |
// Remove the chunk as the head of the list.
|
|
492 |
free_list->remove_chunk(chunk);
|
|
493 |
|
|
494 |
log_trace(gc, metaspace, freelist)("ChunkManager::free_chunks_get: free_list: " PTR_FORMAT " chunks left: " SSIZE_FORMAT ".",
|
|
495 |
p2i(free_list), free_list->count());
|
|
496 |
|
|
497 |
} else {
|
|
498 |
chunk = humongous_dictionary()->get_chunk(word_size);
|
|
499 |
|
|
500 |
if (chunk == NULL) {
|
|
501 |
return NULL;
|
|
502 |
}
|
|
503 |
|
|
504 |
log_debug(gc, metaspace, alloc)("Free list allocate humongous chunk size " SIZE_FORMAT " for requested size " SIZE_FORMAT " waste " SIZE_FORMAT,
|
|
505 |
chunk->word_size(), word_size, chunk->word_size() - word_size);
|
|
506 |
}
|
|
507 |
|
|
508 |
// Chunk has been removed from the chunk manager; update counters.
|
|
509 |
account_for_removed_chunk(chunk);
|
|
510 |
do_update_in_use_info_for_chunk(chunk, true);
|
|
511 |
chunk->container()->inc_container_count();
|
|
512 |
chunk->inc_use_count();
|
|
513 |
|
|
514 |
// Remove it from the links to this freelist
|
|
515 |
chunk->set_next(NULL);
|
|
516 |
chunk->set_prev(NULL);
|
|
517 |
|
|
518 |
// Run some verifications (some more if we did a chunk split)
|
|
519 |
#ifdef ASSERT
|
|
520 |
if (VerifyMetaspace) {
|
|
521 |
locked_verify();
|
|
522 |
VirtualSpaceNode* const vsn = chunk->container();
|
|
523 |
vsn->verify();
|
|
524 |
if (we_did_split_a_chunk) {
|
|
525 |
vsn->verify_free_chunks_are_ideally_merged();
|
|
526 |
}
|
|
527 |
}
|
|
528 |
#endif
|
|
529 |
|
|
530 |
return chunk;
|
|
531 |
}
|
|
532 |
|
|
533 |
Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
|
|
534 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
535 |
slow_locked_verify();
|
|
536 |
|
|
537 |
// Take from the beginning of the list
|
|
538 |
Metachunk* chunk = free_chunks_get(word_size);
|
|
539 |
if (chunk == NULL) {
|
|
540 |
return NULL;
|
|
541 |
}
|
|
542 |
|
|
543 |
assert((word_size <= chunk->word_size()) ||
|
|
544 |
(list_index(chunk->word_size()) == HumongousIndex),
|
|
545 |
"Non-humongous variable sized chunk");
|
|
546 |
LogTarget(Debug, gc, metaspace, freelist) lt;
|
|
547 |
if (lt.is_enabled()) {
|
|
548 |
size_t list_count;
|
|
549 |
if (list_index(word_size) < HumongousIndex) {
|
|
550 |
ChunkList* list = find_free_chunks_list(word_size);
|
|
551 |
list_count = list->count();
|
|
552 |
} else {
|
|
553 |
list_count = humongous_dictionary()->total_count();
|
|
554 |
}
|
|
555 |
LogStream ls(lt);
|
|
556 |
ls.print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk " PTR_FORMAT " size " SIZE_FORMAT " count " SIZE_FORMAT " ",
|
|
557 |
p2i(this), p2i(chunk), chunk->word_size(), list_count);
|
|
558 |
ResourceMark rm;
|
|
559 |
locked_print_free_chunks(&ls);
|
|
560 |
}
|
|
561 |
|
|
562 |
return chunk;
|
|
563 |
}
|
|
564 |
|
|
565 |
void ChunkManager::return_single_chunk(Metachunk* chunk) {
|
|
566 |
const ChunkIndex index = chunk->get_chunk_type();
|
|
567 |
assert_lock_strong(MetaspaceExpand_lock);
|
|
568 |
DEBUG_ONLY(do_verify_chunk(chunk);)
|
|
569 |
assert(chunk != NULL, "Expected chunk.");
|
|
570 |
assert(chunk->container() != NULL, "Container should have been set.");
|
|
571 |
assert(chunk->is_tagged_free() == false, "Chunk should be in use.");
|
|
572 |
index_bounds_check(index);
|
|
573 |
|
|
574 |
// Note: mangle *before* returning the chunk to the freelist or dictionary. It does not
|
|
575 |
// matter for the freelist (non-humongous chunks), but the humongous chunk dictionary
|
|
576 |
// keeps tree node pointers in the chunk payload area which mangle will overwrite.
|
|
577 |
DEBUG_ONLY(chunk->mangle(badMetaWordVal);)
|
|
578 |
|
|
579 |
if (index != HumongousIndex) {
|
|
580 |
// Return non-humongous chunk to freelist.
|
|
581 |
ChunkList* list = free_chunks(index);
|
|
582 |
assert(list->size() == chunk->word_size(), "Wrong chunk type.");
|
|
583 |
list->return_chunk_at_head(chunk);
|
|
584 |
log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " to freelist.",
|
|
585 |
chunk_size_name(index), p2i(chunk));
|
|
586 |
} else {
|
|
587 |
// Return humongous chunk to dictionary.
|
|
588 |
assert(chunk->word_size() > free_chunks(MediumIndex)->size(), "Wrong chunk type.");
|
|
589 |
assert(chunk->word_size() % free_chunks(SpecializedIndex)->size() == 0,
|
|
590 |
"Humongous chunk has wrong alignment.");
|
|
591 |
_humongous_dictionary.return_chunk(chunk);
|
|
592 |
log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " (word size " SIZE_FORMAT ") to freelist.",
|
|
593 |
chunk_size_name(index), p2i(chunk), chunk->word_size());
|
|
594 |
}
|
|
595 |
chunk->container()->dec_container_count();
|
|
596 |
do_update_in_use_info_for_chunk(chunk, false);
|
|
597 |
|
|
598 |
// Chunk has been added; update counters.
|
|
599 |
account_for_added_chunk(chunk);
|
|
600 |
|
|
601 |
// Attempt coalesce returned chunks with its neighboring chunks:
|
|
602 |
// if this chunk is small or special, attempt to coalesce to a medium chunk.
|
|
603 |
if (index == SmallIndex || index == SpecializedIndex) {
|
|
604 |
if (!attempt_to_coalesce_around_chunk(chunk, MediumIndex)) {
|
|
605 |
// This did not work. But if this chunk is special, we still may form a small chunk?
|
|
606 |
if (index == SpecializedIndex) {
|
|
607 |
if (!attempt_to_coalesce_around_chunk(chunk, SmallIndex)) {
|
|
608 |
// give up.
|
|
609 |
}
|
|
610 |
}
|
|
611 |
}
|
|
612 |
}
|
|
613 |
|
|
614 |
}
|
|
615 |
|
|
616 |
void ChunkManager::return_chunk_list(Metachunk* chunks) {
|
|
617 |
if (chunks == NULL) {
|
|
618 |
return;
|
|
619 |
}
|
|
620 |
LogTarget(Trace, gc, metaspace, freelist) log;
|
|
621 |
if (log.is_enabled()) { // tracing
|
|
622 |
log.print("returning list of chunks...");
|
|
623 |
}
|
|
624 |
unsigned num_chunks_returned = 0;
|
|
625 |
size_t size_chunks_returned = 0;
|
|
626 |
Metachunk* cur = chunks;
|
|
627 |
while (cur != NULL) {
|
|
628 |
// Capture the next link before it is changed
|
|
629 |
// by the call to return_chunk_at_head();
|
|
630 |
Metachunk* next = cur->next();
|
|
631 |
if (log.is_enabled()) { // tracing
|
|
632 |
num_chunks_returned ++;
|
|
633 |
size_chunks_returned += cur->word_size();
|
|
634 |
}
|
|
635 |
return_single_chunk(cur);
|
|
636 |
cur = next;
|
|
637 |
}
|
|
638 |
if (log.is_enabled()) { // tracing
|
|
639 |
log.print("returned %u chunks to freelist, total word size " SIZE_FORMAT ".",
|
|
640 |
num_chunks_returned, size_chunks_returned);
|
|
641 |
}
|
|
642 |
}
|
|
643 |
|
|
644 |
void ChunkManager::collect_statistics(ChunkManagerStatistics* out) const {
|
|
645 |
MutexLockerEx cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
|
|
646 |
for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
|
|
647 |
out->chunk_stats(i).add(num_free_chunks(i), size_free_chunks_in_bytes(i) / sizeof(MetaWord));
|
|
648 |
}
|
|
649 |
}
|
|
650 |
|
|
651 |
} // namespace metaspace
|
|
652 |
|
|
653 |
|
|
654 |
|