Mon, 28 Jul 2008 15:30:23 -0700
Merge
1 /*
2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_psOldGen.cpp.incl"
28 inline const char* PSOldGen::select_name() {
29 return UseParallelOldGC ? "ParOldGen" : "PSOldGen";
30 }
32 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment,
33 size_t initial_size, size_t min_size, size_t max_size,
34 const char* perf_data_name, int level):
35 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
36 _max_gen_size(max_size)
37 {
38 initialize(rs, alignment, perf_data_name, level);
39 }
41 PSOldGen::PSOldGen(size_t initial_size,
42 size_t min_size, size_t max_size,
43 const char* perf_data_name, int level):
44 _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size),
45 _max_gen_size(max_size)
46 {}
48 void PSOldGen::initialize(ReservedSpace rs, size_t alignment,
49 const char* perf_data_name, int level) {
50 initialize_virtual_space(rs, alignment);
51 initialize_work(perf_data_name, level);
52 // The old gen can grow to gen_size_limit(). _reserve reflects only
53 // the current maximum that can be committed.
54 assert(_reserved.byte_size() <= gen_size_limit(), "Consistency check");
55 }
57 void PSOldGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) {
59 _virtual_space = new PSVirtualSpace(rs, alignment);
60 if (!_virtual_space->expand_by(_init_gen_size)) {
61 vm_exit_during_initialization("Could not reserve enough space for "
62 "object heap");
63 }
64 }
66 void PSOldGen::initialize_work(const char* perf_data_name, int level) {
67 //
68 // Basic memory initialization
69 //
71 MemRegion limit_reserved((HeapWord*)virtual_space()->low_boundary(),
72 heap_word_size(_max_gen_size));
73 assert(limit_reserved.byte_size() == _max_gen_size,
74 "word vs bytes confusion");
75 //
76 // Object start stuff
77 //
79 start_array()->initialize(limit_reserved);
81 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
82 (HeapWord*)virtual_space()->high_boundary());
84 //
85 // Card table stuff
86 //
88 MemRegion cmr((HeapWord*)virtual_space()->low(),
89 (HeapWord*)virtual_space()->high());
90 if (ZapUnusedHeapArea) {
91 // Mangle newly committed space immediately rather than
92 // waiting for the initialization of the space even though
93 // mangling is related to spaces. Doing it here eliminates
94 // the need to carry along information that a complete mangling
95 // (bottom to end) needs to be done.
96 SpaceMangler::mangle_region(cmr);
97 }
99 Universe::heap()->barrier_set()->resize_covered_region(cmr);
101 CardTableModRefBS* _ct = (CardTableModRefBS*)Universe::heap()->barrier_set();
102 assert (_ct->kind() == BarrierSet::CardTableModRef, "Sanity");
104 // Verify that the start and end of this generation is the start of a card.
105 // If this wasn't true, a single card could span more than one generation,
106 // which would cause problems when we commit/uncommit memory, and when we
107 // clear and dirty cards.
108 guarantee(_ct->is_card_aligned(_reserved.start()), "generation must be card aligned");
109 if (_reserved.end() != Universe::heap()->reserved_region().end()) {
110 // Don't check at the very end of the heap as we'll assert that we're probing off
111 // the end if we try.
112 guarantee(_ct->is_card_aligned(_reserved.end()), "generation must be card aligned");
113 }
115 //
116 // ObjectSpace stuff
117 //
119 _object_space = new MutableSpace();
121 if (_object_space == NULL)
122 vm_exit_during_initialization("Could not allocate an old gen space");
124 object_space()->initialize(cmr,
125 SpaceDecorator::Clear,
126 SpaceDecorator::Mangle);
128 _object_mark_sweep = new PSMarkSweepDecorator(_object_space, start_array(), MarkSweepDeadRatio);
130 if (_object_mark_sweep == NULL)
131 vm_exit_during_initialization("Could not complete allocation of old generation");
133 // Update the start_array
134 start_array()->set_covered_region(cmr);
136 // Generation Counters, generation 'level', 1 subspace
137 _gen_counters = new PSGenerationCounters(perf_data_name, level, 1,
138 virtual_space());
139 _space_counters = new SpaceCounters(perf_data_name, 0,
140 virtual_space()->reserved_size(),
141 _object_space, _gen_counters);
142 }
144 // Assume that the generation has been allocated if its
145 // reserved size is not 0.
146 bool PSOldGen::is_allocated() {
147 return virtual_space()->reserved_size() != 0;
148 }
150 void PSOldGen::precompact() {
151 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
152 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
154 // Reset start array first.
155 debug_only(if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {)
156 start_array()->reset();
157 debug_only(})
159 object_mark_sweep()->precompact();
161 // Now compact the young gen
162 heap->young_gen()->precompact();
163 }
165 void PSOldGen::adjust_pointers() {
166 object_mark_sweep()->adjust_pointers();
167 }
169 void PSOldGen::compact() {
170 object_mark_sweep()->compact(ZapUnusedHeapArea);
171 }
173 void PSOldGen::move_and_update(ParCompactionManager* cm) {
174 PSParallelCompact::move_and_update(cm, PSParallelCompact::old_space_id);
175 }
177 size_t PSOldGen::contiguous_available() const {
178 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size();
179 }
181 // Allocation. We report all successful allocations to the size policy
182 // Note that the perm gen does not use this method, and should not!
183 HeapWord* PSOldGen::allocate(size_t word_size, bool is_tlab) {
184 assert_locked_or_safepoint(Heap_lock);
185 HeapWord* res = allocate_noexpand(word_size, is_tlab);
187 if (res == NULL) {
188 res = expand_and_allocate(word_size, is_tlab);
189 }
191 // Allocations in the old generation need to be reported
192 if (res != NULL) {
193 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
194 heap->size_policy()->tenured_allocation(word_size);
195 }
197 return res;
198 }
200 HeapWord* PSOldGen::expand_and_allocate(size_t word_size, bool is_tlab) {
201 assert(!is_tlab, "TLAB's are not supported in PSOldGen");
202 expand(word_size*HeapWordSize);
203 if (GCExpandToAllocateDelayMillis > 0) {
204 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
205 }
206 return allocate_noexpand(word_size, is_tlab);
207 }
209 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) {
210 expand(word_size*HeapWordSize);
211 if (GCExpandToAllocateDelayMillis > 0) {
212 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
213 }
214 return cas_allocate_noexpand(word_size);
215 }
217 void PSOldGen::expand(size_t bytes) {
218 MutexLocker x(ExpandHeap_lock);
219 const size_t alignment = virtual_space()->alignment();
220 size_t aligned_bytes = align_size_up(bytes, alignment);
221 size_t aligned_expand_bytes = align_size_up(MinHeapDeltaBytes, alignment);
223 bool success = false;
224 if (aligned_expand_bytes > aligned_bytes) {
225 success = expand_by(aligned_expand_bytes);
226 }
227 if (!success) {
228 success = expand_by(aligned_bytes);
229 }
230 if (!success) {
231 success = expand_to_reserved();
232 }
234 if (GC_locker::is_active()) {
235 if (PrintGC && Verbose) {
236 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
237 }
238 }
239 }
241 bool PSOldGen::expand_by(size_t bytes) {
242 assert_lock_strong(ExpandHeap_lock);
243 assert_locked_or_safepoint(Heap_lock);
244 bool result = virtual_space()->expand_by(bytes);
245 if (result) {
246 if (ZapUnusedHeapArea) {
247 // We need to mangle the newly expanded area. The memregion spans
248 // end -> new_end, we assume that top -> end is already mangled.
249 // Do the mangling before post_resize() is called because
250 // the space is available for allocation after post_resize();
251 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high();
252 assert(object_space()->end() < virtual_space_high,
253 "Should be true before post_resize()");
254 MemRegion mangle_region(object_space()->end(), virtual_space_high);
255 // Note that the object space has not yet been updated to
256 // coincede with the new underlying virtual space.
257 SpaceMangler::mangle_region(mangle_region);
258 }
259 post_resize();
260 if (UsePerfData) {
261 _space_counters->update_capacity();
262 _gen_counters->update_all();
263 }
264 }
266 if (result && Verbose && PrintGC) {
267 size_t new_mem_size = virtual_space()->committed_size();
268 size_t old_mem_size = new_mem_size - bytes;
269 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
270 SIZE_FORMAT "K to "
271 SIZE_FORMAT "K",
272 name(), old_mem_size/K, bytes/K, new_mem_size/K);
273 }
275 return result;
276 }
278 bool PSOldGen::expand_to_reserved() {
279 assert_lock_strong(ExpandHeap_lock);
280 assert_locked_or_safepoint(Heap_lock);
282 bool result = true;
283 const size_t remaining_bytes = virtual_space()->uncommitted_size();
284 if (remaining_bytes > 0) {
285 result = expand_by(remaining_bytes);
286 DEBUG_ONLY(if (!result) warning("grow to reserve failed"));
287 }
288 return result;
289 }
291 void PSOldGen::shrink(size_t bytes) {
292 assert_lock_strong(ExpandHeap_lock);
293 assert_locked_or_safepoint(Heap_lock);
295 size_t size = align_size_down(bytes, virtual_space()->alignment());
296 if (size > 0) {
297 assert_lock_strong(ExpandHeap_lock);
298 virtual_space()->shrink_by(bytes);
299 post_resize();
301 if (Verbose && PrintGC) {
302 size_t new_mem_size = virtual_space()->committed_size();
303 size_t old_mem_size = new_mem_size + bytes;
304 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by "
305 SIZE_FORMAT "K to "
306 SIZE_FORMAT "K",
307 name(), old_mem_size/K, bytes/K, new_mem_size/K);
308 }
309 }
310 }
312 void PSOldGen::resize(size_t desired_free_space) {
313 const size_t alignment = virtual_space()->alignment();
314 const size_t size_before = virtual_space()->committed_size();
315 size_t new_size = used_in_bytes() + desired_free_space;
316 if (new_size < used_in_bytes()) {
317 // Overflowed the addition.
318 new_size = gen_size_limit();
319 }
320 // Adjust according to our min and max
321 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size());
323 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?");
324 new_size = align_size_up(new_size, alignment);
326 const size_t current_size = capacity_in_bytes();
328 if (PrintAdaptiveSizePolicy && Verbose) {
329 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
330 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT
331 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT
332 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
333 desired_free_space, used_in_bytes(), new_size, current_size,
334 gen_size_limit(), min_gen_size());
335 }
337 if (new_size == current_size) {
338 // No change requested
339 return;
340 }
341 if (new_size > current_size) {
342 size_t change_bytes = new_size - current_size;
343 expand(change_bytes);
344 } else {
345 size_t change_bytes = current_size - new_size;
346 // shrink doesn't grab this lock, expand does. Is that right?
347 MutexLocker x(ExpandHeap_lock);
348 shrink(change_bytes);
349 }
351 if (PrintAdaptiveSizePolicy) {
352 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
353 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
354 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
355 "collection: %d "
356 "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ",
357 heap->total_collections(),
358 size_before, virtual_space()->committed_size());
359 }
360 }
362 // NOTE! We need to be careful about resizing. During a GC, multiple
363 // allocators may be active during heap expansion. If we allow the
364 // heap resizing to become visible before we have correctly resized
365 // all heap related data structures, we may cause program failures.
366 void PSOldGen::post_resize() {
367 // First construct a memregion representing the new size
368 MemRegion new_memregion((HeapWord*)virtual_space()->low(),
369 (HeapWord*)virtual_space()->high());
370 size_t new_word_size = new_memregion.word_size();
372 start_array()->set_covered_region(new_memregion);
373 Universe::heap()->barrier_set()->resize_covered_region(new_memregion);
375 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high();
377 // ALWAYS do this last!!
378 object_space()->set_end(virtual_space_high);
380 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()),
381 "Sanity");
382 }
384 size_t PSOldGen::gen_size_limit() {
385 return _max_gen_size;
386 }
388 void PSOldGen::reset_after_change() {
389 ShouldNotReachHere();
390 return;
391 }
393 size_t PSOldGen::available_for_expansion() {
394 ShouldNotReachHere();
395 return 0;
396 }
398 size_t PSOldGen::available_for_contraction() {
399 ShouldNotReachHere();
400 return 0;
401 }
403 void PSOldGen::print() const { print_on(tty);}
404 void PSOldGen::print_on(outputStream* st) const {
405 st->print(" %-15s", name());
406 if (PrintGCDetails && Verbose) {
407 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
408 capacity_in_bytes(), used_in_bytes());
409 } else {
410 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
411 capacity_in_bytes()/K, used_in_bytes()/K);
412 }
413 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
414 virtual_space()->low_boundary(),
415 virtual_space()->high(),
416 virtual_space()->high_boundary());
418 st->print(" object"); object_space()->print_on(st);
419 }
421 void PSOldGen::print_used_change(size_t prev_used) const {
422 gclog_or_tty->print(" [%s:", name());
423 gclog_or_tty->print(" " SIZE_FORMAT "K"
424 "->" SIZE_FORMAT "K"
425 "(" SIZE_FORMAT "K)",
426 prev_used / K, used_in_bytes() / K,
427 capacity_in_bytes() / K);
428 gclog_or_tty->print("]");
429 }
431 void PSOldGen::update_counters() {
432 if (UsePerfData) {
433 _space_counters->update_all();
434 _gen_counters->update_all();
435 }
436 }
438 #ifndef PRODUCT
440 void PSOldGen::space_invariants() {
441 assert(object_space()->end() == (HeapWord*) virtual_space()->high(),
442 "Space invariant");
443 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(),
444 "Space invariant");
445 assert(virtual_space()->low_boundary() <= virtual_space()->low(),
446 "Space invariant");
447 assert(virtual_space()->high_boundary() >= virtual_space()->high(),
448 "Space invariant");
449 assert(virtual_space()->low_boundary() == (char*) _reserved.start(),
450 "Space invariant");
451 assert(virtual_space()->high_boundary() == (char*) _reserved.end(),
452 "Space invariant");
453 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(),
454 "Space invariant");
455 }
456 #endif
458 void PSOldGen::verify(bool allow_dirty) {
459 object_space()->verify(allow_dirty);
460 }
461 class VerifyObjectStartArrayClosure : public ObjectClosure {
462 PSOldGen* _gen;
463 ObjectStartArray* _start_array;
465 public:
466 VerifyObjectStartArrayClosure(PSOldGen* gen, ObjectStartArray* start_array) :
467 _gen(gen), _start_array(start_array) { }
469 virtual void do_object(oop obj) {
470 HeapWord* test_addr = (HeapWord*)obj + 1;
471 guarantee(_start_array->object_start(test_addr) == (HeapWord*)obj, "ObjectStartArray cannot find start of object");
472 guarantee(_start_array->is_block_allocated((HeapWord*)obj), "ObjectStartArray missing block allocation");
473 }
474 };
476 void PSOldGen::verify_object_start_array() {
477 VerifyObjectStartArrayClosure check( this, &_start_array );
478 object_iterate(&check);
479 }
481 #ifndef PRODUCT
482 void PSOldGen::record_spaces_top() {
483 assert(ZapUnusedHeapArea, "Not mangling unused space");
484 object_space()->set_top_for_allocations();
485 }
486 #endif