1 /*
  2  * Copyright (c) 2015, 2018, 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 : public OopClosure, public ThreadClosure {
 35 public:
 36   virtual void do_thread(Thread* thread) {
 37     thread->oops_do(this, NULL);
 38   }
 39 };
 40 
 41 typedef OopStorage::ParState<true /* concurrent */, false /* is_const */> ZOopStorageIterator;
 42 
 43 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
 44 class ZSerialOopsDo {
 45 private:
 46   T* const      _iter;
 47   volatile bool _claimed;
 48 
 49 public:
 50   ZSerialOopsDo(T* iter);
 51   void oops_do(ZRootsIteratorClosure* cl);
 52 };
 53 
 54 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
 55 class ZParallelOopsDo {
 56 private:
 57   T* const      _iter;
 58   volatile bool _completed;
 59 
 60 public:
 61   ZParallelOopsDo(T* iter);
 62   void oops_do(ZRootsIteratorClosure* cl);
 63 };
 64 
 65 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
 66 class ZSerialWeakOopsDo {
 67 private:
 68   T* const      _iter;
 69   volatile bool _claimed;
 70 
 71 public:
 72   ZSerialWeakOopsDo(T* iter);
 73   void weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
 74 };
 75 
 76 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
 77 class ZParallelWeakOopsDo {
 78 private:
 79   T* const      _iter;
 80   volatile bool _completed;
 81 
 82 public:
 83   ZParallelWeakOopsDo(T* iter);
 84   void weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
 85 };
 86 
 87 class ZRootsIterator {
 88 private:
 89   void do_universe(ZRootsIteratorClosure* cl);
 90   void do_object_synchronizer(ZRootsIteratorClosure* cl);
 91   void do_management(ZRootsIteratorClosure* cl);
 92   void do_jvmti_export(ZRootsIteratorClosure* cl);
 93   void do_jvmti_weak_export(ZRootsIteratorClosure* cl);
 94   void do_system_dictionary(ZRootsIteratorClosure* cl);
 95   void do_threads(ZRootsIteratorClosure* cl);
 96   void do_code_cache(ZRootsIteratorClosure* cl);
 97 
 98   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_universe>            _universe;
 99   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_object_synchronizer> _object_synchronizer;
100   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_management>          _management;
101   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_jvmti_export>        _jvmti_export;
102   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_jvmti_weak_export>   _jvmti_weak_export;
103   ZSerialOopsDo<ZRootsIterator, &ZRootsIterator::do_system_dictionary>   _system_dictionary;
104   ZParallelOopsDo<ZRootsIterator, &ZRootsIterator::do_threads>           _threads;
105   ZParallelOopsDo<ZRootsIterator, &ZRootsIterator::do_code_cache>        _code_cache;
106 
107 public:
108   ZRootsIterator();
109   ~ZRootsIterator();
110 
111   void oops_do(ZRootsIteratorClosure* cl, bool visit_jvmti_weak_export = false);
112 };
113 
114 class ZConcurrentRootsIterator {
115 private:
116   bool                       _marking;
117   SuspendibleThreadSetJoiner _sts_joiner;
118   ZOopStorageIterator        _jni_handles_iter;
119 
120   void do_jni_handles(ZRootsIteratorClosure* cl);
121   void do_class_loader_data_graph(ZRootsIteratorClosure* cl);
122 
123   ZParallelOopsDo<ZConcurrentRootsIterator, &ZConcurrentRootsIterator::do_jni_handles>             _jni_handles;
124   ZParallelOopsDo<ZConcurrentRootsIterator, &ZConcurrentRootsIterator::do_class_loader_data_graph> _class_loader_data_graph;
125 
126 public:
127   ZConcurrentRootsIterator(bool claim_clds);
128   ~ZConcurrentRootsIterator();
129 
130   void oops_do(ZRootsIteratorClosure* cl);
131 };
132 
133 class ZWeakRootsIterator {
134 private:
135   void do_jvmti_weak_export(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
136   void do_jfr_weak(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
137 
138   ZSerialWeakOopsDo<ZWeakRootsIterator, &ZWeakRootsIterator::do_jvmti_weak_export>  _jvmti_weak_export;
139   ZSerialWeakOopsDo<ZWeakRootsIterator, &ZWeakRootsIterator::do_jfr_weak>           _jfr_weak;
140 
141 public:
142   ZWeakRootsIterator();
143   ~ZWeakRootsIterator();
144 
145   void weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl);
146   void oops_do(ZRootsIteratorClosure* cl);
147 };
148 
149 class ZConcurrentWeakRootsIterator {
150 private:
151   ZOopStorageIterator _vm_weak_handles_iter;
152   ZOopStorageIterator _jni_weak_handles_iter;
153   ZOopStorageIterator _string_table_iter;
154 
155   void do_vm_weak_handles(ZRootsIteratorClosure* cl);
156   void do_jni_weak_handles(ZRootsIteratorClosure* cl);
157   void do_string_table(ZRootsIteratorClosure* cl);
158 
159   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_vm_weak_handles>  _vm_weak_handles;
160   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_jni_weak_handles> _jni_weak_handles;
161   ZParallelOopsDo<ZConcurrentWeakRootsIterator, &ZConcurrentWeakRootsIterator::do_string_table>     _string_table;
162 
163 public:
164   ZConcurrentWeakRootsIterator();
165   ~ZConcurrentWeakRootsIterator();
166 
167   void oops_do(ZRootsIteratorClosure* cl);
168 };
169 
170 class ZThreadRootsIterator {
171 private:
172   void do_threads(ZRootsIteratorClosure* cl);
173 
174   ZParallelOopsDo<ZThreadRootsIterator, &ZThreadRootsIterator::do_threads> _threads;
175 
176 public:
177   ZThreadRootsIterator();
178   ~ZThreadRootsIterator();
179 
180   void oops_do(ZRootsIteratorClosure* cl);
181 };
182 
183 #endif // SHARE_GC_Z_ZROOTSITERATOR_HPP