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 class BarrierSetNMethod;
37
38 // Use ShenandoahReentrantLock as ShenandoahNMethodLock
39 typedef ShenandoahReentrantLock<ShenandoahSimpleLock> ShenandoahNMethodLock;
40 typedef ShenandoahLocker<ShenandoahNMethodLock> ShenandoahNMethodLocker;
41
42 struct ShenandoahPatchableJump {
43 int32_t _rel_pc;
44 int32_t _rel_target_pc;
45 char _gc_state;
46 bool _jump_when_state;
47 };
48
49 // ShenandoahNMethod tuple records the internal locations of oop slots within reclocation stream in
50 // the nmethod. This allows us to quickly scan the oops without doing the nmethod-internal scans,
51 // that sometimes involves parsing the machine code. Note it does not record the oops themselves,
52 // because it would then require handling these tuples as the new class of roots.
53 class ShenandoahNMethod : public CHeapObj<mtGC> {
54 private:
55 nmethod* const _nm;
56 oop** _oops;
57 int _oops_count;
58 ShenandoahPatchableJump* _patchable_jumps;
59 int _patchable_jumps_count;
60 bool _has_non_immed_oops;
61 bool _unregistered;
62 ShenandoahNMethodLock _lock;
63 ShenandoahNMethodLock _ic_lock;
64
65 public:
66 ShenandoahNMethod(nmethod *nm);
67 ~ShenandoahNMethod();
68
69 static bool decode_reloc_jump_when_state(uint16_t reloc) {
70 return (reloc & (1 << 8)) != 0;
71 }
72
73 static char decode_reloc_gc_state(uint16_t reloc) {
74 return (reloc & 0xFF);
75 }
76
77 static uint16_t encode_to_reloc(char gc_state, bool jump_when_state) {
78 uint16_t res = (gc_state & 0xFF) | (jump_when_state ? (1 << 8) : 0);
79 assert(decode_reloc_jump_when_state(res) == jump_when_state, "Round-trip");
80 assert(decode_reloc_gc_state(res) == gc_state, "Round-trip");
81 return res;
82 }
83
84 inline nmethod* nm() const;
85 inline ShenandoahNMethodLock* lock();
86 inline ShenandoahNMethodLock* ic_lock();
87 inline void oops_do(OopClosure* oops, bool fix_relocations = false);
88 // Update oops when the nmethod is re-registered
89 void update();
90
91 inline bool is_unregistered() const;
92
93 static ShenandoahNMethod* for_nmethod(nmethod* nm);
94 static inline ShenandoahNMethodLock* lock_for_nmethod(nmethod* nm);
95 static inline ShenandoahNMethodLock* ic_lock_for_nmethod(nmethod* nm);
96
97 static bool handle_oops(nmethod* nm);
98 static bool handle_jumps(nmethod* nm);
99 static inline void heal_nmethod_metadata(ShenandoahNMethod* nmethod_data);
100 static inline void disarm_nmethod(nmethod* nm);
101
102 static inline ShenandoahNMethod* gc_data(nmethod* nm);
103 static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
104
105 void assert_correct() NOT_DEBUG_RETURN;
106 void assert_same_oops() NOT_DEBUG_RETURN;
107
108 bool has_patchable_jumps() {
109 return _patchable_jumps_count > 0;
110 }
111
112 private:
113 void init_from(nmethod* nm);
114 static void parse(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops, GrowableArray<ShenandoahPatchableJump>& jumps);
115 static bool patch_jump(address pc, address target_pc, bool should_jump);
116 };
117
118 class ShenandoahNMethodTable;
119
120 // ShenandoahNMethodList holds registered nmethod data. The list is reference counted.
121 class ShenandoahNMethodList : public CHeapObj<mtGC> {
122 private:
123 ShenandoahNMethod** _list;
124 const int _size;
125 uint _ref_count;
126
127 private:
128 ~ShenandoahNMethodList();
129
130 public:
131 ShenandoahNMethodList(int size);
132
133 // Reference counting with CoceCache_lock held
134 ShenandoahNMethodList* acquire();
135 void release();
136
137 // Transfer content from other list to 'this' list, up to the limit
138 void transfer(ShenandoahNMethodList* const other, int limit);
139
140 inline int size() const;
141 inline ShenandoahNMethod** list() const;
142 inline ShenandoahNMethod* at(int index) const;
143 inline void set(int index, ShenandoahNMethod* snm);
144 };
145
146 // An opaque snapshot of current nmethod table for iteration
147 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
148 friend class ShenandoahNMethodTable;
149 private:
150 ShenandoahHeap* const _heap;
151 ShenandoahNMethodList* _list;
152 /* snapshot iteration limit */
153 int _limit;
154
155 shenandoah_padding(0);
156 Atomic<size_t> _claimed;
157 shenandoah_padding(1);
158
159 public:
160 ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
161 ~ShenandoahNMethodTableSnapshot();
162
163 void parallel_nmethods_do(NMethodClosure *f);
164 void concurrent_nmethods_do(NMethodClosure* cl);
165 };
166
167 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
168 friend class ShenandoahNMethodTableSnapshot;
169 private:
170 enum {
171 minSize = 1024
172 };
173
174 ShenandoahHeap* const _heap;
175 BarrierSetNMethod* const _bs_nm;
176
177 ShenandoahNMethodList* _list;
178
179 int _index;
180 ShenandoahLock _lock;
181 int _itr_cnt;
182
183 public:
184 ShenandoahNMethodTable();
185 ~ShenandoahNMethodTable();
186
187 void register_nmethod(nmethod* nm);
188 void unregister_nmethod(nmethod* nm);
189
190 bool contain(nmethod* nm) const;
191 int length() const { return _index; }
192
193 // Table iteration support
194 ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
195 void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
196
197 void assert_nmethods_correct() NOT_DEBUG_RETURN;
198 private:
199 // Rebuild table and replace current one
200 void rebuild(int size);
201
202 bool is_full() const {
203 assert(_index <= _list->size(), "Sanity");
204 return _index == _list->size();
205 }
206
207 ShenandoahNMethod* at(int index) const;
208 int index_of(nmethod* nm) const;
209 void remove(int index);
210 void append(ShenandoahNMethod* snm);
211
212 inline bool iteration_in_progress() const;
213 void wait_until_concurrent_iteration_done();
214
215 // Logging support
216 void log_register_nmethod(nmethod* nm);
217 void log_unregister_nmethod(nmethod* nm);
218 };
219
220 class ShenandoahConcurrentNMethodIterator {
221 private:
222 ShenandoahNMethodTable* const _table;
223 ShenandoahNMethodTableSnapshot* _table_snapshot;
224 uint _started_workers;
225 uint _finished_workers;
226
227 public:
228 ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
229
230 void nmethods_do(NMethodClosure* cl);
231 };
232
233 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP