< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahNMethod.cpp

Print this page

  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 

 27 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "runtime/continuation.hpp"
 32 #include "runtime/safepointVerifiers.hpp"
 33 
 34 ShenandoahNMethod::ShenandoahNMethod(nmethod* nm, GrowableArray<oop*>& oops, bool non_immediate_oops) :
 35   _nm(nm), _oops(nullptr), _oops_count(0), _unregistered(false), _lock(), _ic_lock() {
 36 
 37   if (!oops.is_empty()) {
 38     _oops_count = oops.length();
 39     _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
 40     for (int c = 0; c < _oops_count; c++) {
 41       _oops[c] = oops.at(c);
 42     }
 43   }
 44   _has_non_immed_oops = non_immediate_oops;
 45 
 46   assert_same_oops();








 47 }
 48 
 49 ShenandoahNMethod::~ShenandoahNMethod() {
 50   if (_oops != nullptr) {
 51     FREE_C_HEAP_ARRAY(_oops);
 52   }



 53 }
 54 
 55 void ShenandoahNMethod::update() {
 56   ResourceMark rm;
 57   bool non_immediate_oops = false;
 58   GrowableArray<oop*> oops;

 59 
 60   detect_reloc_oops(nm(), oops, non_immediate_oops);
 61   if (oops.length() != _oops_count) {
 62     if (_oops != nullptr) {
 63       FREE_C_HEAP_ARRAY(_oops);
 64       _oops = nullptr;
 65     }
 66 
 67     _oops_count = oops.length();
 68     if (_oops_count > 0) {
 69       _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
 70     }
 71   }
 72 
 73   for (int index = 0; index < _oops_count; index ++) {
 74     _oops[index] = oops.at(index);
 75   }
 76   _has_non_immed_oops = non_immediate_oops;
 77 
 78   assert_same_oops();
 79 }
 80 
 81 void ShenandoahNMethod::detect_reloc_oops(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops) {
 82   has_non_immed_oops = false;
 83   // Find all oops relocations
 84   RelocIterator iter(nm);
 85   while (iter.next()) {
 86     if (iter.type() != relocInfo::oop_type) {
 87       // Not an oop
 88       continue;
 89     }
 90 
 91     oop_Relocation* r = iter.oop_reloc();
 92     if (!r->oop_is_immediate()) {
 93       // Non-immediate oop found
 94       has_non_immed_oops = true;
 95       continue;
 96     }
 97 
 98     oop value = r->oop_value();
 99     if (value != nullptr) {
100       oop* addr = r->oop_addr();
101       shenandoah_assert_correct(addr, value);
102       shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
103       shenandoah_assert_not_forwarded(addr, value);
104       // Non-null immediate oop found. null oops can safely be
105       // ignored since the method will be re-registered if they
106       // are later patched to be non-null.
107       oops.push(addr);

















108     }
109   }
110 }
111 
112 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
113   ResourceMark rm;
114   bool non_immediate_oops = false;
115   GrowableArray<oop*> oops;

116 
117   detect_reloc_oops(nm, oops, non_immediate_oops);
118   return new ShenandoahNMethod(nm, oops, non_immediate_oops);
119 }
120 
121 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
122   ShenandoahNMethod* data = gc_data(nm);
123   assert(data != nullptr, "Sanity");
124   assert(data->lock()->owned_by_self(), "Must hold the lock");
125 
126   ShenandoahHeap* const heap = ShenandoahHeap::heap();
127   if (heap->is_concurrent_weak_root_in_progress() ||
128       heap->is_concurrent_strong_root_in_progress()) {
129     heal_nmethod_metadata(data);
130   } else if (heap->is_concurrent_mark_in_progress()) {
131     ShenandoahKeepAliveClosure cl;
132     data->oops_do(&cl);
133   } else {
134     // There is possibility that GC is cancelled when it arrives final mark.
135     // In this case, concurrent root phase is skipped and degenerated GC should be
136     // followed, where nmethods are disarmed.
137   }
138 }
139 

















140 #ifdef ASSERT















141 void ShenandoahNMethod::assert_correct() {
142   ShenandoahHeap* heap = ShenandoahHeap::heap();
143   for (int c = 0; c < _oops_count; c++) {
144     oop *loc = _oops[c];
145     assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
146     oop o = RawAccess<>::oop_load(loc);
147     shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
148   }
149 
150   oop* const begin = _nm->oops_begin();
151   oop* const end = _nm->oops_end();
152   for (oop* p = begin; p < end; p++) {
153     if (*p != Universe::non_oop_word()) {
154       oop o = RawAccess<>::oop_load(p);
155       shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
156     }
157   }
158 }
159 
160 class ShenandoahNMethodOopDetector : public OopClosure {

188     assert(oops->contains(_oops[index]), "Must contain this oop");
189   }
190 
191   for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) {
192     if (*p == Universe::non_oop_word()) continue;
193     count++;
194     assert(oops->contains(p), "Must contain this oop");
195   }
196 
197   if (oops->length() < count) {
198     stringStream debug_stream;
199     debug_stream.print_cr("detected locs: %d", oops->length());
200     for (int i = 0; i < oops->length(); i++) {
201       debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i)));
202     }
203     debug_stream.print_cr("recorded oops: %d", _oops_count);
204     for (int i = 0; i < _oops_count; i++) {
205       debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i]));
206     }
207     GrowableArray<oop*> check;

208     bool non_immed;
209     detect_reloc_oops(nm(), check, non_immed);
210     debug_stream.print_cr("check oops: %d", check.length());
211     for (int i = 0; i < check.length(); i++) {
212       debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i)));
213     }
214     fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s",
215           oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.freeze());
216   }
217 }
218 #endif
219 
220 ShenandoahNMethodTable::ShenandoahNMethodTable() :
221   _heap(ShenandoahHeap::heap()),
222   _index(0),
223   _itr_cnt(0) {
224   _list = new ShenandoahNMethodList(minSize);
225 }
226 
227 ShenandoahNMethodTable::~ShenandoahNMethodTable() {
228   assert(_list != nullptr, "Sanity");
229   _list->release();
230 }
231 
232 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) {
233   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
234   assert(_index >= 0 && _index <= _list->size(), "Sanity");
235 
236   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
237 
238   if (data != nullptr) {
239     assert(contain(nm), "Must have been registered");
240     assert(nm == data->nm(), "Must be same nmethod");
241     // Prevent updating a nmethod while concurrent iteration is in progress.
242     wait_until_concurrent_iteration_done();
243     ShenandoahNMethodLocker data_locker(data->lock());
244     data->update();

245   } else {
246     // For a new nmethod, we can safely append it to the list, because
247     // concurrent iteration will not touch it.
248     data = ShenandoahNMethod::for_nmethod(nm);
249     assert(data != nullptr, "Sanity");
250     ShenandoahNMethod::attach_gc_data(nm, data);
251     ShenandoahLocker locker(&_lock);
252     log_register_nmethod(nm);
253     append(data);

254   }
255   // Disarm new nmethod
256   ShenandoahNMethod::disarm_nmethod(nm);
257 }
258 
259 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
260   assert_locked_or_safepoint(CodeCache_lock);
261 
262   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
263   assert(data != nullptr, "Sanity");
264   log_unregister_nmethod(nm);
265   ShenandoahLocker locker(&_lock);
266   assert(contain(nm), "Must have been registered");
267 
268   int idx = index_of(nm);
269   assert(idx >= 0 && idx < _index, "Invalid index");
270   ShenandoahNMethod::attach_gc_data(nm, nullptr);
271   remove(idx);
272 }
273 

  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 
 27 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
 28 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 30 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
 31 #include "memory/resourceArea.hpp"
 32 #include "runtime/continuation.hpp"
 33 #include "runtime/safepointVerifiers.hpp"
 34 
 35 ShenandoahNMethod::ShenandoahNMethod(nmethod* nm, GrowableArray<oop*>& oops, bool non_immediate_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers) :
 36   _nm(nm), _oops(nullptr), _oops_count(0), _barriers(nullptr), _barriers_count(0), _unregistered(false), _lock(), _ic_lock() {
 37 
 38   if (!oops.is_empty()) {
 39     _oops_count = oops.length();
 40     _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
 41     for (int c = 0; c < _oops_count; c++) {
 42       _oops[c] = oops.at(c);
 43     }
 44   }
 45   _has_non_immed_oops = non_immediate_oops;
 46 
 47   assert_same_oops();
 48 
 49   if (!barriers.is_empty()) {
 50     _barriers_count = barriers.length();
 51     _barriers = NEW_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _barriers_count, mtGC);
 52     for (int c = 0; c < _barriers_count; c++) {
 53       _barriers[c] = barriers.at(c);
 54     }
 55   }
 56 }
 57 
 58 ShenandoahNMethod::~ShenandoahNMethod() {
 59   if (_oops != nullptr) {
 60     FREE_C_HEAP_ARRAY(_oops);
 61   }
 62   if (_barriers != nullptr) {
 63     FREE_C_HEAP_ARRAY(_barriers);
 64   }
 65 }
 66 
 67 void ShenandoahNMethod::update() {
 68   ResourceMark rm;
 69   bool non_immediate_oops = false;
 70   GrowableArray<oop*> oops;
 71   GrowableArray<ShenandoahNMethodBarrier> barriers;
 72 
 73   parse(nm(), oops, non_immediate_oops, barriers);
 74   if (oops.length() != _oops_count) {
 75     if (_oops != nullptr) {
 76       FREE_C_HEAP_ARRAY(_oops);
 77       _oops = nullptr;
 78     }
 79 
 80     _oops_count = oops.length();
 81     if (_oops_count > 0) {
 82       _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
 83     }
 84   }
 85 
 86   for (int index = 0; index < _oops_count; index ++) {
 87     _oops[index] = oops.at(index);
 88   }
 89   _has_non_immed_oops = non_immediate_oops;
 90 
 91   assert_same_oops();
 92 }
 93 
 94 void ShenandoahNMethod::parse(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers) {
 95   has_non_immed_oops = false;

 96   RelocIterator iter(nm);
 97   while (iter.next()) {
 98     switch (iter.type()) {
 99       case relocInfo::oop_type: {
100         oop_Relocation* r = iter.oop_reloc();
101         if (!r->oop_is_immediate()) {
102           // Non-immediate oop found
103           has_non_immed_oops = true;
104           break;
105         }
106 
107         oop value = r->oop_value();
108         if (value != nullptr) {
109           oop* addr = r->oop_addr();
110           shenandoah_assert_correct(addr, value);
111           shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
112           shenandoah_assert_not_forwarded(addr, value);
113           // Non-null immediate oop found. null oops can safely be
114           // ignored since the method will be re-registered if they
115           // are later patched to be non-null.
116           oops.push(addr);
117         }
118         break;
119       }
120 #ifdef COMPILER2
121       case relocInfo::barrier_type: {
122         barrier_Relocation* r = iter.barrier_reloc();
123 
124         ShenandoahNMethodBarrier b;
125         b._pc = r->addr();
126         // TODO: Parsing the stub address from generated code is kludgy. It also does not work
127         // with nmethod relocation, that can copy the nmethod body with barriers already nop-ped out.
128         b._stub_addr = ShenandoahBarrierSetAssembler::parse_stub_address(b._pc);
129         b._gc_state_fast_index = r->format();
130         barriers.push(b);
131         break;
132       }
133 #endif
134       default:
135         // We do not care about other relocations.
136         break;
137     }
138   }
139 }
140 
141 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
142   ResourceMark rm;
143   bool non_immediate_oops = false;
144   GrowableArray<oop*> oops;
145   GrowableArray<ShenandoahNMethodBarrier> barriers;
146 
147   parse(nm, oops, non_immediate_oops, barriers);
148   return new ShenandoahNMethod(nm, oops, non_immediate_oops, barriers);
149 }
150 
151 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
152   ShenandoahNMethod* data = gc_data(nm);
153   assert(data != nullptr, "Sanity");
154   assert(data->lock()->owned_by_self(), "Must hold the lock");
155 
156   ShenandoahHeap* const heap = ShenandoahHeap::heap();
157   if (heap->is_concurrent_weak_root_in_progress() ||
158       heap->is_concurrent_strong_root_in_progress()) {
159     heal_nmethod_metadata(data);
160   } else if (heap->is_concurrent_mark_in_progress()) {
161     ShenandoahKeepAliveClosure cl;
162     data->oops_do(&cl);
163   } else {
164     // There is possibility that GC is cancelled when it arrives final mark.
165     // In this case, concurrent root phase is skipped and degenerated GC should be
166     // followed, where nmethods are disarmed.
167   }
168 }
169 
170 void ShenandoahNMethod::update_barriers() {
171 #ifdef COMPILER2
172   ShenandoahHeap* heap = ShenandoahHeap::heap();
173 
174   for (int c = 0; c < _barriers_count; c++) {
175     address pc = _barriers[c]._pc;
176     address stub_addr = _barriers[c]._stub_addr;
177     char trigger_state = ShenandoahThreadLocalData::fast_array_index_to_gc_state(_barriers[c]._gc_state_fast_index);
178     if ((heap->gc_state() & trigger_state) != 0) {
179       ShenandoahBarrierSetAssembler::patch_nop_to_branch(pc, stub_addr);
180     } else {
181       ShenandoahBarrierSetAssembler::patch_branch_to_nop(pc);
182     }
183   }
184 #endif
185 }
186 
187 #ifdef ASSERT
188 void ShenandoahNMethod::assert_barriers(nmethod* nm, bool global_expected) {
189 #ifdef COMPILER2
190   ShenandoahHeap* heap = ShenandoahHeap::heap();
191 
192   ShenandoahNMethod* snm = gc_data(nm);
193   for (int c = 0; c < snm->_barriers_count; c++) {
194     address pc = snm->_barriers[c]._pc;
195     char trigger_state = ShenandoahThreadLocalData::fast_array_index_to_gc_state(snm->_barriers[c]._gc_state_fast_index);
196     bool expected = global_expected && ((heap->gc_state() & trigger_state) != 0);
197     bool actual = ShenandoahBarrierSetAssembler::is_active(pc);
198     assert(expected == actual, "armed expected: %s, actual: %s", BOOL_TO_STR(expected), BOOL_TO_STR(actual));
199   }
200 #endif
201 }
202 
203 void ShenandoahNMethod::assert_correct() {
204   ShenandoahHeap* heap = ShenandoahHeap::heap();
205   for (int c = 0; c < _oops_count; c++) {
206     oop *loc = _oops[c];
207     assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
208     oop o = RawAccess<>::oop_load(loc);
209     shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
210   }
211 
212   oop* const begin = _nm->oops_begin();
213   oop* const end = _nm->oops_end();
214   for (oop* p = begin; p < end; p++) {
215     if (*p != Universe::non_oop_word()) {
216       oop o = RawAccess<>::oop_load(p);
217       shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
218     }
219   }
220 }
221 
222 class ShenandoahNMethodOopDetector : public OopClosure {

250     assert(oops->contains(_oops[index]), "Must contain this oop");
251   }
252 
253   for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) {
254     if (*p == Universe::non_oop_word()) continue;
255     count++;
256     assert(oops->contains(p), "Must contain this oop");
257   }
258 
259   if (oops->length() < count) {
260     stringStream debug_stream;
261     debug_stream.print_cr("detected locs: %d", oops->length());
262     for (int i = 0; i < oops->length(); i++) {
263       debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i)));
264     }
265     debug_stream.print_cr("recorded oops: %d", _oops_count);
266     for (int i = 0; i < _oops_count; i++) {
267       debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i]));
268     }
269     GrowableArray<oop*> check;
270     GrowableArray<ShenandoahNMethodBarrier> barriers;
271     bool non_immed;
272     parse(nm(), check, non_immed, barriers);
273     debug_stream.print_cr("check oops: %d", check.length());
274     for (int i = 0; i < check.length(); i++) {
275       debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i)));
276     }
277     fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s",
278           oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.freeze());
279   }
280 }
281 #endif
282 
283 ShenandoahNMethodTable::ShenandoahNMethodTable() :
284   _heap(ShenandoahHeap::heap()),
285   _index(0),
286   _itr_cnt(0) {
287   _list = new ShenandoahNMethodList(minSize);
288 }
289 
290 ShenandoahNMethodTable::~ShenandoahNMethodTable() {
291   assert(_list != nullptr, "Sanity");
292   _list->release();
293 }
294 
295 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) {
296   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
297   assert(_index >= 0 && _index <= _list->size(), "Sanity");
298 
299   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
300 
301   if (data != nullptr) {
302     assert(contain(nm), "Must have been registered");
303     assert(nm == data->nm(), "Must be same nmethod");
304     // Prevent updating a nmethod while concurrent iteration is in progress.
305     wait_until_concurrent_iteration_done();
306     ShenandoahNMethodLocker data_locker(data->lock());
307     data->update();
308     data->update_barriers();
309   } else {
310     // For a new nmethod, we can safely append it to the list, because
311     // concurrent iteration will not touch it.
312     data = ShenandoahNMethod::for_nmethod(nm);
313     assert(data != nullptr, "Sanity");
314     ShenandoahNMethod::attach_gc_data(nm, data);
315     ShenandoahLocker locker(&_lock);
316     log_register_nmethod(nm);
317     append(data);
318     data->update_barriers();
319   }
320   // Disarm new nmethod
321   ShenandoahNMethod::disarm_nmethod(nm);
322 }
323 
324 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
325   assert_locked_or_safepoint(CodeCache_lock);
326 
327   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
328   assert(data != nullptr, "Sanity");
329   log_unregister_nmethod(nm);
330   ShenandoahLocker locker(&_lock);
331   assert(contain(nm), "Must have been registered");
332 
333   int idx = index_of(nm);
334   assert(idx >= 0 && idx < _index, "Invalid index");
335   ShenandoahNMethod::attach_gc_data(nm, nullptr);
336   remove(idx);
337 }
338 
< prev index next >