1 /* 2 * Copyright (c) 2017, 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 #include "code/codeCache.hpp" 26 #include "code/nmethod.hpp" 27 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 28 #include "gc/shenandoah/shenandoahCodeRoots.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "memory/universe.hpp" 31 32 ShenandoahParallelCodeCacheIterator::ShenandoahParallelCodeCacheIterator(const GrowableArray<CodeHeap*>* heaps) { 33 _length = heaps->length(); 34 _iters = NEW_C_HEAP_ARRAY(ShenandoahParallelCodeHeapIterator, _length, mtGC); 35 for (int h = 0; h < _length; h++) { 36 _iters[h] = ShenandoahParallelCodeHeapIterator(heaps->at(h)); 37 } 38 } 39 40 ShenandoahParallelCodeCacheIterator::~ShenandoahParallelCodeCacheIterator() { 41 FREE_C_HEAP_ARRAY(ParallelCodeHeapIterator, _iters); 42 } 43 44 void ShenandoahParallelCodeCacheIterator::parallel_blobs_do(CodeBlobClosure* f) { 45 for (int c = 0; c < _length; c++) { 46 _iters[c].parallel_blobs_do(f); 47 } 48 } 49 50 ShenandoahParallelCodeHeapIterator::ShenandoahParallelCodeHeapIterator(CodeHeap* heap) : 51 _heap(heap), _claimed_idx(0), _finished(false) { 52 } 53 54 void ShenandoahParallelCodeHeapIterator::parallel_blobs_do(CodeBlobClosure* f) { 55 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint"); 56 57 /* 58 * Parallel code heap walk. 59 * 60 * This code makes all threads scan all code heaps, but only one thread would execute the 61 * closure on given blob. This is achieved by recording the "claimed" blocks: if a thread 62 * had claimed the block, it can process all blobs in it. Others have to fast-forward to 63 * next attempt without processing. 64 * 65 * Late threads would return immediately if iterator is finished. 66 */ 67 68 if (_finished) { 69 return; 70 } 71 72 int stride = 256; // educated guess 73 int stride_mask = stride - 1; 74 assert (is_power_of_2(stride), "sanity"); 75 76 int count = 0; 77 bool process_block = true; 78 79 for (CodeBlob *cb = CodeCache::first_blob(_heap); cb != NULL; cb = CodeCache::next_blob(_heap, cb)) { 80 int current = count++; 81 if ((current & stride_mask) == 0) { 82 process_block = (current >= _claimed_idx) && 83 (Atomic::cmpxchg(current + stride, &_claimed_idx, current) == current); 84 } 85 if (process_block) { 86 if (cb->is_alive()) { 87 f->do_code_blob(cb); 88 #ifdef ASSERT 89 if (cb->is_nmethod()) 90 Universe::heap()->verify_nmethod((nmethod*)cb); 91 #endif 92 } 93 } 94 } 95 96 _finished = true; 97 } 98 99 class ShenandoahNMethodOopDetector : public OopClosure { 100 private: 101 ResourceMark rm; // For growable array allocation below. 102 GrowableArray<oop*> _oops; 103 104 public: 105 ShenandoahNMethodOopDetector() : _oops(10) {}; 106 107 void do_oop(oop* o) { 108 _oops.append(o); 109 } 110 void do_oop(narrowOop* o) { 111 fatal("NMethods should not have compressed oops embedded."); 112 } 113 114 GrowableArray<oop*>* oops() { 115 return &_oops; 116 } 117 118 bool has_oops() { 119 return !_oops.is_empty(); 120 } 121 }; 122 123 GrowableArray<ShenandoahNMethod*>* ShenandoahCodeRoots::_recorded_nms; 124 125 void ShenandoahCodeRoots::initialize() { 126 _recorded_nms = new (ResourceObj::C_HEAP, mtGC) GrowableArray<ShenandoahNMethod*>(100, true, mtGC); 127 } 128 129 void ShenandoahCodeRoots::add_nmethod(nmethod* nm) { 130 assert(CodeCache_lock->owned_by_self(), "Must own CodeCache_lock"); 131 switch (ShenandoahCodeRootsStyle) { 132 case 0: 133 case 1: 134 break; 135 case 2: { 136 ShenandoahNMethodOopDetector detector; 137 nm->oops_do(&detector); 138 139 if (detector.has_oops()) { 140 ShenandoahNMethod* nmr = new ShenandoahNMethod(nm, detector.oops()); 141 nmr->assert_alive_and_correct(); 142 int idx = _recorded_nms->find(nm, ShenandoahNMethod::find_with_nmethod); 143 if (idx != -1) { 144 ShenandoahNMethod* old = _recorded_nms->at(idx); 145 _recorded_nms->at_put(idx, nmr); 146 delete old; 147 } else { 148 _recorded_nms->append(nmr); 149 } 150 } 151 break; 152 } 153 default: 154 ShouldNotReachHere(); 155 } 156 }; 157 158 void ShenandoahCodeRoots::remove_nmethod(nmethod* nm) { 159 assert(CodeCache_lock->owned_by_self(), "Must own CodeCache_lock"); 160 switch (ShenandoahCodeRootsStyle) { 161 case 0: 162 case 1: { 163 break; 164 } 165 case 2: { 166 ShenandoahNMethodOopDetector detector; 167 nm->oops_do(&detector, /* allow_zombie = */ true); 168 169 if (detector.has_oops()) { 170 int idx = _recorded_nms->find(nm, ShenandoahNMethod::find_with_nmethod); 171 assert(idx != -1, "nmethod " PTR_FORMAT " should be registered", p2i(nm)); 172 ShenandoahNMethod* old = _recorded_nms->at(idx); 173 old->assert_same_oops(detector.oops()); 174 _recorded_nms->delete_at(idx); 175 delete old; 176 } 177 break; 178 } 179 default: 180 ShouldNotReachHere(); 181 } 182 } 183 184 ShenandoahCodeRootsIterator::ShenandoahCodeRootsIterator() : 185 _heap(ShenandoahHeap::heap()), 186 _par_iterator(CodeCache::heaps()), 187 _claimed(0) { 188 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint"); 189 assert(!Thread::current()->is_Worker_thread(), "Should not be acquired by workers"); 190 switch (ShenandoahCodeRootsStyle) { 191 case 0: 192 case 1: { 193 // No need to do anything here 194 break; 195 } 196 case 2: { 197 CodeCache_lock->lock(); 198 break; 199 } 200 default: 201 ShouldNotReachHere(); 202 } 203 } 204 205 ShenandoahCodeRootsIterator::~ShenandoahCodeRootsIterator() { 206 switch (ShenandoahCodeRootsStyle) { 207 case 0: 208 case 1: { 209 // No need to do anything here 210 break; 211 } 212 case 2: { 213 CodeCache_lock->unlock(); 214 break; 215 } 216 default: 217 ShouldNotReachHere(); 218 } 219 } 220 221 template<bool CSET_FILTER> 222 void ShenandoahCodeRootsIterator::dispatch_parallel_blobs_do(CodeBlobClosure *f) { 223 switch (ShenandoahCodeRootsStyle) { 224 case 0: { 225 if (_seq_claimed.try_set()) { 226 CodeCache::blobs_do(f); 227 } 228 break; 229 } 230 case 1: { 231 _par_iterator.parallel_blobs_do(f); 232 break; 233 } 234 case 2: { 235 ShenandoahCodeRootsIterator::fast_parallel_blobs_do<CSET_FILTER>(f); 236 break; 237 } 238 default: 239 ShouldNotReachHere(); 240 } 241 } 242 243 void ShenandoahAllCodeRootsIterator::possibly_parallel_blobs_do(CodeBlobClosure *f) { 244 ShenandoahCodeRootsIterator::dispatch_parallel_blobs_do<false>(f); 245 } 246 247 void ShenandoahCsetCodeRootsIterator::possibly_parallel_blobs_do(CodeBlobClosure *f) { 248 ShenandoahCodeRootsIterator::dispatch_parallel_blobs_do<true>(f); 249 } 250 251 template <bool CSET_FILTER> 252 void ShenandoahCodeRootsIterator::fast_parallel_blobs_do(CodeBlobClosure *f) { 253 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint"); 254 255 size_t stride = 256; // educated guess 256 257 GrowableArray<ShenandoahNMethod*>* list = ShenandoahCodeRoots::_recorded_nms; 258 259 size_t max = (size_t)list->length(); 260 while (_claimed < max) { 261 size_t cur = Atomic::add(stride, &_claimed) - stride; 262 size_t start = cur; 263 size_t end = MIN2(cur + stride, max); 264 if (start >= max) break; 265 266 for (size_t idx = start; idx < end; idx++) { 267 ShenandoahNMethod* nmr = list->at((int) idx); 268 nmr->assert_alive_and_correct(); 269 270 if (CSET_FILTER && !nmr->has_cset_oops(_heap)) { 271 continue; 272 } 273 274 f->do_code_blob(nmr->nm()); 275 } 276 } 277 } 278 279 ShenandoahNMethod::ShenandoahNMethod(nmethod* nm, GrowableArray<oop*>* oops) { 280 _nm = nm; 281 _oops = NEW_C_HEAP_ARRAY(oop*, oops->length(), mtGC); 282 _oops_count = oops->length(); 283 for (int c = 0; c < _oops_count; c++) { 284 _oops[c] = oops->at(c); 285 } 286 } 287 288 ShenandoahNMethod::~ShenandoahNMethod() { 289 if (_oops != NULL) { 290 FREE_C_HEAP_ARRAY(oop*, _oops); 291 } 292 } 293 294 bool ShenandoahNMethod::has_cset_oops(ShenandoahHeap *heap) { 295 for (int c = 0; c < _oops_count; c++) { 296 oop o = RawAccess<>::oop_load(_oops[c]); 297 if (heap->in_collection_set(o)) { 298 return true; 299 } 300 } 301 return false; 302 } 303 304 #ifdef ASSERT 305 void ShenandoahNMethod::assert_alive_and_correct() { 306 assert(_nm->is_alive(), "only alive nmethods here"); 307 assert(_oops_count > 0, "should have filtered nmethods without oops before"); 308 ShenandoahHeap* heap = ShenandoahHeap::heap(); 309 for (int c = 0; c < _oops_count; c++) { 310 oop *loc = _oops[c]; 311 assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*"); 312 oop o = RawAccess<>::oop_load(loc); 313 shenandoah_assert_correct_except(loc, o, o == NULL || heap->is_full_gc_move_in_progress()); 314 } 315 } 316 317 void ShenandoahNMethod::assert_same_oops(GrowableArray<oop*>* oops) { 318 assert(_oops_count == oops->length(), "should have the same number of oop*"); 319 for (int c = 0; c < _oops_count; c++) { 320 assert(_oops[c] == oops->at(c), "should be the same oop*"); 321 } 322 } 323 #endif