1 /*
  2  * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2019, 2022, Red Hat, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  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), _patchable_jumps(nullptr), _patchable_jumps_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 (_patchable_jumps != nullptr) {
 45     FREE_C_HEAP_ARRAY(_patchable_jumps);
 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<ShenandoahPatchableJump> jumps;
 58 
 59   parse(nm, oops, non_immediate_oops, jumps);
 60 
 61   int new_oops_count = oops.length();
 62   if (_oops_count != new_oops_count) {
 63     if (_oops != nullptr) {
 64       FREE_C_HEAP_ARRAY(_oops);
 65       _oops = nullptr;
 66     }
 67     if (new_oops_count > 0) {
 68       _oops = NEW_C_HEAP_ARRAY(oop*, new_oops_count, mtGC);
 69     }
 70   }
 71   _oops_count = new_oops_count;
 72   for (int c = 0; c < _oops_count; c++) {
 73     _oops[c] = oops.at(c);
 74   }
 75   assert_same_oops();
 76 
 77   int new_jumps_count = jumps.length();
 78   if (_patchable_jumps_count != new_jumps_count) {
 79     if (_patchable_jumps != nullptr) {
 80       FREE_C_HEAP_ARRAY(_patchable_jumps);
 81       _patchable_jumps = nullptr;
 82     }
 83     if (new_jumps_count > 0) {
 84       _patchable_jumps = NEW_C_HEAP_ARRAY(ShenandoahPatchableJump, new_jumps_count, mtGC);
 85     }
 86   }
 87   _patchable_jumps_count = new_jumps_count;
 88   for (int c = 0; c < _patchable_jumps_count; c++) {
 89     _patchable_jumps[c] = jumps.at(c);
 90   }
 91 
 92   _has_non_immed_oops = non_immediate_oops;
 93 }
 94 
 95 void ShenandoahNMethod::parse(nmethod* nm, GrowableArray<oop*>& oops, bool& has_non_immed_oops, GrowableArray<ShenandoahPatchableJump>& jumps) {
 96   has_non_immed_oops = false;
 97   address code_begin = nm->code_begin();
 98   RelocIterator iter(nm);
 99   while (iter.next()) {
100     switch (iter.type()) {
101       case relocInfo::oop_type: {
102         oop_Relocation* r = iter.oop_reloc();
103         if (!r->oop_is_immediate()) {
104           // Non-immediate oop found
105           has_non_immed_oops = true;
106           break;
107         }
108 
109         oop value = r->oop_value();
110         if (value != nullptr) {
111           oop* addr = r->oop_addr();
112           shenandoah_assert_correct(addr, value);
113           shenandoah_assert_not_in_cset_except(addr, value, ShenandoahHeap::heap()->cancelled_gc());
114           shenandoah_assert_not_forwarded(addr, value);
115           // Non-null immediate oop found. null oops can safely be
116           // ignored since the method will be re-registered if they
117           // are later patched to be non-null.
118           oops.push(addr);
119         }
120         break;
121       }
122       case relocInfo::patchable_barrier_type: {
123         patchable_barrier_Relocation* r = iter.patchable_barrier_reloc();
124 
125         ShenandoahPatchableJump b;
126         b._rel_pc = checked_cast<int32_t>(pointer_delta(r->addr(), code_begin, 1));
127         b._rel_target_pc = r->target_offset();
128         b._gc_state = decode_reloc_gc_state(r->metadata());
129         b._jump_when_state = decode_reloc_jump_when_state(r->metadata());
130         jumps.push(b);
131         break;
132       }
133       default:
134         // We do not care about other relocations.
135         break;
136     }
137   }
138 }
139 
140 ShenandoahNMethod* ShenandoahNMethod::for_nmethod(nmethod* nm) {
141   return new ShenandoahNMethod(nm);
142 }
143 
144 bool ShenandoahNMethod::handle_oops(nmethod* nm) {
145   ShenandoahNMethod* data = gc_data(nm);
146   assert(data != nullptr, "Sanity");
147   assert(data->lock()->owned_by_self(), "Must hold the lock");
148 
149   ShenandoahHeap* const heap = ShenandoahHeap::heap();
150   if ((heap->is_concurrent_weak_root_in_progress() && heap->is_evacuation_in_progress()) ||
151       heap->is_concurrent_strong_root_in_progress()) {
152     heal_nmethod_metadata(data);
153     // Assume healing changed the code.
154     return true;
155   } else if (heap->is_concurrent_mark_in_progress()) {
156     ShenandoahKeepAliveClosure cl;
157     data->oops_do(&cl);
158   } else {
159     // There is possibility that GC is cancelled when it arrives final mark.
160     // In this case, concurrent root phase is skipped and degenerated GC should be
161     // followed, where nmethods are disarmed.
162   }
163 
164   // No code modifications happened
165   return false;
166 }
167 
168 bool ShenandoahNMethod::handle_jumps(nmethod* nm) {
169   ShenandoahNMethod* data = gc_data(nm);
170   assert(data != nullptr, "Sanity");
171   assert(data->lock()->owned_by_self(), "Must hold the lock");
172 
173   char gc_state = ShenandoahHeap::heap()->gc_state();
174   address code_begin = nm->code_begin();
175 
176   bool changed = false;
177   for (int c = 0; c < data->_patchable_jumps_count; c++) {
178     ShenandoahPatchableJump& b = data->_patchable_jumps[c];
179     changed |= patch_jump(code_begin + b._rel_pc,
180                           code_begin + b._rel_target_pc,
181                           ((gc_state & b._gc_state) != 0) == b._jump_when_state);
182   }
183   return changed;
184 }
185 
186 // Use precise instruction rewrite code, and only when it recognizes the current insns.
187 //
188 // This patching code is non-atomic, but it runs in two safe contexts:
189 //   a) For new nmethods that are not yet executing and not yet live. This covers the paths
190 //      for newly compiled methods, nmethods that were just relocated, the nmethods that
191 //      were AOT-loaded.
192 //   b) For existing methods in the nmethod entry barrier context. The nmethod entry barriers
193 //      are armed along with stack watermark machinery activation. Together they guarantee
194 //      the nmethod updates are not interleaved with execution, and nmethod would be patched
195 //      before allowing to proceed.
196 //
197 // The icache flushing is also handled on both paths.
198 //
199 bool ShenandoahNMethod::patch_jump(address pc, address target_pc, bool should_jump) {
200   bool patched = true;
201   if (should_jump && ShenandoahBarrierSetAssembler::is_patchable_nop(pc)) {
202     ShenandoahBarrierSetAssembler::insert_patchable_jump(pc, target_pc);
203   } else if (!should_jump && ShenandoahBarrierSetAssembler::is_patchable_jump(pc, target_pc)) {
204     ShenandoahBarrierSetAssembler::insert_patchable_nop(pc);
205   } else {
206     patched = false;
207   }
208 
209   // Failing to change the jump is catastrophic for correctness,
210   // so prefer to crash hard even in product.
211   if (should_jump) {
212     guarantee(ShenandoahBarrierSetAssembler::is_patchable_jump(pc, target_pc),
213       "Should be jump to the same address");
214     assert(ShenandoahBarrierSetAssembler::parse_jump_address(pc) == target_pc,
215       "Cross-checking, jump should be to the same address");
216   } else {
217     guarantee(ShenandoahBarrierSetAssembler::is_patchable_nop(pc),
218       "Should be patchable nop");
219   }
220   return patched;
221 }
222 
223 #ifdef ASSERT
224 void ShenandoahNMethod::assert_correct() {
225   ShenandoahHeap* heap = ShenandoahHeap::heap();
226   for (int c = 0; c < _oops_count; c++) {
227     oop *loc = _oops[c];
228     assert(_nm->code_contains((address) loc) || _nm->oops_contains(loc), "nmethod should contain the oop*");
229     oop o = RawAccess<>::oop_load(loc);
230     shenandoah_assert_correct_except(loc, o, o == nullptr || heap->is_full_gc_move_in_progress());
231   }
232 
233   oop* const begin = _nm->oops_begin();
234   oop* const end = _nm->oops_end();
235   for (oop* p = begin; p < end; p++) {
236     if (*p != Universe::non_oop_word()) {
237       oop o = RawAccess<>::oop_load(p);
238       shenandoah_assert_correct_except(p, o, o == nullptr || heap->is_full_gc_move_in_progress());
239     }
240   }
241 }
242 
243 class ShenandoahNMethodOopDetector : public OopClosure {
244 private:
245   ResourceMark rm; // For growable array allocation below.
246   GrowableArray<oop*> _oops;
247 
248 public:
249   ShenandoahNMethodOopDetector() : _oops(10) {};
250 
251   void do_oop(oop* o) {
252     _oops.append(o);
253   }
254   void do_oop(narrowOop* o) {
255     fatal("NMethods should not have compressed oops embedded.");
256   }
257 
258   GrowableArray<oop*>* oops() {
259     return &_oops;
260   }
261 };
262 
263 void ShenandoahNMethod::assert_same_oops() {
264   ShenandoahNMethodOopDetector detector;
265   nm()->oops_do(&detector);
266 
267   GrowableArray<oop*>* oops = detector.oops();
268 
269   int count = _oops_count;
270   for (int index = 0; index < _oops_count; index ++) {
271     assert(oops->contains(_oops[index]), "Must contain this oop");
272   }
273 
274   for (oop* p = nm()->oops_begin(); p < nm()->oops_end(); p ++) {
275     if (*p == Universe::non_oop_word()) continue;
276     count++;
277     assert(oops->contains(p), "Must contain this oop");
278   }
279 
280   if (oops->length() < count) {
281     stringStream debug_stream;
282     debug_stream.print_cr("detected locs: %d", oops->length());
283     for (int i = 0; i < oops->length(); i++) {
284       debug_stream.print_cr("-> " PTR_FORMAT, p2i(oops->at(i)));
285     }
286     debug_stream.print_cr("recorded oops: %d", _oops_count);
287     for (int i = 0; i < _oops_count; i++) {
288       debug_stream.print_cr("-> " PTR_FORMAT, p2i(_oops[i]));
289     }
290     GrowableArray<oop*> check;
291     GrowableArray<ShenandoahPatchableJump> jumps;
292     bool non_immed;
293     parse(nm(), check, non_immed, jumps);
294     debug_stream.print_cr("check oops: %d", check.length());
295     for (int i = 0; i < check.length(); i++) {
296       debug_stream.print_cr("-> " PTR_FORMAT, p2i(check.at(i)));
297     }
298     fatal("Must match #detected: %d, #recorded: %d, #total: %d, begin: " PTR_FORMAT ", end: " PTR_FORMAT "\n%s",
299           oops->length(), _oops_count, count, p2i(nm()->oops_begin()), p2i(nm()->oops_end()), debug_stream.freeze());
300   }
301 }
302 #endif
303 
304 ShenandoahNMethodTable::ShenandoahNMethodTable() :
305   _heap(ShenandoahHeap::heap()),
306   _bs_nm(BarrierSet::barrier_set()->barrier_set_nmethod()),
307   _index(0),
308   _itr_cnt(0) {
309   _list = new ShenandoahNMethodList(minSize);
310 }
311 
312 ShenandoahNMethodTable::~ShenandoahNMethodTable() {
313   assert(_list != nullptr, "Sanity");
314   _list->release();
315 }
316 
317 void ShenandoahNMethodTable::register_nmethod(nmethod* nm) {
318   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
319   assert(_index >= 0 && _index <= _list->size(), "Sanity");
320 
321   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
322 
323   if (data != nullptr) {
324     // Re-registering the existing nmethod. This is the C1 oop patching path.
325     // We expect no patchable jumps here, as only oops can change in C1 case.
326     assert(contain(nm), "Must have been registered");
327     assert(nm == data->nm(), "Must be same nmethod");
328     assert(nm->is_compiled_by_c1(), "Must be compiled by C1");
329     assert(!data->has_patchable_jumps(), "Must not have patchable jumps");
330     // Prevent updating a nmethod while concurrent iteration is in progress.
331     wait_until_concurrent_iteration_done();
332     ShenandoahNMethodLocker data_locker(data->lock());
333     data->update();
334   } else {
335     // New nmethod, not yet executing. We can safely append it to the list,
336     // because concurrent iteration will not touch it. Ditto we do jump
337     // fixups right here, without relying on nmethod entry barrier to be armed
338     // for new nmethods.
339     data = ShenandoahNMethod::for_nmethod(nm);
340     assert(data != nullptr, "Sanity");
341     ShenandoahNMethod::attach_gc_data(nm, data);
342     ShenandoahLocker locker(&_lock);
343     log_register_nmethod(nm);
344     append(data);
345     ShenandoahNMethodLocker data_locker(data->lock());
346     if (ShenandoahNMethod::handle_jumps(nm)) {
347       ICache::invalidate_range(nm->code_begin(), nm->code_size());
348     }
349     ShenandoahNMethod::disarm_nmethod(nm);
350   }
351 
352   assert(!data->has_patchable_jumps() || _bs_nm->supports_entry_barrier(nm),
353          "NMethods with patchable jumps require entry barrier support");
354 }
355 
356 void ShenandoahNMethodTable::unregister_nmethod(nmethod* nm) {
357   assert_locked_or_safepoint(CodeCache_lock);
358 
359   ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
360   assert(data != nullptr, "Sanity");
361   log_unregister_nmethod(nm);
362   ShenandoahLocker locker(&_lock);
363   assert(contain(nm), "Must have been registered");
364 
365   int idx = index_of(nm);
366   assert(idx >= 0 && idx < _index, "Invalid index");
367   ShenandoahNMethod::attach_gc_data(nm, nullptr);
368   remove(idx);
369 }
370 
371 bool ShenandoahNMethodTable::contain(nmethod* nm) const {
372   return index_of(nm) != -1;
373 }
374 
375 ShenandoahNMethod* ShenandoahNMethodTable::at(int index) const {
376   assert(index >= 0 && index < _index, "Out of bound");
377   return _list->at(index);
378 }
379 
380 int ShenandoahNMethodTable::index_of(nmethod* nm) const {
381   for (int index = 0; index < length(); index ++) {
382     if (at(index)->nm() == nm) {
383       return index;
384     }
385   }
386   return -1;
387 }
388 
389 void ShenandoahNMethodTable::remove(int idx) {
390   shenandoah_assert_locked_or_safepoint(CodeCache_lock);
391   assert(_index >= 0 && _index <= _list->size(), "Sanity");
392 
393   assert(idx >= 0 && idx < _index, "Out of bound");
394   ShenandoahNMethod* snm = _list->at(idx);
395   ShenandoahNMethod* tmp = _list->at(_index - 1);
396   _list->set(idx, tmp);
397   _index --;
398 
399   delete snm;
400 }
401 
402 void ShenandoahNMethodTable::wait_until_concurrent_iteration_done() {
403   assert(CodeCache_lock->owned_by_self(), "Lock must be held");
404   while (iteration_in_progress()) {
405     CodeCache_lock->wait_without_safepoint_check();
406   }
407 }
408 
409 void ShenandoahNMethodTable::append(ShenandoahNMethod* snm) {
410   if (is_full()) {
411     int new_size = 2 * _list->size();
412     // Rebuild table and replace current one
413     rebuild(new_size);
414   }
415 
416   _list->set(_index++,  snm);
417   assert(_index >= 0 && _index <= _list->size(), "Sanity");
418 }
419 
420 void ShenandoahNMethodTable::rebuild(int size) {
421   ShenandoahNMethodList* new_list = new ShenandoahNMethodList(size);
422   new_list->transfer(_list, _index);
423 
424   // Release old list
425   _list->release();
426   _list = new_list;
427 }
428 
429 ShenandoahNMethodTableSnapshot* ShenandoahNMethodTable::snapshot_for_iteration() {
430   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
431   _itr_cnt++;
432   return new ShenandoahNMethodTableSnapshot(this);
433 }
434 
435 void ShenandoahNMethodTable::finish_iteration(ShenandoahNMethodTableSnapshot* snapshot) {
436   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
437   assert(iteration_in_progress(), "Why we here?");
438   assert(snapshot != nullptr, "No snapshot");
439   _itr_cnt--;
440 
441   delete snapshot;
442 }
443 
444 void ShenandoahNMethodTable::log_register_nmethod(nmethod* nm) {
445   LogTarget(Debug, gc, nmethod) log;
446   if (!log.is_enabled()) {
447     return;
448   }
449 
450   ResourceMark rm;
451   log.print("Register NMethod: %s.%s [" PTR_FORMAT "] (%s)",
452             nm->method()->method_holder()->external_name(),
453             nm->method()->name()->as_C_string(),
454             p2i(nm),
455             nm->compiler_name());
456 }
457 
458 void ShenandoahNMethodTable::log_unregister_nmethod(nmethod* nm) {
459   LogTarget(Debug, gc, nmethod) log;
460   if (!log.is_enabled()) {
461     return;
462   }
463 
464   ResourceMark rm;
465   log.print("Unregister NMethod: %s.%s [" PTR_FORMAT "]",
466             nm->method()->method_holder()->external_name(),
467             nm->method()->name()->as_C_string(),
468             p2i(nm));
469 }
470 
471 #ifdef ASSERT
472 void ShenandoahNMethodTable::assert_nmethods_correct() {
473   assert_locked_or_safepoint(CodeCache_lock);
474 
475   for (int index = 0; index < length(); index ++) {
476     ShenandoahNMethod* m = _list->at(index);
477     // Concurrent unloading may have dead nmethods to be cleaned by sweeper
478     if (m->is_unregistered()) continue;
479     m->assert_correct();
480   }
481 }
482 #endif
483 
484 
485 ShenandoahNMethodList::ShenandoahNMethodList(int size) :
486   _size(size), _ref_count(1) {
487   _list = NEW_C_HEAP_ARRAY(ShenandoahNMethod*, size, mtGC);
488 }
489 
490 ShenandoahNMethodList::~ShenandoahNMethodList() {
491   assert(_list != nullptr, "Sanity");
492   assert(_ref_count == 0, "Must be");
493   FREE_C_HEAP_ARRAY(_list);
494 }
495 
496 void ShenandoahNMethodList::transfer(ShenandoahNMethodList* const list, int limit) {
497   assert(limit <= size(), "Sanity");
498   ShenandoahNMethod** old_list = list->list();
499   for (int index = 0; index < limit; index++) {
500     _list[index] = old_list[index];
501   }
502 }
503 
504 ShenandoahNMethodList* ShenandoahNMethodList::acquire() {
505   assert_locked_or_safepoint(CodeCache_lock);
506   _ref_count++;
507   return this;
508 }
509 
510 void ShenandoahNMethodList::release() {
511   assert_locked_or_safepoint(CodeCache_lock);
512   _ref_count--;
513   if (_ref_count == 0) {
514     delete this;
515   }
516 }
517 
518 ShenandoahNMethodTableSnapshot::ShenandoahNMethodTableSnapshot(ShenandoahNMethodTable* table) :
519   _heap(ShenandoahHeap::heap()), _list(table->_list->acquire()), _limit(table->_index), _claimed(0) {
520 }
521 
522 ShenandoahNMethodTableSnapshot::~ShenandoahNMethodTableSnapshot() {
523   _list->release();
524 }
525 
526 void ShenandoahNMethodTableSnapshot::parallel_nmethods_do(NMethodClosure *f) {
527   size_t stride = 256; // educated guess
528 
529   ShenandoahNMethod** const list = _list->list();
530 
531   size_t max = (size_t)_limit;
532   while (_claimed.load_relaxed() < max) {
533     size_t cur = _claimed.fetch_then_add(stride, memory_order_relaxed);
534     size_t start = cur;
535     size_t end = MIN2(cur + stride, max);
536     if (start >= max) break;
537 
538     for (size_t idx = start; idx < end; idx++) {
539       ShenandoahNMethod* nmr = list[idx];
540       assert(nmr != nullptr, "Sanity");
541       if (nmr->is_unregistered()) {
542         continue;
543       }
544 
545       nmr->assert_correct();
546       f->do_nmethod(nmr->nm());
547     }
548   }
549 }
550 
551 void ShenandoahNMethodTableSnapshot::concurrent_nmethods_do(NMethodClosure* cl) {
552   size_t stride = 256; // educated guess
553 
554   ShenandoahNMethod** list = _list->list();
555   size_t max = (size_t)_limit;
556   while (_claimed.load_relaxed() < max) {
557     size_t cur = _claimed.fetch_then_add(stride, memory_order_relaxed);
558     size_t start = cur;
559     size_t end = MIN2(cur + stride, max);
560     if (start >= max) break;
561 
562     for (size_t idx = start; idx < end; idx++) {
563       ShenandoahNMethod* data = list[idx];
564       assert(data != nullptr, "Should not be null");
565       if (!data->is_unregistered()) {
566         cl->do_nmethod(data->nm());
567       }
568     }
569   }
570 }
571 
572 ShenandoahConcurrentNMethodIterator::ShenandoahConcurrentNMethodIterator(ShenandoahNMethodTable* table) :
573   _table(table),
574   _table_snapshot(nullptr),
575   _started_workers(0),
576   _finished_workers(0) {}
577 
578 void ShenandoahConcurrentNMethodIterator::nmethods_do(NMethodClosure* cl) {
579   // Cannot safepoint when iteration is running, because this can cause deadlocks
580   // with other threads waiting on iteration to be over.
581   NoSafepointVerifier nsv;
582 
583   MutexLocker ml(CodeCache_lock, Mutex::_no_safepoint_check_flag);
584 
585   if (_finished_workers > 0) {
586     // Some threads have already finished. We are now in rampdown: we are now
587     // waiting for all currently recorded workers to finish. No new workers
588     // should start.
589     return;
590   }
591 
592   // Record a new worker and initialize the snapshot if it is a first visitor.
593   if (_started_workers++ == 0) {
594     _table_snapshot = _table->snapshot_for_iteration();
595   }
596 
597   // All set, relinquish the lock and go concurrent.
598   {
599     MutexUnlocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
600     _table_snapshot->concurrent_nmethods_do(cl);
601   }
602 
603   // Record completion. Last worker shuts down the iterator and notifies any waiters.
604   uint count = ++_finished_workers;
605   if (count == _started_workers) {
606     _table->finish_iteration(_table_snapshot);
607     CodeCache_lock->notify_all();
608   }
609 }