1 /* 2 * Copyright (c) 1997, 2017, 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/serial/genMarkSweep.hpp" 27 #include "gc/shared/blockOffsetTable.inline.hpp" 28 #include "gc/shared/cardTableRS.hpp" 29 #include "gc/shared/collectedHeap.inline.hpp" 30 #include "gc/shared/gcLocker.hpp" 31 #include "gc/shared/gcTimer.hpp" 32 #include "gc/shared/gcTrace.hpp" 33 #include "gc/shared/genCollectedHeap.hpp" 34 #include "gc/shared/genOopClosures.hpp" 35 #include "gc/shared/genOopClosures.inline.hpp" 36 #include "gc/shared/generation.hpp" 37 #include "gc/shared/space.inline.hpp" 38 #include "gc/shared/spaceDecorator.hpp" 39 #include "logging/log.hpp" 40 #include "memory/allocation.inline.hpp" 41 #include "oops/oop.inline.hpp" 42 #include "runtime/java.hpp" 43 #include "utilities/copy.hpp" 44 #include "utilities/events.hpp" 45 46 Generation::Generation(ReservedSpace rs, size_t initial_size) : 47 _ref_processor(NULL), 48 _gc_manager(NULL) { 49 if (!_virtual_space.initialize(rs, initial_size)) { 50 vm_exit_during_initialization("Could not reserve enough space for " 51 "object heap"); 52 } 53 // Mangle all of the the initial generation. 54 if (ZapUnusedHeapArea) { 55 MemRegion mangle_region((HeapWord*)_virtual_space.low(), 56 (HeapWord*)_virtual_space.high()); 57 SpaceMangler::mangle_region(mangle_region); 58 } 59 _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(), 60 (HeapWord*)_virtual_space.high_boundary()); 61 } 62 63 size_t Generation::initial_size() { 64 GenCollectedHeap* gch = GenCollectedHeap::heap(); 65 if (gch->is_young_gen(this)) { 66 return gch->young_gen_spec()->init_size(); 67 } 68 return gch->old_gen_spec()->init_size(); 69 } 70 71 size_t Generation::max_capacity() const { 72 return reserved().byte_size(); 73 } 74 75 // By default we get a single threaded default reference processor; 76 // generations needing multi-threaded refs processing or discovery override this method. 77 void Generation::ref_processor_init() { 78 assert(_ref_processor == NULL, "a reference processor already exists"); 79 assert(!_reserved.is_empty(), "empty generation?"); 80 _ref_processor = new ReferenceProcessor(_reserved); // a vanilla reference processor 81 if (_ref_processor == NULL) { 82 vm_exit_during_initialization("Could not allocate ReferenceProcessor object"); 83 } 84 } 85 86 void Generation::print() const { print_on(tty); } 87 88 void Generation::print_on(outputStream* st) const { 89 st->print(" %-20s", name()); 90 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 91 capacity()/K, used()/K); 92 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 93 p2i(_virtual_space.low_boundary()), 94 p2i(_virtual_space.high()), 95 p2i(_virtual_space.high_boundary())); 96 } 97 98 void Generation::print_summary_info_on(outputStream* st) { 99 StatRecord* sr = stat_record(); 100 double time = sr->accumulated_time.seconds(); 101 st->print_cr("Accumulated %s generation GC time %3.7f secs, " 102 "%u GC's, avg GC time %3.7f", 103 GenCollectedHeap::heap()->is_young_gen(this) ? "young" : "old" , 104 time, 105 sr->invocations, 106 sr->invocations > 0 ? time / sr->invocations : 0.0); 107 } 108 109 // Utility iterator classes 110 111 class GenerationIsInReservedClosure : public SpaceClosure { 112 public: 113 const void* _p; 114 Space* sp; 115 virtual void do_space(Space* s) { 116 if (sp == NULL) { 117 if (s->is_in_reserved(_p)) sp = s; 118 } 119 } 120 GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {} 121 }; 122 123 class GenerationIsInClosure : public SpaceClosure { 124 public: 125 const void* _p; 126 Space* sp; 127 virtual void do_space(Space* s) { 128 if (sp == NULL) { 129 if (s->is_in(_p)) sp = s; 130 } 131 } 132 GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {} 133 }; 134 135 bool Generation::is_in(const void* p) const { 136 GenerationIsInClosure blk(p); 137 ((Generation*)this)->space_iterate(&blk); 138 return blk.sp != NULL; 139 } 140 141 size_t Generation::max_contiguous_available() const { 142 // The largest number of contiguous free words in this or any higher generation. 143 size_t avail = contiguous_available(); 144 size_t old_avail = 0; 145 if (GenCollectedHeap::heap()->is_young_gen(this)) { 146 old_avail = GenCollectedHeap::heap()->old_gen()->contiguous_available(); 147 } 148 return MAX2(avail, old_avail); 149 } 150 151 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const { 152 size_t available = max_contiguous_available(); 153 bool res = (available >= max_promotion_in_bytes); 154 log_trace(gc)("Generation: promo attempt is%s safe: available(" SIZE_FORMAT ") %s max_promo(" SIZE_FORMAT ")", 155 res? "":" not", available, res? ">=":"<", max_promotion_in_bytes); 156 return res; 157 } 158 159 // Ignores "ref" and calls allocate(). 160 oop Generation::promote(oop obj, size_t obj_size) { 161 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in"); 162 163 #ifndef PRODUCT 164 if (GenCollectedHeap::heap()->promotion_should_fail()) { 165 return NULL; 166 } 167 #endif // #ifndef PRODUCT 168 169 HeapWord* result = allocate(obj_size, false); 170 if (result != NULL) { 171 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size); 172 return oop(result); 173 } else { 174 GenCollectedHeap* gch = GenCollectedHeap::heap(); 175 return gch->handle_failed_promotion(this, obj, obj_size); 176 } 177 } 178 179 oop Generation::par_promote(int thread_num, 180 oop obj, markOop m, size_t word_sz) { 181 // Could do a bad general impl here that gets a lock. But no. 182 ShouldNotCallThis(); 183 return NULL; 184 } 185 186 Space* Generation::space_containing(const void* p) const { 187 GenerationIsInReservedClosure blk(p); 188 // Cast away const 189 ((Generation*)this)->space_iterate(&blk); 190 return blk.sp; 191 } 192 193 // Some of these are mediocre general implementations. Should be 194 // overridden to get better performance. 195 196 class GenerationBlockStartClosure : public SpaceClosure { 197 public: 198 const void* _p; 199 HeapWord* _start; 200 virtual void do_space(Space* s) { 201 if (_start == NULL && s->is_in_reserved(_p)) { 202 _start = s->block_start(_p); 203 } 204 } 205 GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; } 206 }; 207 208 HeapWord* Generation::block_start(const void* p) const { 209 GenerationBlockStartClosure blk(p); 210 // Cast away const 211 ((Generation*)this)->space_iterate(&blk); 212 return blk._start; 213 } 214 215 class GenerationBlockSizeClosure : public SpaceClosure { 216 public: 217 const HeapWord* _p; 218 size_t size; 219 virtual void do_space(Space* s) { 220 if (size == 0 && s->is_in_reserved(_p)) { 221 size = s->block_size(_p); 222 } 223 } 224 GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; } 225 }; 226 227 size_t Generation::block_size(const HeapWord* p) const { 228 GenerationBlockSizeClosure blk(p); 229 // Cast away const 230 ((Generation*)this)->space_iterate(&blk); 231 assert(blk.size > 0, "seems reasonable"); 232 return blk.size; 233 } 234 235 class GenerationBlockIsObjClosure : public SpaceClosure { 236 public: 237 const HeapWord* _p; 238 bool is_obj; 239 virtual void do_space(Space* s) { 240 if (!is_obj && s->is_in_reserved(_p)) { 241 is_obj |= s->block_is_obj(_p); 242 } 243 } 244 GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; } 245 }; 246 247 bool Generation::block_is_obj(const HeapWord* p) const { 248 GenerationBlockIsObjClosure blk(p); 249 // Cast away const 250 ((Generation*)this)->space_iterate(&blk); 251 return blk.is_obj; 252 } 253 254 class GenerationOopIterateClosure : public SpaceClosure { 255 public: 256 ExtendedOopClosure* _cl; 257 virtual void do_space(Space* s) { 258 s->oop_iterate(_cl); 259 } 260 GenerationOopIterateClosure(ExtendedOopClosure* cl) : 261 _cl(cl) {} 262 }; 263 264 void Generation::oop_iterate(ExtendedOopClosure* cl) { 265 GenerationOopIterateClosure blk(cl); 266 space_iterate(&blk); 267 } 268 269 void Generation::younger_refs_in_space_iterate(Space* sp, 270 OopsInGenClosure* cl, 271 uint n_threads) { 272 CardTableRS* rs = GenCollectedHeap::heap()->rem_set(); 273 rs->younger_refs_in_space_iterate(sp, cl, n_threads); 274 } 275 276 class GenerationObjIterateClosure : public SpaceClosure { 277 private: 278 ObjectClosure* _cl; 279 public: 280 virtual void do_space(Space* s) { 281 s->object_iterate(_cl); 282 } 283 GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 284 }; 285 286 void Generation::object_iterate(ObjectClosure* cl) { 287 GenerationObjIterateClosure blk(cl); 288 space_iterate(&blk); 289 } 290 291 class GenerationSafeObjIterateClosure : public SpaceClosure { 292 private: 293 ObjectClosure* _cl; 294 public: 295 virtual void do_space(Space* s) { 296 s->safe_object_iterate(_cl); 297 } 298 GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 299 }; 300 301 void Generation::safe_object_iterate(ObjectClosure* cl) { 302 GenerationSafeObjIterateClosure blk(cl); 303 space_iterate(&blk); 304 } 305 306 void Generation::prepare_for_compaction(CompactPoint* cp) { 307 // Generic implementation, can be specialized 308 CompactibleSpace* space = first_compaction_space(); 309 while (space != NULL) { 310 space->prepare_for_compaction(cp); 311 space = space->next_compaction_space(); 312 } 313 } 314 315 class AdjustPointersClosure: public SpaceClosure { 316 public: 317 void do_space(Space* sp) { 318 sp->adjust_pointers(); 319 } 320 }; 321 322 void Generation::adjust_pointers() { 323 // Note that this is done over all spaces, not just the compactible 324 // ones. 325 AdjustPointersClosure blk; 326 space_iterate(&blk, true); 327 } 328 329 void Generation::compact() { 330 CompactibleSpace* sp = first_compaction_space(); 331 while (sp != NULL) { 332 sp->compact(); 333 sp = sp->next_compaction_space(); 334 } 335 }