1 /*
   2  * Copyright (c) 2010, 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 #ifndef SHARE_GC_PARALLEL_PSCOMPACTIONMANAGER_INLINE_HPP
  26 #define SHARE_GC_PARALLEL_PSCOMPACTIONMANAGER_INLINE_HPP
  27 
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "gc/parallel/parMarkBitMap.hpp"
  30 #include "gc/parallel/psCompactionManager.hpp"
  31 #include "gc/parallel/psParallelCompact.inline.hpp"
  32 #include "gc/shared/taskqueue.inline.hpp"
  33 #include "oops/access.inline.hpp"
  34 #include "oops/arrayOop.inline.hpp"
  35 #include "oops/compressedOops.inline.hpp"
  36 #include "oops/objArrayOop.inline.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "utilities/debug.hpp"
  39 #include "utilities/globalDefinitions.hpp"
  40 
  41 class PCMarkAndPushClosure: public OopClosure {
  42 private:
  43   ParCompactionManager* _compaction_manager;
  44 public:
  45   PCMarkAndPushClosure(ParCompactionManager* cm) : _compaction_manager(cm) { }
  46 
  47   template <typename T> void do_oop_nv(T* p)      { _compaction_manager->mark_and_push(p); }
  48   virtual void do_oop(oop* p)                     { do_oop_nv(p); }
  49   virtual void do_oop(narrowOop* p)               { do_oop_nv(p); }
  50 
  51   // This closure provides its own oop verification code.
  52   debug_only(virtual bool should_verify_oops()    { return false; })
  53 };
  54 
  55 class PCIterateMarkAndPushClosure: public MetadataVisitingOopIterateClosure {
  56 private:
  57   ParCompactionManager* _compaction_manager;
  58 public:
  59   PCIterateMarkAndPushClosure(ParCompactionManager* cm, ReferenceProcessor* rp) : MetadataVisitingOopIterateClosure(rp), _compaction_manager(cm) { }
  60 
  61   template <typename T> void do_oop_nv(T* p)      { _compaction_manager->mark_and_push(p); }
  62   virtual void do_oop(oop* p)                     { do_oop_nv(p); }
  63   virtual void do_oop(narrowOop* p)               { do_oop_nv(p); }
  64 
  65   void do_klass_nv(Klass* k)                      { _compaction_manager->follow_klass(k); }
  66   void do_cld_nv(ClassLoaderData* cld)            { _compaction_manager->follow_class_loader(cld); }
  67 
  68   // This closure provides its own oop verification code.
  69   debug_only(virtual bool should_verify_oops()    { return false; })
  70 };
  71 
  72 inline bool ParCompactionManager::steal(int queue_num, oop& t) {
  73   return oop_task_queues()->steal(queue_num, t);
  74 }
  75 
  76 inline bool ParCompactionManager::steal_objarray(int queue_num, ObjArrayTask& t) {
  77   return _objarray_task_queues->steal(queue_num, t);
  78 }
  79 
  80 inline bool ParCompactionManager::steal(int queue_num, size_t& region) {
  81   return region_task_queues()->steal(queue_num, region);
  82 }
  83 
  84 inline void ParCompactionManager::push(oop obj) {
  85   _marking_stack.push(obj);
  86 }
  87 
  88 void ParCompactionManager::push_objarray(oop obj, size_t index)
  89 {
  90   ObjArrayTask task(obj, index);
  91   assert(task.is_valid(), "bad ObjArrayTask");
  92   _objarray_stack.push(task);
  93 }
  94 
  95 void ParCompactionManager::push_region(size_t index)
  96 {
  97 #ifdef ASSERT
  98   const ParallelCompactData& sd = PSParallelCompact::summary_data();
  99   ParallelCompactData::RegionData* const region_ptr = sd.region(index);
 100   assert(region_ptr->claimed(), "must be claimed");
 101   assert(region_ptr->_pushed++ == 0, "should only be pushed once");
 102 #endif
 103   region_stack()->push(index);
 104 }
 105 
 106 template <typename T>
 107 inline void ParCompactionManager::mark_and_push(T* p) {
 108   T heap_oop = RawAccess<>::oop_load(p);
 109   if (!CompressedOops::is_null(heap_oop)) {
 110     oop obj = CompressedOops::decode_not_null(heap_oop);
 111     assert(ParallelScavengeHeap::heap()->is_in(obj), "should be in heap");
 112 
 113     if (mark_bitmap()->is_unmarked(obj) && PSParallelCompact::mark_obj(obj)) {
 114       push(obj);
 115     }
 116   }
 117 }
 118 
 119 inline void ParCompactionManager::follow_klass(Klass* klass) {
 120   oop holder = klass->class_loader_data()->holder_no_keepalive();
 121   mark_and_push(&holder);
 122 }
 123 
 124 inline void ParCompactionManager::FollowStackClosure::do_void() {
 125   _compaction_manager->follow_marking_stacks();
 126 }
 127 
 128 template <typename T>
 129 inline void follow_array_specialized(objArrayOop obj, int index, ParCompactionManager* cm) {
 130   const size_t len = size_t(obj->length());
 131   const size_t beg_index = size_t(index);
 132   assert(beg_index < len || len == 0, "index too large");
 133 
 134   const size_t stride = MIN2(len - beg_index, (size_t)ObjArrayMarkingStride);
 135   const size_t end_index = beg_index + stride;
 136   T* const base = (T*)obj->base_raw();
 137   T* const beg = base + beg_index;
 138   T* const end = base + end_index;
 139 
 140   if (end_index < len) {
 141     cm->push_objarray(obj, end_index); // Push the continuation.
 142   }
 143 
 144   // Push the non-NULL elements of the next stride on the marking stack.
 145   for (T* e = beg; e < end; e++) {
 146     cm->mark_and_push<T>(e);
 147   }
 148 }
 149 
 150 inline void ParCompactionManager::follow_array(objArrayOop obj, int index) {
 151   if (UseCompressedOops) {
 152     follow_array_specialized<narrowOop>(obj, index, this);
 153   } else {
 154     follow_array_specialized<oop>(obj, index, this);
 155   }
 156 }
 157 
 158 inline void ParCompactionManager::update_contents(oop obj) {
 159   if (!obj->klass()->is_typeArray_klass()) {
 160     PCAdjustPointerClosure apc(this);
 161     obj->oop_iterate(&apc);
 162   }
 163 }
 164 
 165 inline void ParCompactionManager::follow_class_loader(ClassLoaderData* cld) {
 166   PCMarkAndPushClosure mark_and_push_closure(this);
 167   cld->oops_do(&mark_and_push_closure, true);
 168 }
 169 
 170 inline void ParCompactionManager::follow_contents(oop obj) {
 171   assert(PSParallelCompact::mark_bitmap()->is_marked(obj), "should be marked");
 172   if (obj->is_objArray()) {
 173     follow_array(objArrayOop(obj), 0);
 174   } else {
 175     PCIterateMarkAndPushClosure cl(this, PSParallelCompact::ref_processor());
 176     obj->oop_iterate(&cl);
 177   }
 178 }
 179 
 180 #endif // SHARE_GC_PARALLEL_PSCOMPACTIONMANAGER_INLINE_HPP