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