1 /*
2 * Copyright (c) 2019, 2020, Red Hat, Inc. 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
25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP
27
28 #include "code/nmethod.hpp"
29 #include "gc/shenandoah/shenandoahHeap.hpp"
30 #include "gc/shenandoah/shenandoahLock.hpp"
31 #include "gc/shenandoah/shenandoahPadding.hpp"
32 #include "memory/allocation.hpp"
33 #include "runtime/atomic.hpp"
34 #include "utilities/growableArray.hpp"
35
36 // Use ShenandoahReentrantLock as ShenandoahNMethodLock
37 typedef ShenandoahReentrantLock<ShenandoahSimpleLock> ShenandoahNMethodLock;
38 typedef ShenandoahLocker<ShenandoahNMethodLock> ShenandoahNMethodLocker;
39
40 struct ShenandoahNMethodBarrier {
41 address _pc;
42 address _stub_addr;
43 int _index;
44 };
45
46 // ShenandoahNMethod tuple records the internal locations of oop slots within reclocation stream in
47 // the nmethod. This allows us to quickly scan the oops without doing the nmethod-internal scans,
48 // that sometimes involves parsing the machine code. Note it does not record the oops themselves,
49 // because it would then require handling these tuples as the new class of roots.
50 class ShenandoahNMethod : public CHeapObj<mtGC> {
51 private:
52 nmethod* const _nm;
53 oop** _oops;
54 int _oops_count;
55 ShenandoahNMethodBarrier* _barriers;
56 int _barriers_count;
57 bool _has_non_immed_oops;
58 bool _unregistered;
59 ShenandoahNMethodLock _lock;
60 ShenandoahNMethodLock _ic_lock;
61
62 enum ShenandoahNMethodGCState {
63 FORWARDED = ShenandoahHeap::HAS_FORWARDED,
64 MARKING = ShenandoahHeap::MARKING,
65 WEAK = ShenandoahHeap::WEAK_ROOTS,
66 FORWARDED_MARKING = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING,
67 FORWARDED_WEAK = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::WEAK_ROOTS,
68 MARKING_WEAK = ShenandoahHeap::MARKING | ShenandoahHeap::WEAK_ROOTS,
69 FORWARDED_MARKING_WEAK = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING | ShenandoahHeap::WEAK_ROOTS
70 };
71
72 enum ShenandoahNMethodGCStatePos {
73 POS_FORWARDED = 0,
74 POS_MARKING = 1,
75 POS_WEAK = 2,
76 POS_FORWARDED_MARKING = 3,
77 POS_FORWARDED_WEAK = 4,
78 POS_MARKING_WEAK = 5,
79 POS_FORWARDED_MARKING_WEAK = 6,
80 POS_MAX
81 };
82
83 public:
84 ShenandoahNMethod(nmethod *nm);
85 ~ShenandoahNMethod();
86
87 static int gc_state_to_reloc(char gc_state) {
88 if (gc_state == FORWARDED) return POS_FORWARDED;
89 if (gc_state == MARKING) return POS_MARKING;
90 if (gc_state == WEAK) return POS_WEAK;
91 if (gc_state == FORWARDED_MARKING) return POS_FORWARDED_MARKING;
92 if (gc_state == FORWARDED_WEAK) return POS_FORWARDED_WEAK;
93 if (gc_state == MARKING_WEAK) return POS_MARKING_WEAK;
94 if (gc_state == FORWARDED_MARKING_WEAK) return POS_FORWARDED_MARKING_WEAK;
95 ShouldNotReachHere();
96 return 0;
97 }
98
99 static char reloc_to_gc_state(int index) {
100 if (index == POS_FORWARDED) return FORWARDED;
101 if (index == POS_MARKING) return MARKING;
102 if (index == POS_WEAK) return WEAK;
103 if (index == POS_FORWARDED_MARKING) return FORWARDED_MARKING;
104 if (index == POS_FORWARDED_WEAK) return FORWARDED_WEAK;
105 if (index == POS_MARKING_WEAK) return MARKING_WEAK;
106 if (index == POS_FORWARDED_MARKING_WEAK) return FORWARDED_MARKING_WEAK;
107 ShouldNotReachHere();
108 return 0;
109 }
110
111 inline nmethod* nm() const;
112 inline ShenandoahNMethodLock* lock();
113 inline ShenandoahNMethodLock* ic_lock();
114 inline void oops_do(OopClosure* oops, bool fix_relocations = false);
115 // Update oops when the nmethod is re-registered
116 void update();
117
118 inline bool is_unregistered() const;
119
120 static ShenandoahNMethod* for_nmethod(nmethod* nm);
121 static inline ShenandoahNMethodLock* lock_for_nmethod(nmethod* nm);
122 static inline ShenandoahNMethodLock* ic_lock_for_nmethod(nmethod* nm);
123
124 static void heal_nmethod(nmethod* nm);
125 static void update_barriers(nmethod* nm);
126 static inline void heal_nmethod_metadata(ShenandoahNMethod* nmethod_data);
127 static inline void complete_and_disarm_nmethod(nmethod* nm);
128 static inline void complete_and_disarm_nmethod_unlocked(nmethod* nm);
129
130 static inline ShenandoahNMethod* gc_data(nmethod* nm);
131 static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
132
133 static void assert_barriers(nmethod* nm, bool armed) NOT_DEBUG_RETURN;
134 void assert_correct() NOT_DEBUG_RETURN;
135 void assert_same_oops() NOT_DEBUG_RETURN;
136
137 private:
138 void init_from(nmethod* nm);
139 static void parse(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers);
140 };
141
142 class ShenandoahNMethodTable;
143
144 // ShenandoahNMethodList holds registered nmethod data. The list is reference counted.
145 class ShenandoahNMethodList : public CHeapObj<mtGC> {
146 private:
147 ShenandoahNMethod** _list;
148 const int _size;
149 uint _ref_count;
150
151 private:
152 ~ShenandoahNMethodList();
153
154 public:
155 ShenandoahNMethodList(int size);
156
157 // Reference counting with CoceCache_lock held
158 ShenandoahNMethodList* acquire();
159 void release();
160
161 // Transfer content from other list to 'this' list, up to the limit
162 void transfer(ShenandoahNMethodList* const other, int limit);
163
164 inline int size() const;
165 inline ShenandoahNMethod** list() const;
166 inline ShenandoahNMethod* at(int index) const;
167 inline void set(int index, ShenandoahNMethod* snm);
168 };
169
170 // An opaque snapshot of current nmethod table for iteration
171 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
172 friend class ShenandoahNMethodTable;
173 private:
174 ShenandoahHeap* const _heap;
175 ShenandoahNMethodList* _list;
176 /* snapshot iteration limit */
177 int _limit;
178
179 shenandoah_padding(0);
180 Atomic<size_t> _claimed;
181 shenandoah_padding(1);
182
183 public:
184 ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
185 ~ShenandoahNMethodTableSnapshot();
186
187 void parallel_nmethods_do(NMethodClosure *f);
188 void concurrent_nmethods_do(NMethodClosure* cl);
189 };
190
191 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
192 friend class ShenandoahNMethodTableSnapshot;
193 private:
194 enum {
195 minSize = 1024
196 };
197
198 ShenandoahHeap* const _heap;
199 ShenandoahNMethodList* _list;
200
201 int _index;
202 ShenandoahLock _lock;
203 int _itr_cnt;
204
205 public:
206 ShenandoahNMethodTable();
207 ~ShenandoahNMethodTable();
208
209 void register_nmethod(nmethod* nm);
210 void unregister_nmethod(nmethod* nm);
211
212 bool contain(nmethod* nm) const;
213 int length() const { return _index; }
214
215 // Table iteration support
216 ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
217 void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
218
219 void assert_nmethods_correct() NOT_DEBUG_RETURN;
220 private:
221 // Rebuild table and replace current one
222 void rebuild(int size);
223
224 bool is_full() const {
225 assert(_index <= _list->size(), "Sanity");
226 return _index == _list->size();
227 }
228
229 ShenandoahNMethod* at(int index) const;
230 int index_of(nmethod* nm) const;
231 void remove(int index);
232 void append(ShenandoahNMethod* snm);
233
234 inline bool iteration_in_progress() const;
235 void wait_until_concurrent_iteration_done();
236
237 // Logging support
238 void log_register_nmethod(nmethod* nm);
239 void log_unregister_nmethod(nmethod* nm);
240 };
241
242 class ShenandoahConcurrentNMethodIterator {
243 private:
244 ShenandoahNMethodTable* const _table;
245 ShenandoahNMethodTableSnapshot* _table_snapshot;
246 uint _started_workers;
247 uint _finished_workers;
248
249 public:
250 ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
251
252 void nmethods_do(NMethodClosure* cl);
253 };
254
255 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP