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