Fri, 16 Jul 2010 21:33:21 -0700
6962947: shared TaskQueue statistics
Reviewed-by: tonyp, ysr
1 /*
2 * Copyright (c) 2001, 2009, 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 */
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(virtual_space()->alignment());
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 start_array()->reset();
157 object_mark_sweep()->precompact();
159 // Now compact the young gen
160 heap->young_gen()->precompact();
161 }
163 void PSOldGen::adjust_pointers() {
164 object_mark_sweep()->adjust_pointers();
165 }
167 void PSOldGen::compact() {
168 object_mark_sweep()->compact(ZapUnusedHeapArea);
169 }
171 void PSOldGen::move_and_update(ParCompactionManager* cm) {
172 PSParallelCompact::move_and_update(cm, PSParallelCompact::old_space_id);
173 }
175 size_t PSOldGen::contiguous_available() const {
176 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size();
177 }
179 // Allocation. We report all successful allocations to the size policy
180 // Note that the perm gen does not use this method, and should not!
181 HeapWord* PSOldGen::allocate(size_t word_size, bool is_tlab) {
182 assert_locked_or_safepoint(Heap_lock);
183 HeapWord* res = allocate_noexpand(word_size, is_tlab);
185 if (res == NULL) {
186 res = expand_and_allocate(word_size, is_tlab);
187 }
189 // Allocations in the old generation need to be reported
190 if (res != NULL) {
191 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
192 heap->size_policy()->tenured_allocation(word_size);
193 }
195 return res;
196 }
198 HeapWord* PSOldGen::expand_and_allocate(size_t word_size, bool is_tlab) {
199 assert(!is_tlab, "TLAB's are not supported in PSOldGen");
200 expand(word_size*HeapWordSize);
201 if (GCExpandToAllocateDelayMillis > 0) {
202 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
203 }
204 return allocate_noexpand(word_size, is_tlab);
205 }
207 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) {
208 expand(word_size*HeapWordSize);
209 if (GCExpandToAllocateDelayMillis > 0) {
210 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
211 }
212 return cas_allocate_noexpand(word_size);
213 }
215 void PSOldGen::expand(size_t bytes) {
216 if (bytes == 0) {
217 return;
218 }
219 MutexLocker x(ExpandHeap_lock);
220 const size_t alignment = virtual_space()->alignment();
221 size_t aligned_bytes = align_size_up(bytes, alignment);
222 size_t aligned_expand_bytes = align_size_up(MinHeapDeltaBytes, alignment);
223 if (aligned_bytes == 0){
224 // The alignment caused the number of bytes to wrap. An expand_by(0) will
225 // return true with the implication that and expansion was done when it
226 // was not. A call to expand implies a best effort to expand by "bytes"
227 // but not a guarantee. Align down to give a best effort. This is likely
228 // the most that the generation can expand since it has some capacity to
229 // start with.
230 aligned_bytes = align_size_down(bytes, alignment);
231 }
233 bool success = false;
234 if (aligned_expand_bytes > aligned_bytes) {
235 success = expand_by(aligned_expand_bytes);
236 }
237 if (!success) {
238 success = expand_by(aligned_bytes);
239 }
240 if (!success) {
241 success = expand_to_reserved();
242 }
244 if (PrintGC && Verbose) {
245 if (success && GC_locker::is_active()) {
246 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
247 }
248 }
249 }
251 bool PSOldGen::expand_by(size_t bytes) {
252 assert_lock_strong(ExpandHeap_lock);
253 assert_locked_or_safepoint(Heap_lock);
254 if (bytes == 0) {
255 return true; // That's what virtual_space()->expand_by(0) would return
256 }
257 bool result = virtual_space()->expand_by(bytes);
258 if (result) {
259 if (ZapUnusedHeapArea) {
260 // We need to mangle the newly expanded area. The memregion spans
261 // end -> new_end, we assume that top -> end is already mangled.
262 // Do the mangling before post_resize() is called because
263 // the space is available for allocation after post_resize();
264 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high();
265 assert(object_space()->end() < virtual_space_high,
266 "Should be true before post_resize()");
267 MemRegion mangle_region(object_space()->end(), virtual_space_high);
268 // Note that the object space has not yet been updated to
269 // coincede with the new underlying virtual space.
270 SpaceMangler::mangle_region(mangle_region);
271 }
272 post_resize();
273 if (UsePerfData) {
274 _space_counters->update_capacity();
275 _gen_counters->update_all();
276 }
277 }
279 if (result && Verbose && PrintGC) {
280 size_t new_mem_size = virtual_space()->committed_size();
281 size_t old_mem_size = new_mem_size - bytes;
282 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
283 SIZE_FORMAT "K to "
284 SIZE_FORMAT "K",
285 name(), old_mem_size/K, bytes/K, new_mem_size/K);
286 }
288 return result;
289 }
291 bool PSOldGen::expand_to_reserved() {
292 assert_lock_strong(ExpandHeap_lock);
293 assert_locked_or_safepoint(Heap_lock);
295 bool result = true;
296 const size_t remaining_bytes = virtual_space()->uncommitted_size();
297 if (remaining_bytes > 0) {
298 result = expand_by(remaining_bytes);
299 DEBUG_ONLY(if (!result) warning("grow to reserve failed"));
300 }
301 return result;
302 }
304 void PSOldGen::shrink(size_t bytes) {
305 assert_lock_strong(ExpandHeap_lock);
306 assert_locked_or_safepoint(Heap_lock);
308 size_t size = align_size_down(bytes, virtual_space()->alignment());
309 if (size > 0) {
310 assert_lock_strong(ExpandHeap_lock);
311 virtual_space()->shrink_by(bytes);
312 post_resize();
314 if (Verbose && PrintGC) {
315 size_t new_mem_size = virtual_space()->committed_size();
316 size_t old_mem_size = new_mem_size + bytes;
317 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by "
318 SIZE_FORMAT "K to "
319 SIZE_FORMAT "K",
320 name(), old_mem_size/K, bytes/K, new_mem_size/K);
321 }
322 }
323 }
325 void PSOldGen::resize(size_t desired_free_space) {
326 const size_t alignment = virtual_space()->alignment();
327 const size_t size_before = virtual_space()->committed_size();
328 size_t new_size = used_in_bytes() + desired_free_space;
329 if (new_size < used_in_bytes()) {
330 // Overflowed the addition.
331 new_size = gen_size_limit();
332 }
333 // Adjust according to our min and max
334 new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size());
336 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?");
337 new_size = align_size_up(new_size, alignment);
339 const size_t current_size = capacity_in_bytes();
341 if (PrintAdaptiveSizePolicy && Verbose) {
342 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
343 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT
344 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT
345 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
346 desired_free_space, used_in_bytes(), new_size, current_size,
347 gen_size_limit(), min_gen_size());
348 }
350 if (new_size == current_size) {
351 // No change requested
352 return;
353 }
354 if (new_size > current_size) {
355 size_t change_bytes = new_size - current_size;
356 expand(change_bytes);
357 } else {
358 size_t change_bytes = current_size - new_size;
359 // shrink doesn't grab this lock, expand does. Is that right?
360 MutexLocker x(ExpandHeap_lock);
361 shrink(change_bytes);
362 }
364 if (PrintAdaptiveSizePolicy) {
365 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
366 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
367 gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: "
368 "collection: %d "
369 "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ",
370 heap->total_collections(),
371 size_before, virtual_space()->committed_size());
372 }
373 }
375 // NOTE! We need to be careful about resizing. During a GC, multiple
376 // allocators may be active during heap expansion. If we allow the
377 // heap resizing to become visible before we have correctly resized
378 // all heap related data structures, we may cause program failures.
379 void PSOldGen::post_resize() {
380 // First construct a memregion representing the new size
381 MemRegion new_memregion((HeapWord*)virtual_space()->low(),
382 (HeapWord*)virtual_space()->high());
383 size_t new_word_size = new_memregion.word_size();
385 start_array()->set_covered_region(new_memregion);
386 Universe::heap()->barrier_set()->resize_covered_region(new_memregion);
388 // ALWAYS do this last!!
389 object_space()->initialize(new_memregion,
390 SpaceDecorator::DontClear,
391 SpaceDecorator::DontMangle);
393 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()),
394 "Sanity");
395 }
397 size_t PSOldGen::gen_size_limit() {
398 return _max_gen_size;
399 }
401 void PSOldGen::reset_after_change() {
402 ShouldNotReachHere();
403 return;
404 }
406 size_t PSOldGen::available_for_expansion() {
407 ShouldNotReachHere();
408 return 0;
409 }
411 size_t PSOldGen::available_for_contraction() {
412 ShouldNotReachHere();
413 return 0;
414 }
416 void PSOldGen::print() const { print_on(tty);}
417 void PSOldGen::print_on(outputStream* st) const {
418 st->print(" %-15s", name());
419 if (PrintGCDetails && Verbose) {
420 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
421 capacity_in_bytes(), used_in_bytes());
422 } else {
423 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
424 capacity_in_bytes()/K, used_in_bytes()/K);
425 }
426 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
427 virtual_space()->low_boundary(),
428 virtual_space()->high(),
429 virtual_space()->high_boundary());
431 st->print(" object"); object_space()->print_on(st);
432 }
434 void PSOldGen::print_used_change(size_t prev_used) const {
435 gclog_or_tty->print(" [%s:", name());
436 gclog_or_tty->print(" " SIZE_FORMAT "K"
437 "->" SIZE_FORMAT "K"
438 "(" SIZE_FORMAT "K)",
439 prev_used / K, used_in_bytes() / K,
440 capacity_in_bytes() / K);
441 gclog_or_tty->print("]");
442 }
444 void PSOldGen::update_counters() {
445 if (UsePerfData) {
446 _space_counters->update_all();
447 _gen_counters->update_all();
448 }
449 }
451 #ifndef PRODUCT
453 void PSOldGen::space_invariants() {
454 assert(object_space()->end() == (HeapWord*) virtual_space()->high(),
455 "Space invariant");
456 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(),
457 "Space invariant");
458 assert(virtual_space()->low_boundary() <= virtual_space()->low(),
459 "Space invariant");
460 assert(virtual_space()->high_boundary() >= virtual_space()->high(),
461 "Space invariant");
462 assert(virtual_space()->low_boundary() == (char*) _reserved.start(),
463 "Space invariant");
464 assert(virtual_space()->high_boundary() == (char*) _reserved.end(),
465 "Space invariant");
466 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(),
467 "Space invariant");
468 }
469 #endif
471 void PSOldGen::verify(bool allow_dirty) {
472 object_space()->verify(allow_dirty);
473 }
474 class VerifyObjectStartArrayClosure : public ObjectClosure {
475 PSOldGen* _gen;
476 ObjectStartArray* _start_array;
478 public:
479 VerifyObjectStartArrayClosure(PSOldGen* gen, ObjectStartArray* start_array) :
480 _gen(gen), _start_array(start_array) { }
482 virtual void do_object(oop obj) {
483 HeapWord* test_addr = (HeapWord*)obj + 1;
484 guarantee(_start_array->object_start(test_addr) == (HeapWord*)obj, "ObjectStartArray cannot find start of object");
485 guarantee(_start_array->is_block_allocated((HeapWord*)obj), "ObjectStartArray missing block allocation");
486 }
487 };
489 void PSOldGen::verify_object_start_array() {
490 VerifyObjectStartArrayClosure check( this, &_start_array );
491 object_iterate(&check);
492 }
494 #ifndef PRODUCT
495 void PSOldGen::record_spaces_top() {
496 assert(ZapUnusedHeapArea, "Not mangling unused space");
497 object_space()->set_top_for_allocations();
498 }
499 #endif