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 int _rel_pc;
42 int _rel_target;
43 int _metadata;
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 public:
63 ShenandoahNMethod(nmethod *nm);
64 ~ShenandoahNMethod();
65
66 static bool decode_reloc_inverted(int reloc) {
67 return (reloc & (1 << 8)) != 0;
68 }
69
70 static char decode_reloc_gc_state(int reloc) {
71 return (reloc & 0xFF);
72 }
73
74 static int encode_to_reloc(char gc_state, bool inverted) {
75 int res = (gc_state & 0xFF) | (inverted ? (1 << 8) : 0);
76 assert(decode_reloc_inverted(res) == inverted, "Round-trip");
77 assert(decode_reloc_gc_state(res) == gc_state, "Round-trip");
78 return res;
79 }
80
81 inline nmethod* nm() const;
82 inline ShenandoahNMethodLock* lock();
83 inline ShenandoahNMethodLock* ic_lock();
84 inline void oops_do(OopClosure* oops, bool fix_relocations = false);
85 // Update oops when the nmethod is re-registered
86 void update();
87
88 inline bool is_unregistered() const;
89
90 static ShenandoahNMethod* for_nmethod(nmethod* nm);
91 static inline ShenandoahNMethodLock* lock_for_nmethod(nmethod* nm);
92 static inline ShenandoahNMethodLock* ic_lock_for_nmethod(nmethod* nm);
93
94 static void heal_nmethod(nmethod* nm);
95 static bool update_barriers(nmethod* nm);
96 static inline void heal_nmethod_metadata(ShenandoahNMethod* nmethod_data);
97 static inline void disarm_nmethod(nmethod* nm);
98
99 static inline ShenandoahNMethod* gc_data(nmethod* nm);
100 static inline void attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data);
101
102 void assert_correct() NOT_DEBUG_RETURN;
103 void assert_same_oops() NOT_DEBUG_RETURN;
104
105 private:
106 void init_from(nmethod* nm);
107 static void parse(nmethod* nm, GrowableArray<oop*>& oops, bool& _has_non_immed_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers);
108 };
109
110 class ShenandoahNMethodTable;
111
112 // ShenandoahNMethodList holds registered nmethod data. The list is reference counted.
113 class ShenandoahNMethodList : public CHeapObj<mtGC> {
114 private:
115 ShenandoahNMethod** _list;
116 const int _size;
117 uint _ref_count;
118
119 private:
120 ~ShenandoahNMethodList();
121
122 public:
123 ShenandoahNMethodList(int size);
124
125 // Reference counting with CoceCache_lock held
126 ShenandoahNMethodList* acquire();
127 void release();
128
129 // Transfer content from other list to 'this' list, up to the limit
130 void transfer(ShenandoahNMethodList* const other, int limit);
131
132 inline int size() const;
133 inline ShenandoahNMethod** list() const;
134 inline ShenandoahNMethod* at(int index) const;
135 inline void set(int index, ShenandoahNMethod* snm);
136 };
137
138 // An opaque snapshot of current nmethod table for iteration
139 class ShenandoahNMethodTableSnapshot : public CHeapObj<mtGC> {
140 friend class ShenandoahNMethodTable;
141 private:
142 ShenandoahHeap* const _heap;
143 ShenandoahNMethodList* _list;
144 /* snapshot iteration limit */
145 int _limit;
146
147 shenandoah_padding(0);
148 Atomic<size_t> _claimed;
149 shenandoah_padding(1);
150
151 public:
152 ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table);
153 ~ShenandoahNMethodTableSnapshot();
154
155 void parallel_nmethods_do(NMethodClosure *f);
156 void concurrent_nmethods_do(NMethodClosure* cl);
157 };
158
159 class ShenandoahNMethodTable : public CHeapObj<mtGC> {
160 friend class ShenandoahNMethodTableSnapshot;
161 private:
162 enum {
163 minSize = 1024
164 };
165
166 ShenandoahHeap* const _heap;
167 ShenandoahNMethodList* _list;
168
169 int _index;
170 ShenandoahLock _lock;
171 int _itr_cnt;
172
173 public:
174 ShenandoahNMethodTable();
175 ~ShenandoahNMethodTable();
176
177 void register_nmethod(nmethod* nm);
178 void unregister_nmethod(nmethod* nm);
179
180 bool contain(nmethod* nm) const;
181 int length() const { return _index; }
182
183 // Table iteration support
184 ShenandoahNMethodTableSnapshot* snapshot_for_iteration();
185 void finish_iteration(ShenandoahNMethodTableSnapshot* snapshot);
186
187 void assert_nmethods_correct() NOT_DEBUG_RETURN;
188 private:
189 // Rebuild table and replace current one
190 void rebuild(int size);
191
192 bool is_full() const {
193 assert(_index <= _list->size(), "Sanity");
194 return _index == _list->size();
195 }
196
197 ShenandoahNMethod* at(int index) const;
198 int index_of(nmethod* nm) const;
199 void remove(int index);
200 void append(ShenandoahNMethod* snm);
201
202 inline bool iteration_in_progress() const;
203 void wait_until_concurrent_iteration_done();
204
205 // Logging support
206 void log_register_nmethod(nmethod* nm);
207 void log_unregister_nmethod(nmethod* nm);
208 };
209
210 class ShenandoahConcurrentNMethodIterator {
211 private:
212 ShenandoahNMethodTable* const _table;
213 ShenandoahNMethodTableSnapshot* _table_snapshot;
214 uint _started_workers;
215 uint _finished_workers;
216
217 public:
218 ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table);
219
220 void nmethods_do(NMethodClosure* cl);
221 };
222
223 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_HPP