Thu, 11 Dec 2008 12:05:08 -0800
6578152: fill_region_with_object has usability and safety issues
Reviewed-by: apetrusenko, ysr
duke@435 | 1 | /* |
xdono@631 | 2 | * Copyright 1997-2008 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_generation.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | Generation::Generation(ReservedSpace rs, size_t initial_size, int level) : |
duke@435 | 29 | _level(level), |
duke@435 | 30 | _ref_processor(NULL) { |
duke@435 | 31 | if (!_virtual_space.initialize(rs, initial_size)) { |
duke@435 | 32 | vm_exit_during_initialization("Could not reserve enough space for " |
duke@435 | 33 | "object heap"); |
duke@435 | 34 | } |
jmasa@698 | 35 | // Mangle all of the the initial generation. |
jmasa@698 | 36 | if (ZapUnusedHeapArea) { |
jmasa@698 | 37 | MemRegion mangle_region((HeapWord*)_virtual_space.low(), |
jmasa@698 | 38 | (HeapWord*)_virtual_space.high()); |
jmasa@698 | 39 | SpaceMangler::mangle_region(mangle_region); |
jmasa@698 | 40 | } |
duke@435 | 41 | _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(), |
duke@435 | 42 | (HeapWord*)_virtual_space.high_boundary()); |
duke@435 | 43 | } |
duke@435 | 44 | |
duke@435 | 45 | GenerationSpec* Generation::spec() { |
duke@435 | 46 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
duke@435 | 47 | assert(0 <= level() && level() < gch->_n_gens, "Bad gen level"); |
duke@435 | 48 | return gch->_gen_specs[level()]; |
duke@435 | 49 | } |
duke@435 | 50 | |
duke@435 | 51 | size_t Generation::max_capacity() const { |
duke@435 | 52 | return reserved().byte_size(); |
duke@435 | 53 | } |
duke@435 | 54 | |
duke@435 | 55 | void Generation::print_heap_change(size_t prev_used) const { |
duke@435 | 56 | if (PrintGCDetails && Verbose) { |
duke@435 | 57 | gclog_or_tty->print(" " SIZE_FORMAT |
duke@435 | 58 | "->" SIZE_FORMAT |
duke@435 | 59 | "(" SIZE_FORMAT ")", |
duke@435 | 60 | prev_used, used(), capacity()); |
duke@435 | 61 | } else { |
duke@435 | 62 | gclog_or_tty->print(" " SIZE_FORMAT "K" |
duke@435 | 63 | "->" SIZE_FORMAT "K" |
duke@435 | 64 | "(" SIZE_FORMAT "K)", |
duke@435 | 65 | prev_used / K, used() / K, capacity() / K); |
duke@435 | 66 | } |
duke@435 | 67 | } |
duke@435 | 68 | |
duke@435 | 69 | // By default we get a single threaded default reference processor; |
duke@435 | 70 | // generations needing multi-threaded refs discovery override this method. |
duke@435 | 71 | void Generation::ref_processor_init() { |
duke@435 | 72 | assert(_ref_processor == NULL, "a reference processor already exists"); |
duke@435 | 73 | assert(!_reserved.is_empty(), "empty generation?"); |
duke@435 | 74 | _ref_processor = |
duke@435 | 75 | new ReferenceProcessor(_reserved, // span |
duke@435 | 76 | refs_discovery_is_atomic(), // atomic_discovery |
duke@435 | 77 | refs_discovery_is_mt()); // mt_discovery |
duke@435 | 78 | if (_ref_processor == NULL) { |
duke@435 | 79 | vm_exit_during_initialization("Could not allocate ReferenceProcessor object"); |
duke@435 | 80 | } |
duke@435 | 81 | } |
duke@435 | 82 | |
duke@435 | 83 | void Generation::print() const { print_on(tty); } |
duke@435 | 84 | |
duke@435 | 85 | void Generation::print_on(outputStream* st) const { |
duke@435 | 86 | st->print(" %-20s", name()); |
duke@435 | 87 | st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", |
duke@435 | 88 | capacity()/K, used()/K); |
duke@435 | 89 | st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", |
duke@435 | 90 | _virtual_space.low_boundary(), |
duke@435 | 91 | _virtual_space.high(), |
duke@435 | 92 | _virtual_space.high_boundary()); |
duke@435 | 93 | } |
duke@435 | 94 | |
duke@435 | 95 | void Generation::print_summary_info() { print_summary_info_on(tty); } |
duke@435 | 96 | |
duke@435 | 97 | void Generation::print_summary_info_on(outputStream* st) { |
duke@435 | 98 | StatRecord* sr = stat_record(); |
duke@435 | 99 | double time = sr->accumulated_time.seconds(); |
duke@435 | 100 | st->print_cr("[Accumulated GC generation %d time %3.7f secs, " |
duke@435 | 101 | "%d GC's, avg GC time %3.7f]", |
duke@435 | 102 | level(), time, sr->invocations, |
duke@435 | 103 | sr->invocations > 0 ? time / sr->invocations : 0.0); |
duke@435 | 104 | } |
duke@435 | 105 | |
duke@435 | 106 | // Utility iterator classes |
duke@435 | 107 | |
duke@435 | 108 | class GenerationIsInReservedClosure : public SpaceClosure { |
duke@435 | 109 | public: |
duke@435 | 110 | const void* _p; |
duke@435 | 111 | Space* sp; |
duke@435 | 112 | virtual void do_space(Space* s) { |
duke@435 | 113 | if (sp == NULL) { |
duke@435 | 114 | if (s->is_in_reserved(_p)) sp = s; |
duke@435 | 115 | } |
duke@435 | 116 | } |
duke@435 | 117 | GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {} |
duke@435 | 118 | }; |
duke@435 | 119 | |
duke@435 | 120 | class GenerationIsInClosure : public SpaceClosure { |
duke@435 | 121 | public: |
duke@435 | 122 | const void* _p; |
duke@435 | 123 | Space* sp; |
duke@435 | 124 | virtual void do_space(Space* s) { |
duke@435 | 125 | if (sp == NULL) { |
duke@435 | 126 | if (s->is_in(_p)) sp = s; |
duke@435 | 127 | } |
duke@435 | 128 | } |
duke@435 | 129 | GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {} |
duke@435 | 130 | }; |
duke@435 | 131 | |
duke@435 | 132 | bool Generation::is_in(const void* p) const { |
duke@435 | 133 | GenerationIsInClosure blk(p); |
duke@435 | 134 | ((Generation*)this)->space_iterate(&blk); |
duke@435 | 135 | return blk.sp != NULL; |
duke@435 | 136 | } |
duke@435 | 137 | |
duke@435 | 138 | DefNewGeneration* Generation::as_DefNewGeneration() { |
duke@435 | 139 | assert((kind() == Generation::DefNew) || |
duke@435 | 140 | (kind() == Generation::ParNew) || |
duke@435 | 141 | (kind() == Generation::ASParNew), |
duke@435 | 142 | "Wrong youngest generation type"); |
duke@435 | 143 | return (DefNewGeneration*) this; |
duke@435 | 144 | } |
duke@435 | 145 | |
duke@435 | 146 | Generation* Generation::next_gen() const { |
duke@435 | 147 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
duke@435 | 148 | int next = level() + 1; |
duke@435 | 149 | if (next < gch->_n_gens) { |
duke@435 | 150 | return gch->_gens[next]; |
duke@435 | 151 | } else { |
duke@435 | 152 | return NULL; |
duke@435 | 153 | } |
duke@435 | 154 | } |
duke@435 | 155 | |
duke@435 | 156 | size_t Generation::max_contiguous_available() const { |
duke@435 | 157 | // The largest number of contiguous free words in this or any higher generation. |
duke@435 | 158 | size_t max = 0; |
duke@435 | 159 | for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) { |
duke@435 | 160 | size_t avail = gen->contiguous_available(); |
duke@435 | 161 | if (avail > max) { |
duke@435 | 162 | max = avail; |
duke@435 | 163 | } |
duke@435 | 164 | } |
duke@435 | 165 | return max; |
duke@435 | 166 | } |
duke@435 | 167 | |
duke@435 | 168 | bool Generation::promotion_attempt_is_safe(size_t promotion_in_bytes, |
duke@435 | 169 | bool not_used) const { |
duke@435 | 170 | if (PrintGC && Verbose) { |
duke@435 | 171 | gclog_or_tty->print_cr("Generation::promotion_attempt_is_safe" |
duke@435 | 172 | " contiguous_available: " SIZE_FORMAT |
duke@435 | 173 | " promotion_in_bytes: " SIZE_FORMAT, |
duke@435 | 174 | max_contiguous_available(), promotion_in_bytes); |
duke@435 | 175 | } |
duke@435 | 176 | return max_contiguous_available() >= promotion_in_bytes; |
duke@435 | 177 | } |
duke@435 | 178 | |
duke@435 | 179 | // Ignores "ref" and calls allocate(). |
coleenp@548 | 180 | oop Generation::promote(oop obj, size_t obj_size) { |
duke@435 | 181 | assert(obj_size == (size_t)obj->size(), "bad obj_size passed in"); |
duke@435 | 182 | |
duke@435 | 183 | #ifndef PRODUCT |
duke@435 | 184 | if (Universe::heap()->promotion_should_fail()) { |
duke@435 | 185 | return NULL; |
duke@435 | 186 | } |
duke@435 | 187 | #endif // #ifndef PRODUCT |
duke@435 | 188 | |
duke@435 | 189 | HeapWord* result = allocate(obj_size, false); |
duke@435 | 190 | if (result != NULL) { |
duke@435 | 191 | Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size); |
duke@435 | 192 | return oop(result); |
duke@435 | 193 | } else { |
duke@435 | 194 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
coleenp@548 | 195 | return gch->handle_failed_promotion(this, obj, obj_size); |
duke@435 | 196 | } |
duke@435 | 197 | } |
duke@435 | 198 | |
duke@435 | 199 | oop Generation::par_promote(int thread_num, |
duke@435 | 200 | oop obj, markOop m, size_t word_sz) { |
duke@435 | 201 | // Could do a bad general impl here that gets a lock. But no. |
duke@435 | 202 | ShouldNotCallThis(); |
duke@435 | 203 | return NULL; |
duke@435 | 204 | } |
duke@435 | 205 | |
duke@435 | 206 | void Generation::par_promote_alloc_undo(int thread_num, |
duke@435 | 207 | HeapWord* obj, size_t word_sz) { |
duke@435 | 208 | // Could do a bad general impl here that gets a lock. But no. |
duke@435 | 209 | guarantee(false, "No good general implementation."); |
duke@435 | 210 | } |
duke@435 | 211 | |
duke@435 | 212 | Space* Generation::space_containing(const void* p) const { |
duke@435 | 213 | GenerationIsInReservedClosure blk(p); |
duke@435 | 214 | // Cast away const |
duke@435 | 215 | ((Generation*)this)->space_iterate(&blk); |
duke@435 | 216 | return blk.sp; |
duke@435 | 217 | } |
duke@435 | 218 | |
duke@435 | 219 | // Some of these are mediocre general implementations. Should be |
duke@435 | 220 | // overridden to get better performance. |
duke@435 | 221 | |
duke@435 | 222 | class GenerationBlockStartClosure : public SpaceClosure { |
duke@435 | 223 | public: |
duke@435 | 224 | const void* _p; |
duke@435 | 225 | HeapWord* _start; |
duke@435 | 226 | virtual void do_space(Space* s) { |
duke@435 | 227 | if (_start == NULL && s->is_in_reserved(_p)) { |
duke@435 | 228 | _start = s->block_start(_p); |
duke@435 | 229 | } |
duke@435 | 230 | } |
duke@435 | 231 | GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; } |
duke@435 | 232 | }; |
duke@435 | 233 | |
duke@435 | 234 | HeapWord* Generation::block_start(const void* p) const { |
duke@435 | 235 | GenerationBlockStartClosure blk(p); |
duke@435 | 236 | // Cast away const |
duke@435 | 237 | ((Generation*)this)->space_iterate(&blk); |
duke@435 | 238 | return blk._start; |
duke@435 | 239 | } |
duke@435 | 240 | |
duke@435 | 241 | class GenerationBlockSizeClosure : public SpaceClosure { |
duke@435 | 242 | public: |
duke@435 | 243 | const HeapWord* _p; |
duke@435 | 244 | size_t size; |
duke@435 | 245 | virtual void do_space(Space* s) { |
duke@435 | 246 | if (size == 0 && s->is_in_reserved(_p)) { |
duke@435 | 247 | size = s->block_size(_p); |
duke@435 | 248 | } |
duke@435 | 249 | } |
duke@435 | 250 | GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; } |
duke@435 | 251 | }; |
duke@435 | 252 | |
duke@435 | 253 | size_t Generation::block_size(const HeapWord* p) const { |
duke@435 | 254 | GenerationBlockSizeClosure blk(p); |
duke@435 | 255 | // Cast away const |
duke@435 | 256 | ((Generation*)this)->space_iterate(&blk); |
duke@435 | 257 | assert(blk.size > 0, "seems reasonable"); |
duke@435 | 258 | return blk.size; |
duke@435 | 259 | } |
duke@435 | 260 | |
duke@435 | 261 | class GenerationBlockIsObjClosure : public SpaceClosure { |
duke@435 | 262 | public: |
duke@435 | 263 | const HeapWord* _p; |
duke@435 | 264 | bool is_obj; |
duke@435 | 265 | virtual void do_space(Space* s) { |
duke@435 | 266 | if (!is_obj && s->is_in_reserved(_p)) { |
duke@435 | 267 | is_obj |= s->block_is_obj(_p); |
duke@435 | 268 | } |
duke@435 | 269 | } |
duke@435 | 270 | GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; } |
duke@435 | 271 | }; |
duke@435 | 272 | |
duke@435 | 273 | bool Generation::block_is_obj(const HeapWord* p) const { |
duke@435 | 274 | GenerationBlockIsObjClosure blk(p); |
duke@435 | 275 | // Cast away const |
duke@435 | 276 | ((Generation*)this)->space_iterate(&blk); |
duke@435 | 277 | return blk.is_obj; |
duke@435 | 278 | } |
duke@435 | 279 | |
duke@435 | 280 | class GenerationOopIterateClosure : public SpaceClosure { |
duke@435 | 281 | public: |
duke@435 | 282 | OopClosure* cl; |
duke@435 | 283 | MemRegion mr; |
duke@435 | 284 | virtual void do_space(Space* s) { |
duke@435 | 285 | s->oop_iterate(mr, cl); |
duke@435 | 286 | } |
duke@435 | 287 | GenerationOopIterateClosure(OopClosure* _cl, MemRegion _mr) : |
duke@435 | 288 | cl(_cl), mr(_mr) {} |
duke@435 | 289 | }; |
duke@435 | 290 | |
duke@435 | 291 | void Generation::oop_iterate(OopClosure* cl) { |
duke@435 | 292 | GenerationOopIterateClosure blk(cl, _reserved); |
duke@435 | 293 | space_iterate(&blk); |
duke@435 | 294 | } |
duke@435 | 295 | |
duke@435 | 296 | void Generation::oop_iterate(MemRegion mr, OopClosure* cl) { |
duke@435 | 297 | GenerationOopIterateClosure blk(cl, mr); |
duke@435 | 298 | space_iterate(&blk); |
duke@435 | 299 | } |
duke@435 | 300 | |
duke@435 | 301 | void Generation::younger_refs_in_space_iterate(Space* sp, |
duke@435 | 302 | OopsInGenClosure* cl) { |
duke@435 | 303 | GenRemSet* rs = SharedHeap::heap()->rem_set(); |
duke@435 | 304 | rs->younger_refs_in_space_iterate(sp, cl); |
duke@435 | 305 | } |
duke@435 | 306 | |
duke@435 | 307 | class GenerationObjIterateClosure : public SpaceClosure { |
duke@435 | 308 | private: |
duke@435 | 309 | ObjectClosure* _cl; |
duke@435 | 310 | public: |
duke@435 | 311 | virtual void do_space(Space* s) { |
duke@435 | 312 | s->object_iterate(_cl); |
duke@435 | 313 | } |
duke@435 | 314 | GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} |
duke@435 | 315 | }; |
duke@435 | 316 | |
duke@435 | 317 | void Generation::object_iterate(ObjectClosure* cl) { |
duke@435 | 318 | GenerationObjIterateClosure blk(cl); |
duke@435 | 319 | space_iterate(&blk); |
duke@435 | 320 | } |
duke@435 | 321 | |
duke@435 | 322 | void Generation::prepare_for_compaction(CompactPoint* cp) { |
duke@435 | 323 | // Generic implementation, can be specialized |
duke@435 | 324 | CompactibleSpace* space = first_compaction_space(); |
duke@435 | 325 | while (space != NULL) { |
duke@435 | 326 | space->prepare_for_compaction(cp); |
duke@435 | 327 | space = space->next_compaction_space(); |
duke@435 | 328 | } |
duke@435 | 329 | } |
duke@435 | 330 | |
duke@435 | 331 | class AdjustPointersClosure: public SpaceClosure { |
duke@435 | 332 | public: |
duke@435 | 333 | void do_space(Space* sp) { |
duke@435 | 334 | sp->adjust_pointers(); |
duke@435 | 335 | } |
duke@435 | 336 | }; |
duke@435 | 337 | |
duke@435 | 338 | void Generation::adjust_pointers() { |
duke@435 | 339 | // Note that this is done over all spaces, not just the compactible |
duke@435 | 340 | // ones. |
duke@435 | 341 | AdjustPointersClosure blk; |
duke@435 | 342 | space_iterate(&blk, true); |
duke@435 | 343 | } |
duke@435 | 344 | |
duke@435 | 345 | void Generation::compact() { |
duke@435 | 346 | CompactibleSpace* sp = first_compaction_space(); |
duke@435 | 347 | while (sp != NULL) { |
duke@435 | 348 | sp->compact(); |
duke@435 | 349 | sp = sp->next_compaction_space(); |
duke@435 | 350 | } |
duke@435 | 351 | } |
duke@435 | 352 | |
duke@435 | 353 | CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size, |
duke@435 | 354 | int level, |
duke@435 | 355 | GenRemSet* remset) : |
duke@435 | 356 | Generation(rs, initial_byte_size, level), _rs(remset) |
duke@435 | 357 | { |
duke@435 | 358 | HeapWord* start = (HeapWord*)rs.base(); |
duke@435 | 359 | size_t reserved_byte_size = rs.size(); |
duke@435 | 360 | assert((uintptr_t(start) & 3) == 0, "bad alignment"); |
duke@435 | 361 | assert((reserved_byte_size & 3) == 0, "bad alignment"); |
duke@435 | 362 | MemRegion reserved_mr(start, heap_word_size(reserved_byte_size)); |
duke@435 | 363 | _bts = new BlockOffsetSharedArray(reserved_mr, |
duke@435 | 364 | heap_word_size(initial_byte_size)); |
duke@435 | 365 | MemRegion committed_mr(start, heap_word_size(initial_byte_size)); |
duke@435 | 366 | _rs->resize_covered_region(committed_mr); |
duke@435 | 367 | if (_bts == NULL) |
duke@435 | 368 | vm_exit_during_initialization("Could not allocate a BlockOffsetArray"); |
duke@435 | 369 | |
duke@435 | 370 | // Verify that the start and end of this generation is the start of a card. |
duke@435 | 371 | // If this wasn't true, a single card could span more than on generation, |
duke@435 | 372 | // which would cause problems when we commit/uncommit memory, and when we |
duke@435 | 373 | // clear and dirty cards. |
duke@435 | 374 | guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned"); |
duke@435 | 375 | if (reserved_mr.end() != Universe::heap()->reserved_region().end()) { |
duke@435 | 376 | // Don't check at the very end of the heap as we'll assert that we're probing off |
duke@435 | 377 | // the end if we try. |
duke@435 | 378 | guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned"); |
duke@435 | 379 | } |
duke@435 | 380 | } |
duke@435 | 381 | |
jmasa@706 | 382 | bool CardGeneration::expand(size_t bytes, size_t expand_bytes) { |
jmasa@706 | 383 | assert_locked_or_safepoint(Heap_lock); |
jmasa@706 | 384 | if (bytes == 0) { |
jmasa@706 | 385 | return true; // That's what grow_by(0) would return |
jmasa@706 | 386 | } |
jmasa@706 | 387 | size_t aligned_bytes = ReservedSpace::page_align_size_up(bytes); |
jmasa@706 | 388 | if (aligned_bytes == 0){ |
jmasa@706 | 389 | // The alignment caused the number of bytes to wrap. An expand_by(0) will |
jmasa@706 | 390 | // return true with the implication that an expansion was done when it |
jmasa@706 | 391 | // was not. A call to expand implies a best effort to expand by "bytes" |
jmasa@706 | 392 | // but not a guarantee. Align down to give a best effort. This is likely |
jmasa@706 | 393 | // the most that the generation can expand since it has some capacity to |
jmasa@706 | 394 | // start with. |
jmasa@706 | 395 | aligned_bytes = ReservedSpace::page_align_size_down(bytes); |
jmasa@706 | 396 | } |
jmasa@706 | 397 | size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes); |
jmasa@706 | 398 | bool success = false; |
jmasa@706 | 399 | if (aligned_expand_bytes > aligned_bytes) { |
jmasa@706 | 400 | success = grow_by(aligned_expand_bytes); |
jmasa@706 | 401 | } |
jmasa@706 | 402 | if (!success) { |
jmasa@706 | 403 | success = grow_by(aligned_bytes); |
jmasa@706 | 404 | } |
jmasa@706 | 405 | if (!success) { |
jmasa@706 | 406 | success = grow_to_reserved(); |
jmasa@706 | 407 | } |
jmasa@706 | 408 | if (PrintGC && Verbose) { |
jmasa@706 | 409 | if (success && GC_locker::is_active()) { |
jmasa@706 | 410 | gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); |
jmasa@706 | 411 | } |
jmasa@706 | 412 | } |
jmasa@706 | 413 | |
jmasa@706 | 414 | return success; |
jmasa@706 | 415 | } |
jmasa@706 | 416 | |
duke@435 | 417 | |
duke@435 | 418 | // No young generation references, clear this generation's cards. |
duke@435 | 419 | void CardGeneration::clear_remembered_set() { |
duke@435 | 420 | _rs->clear(reserved()); |
duke@435 | 421 | } |
duke@435 | 422 | |
duke@435 | 423 | |
duke@435 | 424 | // Objects in this generation may have moved, invalidate this |
duke@435 | 425 | // generation's cards. |
duke@435 | 426 | void CardGeneration::invalidate_remembered_set() { |
duke@435 | 427 | _rs->invalidate(used_region()); |
duke@435 | 428 | } |
duke@435 | 429 | |
duke@435 | 430 | |
duke@435 | 431 | // Currently nothing to do. |
duke@435 | 432 | void CardGeneration::prepare_for_verify() {} |
duke@435 | 433 | |
duke@435 | 434 | |
duke@435 | 435 | void OneContigSpaceCardGeneration::collect(bool full, |
duke@435 | 436 | bool clear_all_soft_refs, |
duke@435 | 437 | size_t size, |
duke@435 | 438 | bool is_tlab) { |
duke@435 | 439 | SpecializationStats::clear(); |
duke@435 | 440 | // Temporarily expand the span of our ref processor, so |
duke@435 | 441 | // refs discovery is over the entire heap, not just this generation |
duke@435 | 442 | ReferenceProcessorSpanMutator |
duke@435 | 443 | x(ref_processor(), GenCollectedHeap::heap()->reserved_region()); |
duke@435 | 444 | GenMarkSweep::invoke_at_safepoint(_level, ref_processor(), clear_all_soft_refs); |
duke@435 | 445 | SpecializationStats::print(); |
duke@435 | 446 | } |
duke@435 | 447 | |
duke@435 | 448 | HeapWord* |
duke@435 | 449 | OneContigSpaceCardGeneration::expand_and_allocate(size_t word_size, |
duke@435 | 450 | bool is_tlab, |
duke@435 | 451 | bool parallel) { |
duke@435 | 452 | assert(!is_tlab, "OneContigSpaceCardGeneration does not support TLAB allocation"); |
duke@435 | 453 | if (parallel) { |
duke@435 | 454 | MutexLocker x(ParGCRareEvent_lock); |
duke@435 | 455 | HeapWord* result = NULL; |
duke@435 | 456 | size_t byte_size = word_size * HeapWordSize; |
duke@435 | 457 | while (true) { |
duke@435 | 458 | expand(byte_size, _min_heap_delta_bytes); |
duke@435 | 459 | if (GCExpandToAllocateDelayMillis > 0) { |
duke@435 | 460 | os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); |
duke@435 | 461 | } |
duke@435 | 462 | result = _the_space->par_allocate(word_size); |
duke@435 | 463 | if ( result != NULL) { |
duke@435 | 464 | return result; |
duke@435 | 465 | } else { |
duke@435 | 466 | // If there's not enough expansion space available, give up. |
duke@435 | 467 | if (_virtual_space.uncommitted_size() < byte_size) { |
duke@435 | 468 | return NULL; |
duke@435 | 469 | } |
duke@435 | 470 | // else try again |
duke@435 | 471 | } |
duke@435 | 472 | } |
duke@435 | 473 | } else { |
duke@435 | 474 | expand(word_size*HeapWordSize, _min_heap_delta_bytes); |
duke@435 | 475 | return _the_space->allocate(word_size); |
duke@435 | 476 | } |
duke@435 | 477 | } |
duke@435 | 478 | |
jmasa@706 | 479 | bool OneContigSpaceCardGeneration::expand(size_t bytes, size_t expand_bytes) { |
duke@435 | 480 | GCMutexLocker x(ExpandHeap_lock); |
jmasa@706 | 481 | return CardGeneration::expand(bytes, expand_bytes); |
duke@435 | 482 | } |
duke@435 | 483 | |
duke@435 | 484 | |
duke@435 | 485 | void OneContigSpaceCardGeneration::shrink(size_t bytes) { |
duke@435 | 486 | assert_locked_or_safepoint(ExpandHeap_lock); |
duke@435 | 487 | size_t size = ReservedSpace::page_align_size_down(bytes); |
duke@435 | 488 | if (size > 0) { |
duke@435 | 489 | shrink_by(size); |
duke@435 | 490 | } |
duke@435 | 491 | } |
duke@435 | 492 | |
duke@435 | 493 | |
duke@435 | 494 | size_t OneContigSpaceCardGeneration::capacity() const { |
duke@435 | 495 | return _the_space->capacity(); |
duke@435 | 496 | } |
duke@435 | 497 | |
duke@435 | 498 | |
duke@435 | 499 | size_t OneContigSpaceCardGeneration::used() const { |
duke@435 | 500 | return _the_space->used(); |
duke@435 | 501 | } |
duke@435 | 502 | |
duke@435 | 503 | |
duke@435 | 504 | size_t OneContigSpaceCardGeneration::free() const { |
duke@435 | 505 | return _the_space->free(); |
duke@435 | 506 | } |
duke@435 | 507 | |
duke@435 | 508 | MemRegion OneContigSpaceCardGeneration::used_region() const { |
duke@435 | 509 | return the_space()->used_region(); |
duke@435 | 510 | } |
duke@435 | 511 | |
duke@435 | 512 | size_t OneContigSpaceCardGeneration::unsafe_max_alloc_nogc() const { |
duke@435 | 513 | return _the_space->free(); |
duke@435 | 514 | } |
duke@435 | 515 | |
duke@435 | 516 | size_t OneContigSpaceCardGeneration::contiguous_available() const { |
duke@435 | 517 | return _the_space->free() + _virtual_space.uncommitted_size(); |
duke@435 | 518 | } |
duke@435 | 519 | |
duke@435 | 520 | bool OneContigSpaceCardGeneration::grow_by(size_t bytes) { |
duke@435 | 521 | assert_locked_or_safepoint(ExpandHeap_lock); |
duke@435 | 522 | bool result = _virtual_space.expand_by(bytes); |
duke@435 | 523 | if (result) { |
duke@435 | 524 | size_t new_word_size = |
duke@435 | 525 | heap_word_size(_virtual_space.committed_size()); |
duke@435 | 526 | MemRegion mr(_the_space->bottom(), new_word_size); |
duke@435 | 527 | // Expand card table |
duke@435 | 528 | Universe::heap()->barrier_set()->resize_covered_region(mr); |
duke@435 | 529 | // Expand shared block offset array |
duke@435 | 530 | _bts->resize(new_word_size); |
duke@435 | 531 | |
duke@435 | 532 | // Fix for bug #4668531 |
jmasa@698 | 533 | if (ZapUnusedHeapArea) { |
jmasa@698 | 534 | MemRegion mangle_region(_the_space->end(), |
jmasa@698 | 535 | (HeapWord*)_virtual_space.high()); |
jmasa@698 | 536 | SpaceMangler::mangle_region(mangle_region); |
jmasa@698 | 537 | } |
duke@435 | 538 | |
duke@435 | 539 | // Expand space -- also expands space's BOT |
duke@435 | 540 | // (which uses (part of) shared array above) |
duke@435 | 541 | _the_space->set_end((HeapWord*)_virtual_space.high()); |
duke@435 | 542 | |
duke@435 | 543 | // update the space and generation capacity counters |
duke@435 | 544 | update_counters(); |
duke@435 | 545 | |
duke@435 | 546 | if (Verbose && PrintGC) { |
duke@435 | 547 | size_t new_mem_size = _virtual_space.committed_size(); |
duke@435 | 548 | size_t old_mem_size = new_mem_size - bytes; |
duke@435 | 549 | gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by " |
duke@435 | 550 | SIZE_FORMAT "K to " SIZE_FORMAT "K", |
duke@435 | 551 | name(), old_mem_size/K, bytes/K, new_mem_size/K); |
duke@435 | 552 | } |
duke@435 | 553 | } |
duke@435 | 554 | return result; |
duke@435 | 555 | } |
duke@435 | 556 | |
duke@435 | 557 | |
duke@435 | 558 | bool OneContigSpaceCardGeneration::grow_to_reserved() { |
duke@435 | 559 | assert_locked_or_safepoint(ExpandHeap_lock); |
duke@435 | 560 | bool success = true; |
duke@435 | 561 | const size_t remaining_bytes = _virtual_space.uncommitted_size(); |
duke@435 | 562 | if (remaining_bytes > 0) { |
duke@435 | 563 | success = grow_by(remaining_bytes); |
duke@435 | 564 | DEBUG_ONLY(if (!success) warning("grow to reserved failed");) |
duke@435 | 565 | } |
duke@435 | 566 | return success; |
duke@435 | 567 | } |
duke@435 | 568 | |
duke@435 | 569 | void OneContigSpaceCardGeneration::shrink_by(size_t bytes) { |
duke@435 | 570 | assert_locked_or_safepoint(ExpandHeap_lock); |
duke@435 | 571 | // Shrink committed space |
duke@435 | 572 | _virtual_space.shrink_by(bytes); |
duke@435 | 573 | // Shrink space; this also shrinks the space's BOT |
duke@435 | 574 | _the_space->set_end((HeapWord*) _virtual_space.high()); |
duke@435 | 575 | size_t new_word_size = heap_word_size(_the_space->capacity()); |
duke@435 | 576 | // Shrink the shared block offset array |
duke@435 | 577 | _bts->resize(new_word_size); |
duke@435 | 578 | MemRegion mr(_the_space->bottom(), new_word_size); |
duke@435 | 579 | // Shrink the card table |
duke@435 | 580 | Universe::heap()->barrier_set()->resize_covered_region(mr); |
duke@435 | 581 | |
duke@435 | 582 | if (Verbose && PrintGC) { |
duke@435 | 583 | size_t new_mem_size = _virtual_space.committed_size(); |
duke@435 | 584 | size_t old_mem_size = new_mem_size + bytes; |
duke@435 | 585 | gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K to " SIZE_FORMAT "K", |
duke@435 | 586 | name(), old_mem_size/K, new_mem_size/K); |
duke@435 | 587 | } |
duke@435 | 588 | } |
duke@435 | 589 | |
duke@435 | 590 | // Currently nothing to do. |
duke@435 | 591 | void OneContigSpaceCardGeneration::prepare_for_verify() {} |
duke@435 | 592 | |
duke@435 | 593 | |
duke@435 | 594 | void OneContigSpaceCardGeneration::object_iterate(ObjectClosure* blk) { |
duke@435 | 595 | _the_space->object_iterate(blk); |
duke@435 | 596 | } |
duke@435 | 597 | |
duke@435 | 598 | void OneContigSpaceCardGeneration::space_iterate(SpaceClosure* blk, |
duke@435 | 599 | bool usedOnly) { |
duke@435 | 600 | blk->do_space(_the_space); |
duke@435 | 601 | } |
duke@435 | 602 | |
duke@435 | 603 | void OneContigSpaceCardGeneration::object_iterate_since_last_GC(ObjectClosure* blk) { |
duke@435 | 604 | // Deal with delayed initialization of _the_space, |
duke@435 | 605 | // and lack of initialization of _last_gc. |
duke@435 | 606 | if (_last_gc.space() == NULL) { |
duke@435 | 607 | assert(the_space() != NULL, "shouldn't be NULL"); |
duke@435 | 608 | _last_gc = the_space()->bottom_mark(); |
duke@435 | 609 | } |
duke@435 | 610 | the_space()->object_iterate_from(_last_gc, blk); |
duke@435 | 611 | } |
duke@435 | 612 | |
duke@435 | 613 | void OneContigSpaceCardGeneration::younger_refs_iterate(OopsInGenClosure* blk) { |
duke@435 | 614 | blk->set_generation(this); |
duke@435 | 615 | younger_refs_in_space_iterate(_the_space, blk); |
duke@435 | 616 | blk->reset_generation(); |
duke@435 | 617 | } |
duke@435 | 618 | |
duke@435 | 619 | void OneContigSpaceCardGeneration::save_marks() { |
duke@435 | 620 | _the_space->set_saved_mark(); |
duke@435 | 621 | } |
duke@435 | 622 | |
duke@435 | 623 | |
duke@435 | 624 | void OneContigSpaceCardGeneration::reset_saved_marks() { |
duke@435 | 625 | _the_space->reset_saved_mark(); |
duke@435 | 626 | } |
duke@435 | 627 | |
duke@435 | 628 | |
duke@435 | 629 | bool OneContigSpaceCardGeneration::no_allocs_since_save_marks() { |
duke@435 | 630 | return _the_space->saved_mark_at_top(); |
duke@435 | 631 | } |
duke@435 | 632 | |
duke@435 | 633 | #define OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \ |
duke@435 | 634 | \ |
duke@435 | 635 | void OneContigSpaceCardGeneration:: \ |
duke@435 | 636 | oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \ |
duke@435 | 637 | blk->set_generation(this); \ |
duke@435 | 638 | _the_space->oop_since_save_marks_iterate##nv_suffix(blk); \ |
duke@435 | 639 | blk->reset_generation(); \ |
duke@435 | 640 | save_marks(); \ |
duke@435 | 641 | } |
duke@435 | 642 | |
duke@435 | 643 | ALL_SINCE_SAVE_MARKS_CLOSURES(OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN) |
duke@435 | 644 | |
duke@435 | 645 | #undef OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN |
duke@435 | 646 | |
duke@435 | 647 | |
duke@435 | 648 | void OneContigSpaceCardGeneration::gc_epilogue(bool full) { |
duke@435 | 649 | _last_gc = WaterMark(the_space(), the_space()->top()); |
duke@435 | 650 | |
duke@435 | 651 | // update the generation and space performance counters |
duke@435 | 652 | update_counters(); |
jmasa@698 | 653 | if (ZapUnusedHeapArea) { |
jmasa@698 | 654 | the_space()->check_mangled_unused_area_complete(); |
jmasa@698 | 655 | } |
jmasa@698 | 656 | } |
jmasa@698 | 657 | |
jmasa@698 | 658 | void OneContigSpaceCardGeneration::record_spaces_top() { |
jmasa@698 | 659 | assert(ZapUnusedHeapArea, "Not mangling unused space"); |
jmasa@698 | 660 | the_space()->set_top_for_allocations(); |
duke@435 | 661 | } |
duke@435 | 662 | |
duke@435 | 663 | void OneContigSpaceCardGeneration::verify(bool allow_dirty) { |
duke@435 | 664 | the_space()->verify(allow_dirty); |
duke@435 | 665 | } |
duke@435 | 666 | |
duke@435 | 667 | void OneContigSpaceCardGeneration::print_on(outputStream* st) const { |
duke@435 | 668 | Generation::print_on(st); |
duke@435 | 669 | st->print(" the"); |
duke@435 | 670 | the_space()->print_on(st); |
duke@435 | 671 | } |