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