1 /*
   2  * Copyright (c) 1997, 2015, 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   if (level() == 0) {
 157     return gch->old_gen();
 158   } else {
 159     return NULL;
 160   }
 161 }
 162 
 163 size_t Generation::max_contiguous_available() const {
 164   // The largest number of contiguous free words in this or any higher generation.
 165   size_t max = 0;
 166   for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) {
 167     size_t avail = gen->contiguous_available();
 168     if (avail > max) {
 169       max = avail;
 170     }
 171   }
 172   return max;
 173 }
 174 
 175 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
 176   size_t available = max_contiguous_available();
 177   bool   res = (available >= max_promotion_in_bytes);
 178   if (PrintGC && Verbose) {
 179     gclog_or_tty->print_cr(
 180       "Generation: promo attempt is%s safe: available("SIZE_FORMAT") %s max_promo("SIZE_FORMAT")",
 181       res? "":" not", available, res? ">=":"<",
 182       max_promotion_in_bytes);
 183   }
 184   return res;
 185 }
 186 
 187 // Ignores "ref" and calls allocate().
 188 oop Generation::promote(oop obj, size_t obj_size) {
 189   assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
 190 
 191 #ifndef PRODUCT
 192   if (Universe::heap()->promotion_should_fail()) {
 193     return NULL;
 194   }
 195 #endif  // #ifndef PRODUCT
 196 
 197   HeapWord* result = allocate(obj_size, false);
 198   if (result != NULL) {
 199     Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
 200     return oop(result);
 201   } else {
 202     GenCollectedHeap* gch = GenCollectedHeap::heap();
 203     return gch->handle_failed_promotion(this, obj, obj_size);
 204   }
 205 }
 206 
 207 oop Generation::par_promote(int thread_num,
 208                             oop obj, markOop m, size_t word_sz) {
 209   // Could do a bad general impl here that gets a lock.  But no.
 210   ShouldNotCallThis();
 211   return NULL;
 212 }
 213 
 214 Space* Generation::space_containing(const void* p) const {
 215   GenerationIsInReservedClosure blk(p);
 216   // Cast away const
 217   ((Generation*)this)->space_iterate(&blk);
 218   return blk.sp;
 219 }
 220 
 221 // Some of these are mediocre general implementations.  Should be
 222 // overridden to get better performance.
 223 
 224 class GenerationBlockStartClosure : public SpaceClosure {
 225  public:
 226   const void* _p;
 227   HeapWord* _start;
 228   virtual void do_space(Space* s) {
 229     if (_start == NULL && s->is_in_reserved(_p)) {
 230       _start = s->block_start(_p);
 231     }
 232   }
 233   GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; }
 234 };
 235 
 236 HeapWord* Generation::block_start(const void* p) const {
 237   GenerationBlockStartClosure blk(p);
 238   // Cast away const
 239   ((Generation*)this)->space_iterate(&blk);
 240   return blk._start;
 241 }
 242 
 243 class GenerationBlockSizeClosure : public SpaceClosure {
 244  public:
 245   const HeapWord* _p;
 246   size_t size;
 247   virtual void do_space(Space* s) {
 248     if (size == 0 && s->is_in_reserved(_p)) {
 249       size = s->block_size(_p);
 250     }
 251   }
 252   GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; }
 253 };
 254 
 255 size_t Generation::block_size(const HeapWord* p) const {
 256   GenerationBlockSizeClosure blk(p);
 257   // Cast away const
 258   ((Generation*)this)->space_iterate(&blk);
 259   assert(blk.size > 0, "seems reasonable");
 260   return blk.size;
 261 }
 262 
 263 class GenerationBlockIsObjClosure : public SpaceClosure {
 264  public:
 265   const HeapWord* _p;
 266   bool is_obj;
 267   virtual void do_space(Space* s) {
 268     if (!is_obj && s->is_in_reserved(_p)) {
 269       is_obj |= s->block_is_obj(_p);
 270     }
 271   }
 272   GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; }
 273 };
 274 
 275 bool Generation::block_is_obj(const HeapWord* p) const {
 276   GenerationBlockIsObjClosure blk(p);
 277   // Cast away const
 278   ((Generation*)this)->space_iterate(&blk);
 279   return blk.is_obj;
 280 }
 281 
 282 class GenerationOopIterateClosure : public SpaceClosure {
 283  public:
 284   ExtendedOopClosure* _cl;
 285   virtual void do_space(Space* s) {
 286     s->oop_iterate(_cl);
 287   }
 288   GenerationOopIterateClosure(ExtendedOopClosure* cl) :
 289     _cl(cl) {}
 290 };
 291 
 292 void Generation::oop_iterate(ExtendedOopClosure* cl) {
 293   GenerationOopIterateClosure blk(cl);
 294   space_iterate(&blk);
 295 }
 296 
 297 void Generation::younger_refs_in_space_iterate(Space* sp,
 298                                                OopsInGenClosure* cl) {
 299   GenRemSet* rs = GenCollectedHeap::heap()->rem_set();
 300   rs->younger_refs_in_space_iterate(sp, cl);
 301 }
 302 
 303 class GenerationObjIterateClosure : public SpaceClosure {
 304  private:
 305   ObjectClosure* _cl;
 306  public:
 307   virtual void do_space(Space* s) {
 308     s->object_iterate(_cl);
 309   }
 310   GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
 311 };
 312 
 313 void Generation::object_iterate(ObjectClosure* cl) {
 314   GenerationObjIterateClosure blk(cl);
 315   space_iterate(&blk);
 316 }
 317 
 318 class GenerationSafeObjIterateClosure : public SpaceClosure {
 319  private:
 320   ObjectClosure* _cl;
 321  public:
 322   virtual void do_space(Space* s) {
 323     s->safe_object_iterate(_cl);
 324   }
 325   GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
 326 };
 327 
 328 void Generation::safe_object_iterate(ObjectClosure* cl) {
 329   GenerationSafeObjIterateClosure blk(cl);
 330   space_iterate(&blk);
 331 }
 332 
 333 void Generation::prepare_for_compaction(CompactPoint* cp) {
 334   // Generic implementation, can be specialized
 335   CompactibleSpace* space = first_compaction_space();
 336   while (space != NULL) {
 337     space->prepare_for_compaction(cp);
 338     space = space->next_compaction_space();
 339   }
 340 }
 341 
 342 class AdjustPointersClosure: public SpaceClosure {
 343  public:
 344   void do_space(Space* sp) {
 345     sp->adjust_pointers();
 346   }
 347 };
 348 
 349 void Generation::adjust_pointers() {
 350   // Note that this is done over all spaces, not just the compactible
 351   // ones.
 352   AdjustPointersClosure blk;
 353   space_iterate(&blk, true);
 354 }
 355 
 356 void Generation::compact() {
 357   CompactibleSpace* sp = first_compaction_space();
 358   while (sp != NULL) {
 359     sp->compact();
 360     sp = sp->next_compaction_space();
 361   }
 362 }