< 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 
274 bool ShenandoahNMethodTable::contain(nmethod* nm) const {
275   return index_of(nm) != -1;
276 }

  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) :
 36   _nm(nm), _oops(nullptr), _oops_count(0), _barriers(nullptr), _barriers_count(0), _unregistered(false), _lock(), _ic_lock() {
 37   init_from(nm);










 38 }
 39 
 40 ShenandoahNMethod::~ShenandoahNMethod() {
 41   if (_oops != nullptr) {
 42     FREE_C_HEAP_ARRAY(_oops);
 43   }
 44   if (_barriers != nullptr) {
 45     FREE_C_HEAP_ARRAY(_barriers);
 46   }
 47 }
 48 
 49 void ShenandoahNMethod::update() {
 50   init_from(nm());
 51 }
 52 
 53 void ShenandoahNMethod::init_from(nmethod* nm) {
 54   ResourceMark rm;
 55   bool non_immediate_oops = false;
 56   GrowableArray<oop*> oops;
 57   GrowableArray<ShenandoahNMethodBarrier> barriers;
 58 
 59   parse(nm, oops, non_immediate_oops, barriers);
 60 
 61   if (_oops_count != oops.length()) {
 62     _oops_count = oops.length();
 63     if (_oops != nullptr) {
 64       FREE_C_HEAP_ARRAY(_oops);

 65     }
 66     _oops = NEW_C_HEAP_ARRAY(oop*, _oops_count, mtGC);
 67     for (int c = 0; c < _oops_count; c++) {
 68       _oops[c] = oops.at(c);

 69     }
 70   }
 71 
 72   assert_same_oops();
 73 
 74   if (_barriers_count != barriers.length()) {
 75     _barriers_count = barriers.length();
 76     if (_barriers != nullptr) {
 77       FREE_C_HEAP_ARRAY(_barriers);
 78     }
 79     _barriers = NEW_C_HEAP_ARRAY(ShenandoahNMethodBarrier, _barriers_count, mtGC);
 80     for (int c = 0; c < _barriers_count; c++) {
 81       _barriers[c] = barriers.at(c);
 82     }
 83   }

 84 
 85   _has_non_immed_oops = non_immediate_oops;
 86 }
 87 
 88 void ShenandoahNMethod::parse(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops, GrowableArray<ShenandoahNMethodBarrier>& barriers) {
 89   has_non_immed_oops = false;

 90   RelocIterator iter(nm);
 91   while (iter.next()) {
 92     switch (iter.type()) {
 93       case relocInfo::oop_type: {
 94         oop_Relocation* r = iter.oop_reloc();
 95         if (!r->oop_is_immediate()) {
 96           // Non-immediate oop found
 97           has_non_immed_oops = true;
 98           break;
 99         }
100 
101         oop value = r->oop_value();
102         if (value != nullptr) {
103           oop* addr = r->oop_addr();
104           shenandoah_assert_correct(addr, value);
105           shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
106           shenandoah_assert_not_forwarded(addr, value);
107           // Non-null immediate oop found. null oops can safely be
108           // ignored since the method will be re-registered if they
109           // are later patched to be non-null.
110           oops.push(addr);
111         }
112         break;
113       }
114 #ifdef COMPILER2
115       case relocInfo::barrier_type: {
116         barrier_Relocation* r = iter.barrier_reloc();
117 
118         ShenandoahNMethodBarrier b;
119         b._pc = r->addr();
120         // TODO: Parsing the stub address from generated code is kludgy. It also does not work
121         // with nmethod relocation, that can copy the nmethod body with barriers already nop-ped out.
122         b._stub_addr = ShenandoahBarrierSetAssembler::parse_stub_address(b._pc);
123         b._index = r->format();
124         barriers.push(b);
125         break;
126       }
127 #endif
128       default:
129         // We do not care about other relocations.
130         break;
131     }
132   }
133 }
134 
135 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
136   return new ShenandoahNMethod(nm);





