annotate src/share/vm/memory/defNewGeneration.cpp @ 457:27a80744a83b

6778647: snap(), snap_policy() should be renamed setup(), setup_policy() Summary: Renamed Reference{Policy,Pocessor} methods from snap{,_policy}() to setup{,_policy}() Reviewed-by: apetrusenko
author ysr
date Mon, 01 Dec 2008 23:25:24 -0800
parents c96030fff130
children 148e5441d916
rev   line source
duke@0 1 /*
xdono@196 2 * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_defNewGeneration.cpp.incl"
duke@0 27
duke@0 28 //
duke@0 29 // DefNewGeneration functions.
duke@0 30
duke@0 31 // Methods of protected closure types.
duke@0 32
duke@0 33 DefNewGeneration::IsAliveClosure::IsAliveClosure(Generation* g) : _g(g) {
duke@0 34 assert(g->level() == 0, "Optimized for youngest gen.");
duke@0 35 }
duke@0 36 void DefNewGeneration::IsAliveClosure::do_object(oop p) {
duke@0 37 assert(false, "Do not call.");
duke@0 38 }
duke@0 39 bool DefNewGeneration::IsAliveClosure::do_object_b(oop p) {
duke@0 40 return (HeapWord*)p >= _g->reserved().end() || p->is_forwarded();
duke@0 41 }
duke@0 42
duke@0 43 DefNewGeneration::KeepAliveClosure::
duke@0 44 KeepAliveClosure(ScanWeakRefClosure* cl) : _cl(cl) {
duke@0 45 GenRemSet* rs = GenCollectedHeap::heap()->rem_set();
duke@0 46 assert(rs->rs_kind() == GenRemSet::CardTable, "Wrong rem set kind.");
duke@0 47 _rs = (CardTableRS*)rs;
duke@0 48 }
duke@0 49
coleenp@113 50 void DefNewGeneration::KeepAliveClosure::do_oop(oop* p) { DefNewGeneration::KeepAliveClosure::do_oop_work(p); }
coleenp@113 51 void DefNewGeneration::KeepAliveClosure::do_oop(narrowOop* p) { DefNewGeneration::KeepAliveClosure::do_oop_work(p); }
duke@0 52
duke@0 53
duke@0 54 DefNewGeneration::FastKeepAliveClosure::
duke@0 55 FastKeepAliveClosure(DefNewGeneration* g, ScanWeakRefClosure* cl) :
duke@0 56 DefNewGeneration::KeepAliveClosure(cl) {
duke@0 57 _boundary = g->reserved().end();
duke@0 58 }
duke@0 59
coleenp@113 60 void DefNewGeneration::FastKeepAliveClosure::do_oop(oop* p) { DefNewGeneration::FastKeepAliveClosure::do_oop_work(p); }
coleenp@113 61 void DefNewGeneration::FastKeepAliveClosure::do_oop(narrowOop* p) { DefNewGeneration::FastKeepAliveClosure::do_oop_work(p); }
duke@0 62
duke@0 63 DefNewGeneration::EvacuateFollowersClosure::
duke@0 64 EvacuateFollowersClosure(GenCollectedHeap* gch, int level,
duke@0 65 ScanClosure* cur, ScanClosure* older) :
duke@0 66 _gch(gch), _level(level),
duke@0 67 _scan_cur_or_nonheap(cur), _scan_older(older)
duke@0 68 {}
duke@0 69
duke@0 70 void DefNewGeneration::EvacuateFollowersClosure::do_void() {
duke@0 71 do {
duke@0 72 _gch->oop_since_save_marks_iterate(_level, _scan_cur_or_nonheap,
duke@0 73 _scan_older);
duke@0 74 } while (!_gch->no_allocs_since_save_marks(_level));
duke@0 75 }
duke@0 76
duke@0 77 DefNewGeneration::FastEvacuateFollowersClosure::
duke@0 78 FastEvacuateFollowersClosure(GenCollectedHeap* gch, int level,
duke@0 79 DefNewGeneration* gen,
duke@0 80 FastScanClosure* cur, FastScanClosure* older) :
duke@0 81 _gch(gch), _level(level), _gen(gen),
duke@0 82 _scan_cur_or_nonheap(cur), _scan_older(older)
duke@0 83 {}
duke@0 84
duke@0 85 void DefNewGeneration::FastEvacuateFollowersClosure::do_void() {
duke@0 86 do {
duke@0 87 _gch->oop_since_save_marks_iterate(_level, _scan_cur_or_nonheap,
duke@0 88 _scan_older);
duke@0 89 } while (!_gch->no_allocs_since_save_marks(_level));
duke@0 90 guarantee(_gen->promo_failure_scan_stack() == NULL
duke@0 91 || _gen->promo_failure_scan_stack()->length() == 0,
duke@0 92 "Failed to finish scan");
duke@0 93 }
duke@0 94
duke@0 95 ScanClosure::ScanClosure(DefNewGeneration* g, bool gc_barrier) :
duke@0 96 OopsInGenClosure(g), _g(g), _gc_barrier(gc_barrier)
duke@0 97 {
duke@0 98 assert(_g->level() == 0, "Optimized for youngest generation");
duke@0 99 _boundary = _g->reserved().end();
duke@0 100 }
duke@0 101
coleenp@113 102 void ScanClosure::do_oop(oop* p) { ScanClosure::do_oop_work(p); }
coleenp@113 103 void ScanClosure::do_oop(narrowOop* p) { ScanClosure::do_oop_work(p); }
coleenp@113 104
duke@0 105 FastScanClosure::FastScanClosure(DefNewGeneration* g, bool gc_barrier) :
duke@0 106 OopsInGenClosure(g), _g(g), _gc_barrier(gc_barrier)
duke@0 107 {
duke@0 108 assert(_g->level() == 0, "Optimized for youngest generation");
duke@0 109 _boundary = _g->reserved().end();
duke@0 110 }
duke@0 111
coleenp@113 112 void FastScanClosure::do_oop(oop* p) { FastScanClosure::do_oop_work(p); }
coleenp@113 113 void FastScanClosure::do_oop(narrowOop* p) { FastScanClosure::do_oop_work(p); }
coleenp@113 114
duke@0 115 ScanWeakRefClosure::ScanWeakRefClosure(DefNewGeneration* g) :
duke@0 116 OopClosure(g->ref_processor()), _g(g)
duke@0 117 {
duke@0 118 assert(_g->level() == 0, "Optimized for youngest generation");
duke@0 119 _boundary = _g->reserved().end();
duke@0 120 }
duke@0 121
coleenp@113 122 void ScanWeakRefClosure::do_oop(oop* p) { ScanWeakRefClosure::do_oop_work(p); }
coleenp@113 123 void ScanWeakRefClosure::do_oop(narrowOop* p) { ScanWeakRefClosure::do_oop_work(p); }
coleenp@113 124
coleenp@113 125 void FilteringClosure::do_oop(oop* p) { FilteringClosure::do_oop_work(p); }
coleenp@113 126 void FilteringClosure::do_oop(narrowOop* p) { FilteringClosure::do_oop_work(p); }
duke@0 127
duke@0 128 DefNewGeneration::DefNewGeneration(ReservedSpace rs,
duke@0 129 size_t initial_size,
duke@0 130 int level,
duke@0 131 const char* policy)
duke@0 132 : Generation(rs, initial_size, level),
duke@0 133 _objs_with_preserved_marks(NULL),
duke@0 134 _preserved_marks_of_objs(NULL),
duke@0 135 _promo_failure_scan_stack(NULL),
duke@0 136 _promo_failure_drain_in_progress(false),
duke@0 137 _should_allocate_from_space(false)
duke@0 138 {
duke@0 139 MemRegion cmr((HeapWord*)_virtual_space.low(),
duke@0 140 (HeapWord*)_virtual_space.high());
duke@0 141 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@0 142
duke@0 143 if (GenCollectedHeap::heap()->collector_policy()->has_soft_ended_eden()) {
duke@0 144 _eden_space = new ConcEdenSpace(this);
duke@0 145 } else {
duke@0 146 _eden_space = new EdenSpace(this);
duke@0 147 }
duke@0 148 _from_space = new ContiguousSpace();
duke@0 149 _to_space = new ContiguousSpace();
duke@0 150
duke@0 151 if (_eden_space == NULL || _from_space == NULL || _to_space == NULL)
duke@0 152 vm_exit_during_initialization("Could not allocate a new gen space");
duke@0 153
duke@0 154 // Compute the maximum eden and survivor space sizes. These sizes
duke@0 155 // are computed assuming the entire reserved space is committed.
duke@0 156 // These values are exported as performance counters.
duke@0 157 uintx alignment = GenCollectedHeap::heap()->collector_policy()->min_alignment();
duke@0 158 uintx size = _virtual_space.reserved_size();
duke@0 159 _max_survivor_size = compute_survivor_size(size, alignment);
duke@0 160 _max_eden_size = size - (2*_max_survivor_size);
duke@0 161
duke@0 162 // allocate the performance counters
duke@0 163
duke@0 164 // Generation counters -- generation 0, 3 subspaces
duke@0 165 _gen_counters = new GenerationCounters("new", 0, 3, &_virtual_space);
duke@0 166 _gc_counters = new CollectorCounters(policy, 0);
duke@0 167
duke@0 168 _eden_counters = new CSpaceCounters("eden", 0, _max_eden_size, _eden_space,
duke@0 169 _gen_counters);
duke@0 170 _from_counters = new CSpaceCounters("s0", 1, _max_survivor_size, _from_space,
duke@0 171 _gen_counters);
duke@0 172 _to_counters = new CSpaceCounters("s1", 2, _max_survivor_size, _to_space,
duke@0 173 _gen_counters);
duke@0 174
jmasa@263 175 compute_space_boundaries(0, SpaceDecorator::Clear, SpaceDecorator::Mangle);
duke@0 176 update_counters();
duke@0 177 _next_gen = NULL;
duke@0 178 _tenuring_threshold = MaxTenuringThreshold;
duke@0 179 _pretenure_size_threshold_words = PretenureSizeThreshold >> LogHeapWordSize;
duke@0 180 }
duke@0 181
jmasa@263 182 void DefNewGeneration::compute_space_boundaries(uintx minimum_eden_size,
jmasa@263 183 bool clear_space,
jmasa@263 184 bool mangle_space) {
jmasa@263 185 uintx alignment =
jmasa@263 186 GenCollectedHeap::heap()->collector_policy()->min_alignment();
jmasa@263 187
jmasa@263 188 // If the spaces are being cleared (only done at heap initialization
jmasa@263 189 // currently), the survivor spaces need not be empty.
jmasa@263 190 // Otherwise, no care is taken for used areas in the survivor spaces
jmasa@263 191 // so check.
jmasa@263 192 assert(clear_space || (to()->is_empty() && from()->is_empty()),
jmasa@263 193 "Initialization of the survivor spaces assumes these are empty");
duke@0 194
duke@0 195 // Compute sizes
duke@0 196 uintx size = _virtual_space.committed_size();
duke@0 197 uintx survivor_size = compute_survivor_size(size, alignment);
duke@0 198 uintx eden_size = size - (2*survivor_size);
duke@0 199 assert(eden_size > 0 && survivor_size <= eden_size, "just checking");
duke@0 200
duke@0 201 if (eden_size < minimum_eden_size) {
duke@0 202 // May happen due to 64Kb rounding, if so adjust eden size back up
duke@0 203 minimum_eden_size = align_size_up(minimum_eden_size, alignment);
duke@0 204 uintx maximum_survivor_size = (size - minimum_eden_size) / 2;
duke@0 205 uintx unaligned_survivor_size =
duke@0 206 align_size_down(maximum_survivor_size, alignment);
duke@0 207 survivor_size = MAX2(unaligned_survivor_size, alignment);
duke@0 208 eden_size = size - (2*survivor_size);
duke@0 209 assert(eden_size > 0 && survivor_size <= eden_size, "just checking");
duke@0 210 assert(eden_size >= minimum_eden_size, "just checking");
duke@0 211 }
duke@0 212
duke@0 213 char *eden_start = _virtual_space.low();
duke@0 214 char *from_start = eden_start + eden_size;
duke@0 215 char *to_start = from_start + survivor_size;
duke@0 216 char *to_end = to_start + survivor_size;
duke@0 217
duke@0 218 assert(to_end == _virtual_space.high(), "just checking");
duke@0 219 assert(Space::is_aligned((HeapWord*)eden_start), "checking alignment");
duke@0 220 assert(Space::is_aligned((HeapWord*)from_start), "checking alignment");
duke@0 221 assert(Space::is_aligned((HeapWord*)to_start), "checking alignment");
duke@0 222
duke@0 223 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)from_start);
duke@0 224 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)to_start);
duke@0 225 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end);
duke@0 226
jmasa@263 227 // A minimum eden size implies that there is a part of eden that
jmasa@263 228 // is being used and that affects the initialization of any
jmasa@263 229 // newly formed eden.
jmasa@263 230 bool live_in_eden = minimum_eden_size > 0;
jmasa@263 231
jmasa@263 232 // If not clearing the spaces, do some checking to verify that
jmasa@263 233 // the space are already mangled.
jmasa@263 234 if (!clear_space) {
jmasa@263 235 // Must check mangling before the spaces are reshaped. Otherwise,
jmasa@263 236 // the bottom or end of one space may have moved into another
jmasa@263 237 // a failure of the check may not correctly indicate which space
jmasa@263 238 // is not properly mangled.
jmasa@263 239 if (ZapUnusedHeapArea) {
jmasa@263 240 HeapWord* limit = (HeapWord*) _virtual_space.high();
jmasa@263 241 eden()->check_mangled_unused_area(limit);
jmasa@263 242 from()->check_mangled_unused_area(limit);
jmasa@263 243 to()->check_mangled_unused_area(limit);
jmasa@263 244 }
jmasa@263 245 }
jmasa@263 246
jmasa@263 247 // Reset the spaces for their new regions.
jmasa@263 248 eden()->initialize(edenMR,
jmasa@263 249 clear_space && !live_in_eden,
jmasa@263 250 SpaceDecorator::Mangle);
jmasa@263 251 // If clear_space and live_in_eden, we will not have cleared any
duke@0 252 // portion of eden above its top. This can cause newly
duke@0 253 // expanded space not to be mangled if using ZapUnusedHeapArea.
duke@0 254 // We explicitly do such mangling here.
jmasa@263 255 if (ZapUnusedHeapArea && clear_space && live_in_eden && mangle_space) {
duke@0 256 eden()->mangle_unused_area();
duke@0 257 }
jmasa@263 258 from()->initialize(fromMR, clear_space, mangle_space);
jmasa@263 259 to()->initialize(toMR, clear_space, mangle_space);
jmasa@263 260
jmasa@263 261 // Set next compaction spaces.
duke@0 262 eden()->set_next_compaction_space(from());
duke@0 263 // The to-space is normally empty before a compaction so need
duke@0 264 // not be considered. The exception is during promotion
duke@0 265 // failure handling when to-space can contain live objects.
duke@0 266 from()->set_next_compaction_space(NULL);
duke@0 267 }
duke@0 268
duke@0 269 void DefNewGeneration::swap_spaces() {
duke@0 270 ContiguousSpace* s = from();
duke@0 271 _from_space = to();
duke@0 272 _to_space = s;
duke@0 273 eden()->set_next_compaction_space(from());
duke@0 274 // The to-space is normally empty before a compaction so need
duke@0 275 // not be considered. The exception is during promotion
duke@0 276 // failure handling when to-space can contain live objects.
duke@0 277 from()->set_next_compaction_space(NULL);
duke@0 278
duke@0 279 if (UsePerfData) {
duke@0 280 CSpaceCounters* c = _from_counters;
duke@0 281 _from_counters = _to_counters;
duke@0 282 _to_counters = c;
duke@0 283 }
duke@0 284 }
duke@0 285
duke@0 286 bool DefNewGeneration::expand(size_t bytes) {
duke@0 287 MutexLocker x(ExpandHeap_lock);
jmasa@263 288 HeapWord* prev_high = (HeapWord*) _virtual_space.high();
duke@0 289 bool success = _virtual_space.expand_by(bytes);
jmasa@263 290 if (success && ZapUnusedHeapArea) {
jmasa@263 291 // Mangle newly committed space immediately because it
jmasa@263 292 // can be done here more simply that after the new
jmasa@263 293 // spaces have been computed.
jmasa@263 294 HeapWord* new_high = (HeapWord*) _virtual_space.high();
jmasa@263 295 MemRegion mangle_region(prev_high, new_high);
jmasa@263 296 SpaceMangler::mangle_region(mangle_region);
jmasa@263 297 }
duke@0 298
duke@0 299 // Do not attempt an expand-to-the reserve size. The
duke@0 300 // request should properly observe the maximum size of
duke@0 301 // the generation so an expand-to-reserve should be
duke@0 302 // unnecessary. Also a second call to expand-to-reserve
duke@0 303 // value potentially can cause an undue expansion.
duke@0 304 // For example if the first expand fail for unknown reasons,
duke@0 305 // but the second succeeds and expands the heap to its maximum
duke@0 306 // value.
duke@0 307 if (GC_locker::is_active()) {
duke@0 308 if (PrintGC && Verbose) {
jmasa@263 309 gclog_or_tty->print_cr("Garbage collection disabled, "
jmasa@263 310 "expanded heap instead");
duke@0 311 }
duke@0 312 }
duke@0 313
duke@0 314 return success;
duke@0 315 }
duke@0 316
duke@0 317
duke@0 318 void DefNewGeneration::compute_new_size() {
duke@0 319 // This is called after a gc that includes the following generation
duke@0 320 // (which is required to exist.) So from-space will normally be empty.
duke@0 321 // Note that we check both spaces, since if scavenge failed they revert roles.
duke@0 322 // If not we bail out (otherwise we would have to relocate the objects)
duke@0 323 if (!from()->is_empty() || !to()->is_empty()) {
duke@0 324 return;
duke@0 325 }
duke@0 326
duke@0 327 int next_level = level() + 1;
duke@0 328 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 329 assert(next_level < gch->_n_gens,
duke@0 330 "DefNewGeneration cannot be an oldest gen");
duke@0 331
duke@0 332 Generation* next_gen = gch->_gens[next_level];
duke@0 333 size_t old_size = next_gen->capacity();
duke@0 334 size_t new_size_before = _virtual_space.committed_size();
duke@0 335 size_t min_new_size = spec()->init_size();
duke@0 336 size_t max_new_size = reserved().byte_size();
duke@0 337 assert(min_new_size <= new_size_before &&
duke@0 338 new_size_before <= max_new_size,
duke@0 339 "just checking");
duke@0 340 // All space sizes must be multiples of Generation::GenGrain.
duke@0 341 size_t alignment = Generation::GenGrain;
duke@0 342
duke@0 343 // Compute desired new generation size based on NewRatio and
duke@0 344 // NewSizeThreadIncrease
duke@0 345 size_t desired_new_size = old_size/NewRatio;
duke@0 346 int threads_count = Threads::number_of_non_daemon_threads();
duke@0 347 size_t thread_increase_size = threads_count * NewSizeThreadIncrease;
duke@0 348 desired_new_size = align_size_up(desired_new_size + thread_increase_size, alignment);
duke@0 349
duke@0 350 // Adjust new generation size
duke@0 351 desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size);
duke@0 352 assert(desired_new_size <= max_new_size, "just checking");
duke@0 353
duke@0 354 bool changed = false;
duke@0 355 if (desired_new_size > new_size_before) {
duke@0 356 size_t change = desired_new_size - new_size_before;
duke@0 357 assert(change % alignment == 0, "just checking");
duke@0 358 if (expand(change)) {
duke@0 359 changed = true;
duke@0 360 }
duke@0 361 // If the heap failed to expand to the desired size,
duke@0 362 // "changed" will be false. If the expansion failed
duke@0 363 // (and at this point it was expected to succeed),
duke@0 364 // ignore the failure (leaving "changed" as false).
duke@0 365 }
duke@0 366 if (desired_new_size < new_size_before && eden()->is_empty()) {
duke@0 367 // bail out of shrinking if objects in eden
duke@0 368 size_t change = new_size_before - desired_new_size;
duke@0 369 assert(change % alignment == 0, "just checking");
duke@0 370 _virtual_space.shrink_by(change);
duke@0 371 changed = true;
duke@0 372 }
duke@0 373 if (changed) {
jmasa@263 374 // The spaces have already been mangled at this point but
jmasa@263 375 // may not have been cleared (set top = bottom) and should be.
jmasa@263 376 // Mangling was done when the heap was being expanded.
jmasa@263 377 compute_space_boundaries(eden()->used(),
jmasa@263 378 SpaceDecorator::Clear,
jmasa@263 379 SpaceDecorator::DontMangle);
jmasa@263 380 MemRegion cmr((HeapWord*)_virtual_space.low(),
jmasa@263 381 (HeapWord*)_virtual_space.high());
duke@0 382 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@0 383 if (Verbose && PrintGC) {
duke@0 384 size_t new_size_after = _virtual_space.committed_size();
duke@0 385 size_t eden_size_after = eden()->capacity();
duke@0 386 size_t survivor_size_after = from()->capacity();
jmasa@263 387 gclog_or_tty->print("New generation size " SIZE_FORMAT "K->"
jmasa@263 388 SIZE_FORMAT "K [eden="
duke@0 389 SIZE_FORMAT "K,survivor=" SIZE_FORMAT "K]",
jmasa@263 390 new_size_before/K, new_size_after/K,
jmasa@263 391 eden_size_after/K, survivor_size_after/K);
duke@0 392 if (WizardMode) {
duke@0 393 gclog_or_tty->print("[allowed " SIZE_FORMAT "K extra for %d threads]",
duke@0 394 thread_increase_size/K, threads_count);
duke@0 395 }
duke@0 396 gclog_or_tty->cr();
duke@0 397 }
duke@0 398 }
duke@0 399 }
duke@0 400
duke@0 401 void DefNewGeneration::object_iterate_since_last_GC(ObjectClosure* cl) {
duke@0 402 // $$$ This may be wrong in case of "scavenge failure"?
duke@0 403 eden()->object_iterate(cl);
duke@0 404 }
duke@0 405
duke@0 406 void DefNewGeneration::younger_refs_iterate(OopsInGenClosure* cl) {
duke@0 407 assert(false, "NYI -- are you sure you want to call this?");
duke@0 408 }
duke@0 409
duke@0 410
duke@0 411 size_t DefNewGeneration::capacity() const {
duke@0 412 return eden()->capacity()
duke@0 413 + from()->capacity(); // to() is only used during scavenge
duke@0 414 }
duke@0 415
duke@0 416
duke@0 417 size_t DefNewGeneration::used() const {
duke@0 418 return eden()->used()
duke@0 419 + from()->used(); // to() is only used during scavenge
duke@0 420 }
duke@0 421
duke@0 422
duke@0 423 size_t DefNewGeneration::free() const {
duke@0 424 return eden()->free()
duke@0 425 + from()->free(); // to() is only used during scavenge
duke@0 426 }
duke@0 427
duke@0 428 size_t DefNewGeneration::max_capacity() const {
duke@0 429 const size_t alignment = GenCollectedHeap::heap()->collector_policy()->min_alignment();
duke@0 430 const size_t reserved_bytes = reserved().byte_size();
duke@0 431 return reserved_bytes - compute_survivor_size(reserved_bytes, alignment);
duke@0 432 }
duke@0 433
duke@0 434 size_t DefNewGeneration::unsafe_max_alloc_nogc() const {
duke@0 435 return eden()->free();
duke@0 436 }
duke@0 437
duke@0 438 size_t DefNewGeneration::capacity_before_gc() const {
duke@0 439 return eden()->capacity();
duke@0 440 }
duke@0 441
duke@0 442 size_t DefNewGeneration::contiguous_available() const {
duke@0 443 return eden()->free();
duke@0 444 }
duke@0 445
duke@0 446
duke@0 447 HeapWord** DefNewGeneration::top_addr() const { return eden()->top_addr(); }
duke@0 448 HeapWord** DefNewGeneration::end_addr() const { return eden()->end_addr(); }
duke@0 449
duke@0 450 void DefNewGeneration::object_iterate(ObjectClosure* blk) {
duke@0 451 eden()->object_iterate(blk);
duke@0 452 from()->object_iterate(blk);
duke@0 453 }
duke@0 454
duke@0 455
duke@0 456 void DefNewGeneration::space_iterate(SpaceClosure* blk,
duke@0 457 bool usedOnly) {
duke@0 458 blk->do_space(eden());
duke@0 459 blk->do_space(from());
duke@0 460 blk->do_space(to());
duke@0 461 }
duke@0 462
duke@0 463 // The last collection bailed out, we are running out of heap space,
duke@0 464 // so we try to allocate the from-space, too.
duke@0 465 HeapWord* DefNewGeneration::allocate_from_space(size_t size) {
duke@0 466 HeapWord* result = NULL;
duke@0 467 if (PrintGC && Verbose) {
duke@0 468 gclog_or_tty->print("DefNewGeneration::allocate_from_space(%u):"
duke@0 469 " will_fail: %s"
duke@0 470 " heap_lock: %s"
duke@0 471 " free: " SIZE_FORMAT,
duke@0 472 size,
duke@0 473 GenCollectedHeap::heap()->incremental_collection_will_fail() ? "true" : "false",
duke@0 474 Heap_lock->is_locked() ? "locked" : "unlocked",
duke@0 475 from()->free());
duke@0 476 }
duke@0 477 if (should_allocate_from_space() || GC_locker::is_active_and_needs_gc()) {
duke@0 478 if (Heap_lock->owned_by_self() ||
duke@0 479 (SafepointSynchronize::is_at_safepoint() &&
duke@0 480 Thread::current()->is_VM_thread())) {
duke@0 481 // If the Heap_lock is not locked by this thread, this will be called
duke@0 482 // again later with the Heap_lock held.
duke@0 483 result = from()->allocate(size);
duke@0 484 } else if (PrintGC && Verbose) {
duke@0 485 gclog_or_tty->print_cr(" Heap_lock is not owned by self");
duke@0 486 }
duke@0 487 } else if (PrintGC && Verbose) {
duke@0 488 gclog_or_tty->print_cr(" should_allocate_from_space: NOT");
duke@0 489 }
duke@0 490 if (PrintGC && Verbose) {
duke@0 491 gclog_or_tty->print_cr(" returns %s", result == NULL ? "NULL" : "object");
duke@0 492 }
duke@0 493 return result;
duke@0 494 }
duke@0 495
duke@0 496 HeapWord* DefNewGeneration::expand_and_allocate(size_t size,
duke@0 497 bool is_tlab,
duke@0 498 bool parallel) {
duke@0 499 // We don't attempt to expand the young generation (but perhaps we should.)
duke@0 500 return allocate(size, is_tlab);
duke@0 501 }
duke@0 502
duke@0 503
duke@0 504 void DefNewGeneration::collect(bool full,
duke@0 505 bool clear_all_soft_refs,
duke@0 506 size_t size,
duke@0 507 bool is_tlab) {
duke@0 508 assert(full || size > 0, "otherwise we don't want to collect");
duke@0 509 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 510 _next_gen = gch->next_gen(this);
duke@0 511 assert(_next_gen != NULL,
duke@0 512 "This must be the youngest gen, and not the only gen");
duke@0 513
duke@0 514 // If the next generation is too full to accomodate promotion
duke@0 515 // from this generation, pass on collection; let the next generation
duke@0 516 // do it.
duke@0 517 if (!collection_attempt_is_safe()) {
duke@0 518 gch->set_incremental_collection_will_fail();
duke@0 519 return;
duke@0 520 }
duke@0 521 assert(to()->is_empty(), "Else not collection_attempt_is_safe");
duke@0 522
duke@0 523 init_assuming_no_promotion_failure();
duke@0 524
duke@0 525 TraceTime t1("GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
duke@0 526 // Capture heap used before collection (for printing).
duke@0 527 size_t gch_prev_used = gch->used();
duke@0 528
duke@0 529 SpecializationStats::clear();
duke@0 530
duke@0 531 // These can be shared for all code paths
duke@0 532 IsAliveClosure is_alive(this);
duke@0 533 ScanWeakRefClosure scan_weak_ref(this);
duke@0 534
duke@0 535 age_table()->clear();
jmasa@263 536 to()->clear(SpaceDecorator::Mangle);
duke@0 537
duke@0 538 gch->rem_set()->prepare_for_younger_refs_iterate(false);
duke@0 539
duke@0 540 assert(gch->no_allocs_since_save_marks(0),
duke@0 541 "save marks have not been newly set.");
duke@0 542
duke@0 543 // Not very pretty.
duke@0 544 CollectorPolicy* cp = gch->collector_policy();
duke@0 545
duke@0 546 FastScanClosure fsc_with_no_gc_barrier(this, false);
duke@0 547 FastScanClosure fsc_with_gc_barrier(this, true);
duke@0 548
duke@0 549 set_promo_failure_scan_stack_closure(&fsc_with_no_gc_barrier);
duke@0 550 FastEvacuateFollowersClosure evacuate_followers(gch, _level, this,
duke@0 551 &fsc_with_no_gc_barrier,
duke@0 552 &fsc_with_gc_barrier);
duke@0 553
duke@0 554 assert(gch->no_allocs_since_save_marks(0),
duke@0 555 "save marks have not been newly set.");
duke@0 556
duke@0 557 gch->gen_process_strong_roots(_level,
duke@0 558 true, // Process younger gens, if any, as
duke@0 559 // strong roots.
duke@0 560 false,// not collecting permanent generation.
duke@0 561 SharedHeap::SO_AllClasses,
duke@0 562 &fsc_with_gc_barrier,
duke@0 563 &fsc_with_no_gc_barrier);
duke@0 564
duke@0 565 // "evacuate followers".
duke@0 566 evacuate_followers.do_void();
duke@0 567
duke@0 568 FastKeepAliveClosure keep_alive(this, &scan_weak_ref);
ysr@453 569 ReferenceProcessor* rp = ref_processor();
ysr@457 570 rp->setup_policy(clear_all_soft_refs);
ysr@453 571 rp->process_discovered_references(&is_alive, &keep_alive, &evacuate_followers,
ysr@453 572 NULL);
duke@0 573 if (!promotion_failed()) {
duke@0 574 // Swap the survivor spaces.
jmasa@263 575 eden()->clear(SpaceDecorator::Mangle);
jmasa@263 576 from()->clear(SpaceDecorator::Mangle);
jmasa@263 577 if (ZapUnusedHeapArea) {
jmasa@263 578 // This is now done here because of the piece-meal mangling which
jmasa@263 579 // can check for valid mangling at intermediate points in the
jmasa@263 580 // collection(s). When a minor collection fails to collect
jmasa@263 581 // sufficient space resizing of the young generation can occur
jmasa@263 582 // an redistribute the spaces in the young generation. Mangle
jmasa@263 583 // here so that unzapped regions don't get distributed to
jmasa@263 584 // other spaces.
jmasa@263 585 to()->mangle_unused_area();
jmasa@263 586 }
duke@0 587 swap_spaces();
duke@0 588
duke@0 589 assert(to()->is_empty(), "to space should be empty now");
duke@0 590
duke@0 591 // Set the desired survivor size to half the real survivor space
duke@0 592 _tenuring_threshold =
duke@0 593 age_table()->compute_tenuring_threshold(to()->capacity()/HeapWordSize);
duke@0 594
duke@0 595 if (PrintGC && !PrintGCDetails) {
duke@0 596 gch->print_heap_change(gch_prev_used);
duke@0 597 }
duke@0 598 } else {
duke@0 599 assert(HandlePromotionFailure,
duke@0 600 "Should not be here unless promotion failure handling is on");
duke@0 601 assert(_promo_failure_scan_stack != NULL &&
duke@0 602 _promo_failure_scan_stack->length() == 0, "post condition");
duke@0 603
duke@0 604 // deallocate stack and it's elements
duke@0 605 delete _promo_failure_scan_stack;
duke@0 606 _promo_failure_scan_stack = NULL;
duke@0 607
duke@0 608 remove_forwarding_pointers();
duke@0 609 if (PrintGCDetails) {
duke@0 610 gclog_or_tty->print(" (promotion failed)");
duke@0 611 }
duke@0 612 // Add to-space to the list of space to compact
duke@0 613 // when a promotion failure has occurred. In that
duke@0 614 // case there can be live objects in to-space
duke@0 615 // as a result of a partial evacuation of eden
duke@0 616 // and from-space.
duke@0 617 swap_spaces(); // For the sake of uniformity wrt ParNewGeneration::collect().
duke@0 618 from()->set_next_compaction_space(to());
duke@0 619 gch->set_incremental_collection_will_fail();
duke@0 620
duke@0 621 // Reset the PromotionFailureALot counters.
duke@0 622 NOT_PRODUCT(Universe::heap()->reset_promotion_should_fail();)
duke@0 623 }
duke@0 624 // set new iteration safe limit for the survivor spaces
duke@0 625 from()->set_concurrent_iteration_safe_limit(from()->top());
duke@0 626 to()->set_concurrent_iteration_safe_limit(to()->top());
duke@0 627 SpecializationStats::print();
duke@0 628 update_time_of_last_gc(os::javaTimeMillis());
duke@0 629 }
duke@0 630
duke@0 631 class RemoveForwardPointerClosure: public ObjectClosure {
duke@0 632 public:
duke@0 633 void do_object(oop obj) {
duke@0 634 obj->init_mark();
duke@0 635 }
duke@0 636 };
duke@0 637
duke@0 638 void DefNewGeneration::init_assuming_no_promotion_failure() {
duke@0 639 _promotion_failed = false;
duke@0 640 from()->set_next_compaction_space(NULL);
duke@0 641 }
duke@0 642
duke@0 643 void DefNewGeneration::remove_forwarding_pointers() {
duke@0 644 RemoveForwardPointerClosure rspc;
duke@0 645 eden()->object_iterate(&rspc);
duke@0 646 from()->object_iterate(&rspc);
duke@0 647 // Now restore saved marks, if any.
duke@0 648 if (_objs_with_preserved_marks != NULL) {
duke@0 649 assert(_preserved_marks_of_objs != NULL, "Both or none.");
duke@0 650 assert(_objs_with_preserved_marks->length() ==
duke@0 651 _preserved_marks_of_objs->length(), "Both or none.");
duke@0 652 for (int i = 0; i < _objs_with_preserved_marks->length(); i++) {
duke@0 653 oop obj = _objs_with_preserved_marks->at(i);
duke@0 654 markOop m = _preserved_marks_of_objs->at(i);
duke@0 655 obj->set_mark(m);
duke@0 656 }
duke@0 657 delete _objs_with_preserved_marks;
duke@0 658 delete _preserved_marks_of_objs;
duke@0 659 _objs_with_preserved_marks = NULL;
duke@0 660 _preserved_marks_of_objs = NULL;
duke@0 661 }
duke@0 662 }
duke@0 663
duke@0 664 void DefNewGeneration::preserve_mark_if_necessary(oop obj, markOop m) {
duke@0 665 if (m->must_be_preserved_for_promotion_failure(obj)) {
duke@0 666 if (_objs_with_preserved_marks == NULL) {
duke@0 667 assert(_preserved_marks_of_objs == NULL, "Both or none.");
duke@0 668 _objs_with_preserved_marks = new (ResourceObj::C_HEAP)
duke@0 669 GrowableArray<oop>(PreserveMarkStackSize, true);
duke@0 670 _preserved_marks_of_objs = new (ResourceObj::C_HEAP)
duke@0 671 GrowableArray<markOop>(PreserveMarkStackSize, true);
duke@0 672 }
duke@0 673 _objs_with_preserved_marks->push(obj);
duke@0 674 _preserved_marks_of_objs->push(m);
duke@0 675 }
duke@0 676 }
duke@0 677
duke@0 678 void DefNewGeneration::handle_promotion_failure(oop old) {
duke@0 679 preserve_mark_if_necessary(old, old->mark());
duke@0 680 // forward to self
duke@0 681 old->forward_to(old);
duke@0 682 _promotion_failed = true;
duke@0 683
duke@0 684 push_on_promo_failure_scan_stack(old);
duke@0 685
duke@0 686 if (!_promo_failure_drain_in_progress) {
duke@0 687 // prevent recursion in copy_to_survivor_space()
duke@0 688 _promo_failure_drain_in_progress = true;
duke@0 689 drain_promo_failure_scan_stack();
duke@0 690 _promo_failure_drain_in_progress = false;
duke@0 691 }
duke@0 692 }
duke@0 693
coleenp@113 694 oop DefNewGeneration::copy_to_survivor_space(oop old) {
duke@0 695 assert(is_in_reserved(old) && !old->is_forwarded(),
duke@0 696 "shouldn't be scavenging this oop");
duke@0 697 size_t s = old->size();
duke@0 698 oop obj = NULL;
duke@0 699
duke@0 700 // Try allocating obj in to-space (unless too old)
duke@0 701 if (old->age() < tenuring_threshold()) {
duke@0 702 obj = (oop) to()->allocate(s);
duke@0 703 }
duke@0 704
duke@0 705 // Otherwise try allocating obj tenured
duke@0 706 if (obj == NULL) {
coleenp@113 707 obj = _next_gen->promote(old, s);
duke@0 708 if (obj == NULL) {
duke@0 709 if (!HandlePromotionFailure) {
duke@0 710 // A failed promotion likely means the MaxLiveObjectEvacuationRatio flag
duke@0 711 // is incorrectly set. In any case, its seriously wrong to be here!
duke@0 712 vm_exit_out_of_memory(s*wordSize, "promotion");
duke@0 713 }
duke@0 714
duke@0 715 handle_promotion_failure(old);
duke@0 716 return old;
duke@0 717 }
duke@0 718 } else {
duke@0 719 // Prefetch beyond obj
duke@0 720 const intx interval = PrefetchCopyIntervalInBytes;
duke@0 721 Prefetch::write(obj, interval);
duke@0 722
duke@0 723 // Copy obj
duke@0 724 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)obj, s);
duke@0 725
duke@0 726 // Increment age if obj still in new generation
duke@0 727 obj->incr_age();
duke@0 728 age_table()->add(obj, s);
duke@0 729 }
duke@0 730
duke@0 731 // Done, insert forward pointer to obj in this header
duke@0 732 old->forward_to(obj);
duke@0 733
duke@0 734 return obj;
duke@0 735 }
duke@0 736
duke@0 737 void DefNewGeneration::push_on_promo_failure_scan_stack(oop obj) {
duke@0 738 if (_promo_failure_scan_stack == NULL) {
duke@0 739 _promo_failure_scan_stack = new (ResourceObj::C_HEAP)
duke@0 740 GrowableArray<oop>(40, true);
duke@0 741 }
duke@0 742
duke@0 743 _promo_failure_scan_stack->push(obj);
duke@0 744 }
duke@0 745
duke@0 746 void DefNewGeneration::drain_promo_failure_scan_stack() {
duke@0 747 assert(_promo_failure_scan_stack != NULL, "precondition");
duke@0 748
duke@0 749 while (_promo_failure_scan_stack->length() > 0) {
duke@0 750 oop obj = _promo_failure_scan_stack->pop();
duke@0 751 obj->oop_iterate(_promo_failure_scan_stack_closure);
duke@0 752 }
duke@0 753 }
duke@0 754
duke@0 755 void DefNewGeneration::save_marks() {
duke@0 756 eden()->set_saved_mark();
duke@0 757 to()->set_saved_mark();
duke@0 758 from()->set_saved_mark();
duke@0 759 }
duke@0 760
duke@0 761
duke@0 762 void DefNewGeneration::reset_saved_marks() {
duke@0 763 eden()->reset_saved_mark();
duke@0 764 to()->reset_saved_mark();
duke@0 765 from()->reset_saved_mark();
duke@0 766 }
duke@0 767
duke@0 768
duke@0 769 bool DefNewGeneration::no_allocs_since_save_marks() {
duke@0 770 assert(eden()->saved_mark_at_top(), "Violated spec - alloc in eden");
duke@0 771 assert(from()->saved_mark_at_top(), "Violated spec - alloc in from");
duke@0 772 return to()->saved_mark_at_top();
duke@0 773 }
duke@0 774
duke@0 775 #define DefNew_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
duke@0 776 \
duke@0 777 void DefNewGeneration:: \
duke@0 778 oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \
duke@0 779 cl->set_generation(this); \
duke@0 780 eden()->oop_since_save_marks_iterate##nv_suffix(cl); \
duke@0 781 to()->oop_since_save_marks_iterate##nv_suffix(cl); \
duke@0 782 from()->oop_since_save_marks_iterate##nv_suffix(cl); \
duke@0 783 cl->reset_generation(); \
duke@0 784 save_marks(); \
duke@0 785 }
duke@0 786
duke@0 787 ALL_SINCE_SAVE_MARKS_CLOSURES(DefNew_SINCE_SAVE_MARKS_DEFN)
duke@0 788
duke@0 789 #undef DefNew_SINCE_SAVE_MARKS_DEFN
duke@0 790
duke@0 791 void DefNewGeneration::contribute_scratch(ScratchBlock*& list, Generation* requestor,
duke@0 792 size_t max_alloc_words) {
duke@0 793 if (requestor == this || _promotion_failed) return;
duke@0 794 assert(requestor->level() > level(), "DefNewGeneration must be youngest");
duke@0 795
duke@0 796 /* $$$ Assert this? "trace" is a "MarkSweep" function so that's not appropriate.
duke@0 797 if (to_space->top() > to_space->bottom()) {
duke@0 798 trace("to_space not empty when contribute_scratch called");
duke@0 799 }
duke@0 800 */
duke@0 801
duke@0 802 ContiguousSpace* to_space = to();
duke@0 803 assert(to_space->end() >= to_space->top(), "pointers out of order");
duke@0 804 size_t free_words = pointer_delta(to_space->end(), to_space->top());
duke@0 805 if (free_words >= MinFreeScratchWords) {
duke@0 806 ScratchBlock* sb = (ScratchBlock*)to_space->top();
duke@0 807 sb->num_words = free_words;
duke@0 808 sb->next = list;
duke@0 809 list = sb;
duke@0 810 }
duke@0 811 }
duke@0 812
jmasa@263 813 void DefNewGeneration::reset_scratch() {
jmasa@263 814 // If contributing scratch in to_space, mangle all of
jmasa@263 815 // to_space if ZapUnusedHeapArea. This is needed because
jmasa@263 816 // top is not maintained while using to-space as scratch.
jmasa@263 817 if (ZapUnusedHeapArea) {
jmasa@263 818 to()->mangle_unused_area_complete();
jmasa@263 819 }
jmasa@263 820 }
jmasa@263 821
duke@0 822 bool DefNewGeneration::collection_attempt_is_safe() {
duke@0 823 if (!to()->is_empty()) {
duke@0 824 return false;
duke@0 825 }
duke@0 826 if (_next_gen == NULL) {
duke@0 827 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 828 _next_gen = gch->next_gen(this);
duke@0 829 assert(_next_gen != NULL,
duke@0 830 "This must be the youngest gen, and not the only gen");
duke@0 831 }
duke@0 832
duke@0 833 // Decide if there's enough room for a full promotion
duke@0 834 // When using extremely large edens, we effectively lose a
duke@0 835 // large amount of old space. Use the "MaxLiveObjectEvacuationRatio"
duke@0 836 // flag to reduce the minimum evacuation space requirements. If
duke@0 837 // there is not enough space to evacuate eden during a scavenge,
duke@0 838 // the VM will immediately exit with an out of memory error.
duke@0 839 // This flag has not been tested
duke@0 840 // with collectors other than simple mark & sweep.
duke@0 841 //
duke@0 842 // Note that with the addition of promotion failure handling, the
duke@0 843 // VM will not immediately exit but will undo the young generation
duke@0 844 // collection. The parameter is left here for compatibility.
duke@0 845 const double evacuation_ratio = MaxLiveObjectEvacuationRatio / 100.0;
duke@0 846
duke@0 847 // worst_case_evacuation is based on "used()". For the case where this
duke@0 848 // method is called after a collection, this is still appropriate because
duke@0 849 // the case that needs to be detected is one in which a full collection
duke@0 850 // has been done and has overflowed into the young generation. In that
duke@0 851 // case a minor collection will fail (the overflow of the full collection
duke@0 852 // means there is no space in the old generation for any promotion).
duke@0 853 size_t worst_case_evacuation = (size_t)(used() * evacuation_ratio);
duke@0 854
duke@0 855 return _next_gen->promotion_attempt_is_safe(worst_case_evacuation,
duke@0 856 HandlePromotionFailure);
duke@0 857 }
duke@0 858
duke@0 859 void DefNewGeneration::gc_epilogue(bool full) {
duke@0 860 // Check if the heap is approaching full after a collection has
duke@0 861 // been done. Generally the young generation is empty at
duke@0 862 // a minimum at the end of a collection. If it is not, then
duke@0 863 // the heap is approaching full.
duke@0 864 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 865 clear_should_allocate_from_space();
duke@0 866 if (collection_attempt_is_safe()) {
duke@0 867 gch->clear_incremental_collection_will_fail();
duke@0 868 } else {
duke@0 869 gch->set_incremental_collection_will_fail();
duke@0 870 if (full) { // we seem to be running out of space
duke@0 871 set_should_allocate_from_space();
duke@0 872 }
duke@0 873 }
duke@0 874
jmasa@263 875 if (ZapUnusedHeapArea) {
jmasa@263 876 eden()->check_mangled_unused_area_complete();
jmasa@263 877 from()->check_mangled_unused_area_complete();
jmasa@263 878 to()->check_mangled_unused_area_complete();
jmasa@263 879 }
jmasa@263 880
duke@0 881 // update the generation and space performance counters
duke@0 882 update_counters();
duke@0 883 gch->collector_policy()->counters()->update_counters();
duke@0 884 }
duke@0 885
jmasa@263 886 void DefNewGeneration::record_spaces_top() {
jmasa@263 887 assert(ZapUnusedHeapArea, "Not mangling unused space");
jmasa@263 888 eden()->set_top_for_allocations();
jmasa@263 889 to()->set_top_for_allocations();
jmasa@263 890 from()->set_top_for_allocations();
jmasa@263 891 }
jmasa@263 892
jmasa@263 893
duke@0 894 void DefNewGeneration::update_counters() {
duke@0 895 if (UsePerfData) {
duke@0 896 _eden_counters->update_all();
duke@0 897 _from_counters->update_all();
duke@0 898 _to_counters->update_all();
duke@0 899 _gen_counters->update_all();
duke@0 900 }
duke@0 901 }
duke@0 902
duke@0 903 void DefNewGeneration::verify(bool allow_dirty) {
duke@0 904 eden()->verify(allow_dirty);
duke@0 905 from()->verify(allow_dirty);
duke@0 906 to()->verify(allow_dirty);
duke@0 907 }
duke@0 908
duke@0 909 void DefNewGeneration::print_on(outputStream* st) const {
duke@0 910 Generation::print_on(st);
duke@0 911 st->print(" eden");
duke@0 912 eden()->print_on(st);
duke@0 913 st->print(" from");
duke@0 914 from()->print_on(st);
duke@0 915 st->print(" to ");
duke@0 916 to()->print_on(st);
duke@0 917 }
duke@0 918
duke@0 919
duke@0 920 const char* DefNewGeneration::name() const {
duke@0 921 return "def new generation";
duke@0 922 }
coleenp@113 923
coleenp@113 924 // Moved from inline file as they are not called inline
coleenp@113 925 CompactibleSpace* DefNewGeneration::first_compaction_space() const {
coleenp@113 926 return eden();
coleenp@113 927 }
coleenp@113 928
coleenp@113 929 HeapWord* DefNewGeneration::allocate(size_t word_size,
coleenp@113 930 bool is_tlab) {
coleenp@113 931 // This is the slow-path allocation for the DefNewGeneration.
coleenp@113 932 // Most allocations are fast-path in compiled code.
coleenp@113 933 // We try to allocate from the eden. If that works, we are happy.
coleenp@113 934 // Note that since DefNewGeneration supports lock-free allocation, we
coleenp@113 935 // have to use it here, as well.
coleenp@113 936 HeapWord* result = eden()->par_allocate(word_size);
coleenp@113 937 if (result != NULL) {
coleenp@113 938 return result;
coleenp@113 939 }
coleenp@113 940 do {
coleenp@113 941 HeapWord* old_limit = eden()->soft_end();
coleenp@113 942 if (old_limit < eden()->end()) {
coleenp@113 943 // Tell the next generation we reached a limit.
coleenp@113 944 HeapWord* new_limit =
coleenp@113 945 next_gen()->allocation_limit_reached(eden(), eden()->top(), word_size);
coleenp@113 946 if (new_limit != NULL) {
coleenp@113 947 Atomic::cmpxchg_ptr(new_limit, eden()->soft_end_addr(), old_limit);
coleenp@113 948 } else {
coleenp@113 949 assert(eden()->soft_end() == eden()->end(),
coleenp@113 950 "invalid state after allocation_limit_reached returned null");
coleenp@113 951 }
coleenp@113 952 } else {
coleenp@113 953 // The allocation failed and the soft limit is equal to the hard limit,
coleenp@113 954 // there are no reasons to do an attempt to allocate
coleenp@113 955 assert(old_limit == eden()->end(), "sanity check");
coleenp@113 956 break;
coleenp@113 957 }
coleenp@113 958 // Try to allocate until succeeded or the soft limit can't be adjusted
coleenp@113 959 result = eden()->par_allocate(word_size);
coleenp@113 960 } while (result == NULL);
coleenp@113 961
coleenp@113 962 // If the eden is full and the last collection bailed out, we are running
coleenp@113 963 // out of heap space, and we try to allocate the from-space, too.
coleenp@113 964 // allocate_from_space can't be inlined because that would introduce a
coleenp@113 965 // circular dependency at compile time.
coleenp@113 966 if (result == NULL) {
coleenp@113 967 result = allocate_from_space(word_size);
coleenp@113 968 }
coleenp@113 969 return result;
coleenp@113 970 }
coleenp@113 971
coleenp@113 972 HeapWord* DefNewGeneration::par_allocate(size_t word_size,
coleenp@113 973 bool is_tlab) {
coleenp@113 974 return eden()->par_allocate(word_size);
coleenp@113 975 }
coleenp@113 976
coleenp@113 977 void DefNewGeneration::gc_prologue(bool full) {
coleenp@113 978 // Ensure that _end and _soft_end are the same in eden space.
coleenp@113 979 eden()->set_soft_end(eden()->end());
coleenp@113 980 }
coleenp@113 981
coleenp@113 982 size_t DefNewGeneration::tlab_capacity() const {
coleenp@113 983 return eden()->capacity();
coleenp@113 984 }
coleenp@113 985
coleenp@113 986 size_t DefNewGeneration::unsafe_max_tlab_alloc() const {
coleenp@113 987 return unsafe_max_alloc_nogc();
coleenp@113 988 }