1 /*
   2  * Copyright (c) 2015, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP
  26 
  27 #include "code/codeCache.hpp"
  28 #include "gc/shared/oopStorageParState.hpp"
  29 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.hpp"
  31 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  32 #include "gc/shared/strongRootsScope.hpp"
  33 #include "gc/shared/weakProcessor.hpp"
  34 #include "gc/shared/workgroup.hpp"
  35 #include "memory/allocation.hpp"
  36 #include "memory/iterator.hpp"
  37 
  38 class ShenandoahSerialRoot {
  39 public:
  40   typedef void (*OopsDo)(OopClosure*);
  41 private:
  42   volatile bool                             _claimed;
  43   const OopsDo                              _oops_do;
  44   const ShenandoahPhaseTimings::GCParPhases _phase;
  45 
  46 public:
  47   ShenandoahSerialRoot(OopsDo oops_do, ShenandoahPhaseTimings::GCParPhases);
  48   void oops_do(OopClosure* cl, uint worker_id);
  49 };
  50 
  51 class ShenandoahSerialRoots {
  52 private:
  53   ShenandoahSerialRoot  _universe_root;
  54   ShenandoahSerialRoot  _object_synchronizer_root;
  55   ShenandoahSerialRoot  _management_root;
  56   ShenandoahSerialRoot  _system_dictionary_root;
  57   ShenandoahSerialRoot  _jvmti_root;
  58 
  59   // Proxy to make weird Universe::oops_do() signature match OopsDo
  60   static void universe_oops_do(OopClosure* cl) { Universe::oops_do(cl); }
  61 
  62 public:
  63   ShenandoahSerialRoots();
  64   void oops_do(OopClosure* cl, uint worker_id);
  65 };
  66 
  67 class ShenandoahJNIHandleRoots : public ShenandoahSerialRoot {
  68 public:
  69   ShenandoahJNIHandleRoots();
  70 };
  71 
  72 class ShenandoahThreadRoots {
  73 private:
  74   const bool _is_par;
  75 public:
  76   ShenandoahThreadRoots(bool is_par);
  77   ~ShenandoahThreadRoots();
  78 
  79   void oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id);
  80   void threads_do(ThreadClosure* tc, uint worker_id);
  81 };
  82 
  83 class ShenandoahWeakRoots {
  84   OopStorage::ParState<false, false> _par_state_string;
  85   volatile bool                             _claimed;
  86 
  87 public:
  88   ShenandoahWeakRoots(uint n_workers);
  89   ~ShenandoahWeakRoots();
  90 
  91   template <typename IsAlive, typename KeepAlive>
  92   void oops_do(IsAlive* is_alive, KeepAlive* keep_alive, uint worker_id);
  93 };
  94 
  95 class ShenandoahStringDedupRoots {
  96 public:
  97   ShenandoahStringDedupRoots();
  98   ~ShenandoahStringDedupRoots();
  99 
 100   void oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id);
 101 };
 102 
 103 template <typename ITR>
 104 class ShenandoahCodeCacheRoots {
 105 private:
 106   ITR _coderoots_iterator;
 107 public:
 108   ShenandoahCodeCacheRoots();
 109   ~ShenandoahCodeCacheRoots();
 110 
 111   void code_blobs_do(CodeBlobClosure* blob_cl, uint worker_id);
 112 };
 113 
 114 template <bool SINGLE_THREADED>
 115 class ShenandoahClassLoaderDataRoots {
 116 public:
 117   ShenandoahClassLoaderDataRoots();
 118 
 119   void always_strong_cld_do(CLDClosure* clds, uint worker_id);
 120   void cld_do(CLDClosure* clds, uint worker_id);
 121 };
 122 
 123 class ShenandoahRootProcessor : public StackObj {
 124 private:
 125   ShenandoahHeap* const               _heap;
 126   const ShenandoahPhaseTimings::Phase _phase;
 127 public:
 128   ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase);
 129   ~ShenandoahRootProcessor();
 130 
 131   ShenandoahHeap* heap() const { return _heap; }
 132 };
 133 
 134 template <typename ITR>
 135 class ShenandoahRootScanner : public ShenandoahRootProcessor {
 136 private:
 137   ShenandoahSerialRoots                                     _serial_roots;
 138   ShenandoahThreadRoots                                     _thread_roots;
 139   ShenandoahCodeCacheRoots<ITR>                             _code_roots;
 140   ShenandoahJNIHandleRoots                                  _jni_roots;
 141   ShenandoahClassLoaderDataRoots<false /*single threaded*/> _cld_roots;
 142 public:
 143   ShenandoahRootScanner(uint n_workers, ShenandoahPhaseTimings::Phase phase);
 144 
 145   // Apply oops, clds and blobs to all strongly reachable roots in the system,
 146   // during class unloading cycle
 147   void strong_roots_do(uint worker_id, OopClosure* cl);
 148   void strong_roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure* tc = NULL);
 149 
 150   // Apply oops, clds and blobs to all strongly reachable roots and weakly reachable
 151   // roots when class unloading is disabled during this cycle
 152   void roots_do(uint worker_id, OopClosure* cl);
 153   void roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure* tc = NULL);
 154 };
 155 
 156 typedef ShenandoahRootScanner<ShenandoahAllCodeRootsIterator> ShenandoahAllRootScanner;
 157 typedef ShenandoahRootScanner<ShenandoahCsetCodeRootsIterator> ShenandoahCSetRootScanner;
 158 
 159 // This scanner is only for SH::object_iteration() and only supports single-threaded
 160 // root scanning
 161 class ShenandoahHeapIterationRootScanner : public ShenandoahRootProcessor {
 162 private:
 163   ShenandoahSerialRoots                                    _serial_roots;
 164   ShenandoahThreadRoots                                    _thread_roots;
 165   ShenandoahJNIHandleRoots                                 _jni_roots;
 166   ShenandoahClassLoaderDataRoots<true /*single threaded*/> _cld_roots;
 167   ShenandoahCodeCacheRoots<ShenandoahAllCodeRootsIterator> _code_roots;
 168 
 169 public:
 170   ShenandoahHeapIterationRootScanner();
 171 
 172   void roots_do(OopClosure* cl);
 173   void strong_roots_do(OopClosure* cl);
 174 };
 175 
 176 // Evacuate all roots at a safepoint
 177 class ShenandoahRootEvacuator : public ShenandoahRootProcessor {
 178 private:
 179   ShenandoahSerialRoots                                     _serial_roots;
 180   ShenandoahJNIHandleRoots                                  _jni_roots;
 181   ShenandoahClassLoaderDataRoots<false /*single threaded*/> _cld_roots;
 182   ShenandoahThreadRoots                                     _thread_roots;
 183   ShenandoahWeakRoots                                       _weak_roots;
 184   ShenandoahStringDedupRoots                                _dedup_roots;
 185   ShenandoahCodeCacheRoots<ShenandoahCsetCodeRootsIterator> _code_roots;
 186 
 187 public:
 188   ShenandoahRootEvacuator(uint n_workers, ShenandoahPhaseTimings::Phase phase);
 189 
 190   void roots_do(uint worker_id, OopClosure* oops);
 191 };
 192 
 193 // Update all roots at a safepoint
 194 class ShenandoahRootUpdater : public ShenandoahRootProcessor {
 195 private:
 196   ShenandoahSerialRoots                                     _serial_roots;
 197   ShenandoahJNIHandleRoots                                  _jni_roots;
 198   ShenandoahClassLoaderDataRoots<false /*single threaded*/> _cld_roots;
 199   ShenandoahThreadRoots                                     _thread_roots;
 200   ShenandoahWeakRoots                                       _weak_roots;
 201   ShenandoahStringDedupRoots                                _dedup_roots;
 202   ShenandoahCodeCacheRoots<ShenandoahCsetCodeRootsIterator> _code_roots;
 203   const bool                                                _update_code_cache;
 204 
 205 public:
 206   ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase, bool update_code_cache);
 207 
 208   template<typename IsAlive, typename KeepAlive>
 209   void roots_do(uint worker_id, IsAlive* is_alive, KeepAlive* keep_alive);
 210 };
 211 
 212 // Adjuster all roots at a safepoint during full gc
 213 class ShenandoahRootAdjuster : public ShenandoahRootProcessor {
 214 private:
 215   ShenandoahSerialRoots                                     _serial_roots;
 216   ShenandoahJNIHandleRoots                                  _jni_roots;
 217   ShenandoahClassLoaderDataRoots<false /*single threaded*/> _cld_roots;
 218   ShenandoahThreadRoots                                     _thread_roots;
 219   ShenandoahWeakRoots                                       _weak_roots;
 220   ShenandoahStringDedupRoots                                _dedup_roots;
 221   ShenandoahCodeCacheRoots<ShenandoahAllCodeRootsIterator>  _code_roots;
 222 
 223 public:
 224   ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase);
 225 
 226   void roots_do(uint worker_id, OopClosure* oops);
 227 };
 228 
 229 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP