1 /*
   2  * Copyright (c) 2017, 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_SHENANDOAHCODEROOTS_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHCODEROOTS_HPP
  26 
  27 #include "code/codeCache.hpp"
  28 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
  29 #include "gc/shenandoah/shenandoahLock.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/iterator.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 class ShenandoahHeap;
  35 class ShenandoahHeapRegion;
  36 
  37 class ShenandoahParallelCodeHeapIterator {
  38   friend class CodeCache;
  39 private:
  40   CodeHeap*     _heap;
  41   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int));
  42   volatile int  _claimed_idx;
  43   volatile bool _finished;
  44   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
  45 public:
  46   ShenandoahParallelCodeHeapIterator(CodeHeap* heap);
  47   void parallel_blobs_do(CodeBlobClosure* f);
  48 };
  49 
  50 class ShenandoahParallelCodeCacheIterator {
  51   friend class CodeCache;
  52 private:
  53   ShenandoahParallelCodeHeapIterator* _iters;
  54   int                       _length;
  55 
  56   NONCOPYABLE(ShenandoahParallelCodeCacheIterator);
  57 
  58 public:
  59   ShenandoahParallelCodeCacheIterator(const GrowableArray<CodeHeap*>* heaps);
  60   ~ShenandoahParallelCodeCacheIterator();
  61   void parallel_blobs_do(CodeBlobClosure* f);
  62 };
  63 
  64 // ShenandoahNMethod tuple records the internal locations of oop slots within the nmethod.
  65 // This allows us to quickly scan the oops without doing the nmethod-internal scans, that
  66 // sometimes involves parsing the machine code. Note it does not record the oops themselves,
  67 // because it would then require handling these tuples as the new class of roots.
  68 class ShenandoahNMethod : public CHeapObj<mtGC> {
  69 private:
  70   nmethod* _nm;
  71   oop**    _oops;
  72   int      _oops_count;
  73 
  74 public:
  75   ShenandoahNMethod(nmethod *nm, GrowableArray<oop*>* oops);
  76   ~ShenandoahNMethod();
  77 
  78   nmethod* nm() {
  79     return _nm;
  80   }
  81 
  82   bool has_cset_oops(ShenandoahHeap* heap);
  83 
  84   void assert_alive_and_correct() NOT_DEBUG_RETURN;
  85   void assert_same_oops(GrowableArray<oop*>* oops) NOT_DEBUG_RETURN;
  86 
  87   static bool find_with_nmethod(void* nm, ShenandoahNMethod* other) {
  88     return other->_nm == nm;
  89   }
  90 };
  91 
  92 class ShenandoahCodeRootsIterator {
  93   friend class ShenandoahCodeRoots;
  94 protected:
  95   ShenandoahHeap* _heap;
  96   ShenandoahParallelCodeCacheIterator _par_iterator;
  97   ShenandoahSharedFlag _seq_claimed;
  98   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  99   volatile size_t _claimed;
 100   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
 101 protected:
 102   ShenandoahCodeRootsIterator();
 103   ~ShenandoahCodeRootsIterator();
 104 
 105   template<bool CSET_FILTER>
 106   void dispatch_parallel_blobs_do(CodeBlobClosure *f);
 107 
 108   template<bool CSET_FILTER>
 109   void fast_parallel_blobs_do(CodeBlobClosure *f);
 110 };
 111 
 112 class ShenandoahAllCodeRootsIterator : public ShenandoahCodeRootsIterator {
 113 public:
 114   ShenandoahAllCodeRootsIterator() : ShenandoahCodeRootsIterator() {};
 115   void possibly_parallel_blobs_do(CodeBlobClosure *f);
 116 };
 117 
 118 class ShenandoahCsetCodeRootsIterator : public ShenandoahCodeRootsIterator {
 119 public:
 120   ShenandoahCsetCodeRootsIterator() : ShenandoahCodeRootsIterator() {};
 121   void possibly_parallel_blobs_do(CodeBlobClosure* f);
 122 };
 123 
 124 class ShenandoahCodeRoots : public AllStatic {
 125   friend class ShenandoahHeap;
 126   friend class ShenandoahCodeRootsIterator;
 127 
 128 public:
 129   static void initialize();
 130   static void add_nmethod(nmethod* nm);
 131   static void remove_nmethod(nmethod* nm);
 132 
 133 private:
 134   static GrowableArray<ShenandoahNMethod*>* _recorded_nms;
 135   static ShenandoahLock                     _recorded_nms_lock;
 136 };
 137 
 138 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCODEROOTS_HPP