1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc_implementation/shared/gcTimer.hpp" 27 #include "gc_implementation/shared/gcTrace.hpp" 28 #include "gc_implementation/shared/spaceDecorator.hpp" 29 #include "gc_interface/collectedHeap.inline.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "memory/blockOffsetTable.inline.hpp" 32 #include "memory/cardTableRS.hpp" 33 #include "memory/gcLocker.inline.hpp" 34 #include "memory/genCollectedHeap.hpp" 35 #include "memory/genMarkSweep.hpp" 36 #include "memory/genOopClosures.hpp" 37 #include "memory/genOopClosures.inline.hpp" 38 #include "memory/generation.hpp" 39 #include "memory/space.inline.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "runtime/java.hpp" 42 #include "utilities/copy.hpp" 43 #include "utilities/events.hpp" 44 45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 46 47 Generation::Generation(ReservedSpace rs, size_t initial_size, int level) : 48 _level(level), 49 _ref_processor(NULL) { 50 if (!_virtual_space.initialize(rs, initial_size)) { 51 vm_exit_during_initialization("Could not reserve enough space for " 52 "object heap"); 53 } 54 // Mangle all of the the initial generation. 55 if (ZapUnusedHeapArea) { 56 MemRegion mangle_region((HeapWord*)_virtual_space.low(), 57 (HeapWord*)_virtual_space.high()); 58 SpaceMangler::mangle_region(mangle_region); 59 } 60 _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(), 61 (HeapWord*)_virtual_space.high_boundary()); 62 } 63 64 GenerationSpec* Generation::spec() { 65 GenCollectedHeap* gch = GenCollectedHeap::heap(); 66 assert(0 <= level() && level() < gch->_n_gens, "Bad gen level"); 67 return gch->_gen_specs[level()]; 68 } 69 70 size_t Generation::max_capacity() const { 71 return reserved().byte_size(); 72 } 73 74 void Generation::print_heap_change(size_t prev_used) const { 75 if (PrintGCDetails && Verbose) { 76 gclog_or_tty->print(" " SIZE_FORMAT 77 "->" SIZE_FORMAT 78 "(" SIZE_FORMAT ")", 79 prev_used, used(), capacity()); 80 } else { 81 gclog_or_tty->print(" " SIZE_FORMAT "K" 82 "->" SIZE_FORMAT "K" 83 "(" SIZE_FORMAT "K)", 84 prev_used / K, used() / K, capacity() / K); 85 } 86 } 87 88 // By default we get a single threaded default reference processor; 89 // generations needing multi-threaded refs processing or discovery override this method. 90 void Generation::ref_processor_init() { 91 assert(_ref_processor == NULL, "a reference processor already exists"); 92 assert(!_reserved.is_empty(), "empty generation?"); 93 _ref_processor = new ReferenceProcessor(_reserved); // a vanilla reference processor 94 if (_ref_processor == NULL) { 95 vm_exit_during_initialization("Could not allocate ReferenceProcessor object"); 96 } 97 } 98 99 void Generation::print() const { print_on(tty); } 100 101 void Generation::print_on(outputStream* st) const { 102 st->print(" %-20s", name()); 103 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 104 capacity()/K, used()/K); 105 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 106 _virtual_space.low_boundary(), 107 _virtual_space.high(), 108 _virtual_space.high_boundary()); 109 } 110 111 void Generation::print_summary_info() { print_summary_info_on(tty); } 112 113 void Generation::print_summary_info_on(outputStream* st) { 114 StatRecord* sr = stat_record(); 115 double time = sr->accumulated_time.seconds(); 116 st->print_cr("[Accumulated GC generation %d time %3.7f secs, " 117 "%d GC's, avg GC time %3.7f]", 118 level(), time, sr->invocations, 119 sr->invocations > 0 ? time / sr->invocations : 0.0); 120 } 121 122 // Utility iterator classes 123 124 class GenerationIsInReservedClosure : public SpaceClosure { 125 public: 126 const void* _p; 127 Space* sp; 128 virtual void do_space(Space* s) { 129 if (sp == NULL) { 130 if (s->is_in_reserved(_p)) sp = s; 131 } 132 } 133 GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {} 134 }; 135 136 class GenerationIsInClosure : public SpaceClosure { 137 public: 138 const void* _p; 139 Space* sp; 140 virtual void do_space(Space* s) { 141 if (sp == NULL) { 142 if (s->is_in(_p)) sp = s; 143 } 144 } 145 GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {} 146 }; 147 148 bool Generation::is_in(const void* p) const { 149 GenerationIsInClosure blk(p); 150 ((Generation*)this)->space_iterate(&blk); 151 return blk.sp != NULL; 152 } 153 154 Generation* Generation::next_gen() const { 155 GenCollectedHeap* gch = GenCollectedHeap::heap(); 156 int next = level() + 1; 157 if (next < gch->_n_gens) { 158 return gch->_gens[next]; 159 } else { 160 return NULL; 161 } 162 } 163 164 size_t Generation::max_contiguous_available() const { 165 // The largest number of contiguous free words in this or any higher generation. 166 size_t max = 0; 167 for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) { 168 size_t avail = gen->contiguous_available(); 169 if (avail > max) { 170 max = avail; 171 } 172 } 173 return max; 174 } 175 176 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const { 177 size_t available = max_contiguous_available(); 178 bool res = (available >= max_promotion_in_bytes); 179 if (PrintGC && Verbose) { 180 gclog_or_tty->print_cr( 181 "Generation: promo attempt is%s safe: available("SIZE_FORMAT") %s max_promo("SIZE_FORMAT")", 182 res? "":" not", available, res? ">=":"<", 183 max_promotion_in_bytes); 184 } 185 return res; 186 } 187 188 // Ignores "ref" and calls allocate(). 189 oop Generation::promote(oop obj, size_t obj_size) { 190 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in"); 191 192 #ifndef PRODUCT 193 if (Universe::heap()->promotion_should_fail()) { 194 return NULL; 195 } 196 #endif // #ifndef PRODUCT 197 198 HeapWord* result = allocate(obj_size, false); 199 if (result != NULL) { 200 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size); 201 return oop(result); 202 } else { 203 GenCollectedHeap* gch = GenCollectedHeap::heap(); 204 return gch->handle_failed_promotion(this, obj, obj_size); 205 } 206 } 207 208 oop Generation::par_promote(int thread_num, 209 oop obj, markOop m, size_t word_sz) { 210 // Could do a bad general impl here that gets a lock. But no. 211 ShouldNotCallThis(); 212 return NULL; 213 } 214 215 Space* Generation::space_containing(const void* p) const { 216 GenerationIsInReservedClosure blk(p); 217 // Cast away const 218 ((Generation*)this)->space_iterate(&blk); 219 return blk.sp; 220 } 221 222 // Some of these are mediocre general implementations. Should be 223 // overridden to get better performance. 224 225 class GenerationBlockStartClosure : public SpaceClosure { 226 public: 227 const void* _p; 228 HeapWord* _start; 229 virtual void do_space(Space* s) { 230 if (_start == NULL && s->is_in_reserved(_p)) { 231 _start = s->block_start(_p); 232 } 233 } 234 GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; } 235 }; 236 237 HeapWord* Generation::block_start(const void* p) const { 238 GenerationBlockStartClosure blk(p); 239 // Cast away const 240 ((Generation*)this)->space_iterate(&blk); 241 return blk._start; 242 } 243 244 class GenerationBlockSizeClosure : public SpaceClosure { 245 public: 246 const HeapWord* _p; 247 size_t size; 248 virtual void do_space(Space* s) { 249 if (size == 0 && s->is_in_reserved(_p)) { 250 size = s->block_size(_p); 251 } 252 } 253 GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; } 254 }; 255 256 size_t Generation::block_size(const HeapWord* p) const { 257 GenerationBlockSizeClosure blk(p); 258 // Cast away const 259 ((Generation*)this)->space_iterate(&blk); 260 assert(blk.size > 0, "seems reasonable"); 261 return blk.size; 262 } 263 264 class GenerationBlockIsObjClosure : public SpaceClosure { 265 public: 266 const HeapWord* _p; 267 bool is_obj; 268 virtual void do_space(Space* s) { 269 if (!is_obj && s->is_in_reserved(_p)) { 270 is_obj |= s->block_is_obj(_p); 271 } 272 } 273 GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; } 274 }; 275 276 bool Generation::block_is_obj(const HeapWord* p) const { 277 GenerationBlockIsObjClosure blk(p); 278 // Cast away const 279 ((Generation*)this)->space_iterate(&blk); 280 return blk.is_obj; 281 } 282 283 class GenerationOopIterateClosure : public SpaceClosure { 284 public: 285 ExtendedOopClosure* _cl; 286 virtual void do_space(Space* s) { 287 s->oop_iterate(_cl); 288 } 289 GenerationOopIterateClosure(ExtendedOopClosure* cl) : 290 _cl(cl) {} 291 }; 292 293 void Generation::oop_iterate(ExtendedOopClosure* cl) { 294 GenerationOopIterateClosure blk(cl); 295 space_iterate(&blk); 296 } 297 298 void Generation::younger_refs_in_space_iterate(Space* sp, 299 OopsInGenClosure* cl) { 300 GenRemSet* rs = GenCollectedHeap::heap()->rem_set(); 301 rs->younger_refs_in_space_iterate(sp, cl); 302 } 303 304 class GenerationObjIterateClosure : public SpaceClosure { 305 private: 306 ObjectClosure* _cl; 307 public: 308 virtual void do_space(Space* s) { 309 s->object_iterate(_cl); 310 } 311 GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 312 }; 313 314 void Generation::object_iterate(ObjectClosure* cl) { 315 GenerationObjIterateClosure blk(cl); 316 space_iterate(&blk); 317 } 318 319 class GenerationSafeObjIterateClosure : public SpaceClosure { 320 private: 321 ObjectClosure* _cl; 322 public: 323 virtual void do_space(Space* s) { 324 s->safe_object_iterate(_cl); 325 } 326 GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 327 }; 328 329 void Generation::safe_object_iterate(ObjectClosure* cl) { 330 GenerationSafeObjIterateClosure blk(cl); 331 space_iterate(&blk); 332 } 333 334 void Generation::prepare_for_compaction(CompactPoint* cp) { 335 // Generic implementation, can be specialized 336 CompactibleSpace* space = first_compaction_space(); 337 while (space != NULL) { 338 space->prepare_for_compaction(cp); 339 space = space->next_compaction_space(); 340 } 341 } 342 343 class AdjustPointersClosure: public SpaceClosure { 344 public: 345 void do_space(Space* sp) { 346 sp->adjust_pointers(); 347 } 348 }; 349 350 void Generation::adjust_pointers() { 351 // Note that this is done over all spaces, not just the compactible 352 // ones. 353 AdjustPointersClosure blk; 354 space_iterate(&blk, true); 355 } 356 357 void Generation::compact() { 358 CompactibleSpace* sp = first_compaction_space(); 359 while (sp != NULL) { 360 sp->compact(); 361 sp = sp->next_compaction_space(); 362 } 363 }