< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.cpp

Print this page

  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 #include "precompiled.hpp"
 27 #include "classfile/javaClasses.hpp"

 28 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
 29 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
 30 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 31 #include "gc/shenandoah/shenandoahUtils.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "logging/log.hpp"
 34 
 35 static ReferenceType reference_type(oop reference) {
 36   return InstanceKlass::cast(reference->klass())->reference_type();
 37 }
 38 
 39 static const char* reference_type_name(ReferenceType type) {
 40   switch (type) {
 41     case REF_SOFT:
 42       return "Soft";
 43 
 44     case REF_WEAK:
 45       return "Weak";
 46 
 47     case REF_FINAL:

239 }
240 
241 bool ShenandoahReferenceProcessor::is_softly_live(oop reference, ReferenceType type) const {
242   if (type != REF_SOFT) {
243     // Not a SoftReference
244     return false;
245   }
246 
247   // Ask SoftReference policy
248   const jlong clock = java_lang_ref_SoftReference::clock();
249   assert(clock != 0, "Clock not initialized");
250   assert(_soft_reference_policy != NULL, "Policy not initialized");
251   return !_soft_reference_policy->should_clear_reference(reference, clock);
252 }
253 
254 template <typename T>
255 bool ShenandoahReferenceProcessor::should_discover(oop reference, ReferenceType type) const {
256   T* referent_addr = (T*) java_lang_ref_Reference::referent_addr_raw(reference);
257   T heap_oop = RawAccess<>::oop_load(referent_addr);
258   oop referent = CompressedOops::decode(heap_oop);

259 
260   if (is_inactive<T>(reference, referent, type)) {
261     log_trace(gc,ref)("Reference inactive: " PTR_FORMAT, p2i(reference));
262     return false;
263   }
264 
265   if (is_strongly_live(referent)) {
266     log_trace(gc,ref)("Reference strongly live: " PTR_FORMAT, p2i(reference));
267     return false;
268   }
269 
270   if (is_softly_live(reference, type)) {
271     log_trace(gc,ref)("Reference softly live: " PTR_FORMAT, p2i(reference));
272     return false;
273   }
274 





275   return true;
276 }
277 
278 template <typename T>
279 bool ShenandoahReferenceProcessor::should_drop(oop reference, ReferenceType type) const {
280   const oop referent = reference_referent<T>(reference);
281   if (referent == NULL) {
282     // Reference has been cleared, by a call to Reference.enqueue()
283     // or Reference.clear() from the application, which means we
284     // should drop the reference.
285     return true;
286   }
287 
288   // Check if the referent is still alive, in which case we should
289   // drop the reference.
290   if (type == REF_PHANTOM) {
291     return ShenandoahHeap::heap()->complete_marking_context()->is_marked(referent);
292   } else {
293     return ShenandoahHeap::heap()->complete_marking_context()->is_marked_strong(referent);
294   }

343   if (discovered_head == NULL) {
344     // Self-loop tail of list. We distinguish discovered from not-discovered references by looking at their
345     // discovered field: if it is NULL, then it is not-yet discovered, otherwise it is discovered
346     discovered_head = reference;
347   }
348   if (reference_cas_discovered<T>(reference, discovered_head)) {
349     refproc_data.set_discovered_list_head<T>(reference);
350     assert(refproc_data.discovered_list_head<T>() == reference, "reference must be new discovered head");
351     log_trace(gc, ref)("Discovered Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
352     _ref_proc_thread_locals[worker_id].inc_discovered(type);
353   }
354   return true;
355 }
356 
357 bool ShenandoahReferenceProcessor::discover_reference(oop reference, ReferenceType type) {
358   if (!RegisterReferences) {
359     // Reference processing disabled
360     return false;
361   }
362 
363   log_trace(gc, ref)("Encountered Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));

364   uint worker_id = ShenandoahThreadLocalData::worker_id(Thread::current());
365   _ref_proc_thread_locals->inc_encountered(type);
366 
367   if (UseCompressedOops) {
368     return discover<narrowOop>(reference, type, worker_id);
369   } else {
370     return discover<oop>(reference, type, worker_id);
371   }
372 }
373 
374 template <typename T>
375 oop ShenandoahReferenceProcessor::drop(oop reference, ReferenceType type) {
376   log_trace(gc, ref)("Dropped Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
377 
378 #ifdef ASSERT
379   oop referent = reference_referent<T>(reference);
380   assert(referent == NULL || ShenandoahHeap::heap()->marking_context()->is_marked(referent),
381          "only drop references with alive referents");
382 #endif
383 
384   // Unlink and return next in list
385   oop next = reference_discovered<T>(reference);
386   reference_set_discovered<T>(reference, NULL);








387   return next;
388 }
389 
390 template <typename T>
391 T* ShenandoahReferenceProcessor::keep(oop reference, ReferenceType type, uint worker_id) {
392   log_trace(gc, ref)("Enqueued Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
393 
394   // Update statistics
395   _ref_proc_thread_locals[worker_id].inc_enqueued(type);
396 
397   // Make reference inactive
398   make_inactive<T>(reference, type);
399 
400   // Return next in list
401   return reference_discovered_addr<T>(reference);
402 }
403 
404 template <typename T>
405 void ShenandoahReferenceProcessor::process_references(ShenandoahRefProcThreadLocal& refproc_data, uint worker_id) {;
406   log_trace(gc, ref)("Processing discovered list #%u : " PTR_FORMAT, worker_id, p2i(refproc_data.discovered_list_head<T>()));

  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 #include "precompiled.hpp"
 27 #include "classfile/javaClasses.hpp"
 28 #include "gc/shenandoah/mode/shenandoahGenerationalMode.hpp"
 29 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
 30 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
 31 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 32 #include "gc/shenandoah/shenandoahUtils.hpp"
 33 #include "runtime/atomic.hpp"
 34 #include "logging/log.hpp"
 35 
 36 static ReferenceType reference_type(oop reference) {
 37   return InstanceKlass::cast(reference->klass())->reference_type();
 38 }
 39 
 40 static const char* reference_type_name(ReferenceType type) {
 41   switch (type) {
 42     case REF_SOFT:
 43       return "Soft";
 44 
 45     case REF_WEAK:
 46       return "Weak";
 47 
 48     case REF_FINAL:

240 }
241 
242 bool ShenandoahReferenceProcessor::is_softly_live(oop reference, ReferenceType type) const {
243   if (type != REF_SOFT) {
244     // Not a SoftReference
245     return false;
246   }
247 
248   // Ask SoftReference policy
249   const jlong clock = java_lang_ref_SoftReference::clock();
250   assert(clock != 0, "Clock not initialized");
251   assert(_soft_reference_policy != NULL, "Policy not initialized");
252   return !_soft_reference_policy->should_clear_reference(reference, clock);
253 }
254 
255 template <typename T>
256 bool ShenandoahReferenceProcessor::should_discover(oop reference, ReferenceType type) const {
257   T* referent_addr = (T*) java_lang_ref_Reference::referent_addr_raw(reference);
258   T heap_oop = RawAccess<>::oop_load(referent_addr);
259   oop referent = CompressedOops::decode(heap_oop);
260   ShenandoahHeap* heap = ShenandoahHeap::heap();
261 
262   if (is_inactive<T>(reference, referent, type)) {
263     log_trace(gc,ref)("Reference inactive: " PTR_FORMAT, p2i(reference));
264     return false;
265   }
266 
267   if (is_strongly_live(referent)) {
268     log_trace(gc,ref)("Reference strongly live: " PTR_FORMAT, p2i(reference));
269     return false;
270   }
271 
272   if (is_softly_live(reference, type)) {
273     log_trace(gc,ref)("Reference softly live: " PTR_FORMAT, p2i(reference));
274     return false;
275   }
276 
277   if (!heap->is_in_active_generation(referent)) {
278     log_trace(gc,ref)("Referent outside of active generation: " PTR_FORMAT, p2i(referent));
279     return false;
280   }
281 
282   return true;
283 }
284 
285 template <typename T>
286 bool ShenandoahReferenceProcessor::should_drop(oop reference, ReferenceType type) const {
287   const oop referent = reference_referent<T>(reference);
288   if (referent == NULL) {
289     // Reference has been cleared, by a call to Reference.enqueue()
290     // or Reference.clear() from the application, which means we
291     // should drop the reference.
292     return true;
293   }
294 
295   // Check if the referent is still alive, in which case we should
296   // drop the reference.
297   if (type == REF_PHANTOM) {
298     return ShenandoahHeap::heap()->complete_marking_context()->is_marked(referent);
299   } else {
300     return ShenandoahHeap::heap()->complete_marking_context()->is_marked_strong(referent);
301   }

350   if (discovered_head == NULL) {
351     // Self-loop tail of list. We distinguish discovered from not-discovered references by looking at their
352     // discovered field: if it is NULL, then it is not-yet discovered, otherwise it is discovered
353     discovered_head = reference;
354   }
355   if (reference_cas_discovered<T>(reference, discovered_head)) {
356     refproc_data.set_discovered_list_head<T>(reference);
357     assert(refproc_data.discovered_list_head<T>() == reference, "reference must be new discovered head");
358     log_trace(gc, ref)("Discovered Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
359     _ref_proc_thread_locals[worker_id].inc_discovered(type);
360   }
361   return true;
362 }
363 
364 bool ShenandoahReferenceProcessor::discover_reference(oop reference, ReferenceType type) {
365   if (!RegisterReferences) {
366     // Reference processing disabled
367     return false;
368   }
369 
370   log_trace(gc, ref)("Encountered Reference: " PTR_FORMAT " (%s, %s)",
371           p2i(reference), reference_type_name(type), affiliation_name(reference));
372   uint worker_id = ShenandoahThreadLocalData::worker_id(Thread::current());
373   _ref_proc_thread_locals->inc_encountered(type);
374 
375   if (UseCompressedOops) {
376     return discover<narrowOop>(reference, type, worker_id);
377   } else {
378     return discover<oop>(reference, type, worker_id);
379   }
380 }
381 
382 template <typename T>
383 oop ShenandoahReferenceProcessor::drop(oop reference, ReferenceType type) {
384   log_trace(gc, ref)("Dropped Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
385 
386   ShenandoahHeap* heap = ShenandoahHeap::heap();
387   oop referent = reference_referent<T>(reference);
388   assert(referent == NULL || heap->marking_context()->is_marked(referent), "only drop references with alive referents");


389 
390   // Unlink and return next in list
391   oop next = reference_discovered<T>(reference);
392   reference_set_discovered<T>(reference, NULL);
393   // When this reference was discovered, it would not have been marked. If it ends up surviving
394   // the cycle, we need to dirty the card if the reference is old and the referent is young.  Note
395   // that if the reference is not dropped, then its pointer to the referent will be nulled before
396   // evacuation begins so card does not need to be dirtied.
397   if (heap->mode()->is_generational() && heap->is_in_old(reference) && heap->is_in_young(referent)) {
398     // Note: would be sufficient to mark only the card that holds the start of this Reference object.
399     heap->card_scan()->mark_range_as_dirty(cast_from_oop<HeapWord*>(reference), reference->size());
400   }
401   return next;
402 }
403 
404 template <typename T>
405 T* ShenandoahReferenceProcessor::keep(oop reference, ReferenceType type, uint worker_id) {
406   log_trace(gc, ref)("Enqueued Reference: " PTR_FORMAT " (%s)", p2i(reference), reference_type_name(type));
407 
408   // Update statistics
409   _ref_proc_thread_locals[worker_id].inc_enqueued(type);
410 
411   // Make reference inactive
412   make_inactive<T>(reference, type);
413 
414   // Return next in list
415   return reference_discovered_addr<T>(reference);
416 }
417 
418 template <typename T>
419 void ShenandoahReferenceProcessor::process_references(ShenandoahRefProcThreadLocal& refproc_data, uint worker_id) {;
420   log_trace(gc, ref)("Processing discovered list #%u : " PTR_FORMAT, worker_id, p2i(refproc_data.discovered_list_head<T>()));
< prev index next >