Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/instanceRefKlass.cpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/javaClasses.hpp"26#include "classfile/systemDictionary.hpp"27#include "gc_implementation/shared/markSweep.inline.hpp"28#include "gc_interface/collectedHeap.hpp"29#include "gc_interface/collectedHeap.inline.hpp"30#include "memory/genCollectedHeap.hpp"31#include "memory/genOopClosures.inline.hpp"32#include "oops/instanceRefKlass.hpp"33#include "oops/oop.inline.hpp"34#include "utilities/preserveException.hpp"35#include "utilities/macros.hpp"36#if INCLUDE_ALL_GCS37#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"38#include "gc_implementation/g1/g1OopClosures.inline.hpp"39#include "gc_implementation/g1/g1RemSet.inline.hpp"40#include "gc_implementation/g1/heapRegionManager.inline.hpp"41#include "gc_implementation/parNew/parOopClosures.inline.hpp"42#include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"43#include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"44#include "gc_implementation/shenandoah/shenandoahOopClosures.inline.hpp"45#include "oops/oop.pcgc.inline.hpp"46#endif // INCLUDE_ALL_GCS4748PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC4950template <class T>51void specialized_oop_follow_contents(InstanceRefKlass* ref, oop obj) {52T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);53T heap_oop = oopDesc::load_heap_oop(referent_addr);54debug_only(55if(TraceReferenceGC && PrintGCDetails) {56gclog_or_tty->print_cr("InstanceRefKlass::oop_follow_contents " INTPTR_FORMAT, (void *)obj);57}58)59if (!oopDesc::is_null(heap_oop)) {60oop referent = oopDesc::decode_heap_oop_not_null(heap_oop);61if (!referent->is_gc_marked() &&62MarkSweep::ref_processor()->discover_reference(obj, ref->reference_type())) {63// reference was discovered, referent will be traversed later64ref->InstanceKlass::oop_follow_contents(obj);65debug_only(66if(TraceReferenceGC && PrintGCDetails) {67gclog_or_tty->print_cr(" Non NULL enqueued " INTPTR_FORMAT, (void *)obj);68}69)70return;71} else {72// treat referent as normal oop73debug_only(74if(TraceReferenceGC && PrintGCDetails) {75gclog_or_tty->print_cr(" Non NULL normal " INTPTR_FORMAT, (void *)obj);76}77)78MarkSweep::mark_and_push(referent_addr);79}80}81T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);82if (ReferenceProcessor::pending_list_uses_discovered_field()) {83// Treat discovered as normal oop, if ref is not "active",84// i.e. if next is non-NULL.85T next_oop = oopDesc::load_heap_oop(next_addr);86if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"87T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);88debug_only(89if(TraceReferenceGC && PrintGCDetails) {90gclog_or_tty->print_cr(" Process discovered as normal "91INTPTR_FORMAT, discovered_addr);92}93)94MarkSweep::mark_and_push(discovered_addr);95}96} else {97#ifdef ASSERT98// In the case of older JDKs which do not use the discovered99// field for the pending list, an inactive ref (next != NULL)100// must always have a NULL discovered field.101oop next = oopDesc::load_decode_heap_oop(next_addr);102oop discovered = java_lang_ref_Reference::discovered(obj);103assert(oopDesc::is_null(next) || oopDesc::is_null(discovered),104err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL discovered field",105(oopDesc*)obj));106#endif107}108// treat next as normal oop. next is a link in the reference queue.109debug_only(110if(TraceReferenceGC && PrintGCDetails) {111gclog_or_tty->print_cr(" Process next as normal " INTPTR_FORMAT, next_addr);112}113)114MarkSweep::mark_and_push(next_addr);115ref->InstanceKlass::oop_follow_contents(obj);116}117118void InstanceRefKlass::oop_follow_contents(oop obj) {119if (UseCompressedOops) {120specialized_oop_follow_contents<narrowOop>(this, obj);121} else {122specialized_oop_follow_contents<oop>(this, obj);123}124}125126#if INCLUDE_ALL_GCS127template <class T>128void specialized_oop_follow_contents(InstanceRefKlass* ref,129ParCompactionManager* cm,130oop obj) {131T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);132T heap_oop = oopDesc::load_heap_oop(referent_addr);133debug_only(134if(TraceReferenceGC && PrintGCDetails) {135gclog_or_tty->print_cr("InstanceRefKlass::oop_follow_contents " INTPTR_FORMAT, (void *)obj);136}137)138if (!oopDesc::is_null(heap_oop)) {139oop referent = oopDesc::decode_heap_oop_not_null(heap_oop);140if (PSParallelCompact::mark_bitmap()->is_unmarked(referent) &&141PSParallelCompact::ref_processor()->142discover_reference(obj, ref->reference_type())) {143// reference already enqueued, referent will be traversed later144ref->InstanceKlass::oop_follow_contents(cm, obj);145debug_only(146if(TraceReferenceGC && PrintGCDetails) {147gclog_or_tty->print_cr(" Non NULL enqueued " INTPTR_FORMAT, (void *)obj);148}149)150return;151} else {152// treat referent as normal oop153debug_only(154if(TraceReferenceGC && PrintGCDetails) {155gclog_or_tty->print_cr(" Non NULL normal " INTPTR_FORMAT, (void *)obj);156}157)158PSParallelCompact::mark_and_push(cm, referent_addr);159}160}161T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);162if (ReferenceProcessor::pending_list_uses_discovered_field()) {163// Treat discovered as normal oop, if ref is not "active",164// i.e. if next is non-NULL.165T next_oop = oopDesc::load_heap_oop(next_addr);166if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"167T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);168debug_only(169if(TraceReferenceGC && PrintGCDetails) {170gclog_or_tty->print_cr(" Process discovered as normal "171INTPTR_FORMAT, discovered_addr);172}173)174PSParallelCompact::mark_and_push(cm, discovered_addr);175}176} else {177#ifdef ASSERT178// In the case of older JDKs which do not use the discovered179// field for the pending list, an inactive ref (next != NULL)180// must always have a NULL discovered field.181T next = oopDesc::load_heap_oop(next_addr);182oop discovered = java_lang_ref_Reference::discovered(obj);183assert(oopDesc::is_null(next) || oopDesc::is_null(discovered),184err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL discovered field",185(oopDesc*)obj));186#endif187}188PSParallelCompact::mark_and_push(cm, next_addr);189ref->InstanceKlass::oop_follow_contents(cm, obj);190}191192void InstanceRefKlass::oop_follow_contents(ParCompactionManager* cm,193oop obj) {194if (UseCompressedOops) {195specialized_oop_follow_contents<narrowOop>(this, cm, obj);196} else {197specialized_oop_follow_contents<oop>(this, cm, obj);198}199}200#endif // INCLUDE_ALL_GCS201202#ifdef ASSERT203template <class T> void trace_reference_gc(const char *s, oop obj,204T* referent_addr,205T* next_addr,206T* discovered_addr) {207if(TraceReferenceGC && PrintGCDetails) {208gclog_or_tty->print_cr("%s obj " INTPTR_FORMAT, s, (address)obj);209gclog_or_tty->print_cr(" referent_addr/* " INTPTR_FORMAT " / "210INTPTR_FORMAT, referent_addr,211referent_addr ?212(address)oopDesc::load_decode_heap_oop(referent_addr) : NULL);213gclog_or_tty->print_cr(" next_addr/* " INTPTR_FORMAT " / "214INTPTR_FORMAT, next_addr,215next_addr ? (address)oopDesc::load_decode_heap_oop(next_addr) : NULL);216gclog_or_tty->print_cr(" discovered_addr/* " INTPTR_FORMAT " / "217INTPTR_FORMAT, discovered_addr,218discovered_addr ?219(address)oopDesc::load_decode_heap_oop(discovered_addr) : NULL);220}221}222#endif223224template <class T> void specialized_oop_adjust_pointers(InstanceRefKlass *ref, oop obj) {225T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);226MarkSweep::adjust_pointer(referent_addr);227T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);228MarkSweep::adjust_pointer(next_addr);229T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);230MarkSweep::adjust_pointer(discovered_addr);231debug_only(trace_reference_gc("InstanceRefKlass::oop_adjust_pointers", obj,232referent_addr, next_addr, discovered_addr);)233}234235int InstanceRefKlass::oop_adjust_pointers(oop obj) {236int size = size_helper();237InstanceKlass::oop_adjust_pointers(obj);238239if (UseCompressedOops) {240specialized_oop_adjust_pointers<narrowOop>(this, obj);241} else {242specialized_oop_adjust_pointers<oop>(this, obj);243}244return size;245}246247#define InstanceRefKlass_SPECIALIZED_OOP_ITERATE(T, nv_suffix, contains) \248T* disc_addr = (T*)java_lang_ref_Reference::discovered_addr(obj); \249if (closure->apply_to_weak_ref_discovered_field()) { \250closure->do_oop##nv_suffix(disc_addr); \251} \252\253T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj); \254T heap_oop = oopDesc::load_heap_oop(referent_addr); \255ReferenceProcessor* rp = closure->_ref_processor; \256if (!oopDesc::is_null(heap_oop)) { \257oop referent = oopDesc::decode_heap_oop_not_null(heap_oop); \258if (!referent->is_gc_marked() && (rp != NULL) && \259rp->discover_reference(obj, reference_type())) { \260return size; \261} else if (contains(referent_addr)) { \262/* treat referent as normal oop */ \263SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\264closure->do_oop##nv_suffix(referent_addr); \265} \266} \267T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj); \268if (ReferenceProcessor::pending_list_uses_discovered_field()) { \269T next_oop = oopDesc::load_heap_oop(next_addr); \270/* Treat discovered as normal oop, if ref is not "active" (next non-NULL) */\271if (!oopDesc::is_null(next_oop) && contains(disc_addr)) { \272/* i.e. ref is not "active" */ \273debug_only( \274if(TraceReferenceGC && PrintGCDetails) { \275gclog_or_tty->print_cr(" Process discovered as normal " \276INTPTR_FORMAT, disc_addr); \277} \278) \279SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\280closure->do_oop##nv_suffix(disc_addr); \281} \282} else { \283/* In the case of older JDKs which do not use the discovered field for */ \284/* the pending list, an inactive ref (next != NULL) must always have a */ \285/* NULL discovered field. */ \286debug_only( \287T next_oop = oopDesc::load_heap_oop(next_addr); \288T disc_oop = oopDesc::load_heap_oop(disc_addr); \289assert(oopDesc::is_null(next_oop) || oopDesc::is_null(disc_oop), \290err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL" \291"discovered field", (oopDesc*)obj)); \292) \293} \294/* treat next as normal oop */ \295if (contains(next_addr)) { \296SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk); \297closure->do_oop##nv_suffix(next_addr); \298} \299return size; \300301302template <class T> bool contains(T *t) { return true; }303304// Macro to define InstanceRefKlass::oop_oop_iterate for virtual/nonvirtual for305// all closures. Macros calling macros above for each oop size.306307#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \308\309int InstanceRefKlass:: \310oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \311/* Get size before changing pointers */ \312SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\313\314int size = InstanceKlass::oop_oop_iterate##nv_suffix(obj, closure); \315\316if (UseCompressedOops) { \317InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, contains); \318} else { \319InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, contains); \320} \321}322323#if INCLUDE_ALL_GCS324#define InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \325\326int InstanceRefKlass:: \327oop_oop_iterate_backwards##nv_suffix(oop obj, OopClosureType* closure) { \328/* Get size before changing pointers */ \329SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\330\331int size = InstanceKlass::oop_oop_iterate_backwards##nv_suffix(obj, closure); \332\333if (UseCompressedOops) { \334InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, contains); \335} else { \336InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, contains); \337} \338}339#endif // INCLUDE_ALL_GCS340341342#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \343\344int InstanceRefKlass:: \345oop_oop_iterate##nv_suffix##_m(oop obj, \346OopClosureType* closure, \347MemRegion mr) { \348SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\349\350int size = InstanceKlass::oop_oop_iterate##nv_suffix##_m(obj, closure, mr); \351if (UseCompressedOops) { \352InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, mr.contains); \353} else { \354InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, mr.contains); \355} \356}357358ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)359ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)360#if INCLUDE_ALL_GCS361ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)362ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)363#endif // INCLUDE_ALL_GCS364ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)365ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)366367#if INCLUDE_ALL_GCS368template <class T>369void specialized_oop_push_contents(InstanceRefKlass *ref,370PSPromotionManager* pm, oop obj) {371T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);372if (PSScavenge::should_scavenge(referent_addr)) {373ReferenceProcessor* rp = PSScavenge::reference_processor();374if (rp->discover_reference(obj, ref->reference_type())) {375// reference already enqueued, referent and next will be traversed later376ref->InstanceKlass::oop_push_contents(pm, obj);377return;378} else {379// treat referent as normal oop380pm->claim_or_forward_depth(referent_addr);381}382}383// Treat discovered as normal oop, if ref is not "active",384// i.e. if next is non-NULL.385T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);386if (ReferenceProcessor::pending_list_uses_discovered_field()) {387T next_oop = oopDesc::load_heap_oop(next_addr);388if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"389T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);390debug_only(391if(TraceReferenceGC && PrintGCDetails) {392gclog_or_tty->print_cr(" Process discovered as normal "393INTPTR_FORMAT, discovered_addr);394}395)396if (PSScavenge::should_scavenge(discovered_addr)) {397pm->claim_or_forward_depth(discovered_addr);398}399}400} else {401#ifdef ASSERT402// In the case of older JDKs which do not use the discovered403// field for the pending list, an inactive ref (next != NULL)404// must always have a NULL discovered field.405oop next = oopDesc::load_decode_heap_oop(next_addr);406oop discovered = java_lang_ref_Reference::discovered(obj);407assert(oopDesc::is_null(next) || oopDesc::is_null(discovered),408err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL discovered field",409(oopDesc*)obj));410#endif411}412413// Treat next as normal oop; next is a link in the reference queue.414if (PSScavenge::should_scavenge(next_addr)) {415pm->claim_or_forward_depth(next_addr);416}417ref->InstanceKlass::oop_push_contents(pm, obj);418}419420void InstanceRefKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {421if (UseCompressedOops) {422specialized_oop_push_contents<narrowOop>(this, pm, obj);423} else {424specialized_oop_push_contents<oop>(this, pm, obj);425}426}427428template <class T>429void specialized_oop_update_pointers(InstanceRefKlass *ref,430ParCompactionManager* cm, oop obj) {431T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);432PSParallelCompact::adjust_pointer(referent_addr);433T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);434PSParallelCompact::adjust_pointer(next_addr);435T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);436PSParallelCompact::adjust_pointer(discovered_addr);437debug_only(trace_reference_gc("InstanceRefKlass::oop_update_ptrs", obj,438referent_addr, next_addr, discovered_addr);)439}440441int InstanceRefKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {442InstanceKlass::oop_update_pointers(cm, obj);443if (UseCompressedOops) {444specialized_oop_update_pointers<narrowOop>(this, cm, obj);445} else {446specialized_oop_update_pointers<oop>(this, cm, obj);447}448return size_helper();449}450#endif // INCLUDE_ALL_GCS451452void InstanceRefKlass::update_nonstatic_oop_maps(Klass* k) {453// Clear the nonstatic oop-map entries corresponding to referent454// and nextPending field. They are treated specially by the455// garbage collector.456// The discovered field is used only by the garbage collector457// and is also treated specially.458InstanceKlass* ik = InstanceKlass::cast(k);459460// Check that we have the right class461debug_only(static bool first_time = true);462assert(k == SystemDictionary::Reference_klass() && first_time,463"Invalid update of maps");464debug_only(first_time = false);465assert(ik->nonstatic_oop_map_count() == 1, "just checking");466467OopMapBlock* map = ik->start_of_nonstatic_oop_maps();468469// Check that the current map is (2,4) - currently points at field with470// offset 2 (words) and has 4 map entries.471debug_only(int offset = java_lang_ref_Reference::referent_offset);472debug_only(unsigned int count = ((java_lang_ref_Reference::discovered_offset -473java_lang_ref_Reference::referent_offset)/heapOopSize) + 1);474475if (UseSharedSpaces) {476assert(map->offset() == java_lang_ref_Reference::queue_offset &&477map->count() == 1, "just checking");478} else {479assert(map->offset() == offset && map->count() == count,480"just checking");481482// Update map to (3,1) - point to offset of 3 (words) with 1 map entry.483map->set_offset(java_lang_ref_Reference::queue_offset);484map->set_count(1);485}486}487488489// Verification490491void InstanceRefKlass::oop_verify_on(oop obj, outputStream* st) {492InstanceKlass::oop_verify_on(obj, st);493// Verify referent field494oop referent = java_lang_ref_Reference::referent(obj);495496// We should make this general to all heaps497GenCollectedHeap* gch = NULL;498if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap)499gch = GenCollectedHeap::heap();500501if (referent != NULL) {502guarantee(referent->is_oop(), "referent field heap failed");503}504// Verify next field505oop next = java_lang_ref_Reference::next(obj);506if (next != NULL) {507guarantee(next->is_oop(), "next field verify failed");508guarantee(next->is_instanceRef(), "next field verify failed");509}510}511512bool InstanceRefKlass::owns_pending_list_lock(JavaThread* thread) {513if (java_lang_ref_Reference::pending_list_lock() == NULL) return false;514Handle h_lock(thread, java_lang_ref_Reference::pending_list_lock());515return ObjectSynchronizer::current_thread_holds_lock(thread, h_lock);516}517518void InstanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) {519// we may enter this with pending exception set520PRESERVE_EXCEPTION_MARK; // exceptions are never thrown, needed for TRAPS argument521522// Create a HandleMark in case we retry a GC multiple times.523// Each time we attempt the GC, we allocate the handle below524// to hold the pending list lock. We want to free this handle.525HandleMark hm;526527Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());528ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD);529assert(ObjectSynchronizer::current_thread_holds_lock(530JavaThread::current(), h_lock),531"Locking should have succeeded");532if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;533}534535void InstanceRefKlass::release_and_notify_pending_list_lock(536BasicLock *pending_list_basic_lock) {537// we may enter this with pending exception set538PRESERVE_EXCEPTION_MARK; // exceptions are never thrown, needed for TRAPS argument539540// Create a HandleMark in case we retry a GC multiple times.541// Each time we attempt the GC, we allocate the handle below542// to hold the pending list lock. We want to free this handle.543HandleMark hm;544545Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());546assert(ObjectSynchronizer::current_thread_holds_lock(547JavaThread::current(), h_lock),548"Lock should be held");549// Notify waiters on pending lists lock if there is any reference.550if (java_lang_ref_Reference::pending_list() != NULL) {551ObjectSynchronizer::notifyall(h_lock, THREAD);552}553ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD);554if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;555}556557558