< 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(oop*, _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(oop*, _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     ShenandoahEvacOOMScope evac_scope;
130     heal_nmethod_metadata(data);
131   } else if (heap->is_concurrent_mark_in_progress()) {
132     ShenandoahKeepAliveClosure cl;
133     data->oops_do(&cl);
134   } else {
135     // There is possibility that GC is cancelled when it arrives final mark.
136     // In this case, concurrent root phase is skipped and degenerated GC should be
137     // followed, where nmethods are disarmed.
138   }
139 }
140 
















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

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

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

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

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

  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(oop*, _oops);
 61   }
 62   if (_barriers != nullptr) {
 63     FREE_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _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(oop*, _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         // TODO next step: Figure out which GC state we care about in at this fastpath check:
130         // b._gc_state_fast_bit = r->format();
131         barriers.push(b);
132         break;
133       }
134 #endif
135       default:
136         // We do not care about other relocations.
137         break;
138     }
139   }
140 }
141 
142 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
143   ResourceMark rm;
144   bool non_immediate_oops = false;
145   GrowableArray<oop*> oops;
146   GrowableArray<ShenandoahNMethodBarrier> barriers;
147 
148   parse(nm, oops, non_immediate_oops, barriers);
149   return new ShenandoahNMethod(nm, oops, non_immediate_oops, barriers);
150 }
151 
152 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
153   ShenandoahNMethod* data = gc_data(nm);
154   assert(data != nullptr, "Sanity");
155   assert(data->lock()->owned_by_self(), "Must hold the lock");
156 
157   ShenandoahHeap* const heap = ShenandoahHeap::heap();
158   if (heap->is_concurrent_weak_root_in_progress() ||
159       heap->is_concurrent_strong_root_in_progress()) {
160     ShenandoahEvacOOMScope evac_scope;
161     heal_nmethod_metadata(data);
162   } else if (heap->is_concurrent_mark_in_progress()) {
163     ShenandoahKeepAliveClosure cl;
164     data->oops_do(&cl);
165   } else {
166     // There is possibility that GC is cancelled when it arrives final mark.
167     // In this case, concurrent root phase is skipped and degenerated GC should be
168     // followed, where nmethods are disarmed.
169   }
170 }
171 
172 void ShenandoahNMethod::update_barriers() {
173 #ifdef COMPILER2
174   ShenandoahHeap* heap = ShenandoahHeap::heap();
175 
176   for (int c = 0; c < _barriers_count; c++) {
177     address pc = _barriers[c]._pc;
178     address stub_addr = _barriers[c]._stub_addr;
179     if (heap->is_idle()) {
180       ShenandoahBarrierSetAssembler::patch_branch_to_nop(pc);
181     } else {
182       ShenandoahBarrierSetAssembler::patch_nop_to_branch(pc, stub_addr);
183     }
184   }
185 #endif
186 }
187 
188 #ifdef ASSERT
189 void ShenandoahNMethod::assert_correct() {
190   ShenandoahHeap* heap = ShenandoahHeap::heap();
191   for (int c = 0; c < _oops_count; c++) {
192     oop *loc = _oops[c];
193     assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
194     oop o = RawAccess<>::oop_load(loc);
195     shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
196   }
197 
198   oop* const begin = _nm->oops_begin();
199   oop* const end = _nm->oops_end();
200   for (oop* p = begin; p < end; p++) {
201     if (*p != Universe::non_oop_word()) {
202       oop o = RawAccess<>::oop_load(p);
203       shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
204     }
205   }
206 }
207 

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