Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/oops/instanceRefKlass.cpp
48693 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 "oops/oop.pcgc.inline.hpp"45#endif // INCLUDE_ALL_GCS4647PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC4849template <class T>50void specialized_oop_follow_contents(InstanceRefKlass* ref, oop obj) {51T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);52T heap_oop = oopDesc::load_heap_oop(referent_addr);53debug_only(54if(TraceReferenceGC && PrintGCDetails) {55gclog_or_tty->print_cr("InstanceRefKlass::oop_follow_contents " INTPTR_FORMAT, (void *)obj);56}57)58if (!oopDesc::is_null(heap_oop)) {59oop referent = oopDesc::decode_heap_oop_not_null(heap_oop);60if (!referent->is_gc_marked() &&61MarkSweep::ref_processor()->discover_reference(obj, ref->reference_type())) {62// reference was discovered, referent will be traversed later63ref->InstanceKlass::oop_follow_contents(obj);64debug_only(65if(TraceReferenceGC && PrintGCDetails) {66gclog_or_tty->print_cr(" Non NULL enqueued " INTPTR_FORMAT, (void *)obj);67}68)69return;70} else {71// treat referent as normal oop72debug_only(73if(TraceReferenceGC && PrintGCDetails) {74gclog_or_tty->print_cr(" Non NULL normal " INTPTR_FORMAT, (void *)obj);75}76)77MarkSweep::mark_and_push(referent_addr);78}79}80T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);81if (ReferenceProcessor::pending_list_uses_discovered_field()) {82// Treat discovered as normal oop, if ref is not "active",83// i.e. if next is non-NULL.84T next_oop = oopDesc::load_heap_oop(next_addr);85if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"86T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);87debug_only(88if(TraceReferenceGC && PrintGCDetails) {89gclog_or_tty->print_cr(" Process discovered as normal "90INTPTR_FORMAT, discovered_addr);91}92)93MarkSweep::mark_and_push(discovered_addr);94}95} else {96#ifdef ASSERT97// In the case of older JDKs which do not use the discovered98// field for the pending list, an inactive ref (next != NULL)99// must always have a NULL discovered field.100oop next = oopDesc::load_decode_heap_oop(next_addr);101oop discovered = java_lang_ref_Reference::discovered(obj);102assert(oopDesc::is_null(next) || oopDesc::is_null(discovered),103err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL discovered field",104(oopDesc*)obj));105#endif106}107// treat next as normal oop. next is a link in the reference queue.108debug_only(109if(TraceReferenceGC && PrintGCDetails) {110gclog_or_tty->print_cr(" Process next as normal " INTPTR_FORMAT, next_addr);111}112)113MarkSweep::mark_and_push(next_addr);114ref->InstanceKlass::oop_follow_contents(obj);115}116117void InstanceRefKlass::oop_follow_contents(oop obj) {118if (UseCompressedOops) {119specialized_oop_follow_contents<narrowOop>(this, obj);120} else {121specialized_oop_follow_contents<oop>(this, obj);122}123}124125#if INCLUDE_ALL_GCS126template <class T>127void specialized_oop_follow_contents(InstanceRefKlass* ref,128ParCompactionManager* cm,129oop obj) {130T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);131T heap_oop = oopDesc::load_heap_oop(referent_addr);132debug_only(133if(TraceReferenceGC && PrintGCDetails) {134gclog_or_tty->print_cr("InstanceRefKlass::oop_follow_contents " INTPTR_FORMAT, (void *)obj);135}136)137if (!oopDesc::is_null(heap_oop)) {138oop referent = oopDesc::decode_heap_oop_not_null(heap_oop);139if (PSParallelCompact::mark_bitmap()->is_unmarked(referent) &&140PSParallelCompact::ref_processor()->141discover_reference(obj, ref->reference_type())) {142// reference already enqueued, referent will be traversed later143ref->InstanceKlass::oop_follow_contents(cm, obj);144debug_only(145if(TraceReferenceGC && PrintGCDetails) {146gclog_or_tty->print_cr(" Non NULL enqueued " INTPTR_FORMAT, (void *)obj);147}148)149return;150} else {151// treat referent as normal oop152debug_only(153if(TraceReferenceGC && PrintGCDetails) {154gclog_or_tty->print_cr(" Non NULL normal " INTPTR_FORMAT, (void *)obj);155}156)157PSParallelCompact::mark_and_push(cm, referent_addr);158}159}160T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);161if (ReferenceProcessor::pending_list_uses_discovered_field()) {162// Treat discovered as normal oop, if ref is not "active",163// i.e. if next is non-NULL.164T next_oop = oopDesc::load_heap_oop(next_addr);165if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"166T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);167debug_only(168if(TraceReferenceGC && PrintGCDetails) {169gclog_or_tty->print_cr(" Process discovered as normal "170INTPTR_FORMAT, discovered_addr);171}172)173PSParallelCompact::mark_and_push(cm, discovered_addr);174}175} else {176#ifdef ASSERT177// In the case of older JDKs which do not use the discovered178// field for the pending list, an inactive ref (next != NULL)179// must always have a NULL discovered field.180T next = oopDesc::load_heap_oop(next_addr);181oop discovered = java_lang_ref_Reference::discovered(obj);182assert(oopDesc::is_null(next) || oopDesc::is_null(discovered),183err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL discovered field",184(oopDesc*)obj));185#endif186}187PSParallelCompact::mark_and_push(cm, next_addr);188ref->InstanceKlass::oop_follow_contents(cm, obj);189}190191void InstanceRefKlass::oop_follow_contents(ParCompactionManager* cm,192oop obj) {193if (UseCompressedOops) {194specialized_oop_follow_contents<narrowOop>(this, cm, obj);195} else {196specialized_oop_follow_contents<oop>(this, cm, obj);197}198}199#endif // INCLUDE_ALL_GCS200201#ifdef ASSERT202template <class T> void trace_reference_gc(const char *s, oop obj,203T* referent_addr,204T* next_addr,205T* discovered_addr) {206if(TraceReferenceGC && PrintGCDetails) {207gclog_or_tty->print_cr("%s obj " INTPTR_FORMAT, s, (address)obj);208gclog_or_tty->print_cr(" referent_addr/* " INTPTR_FORMAT " / "209INTPTR_FORMAT, referent_addr,210referent_addr ?211(address)oopDesc::load_decode_heap_oop(referent_addr) : NULL);212gclog_or_tty->print_cr(" next_addr/* " INTPTR_FORMAT " / "213INTPTR_FORMAT, next_addr,214next_addr ? (address)oopDesc::load_decode_heap_oop(next_addr) : NULL);215gclog_or_tty->print_cr(" discovered_addr/* " INTPTR_FORMAT " / "216INTPTR_FORMAT, discovered_addr,217discovered_addr ?218(address)oopDesc::load_decode_heap_oop(discovered_addr) : NULL);219}220}221#endif222223template <class T> void specialized_oop_adjust_pointers(InstanceRefKlass *ref, oop obj) {224T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);225MarkSweep::adjust_pointer(referent_addr);226T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);227MarkSweep::adjust_pointer(next_addr);228T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);229MarkSweep::adjust_pointer(discovered_addr);230debug_only(trace_reference_gc("InstanceRefKlass::oop_adjust_pointers", obj,231referent_addr, next_addr, discovered_addr);)232}233234int InstanceRefKlass::oop_adjust_pointers(oop obj) {235int size = size_helper();236InstanceKlass::oop_adjust_pointers(obj);237238if (UseCompressedOops) {239specialized_oop_adjust_pointers<narrowOop>(this, obj);240} else {241specialized_oop_adjust_pointers<oop>(this, obj);242}243return size;244}245246#define InstanceRefKlass_SPECIALIZED_OOP_ITERATE(T, nv_suffix, contains) \247T* disc_addr = (T*)java_lang_ref_Reference::discovered_addr(obj); \248if (closure->apply_to_weak_ref_discovered_field()) { \249closure->do_oop##nv_suffix(disc_addr); \250} \251\252T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj); \253T heap_oop = oopDesc::load_heap_oop(referent_addr); \254ReferenceProcessor* rp = closure->_ref_processor; \255if (!oopDesc::is_null(heap_oop)) { \256oop referent = oopDesc::decode_heap_oop_not_null(heap_oop); \257if (!referent->is_gc_marked() && (rp != NULL) && \258rp->discover_reference(obj, reference_type())) { \259return size; \260} else if (contains(referent_addr)) { \261/* treat referent as normal oop */ \262SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\263closure->do_oop##nv_suffix(referent_addr); \264} \265} \266T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj); \267if (ReferenceProcessor::pending_list_uses_discovered_field()) { \268T next_oop = oopDesc::load_heap_oop(next_addr); \269/* Treat discovered as normal oop, if ref is not "active" (next non-NULL) */\270if (!oopDesc::is_null(next_oop) && contains(disc_addr)) { \271/* i.e. ref is not "active" */ \272debug_only( \273if(TraceReferenceGC && PrintGCDetails) { \274gclog_or_tty->print_cr(" Process discovered as normal " \275INTPTR_FORMAT, disc_addr); \276} \277) \278SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\279closure->do_oop##nv_suffix(disc_addr); \280} \281} else { \282/* In the case of older JDKs which do not use the discovered field for */ \283/* the pending list, an inactive ref (next != NULL) must always have a */ \284/* NULL discovered field. */ \285debug_only( \286T next_oop = oopDesc::load_heap_oop(next_addr); \287T disc_oop = oopDesc::load_heap_oop(disc_addr); \288assert(oopDesc::is_null(next_oop) || oopDesc::is_null(disc_oop), \289err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL" \290"discovered field", (oopDesc*)obj)); \291) \292} \293/* treat next as normal oop */ \294if (contains(next_addr)) { \295SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk); \296closure->do_oop##nv_suffix(next_addr); \297} \298return size; \299300301template <class T> bool contains(T *t) { return true; }302303// Macro to define InstanceRefKlass::oop_oop_iterate for virtual/nonvirtual for304// all closures. Macros calling macros above for each oop size.305306#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \307\308int InstanceRefKlass:: \309oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \310/* Get size before changing pointers */ \311SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\312\313int size = InstanceKlass::oop_oop_iterate##nv_suffix(obj, closure); \314\315if (UseCompressedOops) { \316InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, contains); \317} else { \318InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, contains); \319} \320}321322#if INCLUDE_ALL_GCS323#define InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \324\325int InstanceRefKlass:: \326oop_oop_iterate_backwards##nv_suffix(oop obj, OopClosureType* closure) { \327/* Get size before changing pointers */ \328SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\329\330int size = InstanceKlass::oop_oop_iterate_backwards##nv_suffix(obj, closure); \331\332if (UseCompressedOops) { \333InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, contains); \334} else { \335InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, contains); \336} \337}338#endif // INCLUDE_ALL_GCS339340341#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \342\343int InstanceRefKlass:: \344oop_oop_iterate##nv_suffix##_m(oop obj, \345OopClosureType* closure, \346MemRegion mr) { \347SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\348\349int size = InstanceKlass::oop_oop_iterate##nv_suffix##_m(obj, closure, mr); \350if (UseCompressedOops) { \351InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, mr.contains); \352} else { \353InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, mr.contains); \354} \355}356357ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)358ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)359#if INCLUDE_ALL_GCS360ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)361ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)362#endif // INCLUDE_ALL_GCS363ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)364ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)365366#if INCLUDE_ALL_GCS367template <class T>368void specialized_oop_push_contents(InstanceRefKlass *ref,369PSPromotionManager* pm, oop obj) {370T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);371if (PSScavenge::should_scavenge(referent_addr)) {372ReferenceProcessor* rp = PSScavenge::reference_processor();373if (rp->discover_reference(obj, ref->reference_type())) {374// reference already enqueued, referent and next will be traversed later375ref->InstanceKlass::oop_push_contents(pm, obj);376return;377} else {378// treat referent as normal oop379pm->claim_or_forward_depth(referent_addr);380}381}382// Treat discovered as normal oop, if ref is not "active",383// i.e. if next is non-NULL.384T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);385if (ReferenceProcessor::pending_list_uses_discovered_field()) {386T next_oop = oopDesc::load_heap_oop(next_addr);387if (!oopDesc::is_null(next_oop)) { // i.e. ref is not "active"388T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);389debug_only(390if(TraceReferenceGC && PrintGCDetails) {391gclog_or_tty->print_cr(" Process discovered as normal "392INTPTR_FORMAT, discovered_addr);393}394)395if (PSScavenge::should_scavenge(discovered_addr)) {396pm->claim_or_forward_depth(discovered_addr);397}398}399} else {400#ifdef ASSERT401// In the case of older JDKs which do not use the discovered402// field for the pending list, an inactive ref (next != NULL)403// must always have a NULL discovered field.404oop next = oopDesc::load_decode_heap_oop(next_addr);405oop discovered = java_lang_ref_Reference::discovered(obj);406assert(oopDesc::is_null(next) || oopDesc::is_null(discovered),407err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL discovered field",408(oopDesc*)obj));409#endif410}411412// Treat next as normal oop; next is a link in the reference queue.413if (PSScavenge::should_scavenge(next_addr)) {414pm->claim_or_forward_depth(next_addr);415}416ref->InstanceKlass::oop_push_contents(pm, obj);417}418419void InstanceRefKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {420if (UseCompressedOops) {421specialized_oop_push_contents<narrowOop>(this, pm, obj);422} else {423specialized_oop_push_contents<oop>(this, pm, obj);424}425}426427template <class T>428void specialized_oop_update_pointers(InstanceRefKlass *ref,429ParCompactionManager* cm, oop obj) {430T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);431PSParallelCompact::adjust_pointer(referent_addr);432T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);433PSParallelCompact::adjust_pointer(next_addr);434T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);435PSParallelCompact::adjust_pointer(discovered_addr);436debug_only(trace_reference_gc("InstanceRefKlass::oop_update_ptrs", obj,437referent_addr, next_addr, discovered_addr);)438}439440int InstanceRefKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {441InstanceKlass::oop_update_pointers(cm, obj);442if (UseCompressedOops) {443specialized_oop_update_pointers<narrowOop>(this, cm, obj);444} else {445specialized_oop_update_pointers<oop>(this, cm, obj);446}447return size_helper();448}449#endif // INCLUDE_ALL_GCS450451void InstanceRefKlass::update_nonstatic_oop_maps(Klass* k) {452// Clear the nonstatic oop-map entries corresponding to referent453// and nextPending field. They are treated specially by the454// garbage collector.455// The discovered field is used only by the garbage collector456// and is also treated specially.457InstanceKlass* ik = InstanceKlass::cast(k);458459// Check that we have the right class460debug_only(static bool first_time = true);461assert(k == SystemDictionary::Reference_klass() && first_time,462"Invalid update of maps");463debug_only(first_time = false);464assert(ik->nonstatic_oop_map_count() == 1, "just checking");465466OopMapBlock* map = ik->start_of_nonstatic_oop_maps();467468// Check that the current map is (2,4) - currently points at field with469// offset 2 (words) and has 4 map entries.470debug_only(int offset = java_lang_ref_Reference::referent_offset);471debug_only(unsigned int count = ((java_lang_ref_Reference::discovered_offset -472java_lang_ref_Reference::referent_offset)/heapOopSize) + 1);473474if (UseSharedSpaces) {475assert(map->offset() == java_lang_ref_Reference::queue_offset &&476map->count() == 1, "just checking");477} else {478assert(map->offset() == offset && map->count() == count,479"just checking");480481// Update map to (3,1) - point to offset of 3 (words) with 1 map entry.482map->set_offset(java_lang_ref_Reference::queue_offset);483map->set_count(1);484}485}486487488// Verification489490void InstanceRefKlass::oop_verify_on(oop obj, outputStream* st) {491InstanceKlass::oop_verify_on(obj, st);492// Verify referent field493oop referent = java_lang_ref_Reference::referent(obj);494495// We should make this general to all heaps496GenCollectedHeap* gch = NULL;497if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap)498gch = GenCollectedHeap::heap();499500if (referent != NULL) {501guarantee(referent->is_oop(), "referent field heap failed");502}503// Verify next field504oop next = java_lang_ref_Reference::next(obj);505if (next != NULL) {506guarantee(next->is_oop(), "next field verify failed");507guarantee(next->is_instanceRef(), "next field verify failed");508}509}510511bool InstanceRefKlass::owns_pending_list_lock(JavaThread* thread) {512if (java_lang_ref_Reference::pending_list_lock() == NULL) return false;513Handle h_lock(thread, java_lang_ref_Reference::pending_list_lock());514return ObjectSynchronizer::current_thread_holds_lock(thread, h_lock);515}516517void InstanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) {518// we may enter this with pending exception set519PRESERVE_EXCEPTION_MARK; // exceptions are never thrown, needed for TRAPS argument520521// Create a HandleMark in case we retry a GC multiple times.522// Each time we attempt the GC, we allocate the handle below523// to hold the pending list lock. We want to free this handle.524HandleMark hm;525526Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());527ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD);528assert(ObjectSynchronizer::current_thread_holds_lock(529JavaThread::current(), h_lock),530"Locking should have succeeded");531if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;532}533534void InstanceRefKlass::release_and_notify_pending_list_lock(535BasicLock *pending_list_basic_lock) {536// we may enter this with pending exception set537PRESERVE_EXCEPTION_MARK; // exceptions are never thrown, needed for TRAPS argument538539// Create a HandleMark in case we retry a GC multiple times.540// Each time we attempt the GC, we allocate the handle below541// to hold the pending list lock. We want to free this handle.542HandleMark hm;543544Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());545assert(ObjectSynchronizer::current_thread_holds_lock(546JavaThread::current(), h_lock),547"Lock should be held");548// Notify waiters on pending lists lock if there is any reference.549if (java_lang_ref_Reference::pending_list() != NULL) {550ObjectSynchronizer::notifyall(h_lock, THREAD);551}552ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD);553if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;554}555556557