1 /*
   2  * Copyright (c) 2015, 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 #ifndef SHARE_GC_Z_ZROOTSITERATOR_HPP
  25 #define SHARE_GC_Z_ZROOTSITERATOR_HPP
  26 
  27 #include "gc/shared/oopStorageParState.hpp"
  28 #include "gc/shared/suspendibleThreadSet.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/iterator.hpp"
  31 #include "runtime/thread.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 class ZRootsIteratorClosure;
  35 
  36 typedef OopStorage::ParState<true /* concurrent */, false /* is_const */> ZOopStorageIterator;
  37 
  38 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
  39 class ZSerialOopsDo {
  40 private:
  41   T* const      _iter;
  42   volatile bool _claimed;
  43 
  44 public:
  45   ZSerialOopsDo(T* iter);
  46   void oops_do(ZRootsIteratorClosure* cl);
  47 };
  48 
  49 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
  50 class ZParallelOopsDo {
  51 private:
  52   T* const      _iter;
  53   volatile bool _completed;
  54 
  55 public:
  56   ZParallelOopsDo(T* iter);
  57   void oops_do(ZRootsIteratorClosure* cl);
  58 };
  59 
  60 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
  61 class ZSerialWeakOopsDo {
  62 private:
  63   T* const      _iter;
  64   volatile bool _claimed;
  65 
  66 public:
  67   ZSerialWeakOopsDo(T* iter);
  68   void weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
  69 };
  70 
  71 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
  72 class ZParallelWeakOopsDo {
  73 private:
  74   T* const      _iter;
  75   volatile bool _completed;
  76 
  77 public:
  78   ZParallelWeakOopsDo(T* iter);
  79   void weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
  80 };
  81 
  82 class ZRootsIteratorClosure : public OopClosure {
  83 public:
  84   virtual void do_thread(Thread* thread) {}
  85  
  86   virtual bool should_disarm_nmethods() const {
  87     return false;
  88   }
  89 };
  90 
  91 class ZRootsIterator {
  92 private:
  93   const bool _visit_jvmti_weak_export;
  94 
  95   void do_universe(ZRootsIteratorClosure* cl);
  96   void do_object_synchronizer(ZRootsIteratorClosure* cl);
  97   void do_management(ZRootsIteratorClosure* cl);
  98   void do_jvmti_export(ZRootsIteratorClosure* cl);
  99   void do_jvmti_weak_export(ZRootsIteratorClosure* cl);
 100   void do_system_dictionary(ZRootsIteratorClosure* cl);
 101   void do_threads(ZRootsIteratorClosure* cl);
 102   void do_code_cache(ZRootsIteratorClosure* cl);
 103 
 104   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_universe>            _universe;
 105   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_object_synchronizer> _object_synchronizer;
 106   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_management>          _management;
 107   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_jvmti_export>        _jvmti_export;
 108   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_jvmti_weak_export>   _jvmti_weak_export;
 109   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_system_dictionary>   _system_dictionary;
 110   ZParallelOopsDo<ZRootsIterator, &ZRootsIterator::do_threads>           _threads;
 111   ZParallelOopsDo<ZRootsIterator, &ZRootsIterator::do_code_cache>        _code_cache;
 112 
 113 public:
 114   ZRootsIterator(bool visit_jvmti_weak_export = false);
 115   ~ZRootsIterator();
 116 
 117   void oops_do(ZRootsIteratorClosure* cl);
 118 };
 119 
 120 class ZConcurrentRootsIterator {
 121 private:
 122   ZOopStorageIterator _jni_handles_iter;
 123   ZOopStorageIterator _vm_handles_iter;
 124   const int           _cld_claim;
 125 
 126   void do_jni_handles(ZRootsIteratorClosure* cl);
 127   void do_vm_handles(ZRootsIteratorClosure* cl);
 128   void do_class_loader_data_graph(ZRootsIteratorClosure* cl);
 129 
 130   ZParallelOopsDo<ZConcurrentRootsIterator, &ZConcurrentRootsIterator::do_jni_handles>             _jni_handles;
 131   ZParallelOopsDo<ZConcurrentRootsIterator, &ZConcurrentRootsIterator::do_vm_handles>              _vm_handles;
 132   ZParallelOopsDo<ZConcurrentRootsIterator, &ZConcurrentRootsIterator::do_class_loader_data_graph> _class_loader_data_graph;
 133 
 134 public:
 135   ZConcurrentRootsIterator(int cld_claim);
 136   ~ZConcurrentRootsIterator();
 137 
 138   void oops_do(ZRootsIteratorClosure* cl);
 139 };
 140 
 141 class ZConcurrentRootsIteratorClaimStrong : public ZConcurrentRootsIterator {
 142 public:
 143   ZConcurrentRootsIteratorClaimStrong() :
 144       ZConcurrentRootsIterator(ClassLoaderData::_claim_strong) {}
 145 };
 146 
 147 class ZConcurrentRootsIteratorClaimOther : public ZConcurrentRootsIterator {
 148 public:
 149   ZConcurrentRootsIteratorClaimOther() :
 150       ZConcurrentRootsIterator(ClassLoaderData::_claim_other) {}
 151 };
 152 
 153 class ZConcurrentRootsIteratorClaimNone : public ZConcurrentRootsIterator {
 154 public:
 155   ZConcurrentRootsIteratorClaimNone() :
 156       ZConcurrentRootsIterator(ClassLoaderData::_claim_none) {}
 157 };
 158 
 159 class ZWeakRootsIterator {
 160 private:
 161   void do_jvmti_weak_export(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
 162   void do_jfr_weak(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
 163 
 164   ZSerialWeakOopsDo<ZWeakRootsIterator, &ZWeakRootsIterator::do_jvmti_weak_export> _jvmti_weak_export;
 165   ZSerialWeakOopsDo<ZWeakRootsIterator, &ZWeakRootsIterator::do_jfr_weak>          _jfr_weak;
 166 
 167 public:
 168   ZWeakRootsIterator();
 169   ~ZWeakRootsIterator();
 170 
 171   void weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
 172   void oops_do(ZRootsIteratorClosure* cl);
 173 };
 174 
 175 class ZConcurrentWeakRootsIterator {
 176 private:
 177   ZOopStorageIterator _vm_weak_handles_iter;
 178   ZOopStorageIterator _jni_weak_handles_iter;
 179   ZOopStorageIterator _string_table_iter;
 180   ZOopStorageIterator _resolved_method_table_iter;
 181 
 182   void do_vm_weak_handles(ZRootsIteratorClosure* cl);
 183   void do_jni_weak_handles(ZRootsIteratorClosure* cl);
 184   void do_string_table(ZRootsIteratorClosure* cl);
 185   void do_resolved_method_table(ZRootsIteratorClosure* cl);
 186 
 187   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_vm_weak_handles>       _vm_weak_handles;
 188   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_jni_weak_handles>      _jni_weak_handles;
 189   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_string_table>          _string_table;
 190   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_resolved_method_table> _resolved_method_table;
 191 
 192 public:
 193   ZConcurrentWeakRootsIterator();
 194   ~ZConcurrentWeakRootsIterator();
 195 
 196   void oops_do(ZRootsIteratorClosure* cl);
 197 };
 198 
 199 #endif // SHARE_GC_Z_ZROOTSITERATOR_HPP