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/weakProcessorPhaseTimes.hpp"
  35 #include "gc/shared/workgroup.hpp"
  36 #include "memory/allocation.hpp"
  37 #include "memory/iterator.hpp"
  38 
  39 class ShenandoahSerialRoot {
  40 public:
  41   typedef void (*OopsDo)(OopClosure*);
  42 private:
  43   volatile bool                             _claimed;
  44   const OopsDo                              _oops_do;
  45   const ShenandoahPhaseTimings::GCParPhases _phase;
  46 
  47 public:
  48   ShenandoahSerialRoot(OopsDo oops_do, ShenandoahPhaseTimings::GCParPhases);
  49   void oops_do(OopClosure* cl, uint worker_id);
  50 };
  51 
  52 class ShenandoahSerialRoots {
  53 private:
  54   ShenandoahSerialRoot  _universe_root;
  55   ShenandoahSerialRoot  _object_synchronizer_root;
  56   ShenandoahSerialRoot  _management_root;
  57   ShenandoahSerialRoot  _system_dictionary_root;
  58   ShenandoahSerialRoot  _jvmti_root;
  59   ShenandoahSerialRoot  _jni_handle_root;
  60 public:
  61   ShenandoahSerialRoots();
  62   void oops_do(OopClosure* cl, uint worker_id);
  63 };
  64 
  65 class ShenandoahThreadRoots {
  66 private:
  67   const bool _is_par;
  68 public:
  69   ShenandoahThreadRoots(bool is_par);
  70   ~ShenandoahThreadRoots();
  71 
  72   void oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id);
  73   void threads_do(ThreadClosure* tc, uint worker_id);
  74 };
  75 
  76 class ShenandoahWeakRoots {
  77 private:
  78   WeakProcessorPhaseTimes _process_timings;
  79   WeakProcessor::Task     _task;
  80 public:
  81   ShenandoahWeakRoots(uint n_workers);
  82   ~ShenandoahWeakRoots();
  83 
  84   template <typename IsAlive, typename KeepAlive>
  85   void oops_do(IsAlive* is_alive, KeepAlive* keep_alive, uint worker_id);
  86 };
  87 
  88 class ShenandoahStringDedupRoots {
  89 public:
  90   ShenandoahStringDedupRoots();
  91   ~ShenandoahStringDedupRoots();
  92 
  93   void oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id);
  94 };
  95 
  96 template <typename ITR>
  97 class ShenandoahCodeCacheRoots {
  98 private:
  99   ITR _coderoots_iterator;
 100 public:
 101   ShenandoahCodeCacheRoots();
 102   ~ShenandoahCodeCacheRoots();
 103 
 104   void code_blobs_do(CodeBlobClosure* blob_cl, uint worker_id);
 105 };
 106 
 107 class ShenandoahClassLoaderDataRoots {
 108 public:
 109   ShenandoahClassLoaderDataRoots();
 110 
 111   void clds_do(CLDClosure* strong_clds, CLDClosure* weak_clds, uint worker_id);
 112 };
 113 
 114 class ShenandoahRootProcessor : public StackObj {
 115 private:
 116   ShenandoahHeap* const               _heap;
 117   const ShenandoahPhaseTimings::Phase _phase;
 118 public:
 119   ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase);
 120   ~ShenandoahRootProcessor();
 121 
 122   ShenandoahHeap* heap() const { return _heap; }
 123 };
 124 
 125 template <typename ITR>
 126 class ShenandoahRootScanner : public ShenandoahRootProcessor {
 127 private:
 128   ShenandoahSerialRoots          _serial_roots;
 129   ShenandoahClassLoaderDataRoots _cld_roots;
 130   ShenandoahThreadRoots          _thread_roots;
 131   ShenandoahCodeCacheRoots<ITR>  _code_roots;
 132 public:
 133   ShenandoahRootScanner(uint n_workers, ShenandoahPhaseTimings::Phase phase);
 134 
 135   // Apply oops, clds and blobs to all strongly reachable roots in the system,
 136   // during class unloading cycle
 137   void strong_roots_do(uint worker_id, OopClosure* cl);
 138   void strong_roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure* tc = NULL);
 139 
 140   // Apply oops, clds and blobs to all strongly reachable roots and weakly reachable
 141   // roots when class unloading is disabled during this cycle
 142   void roots_do(uint worker_id, OopClosure* cl);
 143   void roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure* tc = NULL);
 144 };
 145 
 146 typedef ShenandoahRootScanner<ShenandoahAllCodeRootsIterator> ShenandoahAllRootScanner;
 147 typedef ShenandoahRootScanner<ShenandoahCsetCodeRootsIterator> ShenandoahCSetRootScanner;
 148 
 149 // Evacuate all roots at a safepoint
 150 class ShenandoahRootEvacuator : public ShenandoahRootProcessor {
 151 private:
 152   ShenandoahSerialRoots          _serial_roots;
 153   ShenandoahClassLoaderDataRoots _cld_roots;
 154   ShenandoahThreadRoots          _thread_roots;
 155   ShenandoahWeakRoots            _weak_roots;
 156   ShenandoahStringDedupRoots     _dedup_roots;
 157   ShenandoahCodeCacheRoots<ShenandoahCsetCodeRootsIterator> _code_roots;
 158 
 159 public:
 160   ShenandoahRootEvacuator(uint n_workers, ShenandoahPhaseTimings::Phase phase);
 161 
 162   void roots_do(uint worker_id, OopClosure* oops);
 163 };
 164 
 165 // Update all roots at a safepoint
 166 class ShenandoahRootUpdater : public ShenandoahRootProcessor {
 167 private:
 168   ShenandoahSerialRoots          _serial_roots;
 169   ShenandoahClassLoaderDataRoots _cld_roots;
 170   ShenandoahThreadRoots          _thread_roots;
 171   ShenandoahWeakRoots            _weak_roots;
 172   ShenandoahStringDedupRoots     _dedup_roots;
 173   ShenandoahCodeCacheRoots<ShenandoahCsetCodeRootsIterator> _code_roots;
 174   const bool                     _update_code_cache;
 175 
 176 public:
 177   ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase, bool update_code_cache);
 178 
 179   template<typename IsAlive, typename KeepAlive>
 180   void roots_do(uint worker_id, IsAlive* is_alive, KeepAlive* keep_alive);
 181 };
 182 
 183 // Adjuster all roots at a safepoint during full gc
 184 class ShenandoahRootAdjuster : public ShenandoahRootProcessor {
 185 private:
 186   ShenandoahSerialRoots          _serial_roots;
 187   ShenandoahClassLoaderDataRoots _cld_roots;
 188   ShenandoahThreadRoots          _thread_roots;
 189   ShenandoahWeakRoots            _weak_roots;
 190   ShenandoahStringDedupRoots     _dedup_roots;
 191   ShenandoahCodeCacheRoots<ShenandoahAllCodeRootsIterator> _code_roots;
 192 
 193 public:
 194   ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase);
 195 
 196   void roots_do(uint worker_id, OopClosure* oops);
 197 };
 198 
 199 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP