1 /*
  2  * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2021, Red Hat, Inc. and/or its affiliates.
  4  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "classfile/javaClasses.hpp"
 29 #include "gc/shared/workerThread.hpp"
 30 #include "gc/shenandoah/shenandoahGeneration.hpp"
 31 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
 32 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
 33 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 34 #include "gc/shenandoah/shenandoahUtils.hpp"
 35 #include "runtime/atomic.hpp"
 36 #include "logging/log.hpp"
 37 
 38 static ReferenceType reference_type(oop reference) {
 39   return InstanceKlass::cast(reference->klass())->reference_type();
 40 }
 41 
 42 static const char* reference_type_name(ReferenceType type) {
 43   switch (type) {
 44     case REF_SOFT:
 45       return "Soft";
 46 
 47     case REF_WEAK:
 48       return "Weak";
 49 
 50     case REF_FINAL:
 51       return "Final";
 52 
 53     case REF_PHANTOM:
 54       return "Phantom";
 55 
 56     default:
 57       ShouldNotReachHere();
 58       return nullptr;
 59   }
 60 }
 61 
 62 template <typename T>
 63 static void card_mark_barrier(T* field, oop value) {
 64   ShenandoahHeap* heap = ShenandoahHeap::heap();
 65   assert(heap->is_in_or_null(value), "Should be in heap");
 66   assert(ShenandoahCardBarrier, "Card-mark barrier should be on");
 67   if (heap->is_in_old(field) && heap->is_in_young(value)) {
 68     // For Shenandoah, each generation collects all the _referents_ that belong to the
 69     // collected generation. We can end up with discovered lists that contain a mixture
 70     // of old and young _references_. These references are linked together through the
 71     // discovered field in java.lang.Reference. In some cases, creating or editing this
 72     // list may result in the creation of _new_ old-to-young pointers which must dirty
 73     // the corresponding card. Failing to do this may cause heap verification errors and
 74     // lead to incorrect GC behavior.
 75     heap->card_scan()->mark_card_as_dirty(reinterpret_cast<HeapWord*>(field));
 76   }
 77 }
 78 
 79 template <typename T>
 80 static void set_oop_field(T* field, oop value);
 81 
 82 template <>
 83 void set_oop_field<oop>(oop* field, oop value) {
 84   *field = value;
 85   if (ShenandoahCardBarrier) {
 86     card_mark_barrier(field, value);
 87   }
 88 }
 89 
 90 template <>
 91 void set_oop_field<narrowOop>(narrowOop* field, oop value) {
 92   *field = CompressedOops::encode(value);
 93   if (ShenandoahCardBarrier) {
 94     card_mark_barrier(field, value);
 95   }
 96 }
 97 
 98 static oop lrb(oop obj) {
 99   if (obj != nullptr && ShenandoahHeap::heap()->marking_context()->is_marked(obj)) {
100     return ShenandoahBarrierSet::barrier_set()->load_reference_barrier(obj);
101   } else {
102     return obj;
103   }
104 }
105 
106 template <typename T>
107 static volatile T* reference_referent_addr(oop reference) {
108   return (volatile T*)java_lang_ref_Reference::referent_addr_raw(reference);
109 }
110 
111 template <typename T>
112 static oop reference_referent(oop reference) {
113   T heap_oop = Atomic::load(reference_referent_addr<T>(reference));
114   return CompressedOops::decode(heap_oop);
115 }
116 
117 static void reference_clear_referent(oop reference) {
118   java_lang_ref_Reference::clear_referent_raw(reference);
119 }
120 
121 template <typename T>
122 static T* reference_discovered_addr(oop reference) {
123   return reinterpret_cast<T*>(java_lang_ref_Reference::discovered_addr_raw(reference));
124 }
125 
126 template <typename T>
127 static oop reference_discovered(oop reference) {
128   T heap_oop = *reference_discovered_addr<T>(reference);
129   return lrb(CompressedOops::decode(heap_oop));
130 }
131 
132 template <typename T>
133 static void reference_set_discovered(oop reference, oop discovered);
134 
135 template <>
136 void reference_set_discovered<oop>(oop reference, oop discovered) {
137   *reference_discovered_addr<oop>(reference) = discovered;
138 }
139 
140 template <>
141 void reference_set_discovered<narrowOop>(oop reference, oop discovered) {
142   *reference_discovered_addr<narrowOop>(reference) = CompressedOops::encode(discovered);
143 }
144 
145 template<typename T>
146 static bool reference_cas_discovered(oop reference, oop discovered) {
147   T* addr = reinterpret_cast<T *>(java_lang_ref_Reference::discovered_addr_raw(reference));
148   return ShenandoahHeap::atomic_update_oop_check(discovered, addr, nullptr);
149 }
150 
151 template <typename T>
152 static T* reference_next_addr(oop reference) {
153   return reinterpret_cast<T*>(java_lang_ref_Reference::next_addr_raw(reference));
154 }
155 
156 template <typename T>
157 static oop reference_next(oop reference) {
158   T heap_oop = RawAccess<>::oop_load(reference_next_addr<T>(reference));
159   return lrb(CompressedOops::decode(heap_oop));
160 }
161 
162 static void reference_set_next(oop reference, oop next) {
163   java_lang_ref_Reference::set_next_raw(reference, next);
164 }
165 
166 static void soft_reference_update_clock() {
167   const jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
168   java_lang_ref_SoftReference::set_clock(now);
169 }
170 
171 ShenandoahRefProcThreadLocal::ShenandoahRefProcThreadLocal() :
172   _discovered_list(nullptr),
173   _encountered_count(),
174   _discovered_count(),
175   _enqueued_count() {
176 }
177 
178 void ShenandoahRefProcThreadLocal::reset() {
179   _discovered_list = nullptr;
180   _mark_closure = nullptr;
181   for (uint i = 0; i < reference_type_count; i++) {
182     _encountered_count[i] = 0;
183     _discovered_count[i] = 0;
184     _enqueued_count[i] = 0;
185   }
186 }
187 
188 template <typename T>
189 T* ShenandoahRefProcThreadLocal::discovered_list_addr() {
190   return reinterpret_cast<T*>(&_discovered_list);
191 }
192 
193 template <>
194 oop ShenandoahRefProcThreadLocal::discovered_list_head<oop>() const {
195   return *reinterpret_cast<const oop*>(&_discovered_list);
196 }
197 
198 template <>
199 oop ShenandoahRefProcThreadLocal::discovered_list_head<narrowOop>() const {
200   return CompressedOops::decode(*reinterpret_cast<const narrowOop*>(&_discovered_list));
201 }
202 
203 template <>
204 void ShenandoahRefProcThreadLocal::set_discovered_list_head<narrowOop>(oop head) {
205   *discovered_list_addr<narrowOop>() = CompressedOops::encode(head);
206 }
207 
208 template <>
209 void ShenandoahRefProcThreadLocal::set_discovered_list_head<oop>(oop head) {
210   *discovered_list_addr<oop>() = head;
211 }
212 
213 ShenandoahReferenceProcessor::ShenandoahReferenceProcessor(uint max_workers) :
214   _soft_reference_policy(nullptr),
215   _ref_proc_thread_locals(NEW_C_HEAP_ARRAY(ShenandoahRefProcThreadLocal, max_workers, mtGC)),
216   _pending_list(nullptr),
217   _pending_list_tail(&_pending_list),
218   _iterate_discovered_list_id(0U),
219   _stats() {
220   for (size_t i = 0; i < max_workers; i++) {
221     _ref_proc_thread_locals[i].reset();
222   }
223 }
224 
225 void ShenandoahReferenceProcessor::reset_thread_locals() {
226   uint max_workers = ShenandoahHeap::heap()->max_workers();
227   for (uint i = 0; i < max_workers; i++) {
228     _ref_proc_thread_locals[i].reset();
229   }
230 }
231 
232 void ShenandoahReferenceProcessor::set_mark_closure(uint worker_id, ShenandoahMarkRefsSuperClosure* mark_closure) {
233   _ref_proc_thread_locals[worker_id].set_mark_closure(mark_closure);
234 }
235 
236 void ShenandoahReferenceProcessor::set_soft_reference_policy(bool clear) {
237   static AlwaysClearPolicy always_clear_policy;
238   static LRUMaxHeapPolicy lru_max_heap_policy;
239 
240   if (clear) {
241     log_info(gc, ref)("Clearing All SoftReferences");
242     _soft_reference_policy = &always_clear_policy;
243   } else {
244     _soft_reference_policy = &lru_max_heap_policy;
245   }
246 
247   _soft_reference_policy->setup();
248 }
249 
250 template <typename T>
251 bool ShenandoahReferenceProcessor::is_inactive(oop reference, oop referent, ReferenceType type) const {
252   if (type == REF_FINAL) {
253     // A FinalReference is inactive if its next field is non-null. An application can't
254     // call enqueue() or clear() on a FinalReference.
255     return reference_next<T>(reference) != nullptr;
256   } else {
257     // A non-FinalReference is inactive if the referent is null. The referent can only
258     // be null if the application called Reference.enqueue() or Reference.clear().
259     return referent == nullptr;
260   }
261 }
262 
263 bool ShenandoahReferenceProcessor::is_strongly_live(oop referent) const {
264   return ShenandoahHeap::heap()->marking_context()->is_marked_strong(referent);
265 }
266 
267 bool ShenandoahReferenceProcessor::is_softly_live(oop reference, ReferenceType type) const {
268   if (type != REF_SOFT) {
269     // Not a SoftReference
270     return false;
271   }
272 
273   // Ask SoftReference policy
274   const jlong clock = java_lang_ref_SoftReference::clock();
275   assert(clock != 0, "Clock not initialized");
276   assert(_soft_reference_policy != nullptr, "Policy not initialized");
277   return !_soft_reference_policy->should_clear_reference(reference, clock);
278 }
279 
280 template <typename T>
281 bool ShenandoahReferenceProcessor::should_discover(oop reference, ReferenceType type) const {
282   T* referent_addr = (T*) java_lang_ref_Reference::referent_addr_raw(reference);
283   T heap_oop = RawAccess<>::oop_load(referent_addr);
284   oop referent = CompressedOops::decode(heap_oop);
285   ShenandoahHeap* heap = ShenandoahHeap::heap();
286 
287   if (is_inactive<T>(reference, referent, type)) {
288     log_trace(gc,ref)("Reference inactive: " PTR_FORMAT, p2i(reference));
289     return false;
290   }
291 
292   if (is_strongly_live(referent)) {
293     log_trace(gc,ref)("Reference strongly live: " PTR_FORMAT, p2i(reference));
294     return false;
295   }
296 
297   if (is_softly_live(reference, type)) {
298     log_trace(gc,ref)("Reference softly live: " PTR_FORMAT, p2i(reference));
299     return false;
300   }
301 
302   if (!heap->is_in_active_generation(referent)) {
303     log_trace(gc,ref)("Referent outside of active generation: " PTR_FORMAT, p2i(referent));
304     return false;
305   }
306 
307   return true;
308 }
309 
310 template <typename T>
311 bool ShenandoahReferenceProcessor::should_drop(oop reference, ReferenceType type) const {
312   const oop referent = reference_referent<T>(reference);
313   if (referent == nullptr) {
314     // Reference has been cleared, by a call to Reference.enqueue()
315     // or Reference.clear() from the application, which means we
316     // should drop the reference.
317     return true;
318   }
319 
320   // Check if the referent is still alive, in which case we should
321   // drop the reference.
322   if (type == REF_PHANTOM) {
323     return ShenandoahHeap::heap()->complete_marking_context()->is_marked(referent);
324   } else {
325     return ShenandoahHeap::heap()->complete_marking_context()->is_marked_strong(referent);
326   }
327 }
328 
329 template <typename T>
330 void ShenandoahReferenceProcessor::make_inactive(oop reference, ReferenceType type) const {
331   if (type == REF_FINAL) {
332     // Don't clear referent. It is needed by the Finalizer thread to make the call
333     // to finalize(). A FinalReference is instead made inactive by self-looping the
334     // next field. An application can't call FinalReference.enqueue(), so there is
335     // no race to worry about when setting the next field.
336     assert(reference_next<T>(reference) == nullptr, "Already inactive");
337     assert(ShenandoahHeap::heap()->marking_context()->is_marked(reference_referent<T>(reference)), "only make inactive final refs with alive referents");
338     reference_set_next(reference, reference);
339   } else {
340     // Clear referent
341     reference_clear_referent(reference);
342   }
343 }
344 
345 template <typename T>
346 bool ShenandoahReferenceProcessor::discover(oop reference, ReferenceType type, uint worker_id) {
347   if (!should_discover<T>(reference, type)) {
348     // Not discovered
349     return false;
350   }
351 
352   if (reference_discovered<T>(reference) != nullptr) {
353     // Already discovered. This can happen if the reference is marked finalizable first, and then strong,
354     // in which case it will be seen 2x by marking.
355     log_trace(gc,ref)("Reference already discovered: " PTR_FORMAT, p2i(reference));
356     return true;
357   }
358 
359   if (type == REF_FINAL) {
360     ShenandoahMarkRefsSuperClosure* cl = _ref_proc_thread_locals[worker_id].mark_closure();
361     bool weak = cl->is_weak();
362     cl->set_weak(true);
363     if (UseCompressedOops) {
364       cl->do_oop(reinterpret_cast<narrowOop*>(java_lang_ref_Reference::referent_addr_raw(reference)));
365     } else {
366       cl->do_oop(reinterpret_cast<oop*>(java_lang_ref_Reference::referent_addr_raw(reference)));
367     }
368     cl->set_weak(weak);
369   }
370 
371   // Add reference to discovered list
372   // Each worker thread has a private copy of refproc_data, which includes a private discovered list.  This means
373   // there's no risk that a different worker thread will try to manipulate my discovered list head while I'm making
374   // reference the head of my discovered list.
375   ShenandoahRefProcThreadLocal& refproc_data = _ref_proc_thread_locals[worker_id];
376   oop discovered_head = refproc_data.discovered_list_head<T>();
377   if (discovered_head == nullptr) {
378     // Self-loop tail of list. We distinguish discovered from not-discovered references by looking at their
379     // discovered field: if it is null, then it is not-yet discovered, otherwise it is discovered
380     discovered_head = reference;
381   }
382   if (reference_cas_discovered<T>(reference, discovered_head)) {
383     // We successfully set this reference object's next pointer to discovered_head.  This marks reference as discovered.
384     // If reference_cas_discovered fails, that means some other worker thread took credit for discovery of this reference,
385     // and that other thread will place reference on its discovered list, so I can ignore reference.
386 
387     // In case we have created an interesting pointer, mark the remembered set card as dirty.
388     ShenandoahHeap* heap = ShenandoahHeap::heap();
389     if (ShenandoahCardBarrier) {
390       T* addr = reinterpret_cast<T*>(java_lang_ref_Reference::discovered_addr_raw(reference));
391       card_mark_barrier(addr, discovered_head);
392     }
393 
394     // Make the discovered_list_head point to reference.
395     refproc_data.set_discovered_list_head<T>(reference);
396     assert(refproc_data.discovered_list_head<T>() == reference, "reference must be new discovered head");
397     log_trace(gc, ref)("Discovered Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
398     _ref_proc_thread_locals[worker_id].inc_discovered(type);
399   }
400   return true;
401 }
402 
403 bool ShenandoahReferenceProcessor::discover_reference(oop reference, ReferenceType type) {
404   if (!RegisterReferences) {
405     // Reference processing disabled
406     return false;
407   }
408 
409   log_trace(gc, ref)("Encountered Reference: " PTR_FORMAT " (%s, %s)",
410           p2i(reference), reference_type_name(type), ShenandoahHeap::heap()->heap_region_containing(reference)->affiliation_name());
411   uint worker_id = WorkerThread::worker_id();
412   _ref_proc_thread_locals[worker_id].inc_encountered(type);
413 
414   if (UseCompressedOops) {
415     return discover<narrowOop>(reference, type, worker_id);
416   } else {
417     return discover<oop>(reference, type, worker_id);
418   }
419 }
420 
421 template <typename T>
422 oop ShenandoahReferenceProcessor::drop(oop reference, ReferenceType type) {
423   log_trace(gc, ref)("Dropped Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
424 
425   ShenandoahHeap* heap = ShenandoahHeap::heap();
426   oop referent = reference_referent<T>(reference);
427   assert(referent == nullptr || heap->marking_context()->is_marked(referent), "only drop references with alive referents");
428 
429   // Unlink and return next in list
430   oop next = reference_discovered<T>(reference);
431   reference_set_discovered<T>(reference, nullptr);
432   // When this reference was discovered, it would not have been marked. If it ends up surviving
433   // the cycle, we need to dirty the card if the reference is old and the referent is young.  Note
434   // that if the reference is not dropped, then its pointer to the referent will be nulled before
435   // evacuation begins so card does not need to be dirtied.
436   if (heap->mode()->is_generational() && heap->is_in_old(reference) && heap->is_in_young(referent)) {
437     // Note: would be sufficient to mark only the card that holds the start of this Reference object.
438     heap->card_scan()->mark_range_as_dirty(cast_from_oop<HeapWord*>(reference), reference->size());
439   }
440   return next;
441 }
442 
443 template <typename T>
444 T* ShenandoahReferenceProcessor::keep(oop reference, ReferenceType type, uint worker_id) {
445   log_trace(gc, ref)("Enqueued Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
446 
447   // Update statistics
448   _ref_proc_thread_locals[worker_id].inc_enqueued(type);
449 
450   // Make reference inactive
451   make_inactive<T>(reference, type);
452 
453   // Return next in list
454   return reference_discovered_addr<T>(reference);
455 }
456 
457 template <typename T>
458 void ShenandoahReferenceProcessor::process_references(ShenandoahRefProcThreadLocal& refproc_data, uint worker_id) {
459   log_trace(gc, ref)("Processing discovered list #%u : " PTR_FORMAT, worker_id, p2i(refproc_data.discovered_list_head<T>()));
460   T* list = refproc_data.discovered_list_addr<T>();
461   // The list head is basically a GC root, we need to resolve and update it,
462   // otherwise we will later swap a from-space ref into Universe::pending_list().
463   if (!CompressedOops::is_null(*list)) {
464     oop first_resolved = lrb(CompressedOops::decode_not_null(*list));
465     set_oop_field(list, first_resolved);
466   }
467   T* p = list;
468   while (true) {
469     const oop reference = lrb(CompressedOops::decode(*p));
470     if (reference == nullptr) {
471       break;
472     }
473     log_trace(gc, ref)("Processing reference: " PTR_FORMAT, p2i(reference));
474     const ReferenceType type = reference_type(reference);
475 
476     if (should_drop<T>(reference, type)) {
477       set_oop_field(p, drop<T>(reference, type));
478     } else {
479       p = keep<T>(reference, type, worker_id);
480     }
481 
482     const oop discovered = lrb(reference_discovered<T>(reference));
483     if (reference == discovered) {
484       // Reset terminating self-loop to null
485       reference_set_discovered<T>(reference, oop(nullptr));
486       break;
487     }
488   }
489 
490   // Prepend discovered references to internal pending list
491   // set_oop_field maintains the card mark barrier as this list is constructed.
492   if (!CompressedOops::is_null(*list)) {
493     oop head = lrb(CompressedOops::decode_not_null(*list));
494     shenandoah_assert_not_in_cset_except(&head, head, ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahLoadRefBarrier);
495     oop prev = Atomic::xchg(&_pending_list, head);
496     set_oop_field(p, prev);
497     if (prev == nullptr) {
498       // First to prepend to list, record tail
499       _pending_list_tail = reinterpret_cast<void*>(p);
500     }
501 
502     // Clear discovered list
503     set_oop_field(list, oop(nullptr));
504   }
505 }
506 
507 void ShenandoahReferenceProcessor::work() {
508   // Process discovered references
509   uint max_workers = ShenandoahHeap::heap()->max_workers();
510   uint worker_id = Atomic::add(&_iterate_discovered_list_id, 1U, memory_order_relaxed) - 1;
511   while (worker_id < max_workers) {
512     if (UseCompressedOops) {
513       process_references<narrowOop>(_ref_proc_thread_locals[worker_id], worker_id);
514     } else {
515       process_references<oop>(_ref_proc_thread_locals[worker_id], worker_id);
516     }
517     worker_id = Atomic::add(&_iterate_discovered_list_id, 1U, memory_order_relaxed) - 1;
518   }
519 }
520 
521 class ShenandoahReferenceProcessorTask : public WorkerTask {
522 private:
523   bool const                          _concurrent;
524   ShenandoahPhaseTimings::Phase const _phase;
525   ShenandoahReferenceProcessor* const _reference_processor;
526 
527 public:
528   ShenandoahReferenceProcessorTask(ShenandoahPhaseTimings::Phase phase, bool concurrent, ShenandoahReferenceProcessor* reference_processor) :
529     WorkerTask("ShenandoahReferenceProcessorTask"),
530     _concurrent(concurrent),
531     _phase(phase),
532     _reference_processor(reference_processor) {
533   }
534 
535   virtual void work(uint worker_id) {
536     if (_concurrent) {
537       ShenandoahConcurrentWorkerSession worker_session(worker_id);
538       ShenandoahWorkerTimingsTracker x(_phase, ShenandoahPhaseTimings::WeakRefProc, worker_id);
539       _reference_processor->work();
540     } else {
541       ShenandoahParallelWorkerSession worker_session(worker_id);
542       ShenandoahWorkerTimingsTracker x(_phase, ShenandoahPhaseTimings::WeakRefProc, worker_id);
543       _reference_processor->work();
544     }
545   }
546 };
547 
548 void ShenandoahReferenceProcessor::process_references(ShenandoahPhaseTimings::Phase phase, WorkerThreads* workers, bool concurrent) {
549 
550   Atomic::release_store_fence(&_iterate_discovered_list_id, 0U);
551 
552   // Process discovered lists
553   ShenandoahReferenceProcessorTask task(phase, concurrent, this);
554   workers->run_task(&task);
555 
556   // Update SoftReference clock
557   soft_reference_update_clock();
558 
559   // Collect, log and trace statistics
560   collect_statistics();
561 
562   enqueue_references(concurrent);
563 }
564 
565 void ShenandoahReferenceProcessor::enqueue_references_locked() {
566   // Prepend internal pending list to external pending list
567   shenandoah_assert_not_in_cset_except(&_pending_list, _pending_list, ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahLoadRefBarrier);
568 
569   // During reference processing, we maintain a local list of references that are identified by
570   //   _pending_list and _pending_list_tail.  _pending_list_tail points to the next field of the last Reference object on
571   //   the local list.
572   //
573   // There is also a global list of reference identified by Universe::_reference_pending_list
574 
575   // The following code has the effect of:
576   //  1. Making the global Universe::_reference_pending_list point to my local list
577   //  2. Overwriting the next field of the last Reference on my local list to point at the previous head of the
578   //     global Universe::_reference_pending_list
579 
580   oop former_head_of_global_list = Universe::swap_reference_pending_list(_pending_list);
581   if (UseCompressedOops) {
582     set_oop_field<narrowOop>(reinterpret_cast<narrowOop*>(_pending_list_tail), former_head_of_global_list);
583   } else {
584     set_oop_field<oop>(reinterpret_cast<oop*>(_pending_list_tail), former_head_of_global_list);
585   }
586 }
587 
588 void ShenandoahReferenceProcessor::enqueue_references(bool concurrent) {
589   if (_pending_list == nullptr) {
590     // Nothing to enqueue
591     return;
592   }
593   if (!concurrent) {
594     // When called from mark-compact or degen-GC, the locking is done by the VMOperation,
595     enqueue_references_locked();
596   } else {
597     // Heap_lock protects external pending list
598     MonitorLocker ml(Heap_lock);
599 
600     enqueue_references_locked();
601 
602     // Notify ReferenceHandler thread
603     ml.notify_all();
604   }
605 
606   // Reset internal pending list
607   _pending_list = nullptr;
608   _pending_list_tail = &_pending_list;
609 }
610 
611 template<typename T>
612 void ShenandoahReferenceProcessor::clean_discovered_list(T* list) {
613   T discovered = *list;
614   while (!CompressedOops::is_null(discovered)) {
615     oop discovered_ref = CompressedOops::decode_not_null(discovered);
616     set_oop_field<T>(list, oop(nullptr));
617     list = reference_discovered_addr<T>(discovered_ref);
618     discovered = *list;
619   }
620 }
621 
622 void ShenandoahReferenceProcessor::abandon_partial_discovery() {
623   uint max_workers = ShenandoahHeap::heap()->max_workers();
624   for (uint index = 0; index < max_workers; index++) {
625     if (UseCompressedOops) {
626       clean_discovered_list<narrowOop>(_ref_proc_thread_locals[index].discovered_list_addr<narrowOop>());
627     } else {
628       clean_discovered_list<oop>(_ref_proc_thread_locals[index].discovered_list_addr<oop>());
629     }
630   }
631   if (_pending_list != nullptr) {
632     oop pending = _pending_list;
633     _pending_list = nullptr;
634     if (UseCompressedOops) {
635       narrowOop* list = reference_discovered_addr<narrowOop>(pending);
636       clean_discovered_list<narrowOop>(list);
637     } else {
638       oop* list = reference_discovered_addr<oop>(pending);
639       clean_discovered_list<oop>(list);
640     }
641   }
642   _pending_list_tail = &_pending_list;
643 }
644 
645 void ShenandoahReferenceProcessor::collect_statistics() {
646   Counters encountered = {};
647   Counters discovered = {};
648   Counters enqueued = {};
649   uint max_workers = ShenandoahHeap::heap()->max_workers();
650   for (uint i = 0; i < max_workers; i++) {
651     for (size_t type = 0; type < reference_type_count; type++) {
652       encountered[type] += _ref_proc_thread_locals[i].encountered((ReferenceType)type);
653       discovered[type] += _ref_proc_thread_locals[i].discovered((ReferenceType)type);
654       enqueued[type] += _ref_proc_thread_locals[i].enqueued((ReferenceType)type);
655     }
656   }
657 
658   _stats = ReferenceProcessorStats(discovered[REF_SOFT],
659                                    discovered[REF_WEAK],
660                                    discovered[REF_FINAL],
661                                    discovered[REF_PHANTOM]);
662 
663   log_info(gc,ref)("Encountered references: Soft: " SIZE_FORMAT ", Weak: " SIZE_FORMAT ", Final: " SIZE_FORMAT ", Phantom: " SIZE_FORMAT,
664                    encountered[REF_SOFT], encountered[REF_WEAK], encountered[REF_FINAL], encountered[REF_PHANTOM]);
665   log_info(gc,ref)("Discovered  references: Soft: " SIZE_FORMAT ", Weak: " SIZE_FORMAT ", Final: " SIZE_FORMAT ", Phantom: " SIZE_FORMAT,
666                    discovered[REF_SOFT], discovered[REF_WEAK], discovered[REF_FINAL], discovered[REF_PHANTOM]);
667   log_info(gc,ref)("Enqueued    references: Soft: " SIZE_FORMAT ", Weak: " SIZE_FORMAT ", Final: " SIZE_FORMAT ", Phantom: " SIZE_FORMAT,
668                    enqueued[REF_SOFT], enqueued[REF_WEAK], enqueued[REF_FINAL], enqueued[REF_PHANTOM]);
669 }