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