1 /*
   2  * Copyright (c) 2002, 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 #ifndef SHARE_VM_GC_PARALLEL_PSSCAVENGE_INLINE_HPP
  26 #define SHARE_VM_GC_PARALLEL_PSSCAVENGE_INLINE_HPP
  27 
  28 #include "gc/parallel/cardTableExtension.hpp"
  29 #include "gc/parallel/parallelScavengeHeap.hpp"
  30 #include "gc/parallel/psPromotionManager.inline.hpp"
  31 #include "gc/parallel/psScavenge.hpp"
  32 #include "logging/log.hpp"
  33 #include "memory/iterator.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 
  36 inline void PSScavenge::save_to_space_top_before_gc() {
  37   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  38   _to_space_top_before_gc = heap->young_gen()->to_space()->top();
  39 }
  40 
  41 // Adaptive size policy support.  When the young generation/old generation
  42 // boundary moves, _young_generation_boundary must be reset
  43 void PSScavenge::set_young_generation_boundary(HeapWord* v) {
  44   _young_generation_boundary = v;
  45   if (UseCompressedOops) {
  46     _young_generation_boundary_compressed = (uintptr_t)oopDesc::encode_heap_oop((oop)v);
  47   }
  48 }
  49 
  50 template <class T> inline bool PSScavenge::should_scavenge(T* p) {
  51   T heap_oop = oopDesc::load_heap_oop(p);
  52   return PSScavenge::is_obj_in_young(heap_oop);
  53 }
  54 
  55 template <class T>
  56 inline bool PSScavenge::should_scavenge(T* p, MutableSpace* to_space) {
  57   if (should_scavenge(p)) {
  58     oop obj = oopDesc::load_decode_heap_oop_not_null(p);
  59     // Skip objects copied to to_space since the scavenge started.
  60     HeapWord* const addr = (HeapWord*)obj;
  61     return addr < to_space_top_before_gc() || addr >= to_space->end();
  62   }
  63   return false;
  64 }
  65 
  66 template <class T>
  67 inline bool PSScavenge::should_scavenge(T* p, bool check_to_space) {
  68   if (check_to_space) {
  69     ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  70     return should_scavenge(p, heap->young_gen()->to_space());
  71   }
  72   return should_scavenge(p);
  73 }
  74 
  75 template<bool promote_immediately>
  76 class PSRootsClosure: public OopClosure {
  77  private:
  78   PSPromotionManager* _promotion_manager;
  79 
  80  protected:
  81   template <class T> void do_oop_work(T *p) {
  82     if (PSScavenge::should_scavenge(p)) {
  83       // We never card mark roots, maybe call a func without test?
  84       _promotion_manager->copy_and_push_safe_barrier<T, promote_immediately>(p);
  85     }
  86   }
  87  public:
  88   PSRootsClosure(PSPromotionManager* pm) : _promotion_manager(pm) { }
  89   void do_oop(oop* p)       { PSRootsClosure::do_oop_work(p); }
  90   void do_oop(narrowOop* p) { PSRootsClosure::do_oop_work(p); }
  91 };
  92 
  93 typedef PSRootsClosure</*promote_immediately=*/false> PSScavengeRootsClosure;
  94 typedef PSRootsClosure</*promote_immediately=*/true> PSPromoteRootsClosure;
  95 
  96 // Scavenges a single oop in a Klass.
  97 class PSScavengeFromKlassClosure: public OopClosure {
  98  private:
  99   PSPromotionManager* _pm;
 100   // Used to redirty a scanned klass if it has oops
 101   // pointing to the young generation after being scanned.
 102   Klass*             _scanned_klass;
 103  public:
 104   PSScavengeFromKlassClosure(PSPromotionManager* pm) : _pm(pm), _scanned_klass(NULL) { }
 105   void do_oop(narrowOop* p) { ShouldNotReachHere(); }
 106   void do_oop(oop* p)       {
 107     ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
 108     assert(!psh->is_in_reserved(p), "GC barrier needed");
 109     if (PSScavenge::should_scavenge(p)) {
 110       assert(PSScavenge::should_scavenge(p, true), "revisiting object?");
 111 
 112       oop o = *p;
 113       oop new_obj;
 114       if (o->is_forwarded()) {
 115         new_obj = o->forwardee();
 116       } else {
 117         new_obj = _pm->copy_to_survivor_space</*promote_immediately=*/false>(o);
 118       }
 119       oopDesc::encode_store_heap_oop_not_null(p, new_obj);
 120 
 121       if (PSScavenge::is_obj_in_young(new_obj)) {
 122         do_klass_barrier();
 123       }
 124     }
 125   }
 126 
 127   void set_scanned_klass(Klass* klass) {
 128     assert(_scanned_klass == NULL || klass == NULL, "Should always only handling one klass at a time");
 129     _scanned_klass = klass;
 130   }
 131 
 132  private:
 133   void do_klass_barrier() {
 134     assert(_scanned_klass != NULL, "Should not be called without having a scanned klass");
 135     _scanned_klass->record_modified_oops();
 136   }
 137 
 138 };
 139 
 140 // Scavenges the oop in a Klass.
 141 class PSScavengeKlassClosure: public KlassClosure {
 142  private:
 143   PSScavengeFromKlassClosure _oop_closure;
 144  protected:
 145  public:
 146   PSScavengeKlassClosure(PSPromotionManager* pm) : _oop_closure(pm) { }
 147   void do_klass(Klass* klass) {
 148     // If the klass has not been dirtied we know that there's
 149     // no references into  the young gen and we can skip it.
 150 
 151     NOT_PRODUCT(ResourceMark rm);
 152     log_develop_trace(gc, scavenge)("PSScavengeKlassClosure::do_klass " PTR_FORMAT ", %s, dirty: %s",
 153                                     p2i(klass),
 154                                     klass->external_name(),
 155                                     klass->has_modified_oops() ? "true" : "false");
 156 
 157     if (klass->has_modified_oops()) {
 158       // Clean the klass since we're going to scavenge all the metadata.
 159       klass->clear_modified_oops();
 160 
 161       // Setup the promotion manager to redirty this klass
 162       // if references are left in the young gen.
 163       _oop_closure.set_scanned_klass(klass);
 164 
 165       klass->oops_do(&_oop_closure);
 166 
 167       _oop_closure.set_scanned_klass(NULL);
 168     }
 169   }
 170 };
 171 
 172 #endif // SHARE_VM_GC_PARALLEL_PSSCAVENGE_INLINE_HPP