src/share/vm/memory/generation.cpp

Fri, 21 Feb 2014 10:01:20 +0100

author
stefank
date
Fri, 21 Feb 2014 10:01:20 +0100
changeset 6973
4af19b914f53
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
child 6978
30c99d8e0f02
permissions
-rw-r--r--

8035393: Use CLDClosure instead of CLDToOopClosure in frame::oops_interpreted_do
Reviewed-by: tschatzl, coleenp

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
sla@5237 26 #include "gc_implementation/shared/gcTimer.hpp"
sla@5237 27 #include "gc_implementation/shared/gcTrace.hpp"
stefank@2314 28 #include "gc_implementation/shared/spaceDecorator.hpp"
stefank@2314 29 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 30 #include "memory/allocation.inline.hpp"
stefank@2314 31 #include "memory/blockOffsetTable.inline.hpp"
stefank@2314 32 #include "memory/cardTableRS.hpp"
stefank@2314 33 #include "memory/gcLocker.inline.hpp"
stefank@2314 34 #include "memory/genCollectedHeap.hpp"
stefank@2314 35 #include "memory/genMarkSweep.hpp"
stefank@2314 36 #include "memory/genOopClosures.hpp"
stefank@2314 37 #include "memory/genOopClosures.inline.hpp"
stefank@2314 38 #include "memory/generation.hpp"
stefank@2314 39 #include "memory/generation.inline.hpp"
stefank@2314 40 #include "memory/space.inline.hpp"
stefank@2314 41 #include "oops/oop.inline.hpp"
stefank@2314 42 #include "runtime/java.hpp"
stefank@2314 43 #include "utilities/copy.hpp"
stefank@2314 44 #include "utilities/events.hpp"
duke@435 45
drchase@6680 46 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 47
duke@435 48 Generation::Generation(ReservedSpace rs, size_t initial_size, int level) :
duke@435 49 _level(level),
duke@435 50 _ref_processor(NULL) {
duke@435 51 if (!_virtual_space.initialize(rs, initial_size)) {
duke@435 52 vm_exit_during_initialization("Could not reserve enough space for "
duke@435 53 "object heap");
duke@435 54 }
jmasa@698 55 // Mangle all of the the initial generation.
jmasa@698 56 if (ZapUnusedHeapArea) {
jmasa@698 57 MemRegion mangle_region((HeapWord*)_virtual_space.low(),
jmasa@698 58 (HeapWord*)_virtual_space.high());
jmasa@698 59 SpaceMangler::mangle_region(mangle_region);
jmasa@698 60 }
duke@435 61 _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(),
duke@435 62 (HeapWord*)_virtual_space.high_boundary());
duke@435 63 }
duke@435 64
duke@435 65 GenerationSpec* Generation::spec() {
duke@435 66 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 67 assert(0 <= level() && level() < gch->_n_gens, "Bad gen level");
duke@435 68 return gch->_gen_specs[level()];
duke@435 69 }
duke@435 70
duke@435 71 size_t Generation::max_capacity() const {
duke@435 72 return reserved().byte_size();
duke@435 73 }
duke@435 74
duke@435 75 void Generation::print_heap_change(size_t prev_used) const {
duke@435 76 if (PrintGCDetails && Verbose) {
duke@435 77 gclog_or_tty->print(" " SIZE_FORMAT
duke@435 78 "->" SIZE_FORMAT
duke@435 79 "(" SIZE_FORMAT ")",
duke@435 80 prev_used, used(), capacity());
duke@435 81 } else {
duke@435 82 gclog_or_tty->print(" " SIZE_FORMAT "K"
duke@435 83 "->" SIZE_FORMAT "K"
duke@435 84 "(" SIZE_FORMAT "K)",
duke@435 85 prev_used / K, used() / K, capacity() / K);
duke@435 86 }
duke@435 87 }
duke@435 88
duke@435 89 // By default we get a single threaded default reference processor;
ysr@2651 90 // generations needing multi-threaded refs processing or discovery override this method.
duke@435 91 void Generation::ref_processor_init() {
duke@435 92 assert(_ref_processor == NULL, "a reference processor already exists");
duke@435 93 assert(!_reserved.is_empty(), "empty generation?");
ysr@2651 94 _ref_processor = new ReferenceProcessor(_reserved); // a vanilla reference processor
duke@435 95 if (_ref_processor == NULL) {
duke@435 96 vm_exit_during_initialization("Could not allocate ReferenceProcessor object");
duke@435 97 }
duke@435 98 }
duke@435 99
duke@435 100 void Generation::print() const { print_on(tty); }
duke@435 101
duke@435 102 void Generation::print_on(outputStream* st) const {
duke@435 103 st->print(" %-20s", name());
duke@435 104 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
duke@435 105 capacity()/K, used()/K);
duke@435 106 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
duke@435 107 _virtual_space.low_boundary(),
duke@435 108 _virtual_space.high(),
duke@435 109 _virtual_space.high_boundary());
duke@435 110 }
duke@435 111
duke@435 112 void Generation::print_summary_info() { print_summary_info_on(tty); }
duke@435 113
duke@435 114 void Generation::print_summary_info_on(outputStream* st) {
duke@435 115 StatRecord* sr = stat_record();
duke@435 116 double time = sr->accumulated_time.seconds();
duke@435 117 st->print_cr("[Accumulated GC generation %d time %3.7f secs, "
duke@435 118 "%d GC's, avg GC time %3.7f]",
duke@435 119 level(), time, sr->invocations,
duke@435 120 sr->invocations > 0 ? time / sr->invocations : 0.0);
duke@435 121 }
duke@435 122
duke@435 123 // Utility iterator classes
duke@435 124
duke@435 125 class GenerationIsInReservedClosure : public SpaceClosure {
duke@435 126 public:
duke@435 127 const void* _p;
duke@435 128 Space* sp;
duke@435 129 virtual void do_space(Space* s) {
duke@435 130 if (sp == NULL) {
duke@435 131 if (s->is_in_reserved(_p)) sp = s;
duke@435 132 }
duke@435 133 }
duke@435 134 GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {}
duke@435 135 };
duke@435 136
duke@435 137 class GenerationIsInClosure : public SpaceClosure {
duke@435 138 public:
duke@435 139 const void* _p;
duke@435 140 Space* sp;
duke@435 141 virtual void do_space(Space* s) {
duke@435 142 if (sp == NULL) {
duke@435 143 if (s->is_in(_p)) sp = s;
duke@435 144 }
duke@435 145 }
duke@435 146 GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {}
duke@435 147 };
duke@435 148
duke@435 149 bool Generation::is_in(const void* p) const {
duke@435 150 GenerationIsInClosure blk(p);
duke@435 151 ((Generation*)this)->space_iterate(&blk);
duke@435 152 return blk.sp != NULL;
duke@435 153 }
duke@435 154
duke@435 155 DefNewGeneration* Generation::as_DefNewGeneration() {
duke@435 156 assert((kind() == Generation::DefNew) ||
duke@435 157 (kind() == Generation::ParNew) ||
duke@435 158 (kind() == Generation::ASParNew),
duke@435 159 "Wrong youngest generation type");
duke@435 160 return (DefNewGeneration*) this;
duke@435 161 }
duke@435 162
duke@435 163 Generation* Generation::next_gen() const {
duke@435 164 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 165 int next = level() + 1;
duke@435 166 if (next < gch->_n_gens) {
duke@435 167 return gch->_gens[next];
duke@435 168 } else {
duke@435 169 return NULL;
duke@435 170 }
duke@435 171 }
duke@435 172
duke@435 173 size_t Generation::max_contiguous_available() const {
duke@435 174 // The largest number of contiguous free words in this or any higher generation.
duke@435 175 size_t max = 0;
duke@435 176 for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) {
duke@435 177 size_t avail = gen->contiguous_available();
duke@435 178 if (avail > max) {
duke@435 179 max = avail;
duke@435 180 }
duke@435 181 }
duke@435 182 return max;
duke@435 183 }
duke@435 184
ysr@2243 185 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
ysr@2243 186 size_t available = max_contiguous_available();
ysr@2243 187 bool res = (available >= max_promotion_in_bytes);
duke@435 188 if (PrintGC && Verbose) {
ysr@2243 189 gclog_or_tty->print_cr(
ysr@2243 190 "Generation: promo attempt is%s safe: available("SIZE_FORMAT") %s max_promo("SIZE_FORMAT")",
ysr@2243 191 res? "":" not", available, res? ">=":"<",
ysr@2243 192 max_promotion_in_bytes);
duke@435 193 }
ysr@2243 194 return res;
duke@435 195 }
duke@435 196
duke@435 197 // Ignores "ref" and calls allocate().
coleenp@548 198 oop Generation::promote(oop obj, size_t obj_size) {
duke@435 199 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
duke@435 200
duke@435 201 #ifndef PRODUCT
duke@435 202 if (Universe::heap()->promotion_should_fail()) {
duke@435 203 return NULL;
duke@435 204 }
duke@435 205 #endif // #ifndef PRODUCT
duke@435 206
duke@435 207 HeapWord* result = allocate(obj_size, false);
duke@435 208 if (result != NULL) {
duke@435 209 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
duke@435 210 return oop(result);
duke@435 211 } else {
duke@435 212 GenCollectedHeap* gch = GenCollectedHeap::heap();
coleenp@548 213 return gch->handle_failed_promotion(this, obj, obj_size);
duke@435 214 }
duke@435 215 }
duke@435 216
duke@435 217 oop Generation::par_promote(int thread_num,
duke@435 218 oop obj, markOop m, size_t word_sz) {
duke@435 219 // Could do a bad general impl here that gets a lock. But no.
duke@435 220 ShouldNotCallThis();
duke@435 221 return NULL;
duke@435 222 }
duke@435 223
duke@435 224 void Generation::par_promote_alloc_undo(int thread_num,
duke@435 225 HeapWord* obj, size_t word_sz) {
duke@435 226 // Could do a bad general impl here that gets a lock. But no.
duke@435 227 guarantee(false, "No good general implementation.");
duke@435 228 }
duke@435 229
duke@435 230 Space* Generation::space_containing(const void* p) const {
duke@435 231 GenerationIsInReservedClosure blk(p);
duke@435 232 // Cast away const
duke@435 233 ((Generation*)this)->space_iterate(&blk);
duke@435 234 return blk.sp;
duke@435 235 }
duke@435 236
duke@435 237 // Some of these are mediocre general implementations. Should be
duke@435 238 // overridden to get better performance.
duke@435 239
duke@435 240 class GenerationBlockStartClosure : public SpaceClosure {
duke@435 241 public:
duke@435 242 const void* _p;
duke@435 243 HeapWord* _start;
duke@435 244 virtual void do_space(Space* s) {
duke@435 245 if (_start == NULL && s->is_in_reserved(_p)) {
duke@435 246 _start = s->block_start(_p);
duke@435 247 }
duke@435 248 }
duke@435 249 GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; }
duke@435 250 };
duke@435 251
duke@435 252 HeapWord* Generation::block_start(const void* p) const {
duke@435 253 GenerationBlockStartClosure blk(p);
duke@435 254 // Cast away const
duke@435 255 ((Generation*)this)->space_iterate(&blk);
duke@435 256 return blk._start;
duke@435 257 }
duke@435 258
duke@435 259 class GenerationBlockSizeClosure : public SpaceClosure {
duke@435 260 public:
duke@435 261 const HeapWord* _p;
duke@435 262 size_t size;
duke@435 263 virtual void do_space(Space* s) {
duke@435 264 if (size == 0 && s->is_in_reserved(_p)) {
duke@435 265 size = s->block_size(_p);
duke@435 266 }
duke@435 267 }
duke@435 268 GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; }
duke@435 269 };
duke@435 270
duke@435 271 size_t Generation::block_size(const HeapWord* p) const {
duke@435 272 GenerationBlockSizeClosure blk(p);
duke@435 273 // Cast away const
duke@435 274 ((Generation*)this)->space_iterate(&blk);
duke@435 275 assert(blk.size > 0, "seems reasonable");
duke@435 276 return blk.size;
duke@435 277 }
duke@435 278
duke@435 279 class GenerationBlockIsObjClosure : public SpaceClosure {
duke@435 280 public:
duke@435 281 const HeapWord* _p;
duke@435 282 bool is_obj;
duke@435 283 virtual void do_space(Space* s) {
duke@435 284 if (!is_obj && s->is_in_reserved(_p)) {
duke@435 285 is_obj |= s->block_is_obj(_p);
duke@435 286 }
duke@435 287 }
duke@435 288 GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; }
duke@435 289 };
duke@435 290
duke@435 291 bool Generation::block_is_obj(const HeapWord* p) const {
duke@435 292 GenerationBlockIsObjClosure blk(p);
duke@435 293 // Cast away const
duke@435 294 ((Generation*)this)->space_iterate(&blk);
duke@435 295 return blk.is_obj;
duke@435 296 }
duke@435 297
duke@435 298 class GenerationOopIterateClosure : public SpaceClosure {
duke@435 299 public:
coleenp@4037 300 ExtendedOopClosure* cl;
duke@435 301 MemRegion mr;
duke@435 302 virtual void do_space(Space* s) {
duke@435 303 s->oop_iterate(mr, cl);
duke@435 304 }
coleenp@4037 305 GenerationOopIterateClosure(ExtendedOopClosure* _cl, MemRegion _mr) :
duke@435 306 cl(_cl), mr(_mr) {}
duke@435 307 };
duke@435 308
coleenp@4037 309 void Generation::oop_iterate(ExtendedOopClosure* cl) {
duke@435 310 GenerationOopIterateClosure blk(cl, _reserved);
duke@435 311 space_iterate(&blk);
duke@435 312 }
duke@435 313
coleenp@4037 314 void Generation::oop_iterate(MemRegion mr, ExtendedOopClosure* cl) {
duke@435 315 GenerationOopIterateClosure blk(cl, mr);
duke@435 316 space_iterate(&blk);
duke@435 317 }
duke@435 318
duke@435 319 void Generation::younger_refs_in_space_iterate(Space* sp,
duke@435 320 OopsInGenClosure* cl) {
duke@435 321 GenRemSet* rs = SharedHeap::heap()->rem_set();
duke@435 322 rs->younger_refs_in_space_iterate(sp, cl);
duke@435 323 }
duke@435 324
duke@435 325 class GenerationObjIterateClosure : public SpaceClosure {
duke@435 326 private:
duke@435 327 ObjectClosure* _cl;
duke@435 328 public:
duke@435 329 virtual void do_space(Space* s) {
duke@435 330 s->object_iterate(_cl);
duke@435 331 }
duke@435 332 GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
duke@435 333 };
duke@435 334
duke@435 335 void Generation::object_iterate(ObjectClosure* cl) {
duke@435 336 GenerationObjIterateClosure blk(cl);
duke@435 337 space_iterate(&blk);
duke@435 338 }
duke@435 339
jmasa@952 340 class GenerationSafeObjIterateClosure : public SpaceClosure {
jmasa@952 341 private:
jmasa@952 342 ObjectClosure* _cl;
jmasa@952 343 public:
jmasa@952 344 virtual void do_space(Space* s) {
jmasa@952 345 s->safe_object_iterate(_cl);
jmasa@952 346 }
jmasa@952 347 GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
jmasa@952 348 };
jmasa@952 349
jmasa@952 350 void Generation::safe_object_iterate(ObjectClosure* cl) {
jmasa@952 351 GenerationSafeObjIterateClosure blk(cl);
jmasa@952 352 space_iterate(&blk);
jmasa@952 353 }
jmasa@952 354
duke@435 355 void Generation::prepare_for_compaction(CompactPoint* cp) {
duke@435 356 // Generic implementation, can be specialized
duke@435 357 CompactibleSpace* space = first_compaction_space();
duke@435 358 while (space != NULL) {
duke@435 359 space->prepare_for_compaction(cp);
duke@435 360 space = space->next_compaction_space();
duke@435 361 }
duke@435 362 }
duke@435 363
duke@435 364 class AdjustPointersClosure: public SpaceClosure {
duke@435 365 public:
duke@435 366 void do_space(Space* sp) {
duke@435 367 sp->adjust_pointers();
duke@435 368 }
duke@435 369 };
duke@435 370
duke@435 371 void Generation::adjust_pointers() {
duke@435 372 // Note that this is done over all spaces, not just the compactible
duke@435 373 // ones.
duke@435 374 AdjustPointersClosure blk;
duke@435 375 space_iterate(&blk, true);
duke@435 376 }
duke@435 377
duke@435 378 void Generation::compact() {
duke@435 379 CompactibleSpace* sp = first_compaction_space();
duke@435 380 while (sp != NULL) {
duke@435 381 sp->compact();
duke@435 382 sp = sp->next_compaction_space();
duke@435 383 }
duke@435 384 }
duke@435 385
duke@435 386 CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size,
duke@435 387 int level,
duke@435 388 GenRemSet* remset) :
jmasa@4900 389 Generation(rs, initial_byte_size, level), _rs(remset),
jmasa@4900 390 _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(),
jmasa@4900 391 _used_at_prologue()
duke@435 392 {
duke@435 393 HeapWord* start = (HeapWord*)rs.base();
duke@435 394 size_t reserved_byte_size = rs.size();
duke@435 395 assert((uintptr_t(start) & 3) == 0, "bad alignment");
duke@435 396 assert((reserved_byte_size & 3) == 0, "bad alignment");
duke@435 397 MemRegion reserved_mr(start, heap_word_size(reserved_byte_size));
duke@435 398 _bts = new BlockOffsetSharedArray(reserved_mr,
duke@435 399 heap_word_size(initial_byte_size));
duke@435 400 MemRegion committed_mr(start, heap_word_size(initial_byte_size));
duke@435 401 _rs->resize_covered_region(committed_mr);
duke@435 402 if (_bts == NULL)
duke@435 403 vm_exit_during_initialization("Could not allocate a BlockOffsetArray");
duke@435 404
duke@435 405 // Verify that the start and end of this generation is the start of a card.
duke@435 406 // If this wasn't true, a single card could span more than on generation,
duke@435 407 // which would cause problems when we commit/uncommit memory, and when we
duke@435 408 // clear and dirty cards.
duke@435 409 guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned");
duke@435 410 if (reserved_mr.end() != Universe::heap()->reserved_region().end()) {
duke@435 411 // Don't check at the very end of the heap as we'll assert that we're probing off
duke@435 412 // the end if we try.
duke@435 413 guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned");
duke@435 414 }
jmasa@4900 415 _min_heap_delta_bytes = MinHeapDeltaBytes;
jmasa@4900 416 _capacity_at_prologue = initial_byte_size;
jmasa@4900 417 _used_at_prologue = 0;
duke@435 418 }
duke@435 419
jmasa@706 420 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) {
jmasa@706 421 assert_locked_or_safepoint(Heap_lock);
jmasa@706 422 if (bytes == 0) {
jmasa@706 423 return true; // That's what grow_by(0) would return
jmasa@706 424 }
jmasa@706 425 size_t aligned_bytes = ReservedSpace::page_align_size_up(bytes);
jmasa@706 426 if (aligned_bytes == 0){
jmasa@706 427 // The alignment caused the number of bytes to wrap. An expand_by(0) will
jmasa@706 428 // return true with the implication that an expansion was done when it
jmasa@706 429 // was not. A call to expand implies a best effort to expand by "bytes"
jmasa@706 430 // but not a guarantee. Align down to give a best effort. This is likely
jmasa@706 431 // the most that the generation can expand since it has some capacity to
jmasa@706 432 // start with.
jmasa@706 433 aligned_bytes = ReservedSpace::page_align_size_down(bytes);
jmasa@706 434 }
jmasa@706 435 size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes);
jmasa@706 436 bool success = false;
jmasa@706 437 if (aligned_expand_bytes > aligned_bytes) {
jmasa@706 438 success = grow_by(aligned_expand_bytes);
jmasa@706 439 }
jmasa@706 440 if (!success) {
jmasa@706 441 success = grow_by(aligned_bytes);
jmasa@706 442 }
jmasa@706 443 if (!success) {
jmasa@706 444 success = grow_to_reserved();
jmasa@706 445 }
jmasa@706 446 if (PrintGC && Verbose) {
coleenp@4037 447 if (success && GC_locker::is_active_and_needs_gc()) {
jmasa@706 448 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
jmasa@706 449 }
jmasa@706 450 }
jmasa@706 451
jmasa@706 452 return success;
jmasa@706 453 }
jmasa@706 454
duke@435 455
duke@435 456 // No young generation references, clear this generation's cards.
duke@435 457 void CardGeneration::clear_remembered_set() {
duke@435 458 _rs->clear(reserved());
duke@435 459 }
duke@435 460
duke@435 461
duke@435 462 // Objects in this generation may have moved, invalidate this
duke@435 463 // generation's cards.
duke@435 464 void CardGeneration::invalidate_remembered_set() {
duke@435 465 _rs->invalidate(used_region());
duke@435 466 }
duke@435 467
duke@435 468
jmasa@4900 469 void CardGeneration::compute_new_size() {
jmasa@4900 470 assert(_shrink_factor <= 100, "invalid shrink factor");
jmasa@4900 471 size_t current_shrink_factor = _shrink_factor;
jmasa@4900 472 _shrink_factor = 0;
jmasa@4900 473
jmasa@4900 474 // We don't have floating point command-line arguments
jmasa@4900 475 // Note: argument processing ensures that MinHeapFreeRatio < 100.
jmasa@4900 476 const double minimum_free_percentage = MinHeapFreeRatio / 100.0;
jmasa@4900 477 const double maximum_used_percentage = 1.0 - minimum_free_percentage;
jmasa@4900 478
jmasa@4900 479 // Compute some numbers about the state of the heap.
jmasa@4900 480 const size_t used_after_gc = used();
jmasa@4900 481 const size_t capacity_after_gc = capacity();
jmasa@4900 482
jmasa@4900 483 const double min_tmp = used_after_gc / maximum_used_percentage;
jmasa@4900 484 size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx));
jmasa@4900 485 // Don't shrink less than the initial generation size
jmasa@4900 486 minimum_desired_capacity = MAX2(minimum_desired_capacity,
jmasa@4900 487 spec()->init_size());
jmasa@4900 488 assert(used_after_gc <= minimum_desired_capacity, "sanity check");
jmasa@4900 489
jmasa@4900 490 if (PrintGC && Verbose) {
jmasa@4900 491 const size_t free_after_gc = free();
jmasa@4900 492 const double free_percentage = ((double)free_after_gc) / capacity_after_gc;
jmasa@4900 493 gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: ");
jmasa@4900 494 gclog_or_tty->print_cr(" "
jmasa@4900 495 " minimum_free_percentage: %6.2f"
jmasa@4900 496 " maximum_used_percentage: %6.2f",
jmasa@4900 497 minimum_free_percentage,
jmasa@4900 498 maximum_used_percentage);
jmasa@4900 499 gclog_or_tty->print_cr(" "
jmasa@4900 500 " free_after_gc : %6.1fK"
jmasa@4900 501 " used_after_gc : %6.1fK"
jmasa@4900 502 " capacity_after_gc : %6.1fK",
jmasa@4900 503 free_after_gc / (double) K,
jmasa@4900 504 used_after_gc / (double) K,
jmasa@4900 505 capacity_after_gc / (double) K);
jmasa@4900 506 gclog_or_tty->print_cr(" "
jmasa@4900 507 " free_percentage: %6.2f",
jmasa@4900 508 free_percentage);
jmasa@4900 509 }
jmasa@4900 510
jmasa@4900 511 if (capacity_after_gc < minimum_desired_capacity) {
jmasa@4900 512 // If we have less free space than we want then expand
jmasa@4900 513 size_t expand_bytes = minimum_desired_capacity - capacity_after_gc;
jmasa@4900 514 // Don't expand unless it's significant
jmasa@4900 515 if (expand_bytes >= _min_heap_delta_bytes) {
jmasa@4900 516 expand(expand_bytes, 0); // safe if expansion fails
jmasa@4900 517 }
jmasa@4900 518 if (PrintGC && Verbose) {
jmasa@4900 519 gclog_or_tty->print_cr(" expanding:"
jmasa@4900 520 " minimum_desired_capacity: %6.1fK"
jmasa@4900 521 " expand_bytes: %6.1fK"
jmasa@4900 522 " _min_heap_delta_bytes: %6.1fK",
jmasa@4900 523 minimum_desired_capacity / (double) K,
jmasa@4900 524 expand_bytes / (double) K,
jmasa@4900 525 _min_heap_delta_bytes / (double) K);
jmasa@4900 526 }
jmasa@4900 527 return;
jmasa@4900 528 }
jmasa@4900 529
jmasa@4900 530 // No expansion, now see if we want to shrink
jmasa@4900 531 size_t shrink_bytes = 0;
jmasa@4900 532 // We would never want to shrink more than this
jmasa@4900 533 size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity;
jmasa@4900 534
jmasa@4900 535 if (MaxHeapFreeRatio < 100) {
jmasa@4900 536 const double maximum_free_percentage = MaxHeapFreeRatio / 100.0;
jmasa@4900 537 const double minimum_used_percentage = 1.0 - maximum_free_percentage;
jmasa@4900 538 const double max_tmp = used_after_gc / minimum_used_percentage;
jmasa@4900 539 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
jmasa@4900 540 maximum_desired_capacity = MAX2(maximum_desired_capacity,
jmasa@4900 541 spec()->init_size());
jmasa@4900 542 if (PrintGC && Verbose) {
jmasa@4900 543 gclog_or_tty->print_cr(" "
jmasa@4900 544 " maximum_free_percentage: %6.2f"
jmasa@4900 545 " minimum_used_percentage: %6.2f",
jmasa@4900 546 maximum_free_percentage,
jmasa@4900 547 minimum_used_percentage);
jmasa@4900 548 gclog_or_tty->print_cr(" "
jmasa@4900 549 " _capacity_at_prologue: %6.1fK"
jmasa@4900 550 " minimum_desired_capacity: %6.1fK"
jmasa@4900 551 " maximum_desired_capacity: %6.1fK",
jmasa@4900 552 _capacity_at_prologue / (double) K,
jmasa@4900 553 minimum_desired_capacity / (double) K,
jmasa@4900 554 maximum_desired_capacity / (double) K);
jmasa@4900 555 }
jmasa@4900 556 assert(minimum_desired_capacity <= maximum_desired_capacity,
jmasa@4900 557 "sanity check");
jmasa@4900 558
jmasa@4900 559 if (capacity_after_gc > maximum_desired_capacity) {
jmasa@4900 560 // Capacity too large, compute shrinking size
jmasa@4900 561 shrink_bytes = capacity_after_gc - maximum_desired_capacity;
jmasa@4900 562 // We don't want shrink all the way back to initSize if people call
jmasa@4900 563 // System.gc(), because some programs do that between "phases" and then
jmasa@4900 564 // we'd just have to grow the heap up again for the next phase. So we
jmasa@4900 565 // damp the shrinking: 0% on the first call, 10% on the second call, 40%
jmasa@4900 566 // on the third call, and 100% by the fourth call. But if we recompute
jmasa@4900 567 // size without shrinking, it goes back to 0%.
jmasa@4900 568 shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
jmasa@4900 569 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size");
jmasa@4900 570 if (current_shrink_factor == 0) {
jmasa@4900 571 _shrink_factor = 10;
jmasa@4900 572 } else {
jmasa@4900 573 _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100);
jmasa@4900 574 }
jmasa@4900 575 if (PrintGC && Verbose) {
jmasa@4900 576 gclog_or_tty->print_cr(" "
jmasa@4900 577 " shrinking:"
jmasa@4900 578 " initSize: %.1fK"
jmasa@4900 579 " maximum_desired_capacity: %.1fK",
jmasa@4900 580 spec()->init_size() / (double) K,
jmasa@4900 581 maximum_desired_capacity / (double) K);
jmasa@4900 582 gclog_or_tty->print_cr(" "
jmasa@4900 583 " shrink_bytes: %.1fK"
jmasa@4900 584 " current_shrink_factor: %d"
jmasa@4900 585 " new shrink factor: %d"
jmasa@4900 586 " _min_heap_delta_bytes: %.1fK",
jmasa@4900 587 shrink_bytes / (double) K,
jmasa@4900 588 current_shrink_factor,
jmasa@4900 589 _shrink_factor,
jmasa@4900 590 _min_heap_delta_bytes / (double) K);
jmasa@4900 591 }
jmasa@4900 592 }
jmasa@4900 593 }
jmasa@4900 594
jmasa@4900 595 if (capacity_after_gc > _capacity_at_prologue) {
jmasa@4900 596 // We might have expanded for promotions, in which case we might want to
jmasa@4900 597 // take back that expansion if there's room after GC. That keeps us from
jmasa@4900 598 // stretching the heap with promotions when there's plenty of room.
jmasa@4900 599 size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue;
jmasa@4900 600 expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes);
jmasa@4900 601 // We have two shrinking computations, take the largest
jmasa@4900 602 shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion);
jmasa@4900 603 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size");
jmasa@4900 604 if (PrintGC && Verbose) {
jmasa@4900 605 gclog_or_tty->print_cr(" "
jmasa@4900 606 " aggressive shrinking:"
jmasa@4900 607 " _capacity_at_prologue: %.1fK"
jmasa@4900 608 " capacity_after_gc: %.1fK"
jmasa@4900 609 " expansion_for_promotion: %.1fK"
jmasa@4900 610 " shrink_bytes: %.1fK",
jmasa@4900 611 capacity_after_gc / (double) K,
jmasa@4900 612 _capacity_at_prologue / (double) K,
jmasa@4900 613 expansion_for_promotion / (double) K,
jmasa@4900 614 shrink_bytes / (double) K);
jmasa@4900 615 }
jmasa@4900 616 }
jmasa@4900 617 // Don't shrink unless it's significant
jmasa@4900 618 if (shrink_bytes >= _min_heap_delta_bytes) {
jmasa@4900 619 shrink(shrink_bytes);
jmasa@4900 620 }
jmasa@4900 621 }
jmasa@4900 622
duke@435 623 // Currently nothing to do.
duke@435 624 void CardGeneration::prepare_for_verify() {}
duke@435 625
duke@435 626
duke@435 627 void OneContigSpaceCardGeneration::collect(bool full,
duke@435 628 bool clear_all_soft_refs,
duke@435 629 size_t size,
duke@435 630 bool is_tlab) {
sla@5237 631 GenCollectedHeap* gch = GenCollectedHeap::heap();
sla@5237 632
duke@435 633 SpecializationStats::clear();
duke@435 634 // Temporarily expand the span of our ref processor, so
duke@435 635 // refs discovery is over the entire heap, not just this generation
duke@435 636 ReferenceProcessorSpanMutator
sla@5237 637 x(ref_processor(), gch->reserved_region());
sla@5237 638
sla@5237 639 STWGCTimer* gc_timer = GenMarkSweep::gc_timer();
mgronlun@6131 640 gc_timer->register_gc_start();
sla@5237 641
sla@5237 642 SerialOldTracer* gc_tracer = GenMarkSweep::gc_tracer();
sla@5237 643 gc_tracer->report_gc_start(gch->gc_cause(), gc_timer->gc_start());
sla@5237 644
duke@435 645 GenMarkSweep::invoke_at_safepoint(_level, ref_processor(), clear_all_soft_refs);
sla@5237 646
mgronlun@6131 647 gc_timer->register_gc_end();
sla@5237 648
mgronlun@6131 649 gc_tracer->report_gc_end(gc_timer->gc_end(), gc_timer->time_partitions());
sla@5237 650
duke@435 651 SpecializationStats::print();
duke@435 652 }
duke@435 653
duke@435 654 HeapWord*
duke@435 655 OneContigSpaceCardGeneration::expand_and_allocate(size_t word_size,
duke@435 656 bool is_tlab,
duke@435 657 bool parallel) {
duke@435 658 assert(!is_tlab, "OneContigSpaceCardGeneration does not support TLAB allocation");
duke@435 659 if (parallel) {
duke@435 660 MutexLocker x(ParGCRareEvent_lock);
duke@435 661 HeapWord* result = NULL;
duke@435 662 size_t byte_size = word_size * HeapWordSize;
duke@435 663 while (true) {
duke@435 664 expand(byte_size, _min_heap_delta_bytes);
duke@435 665 if (GCExpandToAllocateDelayMillis > 0) {
duke@435 666 os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
duke@435 667 }
duke@435 668 result = _the_space->par_allocate(word_size);
duke@435 669 if ( result != NULL) {
duke@435 670 return result;
duke@435 671 } else {
duke@435 672 // If there's not enough expansion space available, give up.
duke@435 673 if (_virtual_space.uncommitted_size() < byte_size) {
duke@435 674 return NULL;
duke@435 675 }
duke@435 676 // else try again
duke@435 677 }
duke@435 678 }
duke@435 679 } else {
duke@435 680 expand(word_size*HeapWordSize, _min_heap_delta_bytes);
duke@435 681 return _the_space->allocate(word_size);
duke@435 682 }
duke@435 683 }
duke@435 684
jmasa@706 685 bool OneContigSpaceCardGeneration::expand(size_t bytes, size_t expand_bytes) {
duke@435 686 GCMutexLocker x(ExpandHeap_lock);
jmasa@706 687 return CardGeneration::expand(bytes, expand_bytes);
duke@435 688 }
duke@435 689
duke@435 690
duke@435 691 void OneContigSpaceCardGeneration::shrink(size_t bytes) {
duke@435 692 assert_locked_or_safepoint(ExpandHeap_lock);
duke@435 693 size_t size = ReservedSpace::page_align_size_down(bytes);
duke@435 694 if (size > 0) {
duke@435 695 shrink_by(size);
duke@435 696 }
duke@435 697 }
duke@435 698
duke@435 699
duke@435 700 size_t OneContigSpaceCardGeneration::capacity() const {
duke@435 701 return _the_space->capacity();
duke@435 702 }
duke@435 703
duke@435 704
duke@435 705 size_t OneContigSpaceCardGeneration::used() const {
duke@435 706 return _the_space->used();
duke@435 707 }
duke@435 708
duke@435 709
duke@435 710 size_t OneContigSpaceCardGeneration::free() const {
duke@435 711 return _the_space->free();
duke@435 712 }
duke@435 713
duke@435 714 MemRegion OneContigSpaceCardGeneration::used_region() const {
duke@435 715 return the_space()->used_region();
duke@435 716 }
duke@435 717
duke@435 718 size_t OneContigSpaceCardGeneration::unsafe_max_alloc_nogc() const {
duke@435 719 return _the_space->free();
duke@435 720 }
duke@435 721
duke@435 722 size_t OneContigSpaceCardGeneration::contiguous_available() const {
duke@435 723 return _the_space->free() + _virtual_space.uncommitted_size();
duke@435 724 }
duke@435 725
duke@435 726 bool OneContigSpaceCardGeneration::grow_by(size_t bytes) {
duke@435 727 assert_locked_or_safepoint(ExpandHeap_lock);
duke@435 728 bool result = _virtual_space.expand_by(bytes);
duke@435 729 if (result) {
duke@435 730 size_t new_word_size =
duke@435 731 heap_word_size(_virtual_space.committed_size());
duke@435 732 MemRegion mr(_the_space->bottom(), new_word_size);
duke@435 733 // Expand card table
duke@435 734 Universe::heap()->barrier_set()->resize_covered_region(mr);
duke@435 735 // Expand shared block offset array
duke@435 736 _bts->resize(new_word_size);
duke@435 737
duke@435 738 // Fix for bug #4668531
jmasa@698 739 if (ZapUnusedHeapArea) {
jmasa@698 740 MemRegion mangle_region(_the_space->end(),
jmasa@698 741 (HeapWord*)_virtual_space.high());
jmasa@698 742 SpaceMangler::mangle_region(mangle_region);
jmasa@698 743 }
duke@435 744
duke@435 745 // Expand space -- also expands space's BOT
duke@435 746 // (which uses (part of) shared array above)
duke@435 747 _the_space->set_end((HeapWord*)_virtual_space.high());
duke@435 748
duke@435 749 // update the space and generation capacity counters
duke@435 750 update_counters();
duke@435 751
duke@435 752 if (Verbose && PrintGC) {
duke@435 753 size_t new_mem_size = _virtual_space.committed_size();
duke@435 754 size_t old_mem_size = new_mem_size - bytes;
duke@435 755 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
duke@435 756 SIZE_FORMAT "K to " SIZE_FORMAT "K",
duke@435 757 name(), old_mem_size/K, bytes/K, new_mem_size/K);
duke@435 758 }
duke@435 759 }
duke@435 760 return result;
duke@435 761 }
duke@435 762
duke@435 763
duke@435 764 bool OneContigSpaceCardGeneration::grow_to_reserved() {
duke@435 765 assert_locked_or_safepoint(ExpandHeap_lock);
duke@435 766 bool success = true;
duke@435 767 const size_t remaining_bytes = _virtual_space.uncommitted_size();
duke@435 768 if (remaining_bytes > 0) {
duke@435 769 success = grow_by(remaining_bytes);
duke@435 770 DEBUG_ONLY(if (!success) warning("grow to reserved failed");)
duke@435 771 }
duke@435 772 return success;
duke@435 773 }
duke@435 774
duke@435 775 void OneContigSpaceCardGeneration::shrink_by(size_t bytes) {
duke@435 776 assert_locked_or_safepoint(ExpandHeap_lock);
duke@435 777 // Shrink committed space
duke@435 778 _virtual_space.shrink_by(bytes);
duke@435 779 // Shrink space; this also shrinks the space's BOT
duke@435 780 _the_space->set_end((HeapWord*) _virtual_space.high());
duke@435 781 size_t new_word_size = heap_word_size(_the_space->capacity());
duke@435 782 // Shrink the shared block offset array
duke@435 783 _bts->resize(new_word_size);
duke@435 784 MemRegion mr(_the_space->bottom(), new_word_size);
duke@435 785 // Shrink the card table
duke@435 786 Universe::heap()->barrier_set()->resize_covered_region(mr);
duke@435 787
duke@435 788 if (Verbose && PrintGC) {
duke@435 789 size_t new_mem_size = _virtual_space.committed_size();
duke@435 790 size_t old_mem_size = new_mem_size + bytes;
duke@435 791 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K to " SIZE_FORMAT "K",
duke@435 792 name(), old_mem_size/K, new_mem_size/K);
duke@435 793 }
duke@435 794 }
duke@435 795
duke@435 796 // Currently nothing to do.
duke@435 797 void OneContigSpaceCardGeneration::prepare_for_verify() {}
duke@435 798
duke@435 799
ysr@1486 800 // Override for a card-table generation with one contiguous
ysr@1486 801 // space. NOTE: For reasons that are lost in the fog of history,
ysr@1486 802 // this code is used when you iterate over perm gen objects,
ysr@1486 803 // even when one uses CDS, where the perm gen has a couple of
ysr@1486 804 // other spaces; this is because CompactingPermGenGen derives
ysr@1486 805 // from OneContigSpaceCardGeneration. This should be cleaned up,
ysr@1486 806 // see CR 6897789..
duke@435 807 void OneContigSpaceCardGeneration::object_iterate(ObjectClosure* blk) {
duke@435 808 _the_space->object_iterate(blk);
duke@435 809 }
duke@435 810
duke@435 811 void OneContigSpaceCardGeneration::space_iterate(SpaceClosure* blk,
duke@435 812 bool usedOnly) {
duke@435 813 blk->do_space(_the_space);
duke@435 814 }
duke@435 815
duke@435 816 void OneContigSpaceCardGeneration::younger_refs_iterate(OopsInGenClosure* blk) {
duke@435 817 blk->set_generation(this);
duke@435 818 younger_refs_in_space_iterate(_the_space, blk);
duke@435 819 blk->reset_generation();
duke@435 820 }
duke@435 821
duke@435 822 void OneContigSpaceCardGeneration::save_marks() {
duke@435 823 _the_space->set_saved_mark();
duke@435 824 }
duke@435 825
duke@435 826
duke@435 827 void OneContigSpaceCardGeneration::reset_saved_marks() {
duke@435 828 _the_space->reset_saved_mark();
duke@435 829 }
duke@435 830
duke@435 831
duke@435 832 bool OneContigSpaceCardGeneration::no_allocs_since_save_marks() {
duke@435 833 return _the_space->saved_mark_at_top();
duke@435 834 }
duke@435 835
duke@435 836 #define OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \
duke@435 837 \
duke@435 838 void OneContigSpaceCardGeneration:: \
duke@435 839 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \
duke@435 840 blk->set_generation(this); \
duke@435 841 _the_space->oop_since_save_marks_iterate##nv_suffix(blk); \
duke@435 842 blk->reset_generation(); \
duke@435 843 save_marks(); \
duke@435 844 }
duke@435 845
duke@435 846 ALL_SINCE_SAVE_MARKS_CLOSURES(OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN)
duke@435 847
duke@435 848 #undef OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN
duke@435 849
duke@435 850
duke@435 851 void OneContigSpaceCardGeneration::gc_epilogue(bool full) {
duke@435 852 _last_gc = WaterMark(the_space(), the_space()->top());
duke@435 853
duke@435 854 // update the generation and space performance counters
duke@435 855 update_counters();
jmasa@698 856 if (ZapUnusedHeapArea) {
jmasa@698 857 the_space()->check_mangled_unused_area_complete();
jmasa@698 858 }
jmasa@698 859 }
jmasa@698 860
jmasa@698 861 void OneContigSpaceCardGeneration::record_spaces_top() {
jmasa@698 862 assert(ZapUnusedHeapArea, "Not mangling unused space");
jmasa@698 863 the_space()->set_top_for_allocations();
duke@435 864 }
duke@435 865
brutisso@3711 866 void OneContigSpaceCardGeneration::verify() {
brutisso@3711 867 the_space()->verify();
duke@435 868 }
duke@435 869
duke@435 870 void OneContigSpaceCardGeneration::print_on(outputStream* st) const {
duke@435 871 Generation::print_on(st);
duke@435 872 st->print(" the");
duke@435 873 the_space()->print_on(st);
duke@435 874 }

mercurial