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