137 }
138 
139 void ShenandoahNMethod::heal_nmethod(nmethod* nm) {
140   ShenandoahNMethod* data = gc_data(nm);
141   assert(data != nullptr, "Sanity");
142   assert(data->lock()->owned_by_self(), "Must hold the lock");
143 
144   ShenandoahHeap* const heap = ShenandoahHeap::heap();
145   if (heap->is_evacuation_in_progress()) {

146     heal_nmethod_metadata(data);
147   } else if (heap->is_concurrent_mark_in_progress()) {
148     ShenandoahKeepAliveClosure cl;
149     data->oops_do(&cl);
150   } else {
151     // There is possibility that GC is cancelled when it arrives final mark.
152     // In this case, concurrent root phase is skipped and degenerated GC should be
153     // followed, where nmethods are disarmed.
154   }
155 }
156 
157 void ShenandoahNMethod::update_barriers(nmethod* nm) {
158 #ifdef COMPILER2
159   ShenandoahNMethod* data = gc_data(nm);
160   assert(data != nullptr, "Sanity");
161   assert(data->lock()->owned_by_self(), "Must hold the lock");
162 
163   char gc_state = ShenandoahHeap::heap()->gc_state();
164 
165   for (int c = 0; c < data->_barriers_count; c++) {
166     address pc = data->_barriers[c]._pc;
167     address stub_addr = data->_barriers[c]._stub_addr;
168     char trigger_state = reloc_to_gc_state(data->_barriers[c]._index);
169 
170     if ((gc_state & trigger_state) != 0) {
171       ShenandoahBarrierSetAssembler::patch_nop_to_branch(pc, stub_addr);
172     } else {
173       ShenandoahBarrierSetAssembler::patch_branch_to_nop(pc);
174     }
175   }
176 #endif
177 }
178 
179 #ifdef ASSERT
180 void ShenandoahNMethod::assert_barriers(nmethod* nm, bool global_expected) {
181 #ifdef COMPILER2
182   ShenandoahHeap* heap = ShenandoahHeap::heap();
183 
184   ShenandoahNMethod* snm = gc_data(nm);
185   for (int c = 0; c < snm->_barriers_count; c++) {
186     address pc = snm->_barriers[c]._pc;
187     char trigger_state = reloc_to_gc_state(snm->_barriers[c]._index);
188     bool expected = global_expected && ((heap->gc_state() & trigger_state) != 0);
189     bool actual = ShenandoahBarrierSetAssembler::is_active(pc);
190     assert(expected == actual, "armed expected: %s, actual: %s", BOOL_TO_STR(expected), BOOL_TO_STR(actual));
191   }
192 #endif
193 }
194 
195 void ShenandoahNMethod::assert_correct() {
196   ShenandoahHeap* heap = ShenandoahHeap::heap();
197   for (int c = 0; c < _oops_count; c++) {
198     oop *loc = _oops[c];
199     assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
200     oop o = RawAccess<>::oop_load(loc);
201     shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
202   }
203 
204   oop* const begin = _nm->oops_begin();
205   oop* const end = _nm->oops_end();
206   for (oop* p = begin; p < end; p++) {
207     if (*p != Universe::non_oop_word()) {
208       oop o = RawAccess<>::oop_load(p);
209       shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
210     }
211   }
212 }
213 
214 class ShenandoahNMethodOopDetector : public OopClosure {

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


314 }
315 
316 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
317   assert_locked_or_safepoint(CodeCache_lock);
318 
319   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
320   assert(data != nullptr, "Sanity");
321   log_unregister_nmethod(nm);
322   ShenandoahLocker locker(&_lock);
323   assert(contain(nm), "Must have been registered");
324 
325   int idx = index_of(nm);
326   assert(idx >= 0 && idx < _index, "Invalid index");
327   ShenandoahNMethod::attach_gc_data(nm, nullptr);
328   remove(idx);
329 }
330 
331 bool ShenandoahNMethodTable::contain(nmethod* nm) const {
332   return index_of(nm) != -1;
333 }
< prev index next >