src/share/vm/memory/referenceProcessor.cpp

Tue, 16 Feb 2016 21:42:29 +0000

author
poonam
date
Tue, 16 Feb 2016 21:42:29 +0000
changeset 8308
6acf14e730dd
parent 7741
35c7330b68e2
child 7994
04ff2f6cd0eb
child 9665
a8441ccaff15
permissions
-rw-r--r--

8072725: Provide more granular levels for GC verification
Summary: Add option VerifySubSet to selectively verify the memory sub-systems
Reviewed-by: kevinw, jmasa

duke@435 1 /*
drchase@6680 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/javaClasses.hpp"
stefank@2314 27 #include "classfile/systemDictionary.hpp"
sla@5237 28 #include "gc_implementation/shared/gcTimer.hpp"
sla@5237 29 #include "gc_implementation/shared/gcTraceTime.hpp"
stefank@2314 30 #include "gc_interface/collectedHeap.hpp"
stefank@2314 31 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 32 #include "memory/referencePolicy.hpp"
stefank@2314 33 #include "memory/referenceProcessor.hpp"
stefank@2314 34 #include "oops/oop.inline.hpp"
stefank@2314 35 #include "runtime/java.hpp"
stefank@2314 36 #include "runtime/jniHandles.hpp"
duke@435 37
drchase@6680 38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 39
ysr@888 40 ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL;
ysr@888 41 ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy = NULL;
ysr@3117 42 bool ReferenceProcessor::_pending_list_uses_discovered_field = false;
johnc@3188 43 jlong ReferenceProcessor::_soft_ref_timestamp_clock = 0;
ysr@888 44
duke@435 45 void referenceProcessor_init() {
duke@435 46 ReferenceProcessor::init_statics();
duke@435 47 }
duke@435 48
duke@435 49 void ReferenceProcessor::init_statics() {
johnc@3339 50 // We need a monotonically non-deccreasing time in ms but
johnc@3339 51 // os::javaTimeMillis() does not guarantee monotonicity.
johnc@3339 52 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
johnc@3188 53
johnc@3188 54 // Initialize the soft ref timestamp clock.
johnc@3188 55 _soft_ref_timestamp_clock = now;
johnc@3188 56 // Also update the soft ref clock in j.l.r.SoftReference
johnc@3188 57 java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock);
duke@435 58
ysr@888 59 _always_clear_soft_ref_policy = new AlwaysClearPolicy();
ysr@888 60 _default_soft_ref_policy = new COMPILER2_PRESENT(LRUMaxHeapPolicy())
ysr@888 61 NOT_COMPILER2(LRUCurrentHeapPolicy());
ysr@888 62 if (_always_clear_soft_ref_policy == NULL || _default_soft_ref_policy == NULL) {
ysr@888 63 vm_exit_during_initialization("Could not allocate reference policy object");
ysr@888 64 }
duke@435 65 guarantee(RefDiscoveryPolicy == ReferenceBasedDiscovery ||
duke@435 66 RefDiscoveryPolicy == ReferentBasedDiscovery,
duke@435 67 "Unrecongnized RefDiscoveryPolicy");
ysr@3117 68 _pending_list_uses_discovered_field = JDK_Version::current().pending_list_uses_discovered_field();
duke@435 69 }
duke@435 70
johnc@3188 71 void ReferenceProcessor::enable_discovery(bool verify_disabled, bool check_no_refs) {
johnc@3188 72 #ifdef ASSERT
johnc@3188 73 // Verify that we're not currently discovering refs
johnc@3188 74 assert(!verify_disabled || !_discovering_refs, "nested call?");
johnc@3188 75
johnc@3188 76 if (check_no_refs) {
johnc@3188 77 // Verify that the discovered lists are empty
johnc@3188 78 verify_no_references_recorded();
johnc@3188 79 }
johnc@3188 80 #endif // ASSERT
johnc@3188 81
johnc@3188 82 // Someone could have modified the value of the static
johnc@3188 83 // field in the j.l.r.SoftReference class that holds the
johnc@3188 84 // soft reference timestamp clock using reflection or
johnc@3188 85 // Unsafe between GCs. Unconditionally update the static
johnc@3188 86 // field in ReferenceProcessor here so that we use the new
johnc@3188 87 // value during reference discovery.
johnc@3188 88
johnc@3188 89 _soft_ref_timestamp_clock = java_lang_ref_SoftReference::clock();
johnc@3188 90 _discovering_refs = true;
johnc@3188 91 }
johnc@3188 92
duke@435 93 ReferenceProcessor::ReferenceProcessor(MemRegion span,
ysr@2651 94 bool mt_processing,
jmasa@3357 95 uint mt_processing_degree,
ysr@2651 96 bool mt_discovery,
jmasa@3357 97 uint mt_discovery_degree,
coleenp@548 98 bool atomic_discovery,
brutisso@6719 99 BoolObjectClosure* is_alive_non_header) :
duke@435 100 _discovering_refs(false),
duke@435 101 _enqueuing_is_done(false),
ysr@2651 102 _is_alive_non_header(is_alive_non_header),
duke@435 103 _processing_is_mt(mt_processing),
duke@435 104 _next_id(0)
duke@435 105 {
duke@435 106 _span = span;
duke@435 107 _discovery_is_atomic = atomic_discovery;
duke@435 108 _discovery_is_mt = mt_discovery;
jmasa@3357 109 _num_q = MAX2(1U, mt_processing_degree);
ysr@2651 110 _max_num_q = MAX2(_num_q, mt_discovery_degree);
johnc@3210 111 _discovered_refs = NEW_C_HEAP_ARRAY(DiscoveredList,
zgu@3900 112 _max_num_q * number_of_subclasses_of_ref(), mtGC);
zgu@3900 113
johnc@3210 114 if (_discovered_refs == NULL) {
duke@435 115 vm_exit_during_initialization("Could not allocated RefProc Array");
duke@435 116 }
johnc@3210 117 _discoveredSoftRefs = &_discovered_refs[0];
jmasa@2188 118 _discoveredWeakRefs = &_discoveredSoftRefs[_max_num_q];
jmasa@2188 119 _discoveredFinalRefs = &_discoveredWeakRefs[_max_num_q];
jmasa@2188 120 _discoveredPhantomRefs = &_discoveredFinalRefs[_max_num_q];
jmasa@7469 121 _discoveredCleanerRefs = &_discoveredPhantomRefs[_max_num_q];
johnc@3210 122
johnc@3210 123 // Initialize all entries to NULL
jmasa@3357 124 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
johnc@3210 125 _discovered_refs[i].set_head(NULL);
johnc@3210 126 _discovered_refs[i].set_length(0);
duke@435 127 }
johnc@3210 128
ysr@2651 129 setup_policy(false /* default soft ref policy */);
duke@435 130 }
duke@435 131
duke@435 132 #ifndef PRODUCT
duke@435 133 void ReferenceProcessor::verify_no_references_recorded() {
duke@435 134 guarantee(!_discovering_refs, "Discovering refs?");
jmasa@3357 135 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
johnc@3210 136 guarantee(_discovered_refs[i].is_empty(),
duke@435 137 "Found non-empty discovered list");
duke@435 138 }
duke@435 139 }
duke@435 140 #endif
duke@435 141
duke@435 142 void ReferenceProcessor::weak_oops_do(OopClosure* f) {
jmasa@3357 143 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
coleenp@548 144 if (UseCompressedOops) {
johnc@3210 145 f->do_oop((narrowOop*)_discovered_refs[i].adr_head());
coleenp@548 146 } else {
johnc@3210 147 f->do_oop((oop*)_discovered_refs[i].adr_head());
coleenp@548 148 }
duke@435 149 }
duke@435 150 }
duke@435 151
coleenp@548 152 void ReferenceProcessor::update_soft_ref_master_clock() {
duke@435 153 // Update (advance) the soft ref master clock field. This must be done
duke@435 154 // after processing the soft ref list.
johnc@3339 155
johnc@3339 156 // We need a monotonically non-deccreasing time in ms but
johnc@3339 157 // os::javaTimeMillis() does not guarantee monotonicity.
johnc@3339 158 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
johnc@3188 159 jlong soft_ref_clock = java_lang_ref_SoftReference::clock();
johnc@3188 160 assert(soft_ref_clock == _soft_ref_timestamp_clock, "soft ref clocks out of sync");
johnc@3188 161
duke@435 162 NOT_PRODUCT(
johnc@3188 163 if (now < _soft_ref_timestamp_clock) {
johnc@3188 164 warning("time warp: "INT64_FORMAT" to "INT64_FORMAT,
johnc@3188 165 _soft_ref_timestamp_clock, now);
duke@435 166 }
duke@435 167 )
johnc@3339 168 // The values of now and _soft_ref_timestamp_clock are set using
johnc@3339 169 // javaTimeNanos(), which is guaranteed to be monotonically
johnc@3339 170 // non-decreasing provided the underlying platform provides such
johnc@3339 171 // a time source (and it is bug free).
johnc@3339 172 // In product mode, however, protect ourselves from non-monotonicty.
johnc@3188 173 if (now > _soft_ref_timestamp_clock) {
johnc@3188 174 _soft_ref_timestamp_clock = now;
duke@435 175 java_lang_ref_SoftReference::set_clock(now);
duke@435 176 }
duke@435 177 // Else leave clock stalled at its old value until time progresses
duke@435 178 // past clock value.
duke@435 179 }
duke@435 180
sla@5237 181 size_t ReferenceProcessor::total_count(DiscoveredList lists[]) {
sla@5237 182 size_t total = 0;
sla@5237 183 for (uint i = 0; i < _max_num_q; ++i) {
sla@5237 184 total += lists[i].length();
sla@5237 185 }
sla@5237 186 return total;
sla@5237 187 }
sla@5237 188
sla@5237 189 ReferenceProcessorStats ReferenceProcessor::process_discovered_references(
duke@435 190 BoolObjectClosure* is_alive,
duke@435 191 OopClosure* keep_alive,
duke@435 192 VoidClosure* complete_gc,
sla@5237 193 AbstractRefProcTaskExecutor* task_executor,
brutisso@6904 194 GCTimer* gc_timer,
brutisso@6904 195 GCId gc_id) {
duke@435 196 NOT_PRODUCT(verify_ok_to_handle_reflists());
duke@435 197
duke@435 198 assert(!enqueuing_is_done(), "If here enqueuing should not be complete");
duke@435 199 // Stop treating discovered references specially.
duke@435 200 disable_discovery();
duke@435 201
johnc@3188 202 // If discovery was concurrent, someone could have modified
johnc@3188 203 // the value of the static field in the j.l.r.SoftReference
johnc@3188 204 // class that holds the soft reference timestamp clock using
johnc@3188 205 // reflection or Unsafe between when discovery was enabled and
johnc@3188 206 // now. Unconditionally update the static field in ReferenceProcessor
johnc@3188 207 // here so that we use the new value during processing of the
johnc@3188 208 // discovered soft refs.
johnc@3188 209
johnc@3188 210 _soft_ref_timestamp_clock = java_lang_ref_SoftReference::clock();
johnc@3188 211
duke@435 212 bool trace_time = PrintGCDetails && PrintReferenceGC;
sla@5237 213
duke@435 214 // Soft references
sla@5237 215 size_t soft_count = 0;
duke@435 216 {
brutisso@6904 217 GCTraceTime tt("SoftReference", trace_time, false, gc_timer, gc_id);
sla@5237 218 soft_count =
sla@5237 219 process_discovered_reflist(_discoveredSoftRefs, _current_soft_ref_policy, true,
sla@5237 220 is_alive, keep_alive, complete_gc, task_executor);
duke@435 221 }
duke@435 222
duke@435 223 update_soft_ref_master_clock();
duke@435 224
duke@435 225 // Weak references
sla@5237 226 size_t weak_count = 0;
duke@435 227 {
brutisso@6904 228 GCTraceTime tt("WeakReference", trace_time, false, gc_timer, gc_id);
sla@5237 229 weak_count =
sla@5237 230 process_discovered_reflist(_discoveredWeakRefs, NULL, true,
sla@5237 231 is_alive, keep_alive, complete_gc, task_executor);
duke@435 232 }
duke@435 233
duke@435 234 // Final references
sla@5237 235 size_t final_count = 0;
duke@435 236 {
brutisso@6904 237 GCTraceTime tt("FinalReference", trace_time, false, gc_timer, gc_id);
sla@5237 238 final_count =
sla@5237 239 process_discovered_reflist(_discoveredFinalRefs, NULL, false,
sla@5237 240 is_alive, keep_alive, complete_gc, task_executor);
duke@435 241 }
duke@435 242
duke@435 243 // Phantom references
sla@5237 244 size_t phantom_count = 0;
duke@435 245 {
brutisso@6904 246 GCTraceTime tt("PhantomReference", trace_time, false, gc_timer, gc_id);
sla@5237 247 phantom_count =
sla@5237 248 process_discovered_reflist(_discoveredPhantomRefs, NULL, false,
sla@5237 249 is_alive, keep_alive, complete_gc, task_executor);
jmasa@7469 250
jmasa@7469 251 // Process cleaners, but include them in phantom statistics. We expect
jmasa@7469 252 // Cleaner references to be temporary, and don't want to deal with
jmasa@7469 253 // possible incompatibilities arising from making it more visible.
jmasa@7469 254 phantom_count +=
kbarrett@7741 255 process_discovered_reflist(_discoveredCleanerRefs, NULL, true,
jmasa@7469 256 is_alive, keep_alive, complete_gc, task_executor);
duke@435 257 }
duke@435 258
duke@435 259 // Weak global JNI references. It would make more sense (semantically) to
duke@435 260 // traverse these simultaneously with the regular weak references above, but
duke@435 261 // that is not how the JDK1.2 specification is. See #4126360. Native code can
duke@435 262 // thus use JNI weak references to circumvent the phantom references and
duke@435 263 // resurrect a "post-mortem" object.
duke@435 264 {
brutisso@6904 265 GCTraceTime tt("JNI Weak Reference", trace_time, false, gc_timer, gc_id);
duke@435 266 if (task_executor != NULL) {
duke@435 267 task_executor->set_single_threaded_mode();
duke@435 268 }
duke@435 269 process_phaseJNI(is_alive, keep_alive, complete_gc);
duke@435 270 }
sla@5237 271
sla@5237 272 return ReferenceProcessorStats(soft_count, weak_count, final_count, phantom_count);
duke@435 273 }
duke@435 274
duke@435 275 #ifndef PRODUCT
duke@435 276 // Calculate the number of jni handles.
coleenp@548 277 uint ReferenceProcessor::count_jni_refs() {
duke@435 278 class AlwaysAliveClosure: public BoolObjectClosure {
duke@435 279 public:
coleenp@548 280 virtual bool do_object_b(oop obj) { return true; }
duke@435 281 };
duke@435 282
duke@435 283 class CountHandleClosure: public OopClosure {
duke@435 284 private:
duke@435 285 int _count;
duke@435 286 public:
duke@435 287 CountHandleClosure(): _count(0) {}
coleenp@548 288 void do_oop(oop* unused) { _count++; }
coleenp@548 289 void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
duke@435 290 int count() { return _count; }
duke@435 291 };
duke@435 292 CountHandleClosure global_handle_count;
duke@435 293 AlwaysAliveClosure always_alive;
duke@435 294 JNIHandles::weak_oops_do(&always_alive, &global_handle_count);
duke@435 295 return global_handle_count.count();
duke@435 296 }
duke@435 297 #endif
duke@435 298
duke@435 299 void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive,
duke@435 300 OopClosure* keep_alive,
duke@435 301 VoidClosure* complete_gc) {
duke@435 302 #ifndef PRODUCT
duke@435 303 if (PrintGCDetails && PrintReferenceGC) {
duke@435 304 unsigned int count = count_jni_refs();
duke@435 305 gclog_or_tty->print(", %u refs", count);
duke@435 306 }
duke@435 307 #endif
duke@435 308 JNIHandles::weak_oops_do(is_alive, keep_alive);
duke@435 309 complete_gc->do_void();
duke@435 310 }
duke@435 311
coleenp@548 312
coleenp@548 313 template <class T>
phh@1558 314 bool enqueue_discovered_ref_helper(ReferenceProcessor* ref,
phh@1558 315 AbstractRefProcTaskExecutor* task_executor) {
coleenp@548 316
duke@435 317 // Remember old value of pending references list
coleenp@548 318 T* pending_list_addr = (T*)java_lang_ref_Reference::pending_list_addr();
coleenp@548 319 T old_pending_list_value = *pending_list_addr;
duke@435 320
duke@435 321 // Enqueue references that are not made active again, and
duke@435 322 // clear the decks for the next collection (cycle).
coleenp@548 323 ref->enqueue_discovered_reflists((HeapWord*)pending_list_addr, task_executor);
pliden@6395 324 // Do the post-barrier on pending_list_addr missed in
pliden@6395 325 // enqueue_discovered_reflist.
pliden@6395 326 oopDesc::bs()->write_ref_field(pending_list_addr, oopDesc::load_decode_heap_oop(pending_list_addr));
duke@435 327
duke@435 328 // Stop treating discovered references specially.
coleenp@548 329 ref->disable_discovery();
duke@435 330
duke@435 331 // Return true if new pending references were added
duke@435 332 return old_pending_list_value != *pending_list_addr;
duke@435 333 }
duke@435 334
coleenp@548 335 bool ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor) {
coleenp@548 336 NOT_PRODUCT(verify_ok_to_handle_reflists());
coleenp@548 337 if (UseCompressedOops) {
coleenp@548 338 return enqueue_discovered_ref_helper<narrowOop>(this, task_executor);
coleenp@548 339 } else {
coleenp@548 340 return enqueue_discovered_ref_helper<oop>(this, task_executor);
coleenp@548 341 }
coleenp@548 342 }
coleenp@548 343
duke@435 344 void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list,
coleenp@548 345 HeapWord* pending_list_addr) {
duke@435 346 // Given a list of refs linked through the "discovered" field
ysr@3117 347 // (java.lang.ref.Reference.discovered), self-loop their "next" field
ysr@3117 348 // thus distinguishing them from active References, then
ysr@3117 349 // prepend them to the pending list.
brutisso@6719 350 //
brutisso@6719 351 // The Java threads will see the Reference objects linked together through
brutisso@6719 352 // the discovered field. Instead of trying to do the write barrier updates
brutisso@6719 353 // in all places in the reference processor where we manipulate the discovered
brutisso@6719 354 // field we make sure to do the barrier here where we anyway iterate through
brutisso@6719 355 // all linked Reference objects. Note that it is important to not dirty any
brutisso@6719 356 // cards during reference processing since this will cause card table
brutisso@6719 357 // verification to fail for G1.
brutisso@6719 358 //
ysr@3117 359 // BKWRD COMPATIBILITY NOTE: For older JDKs (prior to the fix for 4956777),
ysr@3117 360 // the "next" field is used to chain the pending list, not the discovered
ysr@3117 361 // field.
duke@435 362 if (TraceReferenceGC && PrintGCDetails) {
duke@435 363 gclog_or_tty->print_cr("ReferenceProcessor::enqueue_discovered_reflist list "
duke@435 364 INTPTR_FORMAT, (address)refs_list.head());
duke@435 365 }
stefank@3115 366
stefank@3115 367 oop obj = NULL;
ysr@3117 368 oop next_d = refs_list.head();
brutisso@6719 369 if (pending_list_uses_discovered_field()) { // New behavior
ysr@3117 370 // Walk down the list, self-looping the next field
ysr@3117 371 // so that the References are not considered active.
ysr@3117 372 while (obj != next_d) {
ysr@3117 373 obj = next_d;
ysr@3117 374 assert(obj->is_instanceRef(), "should be reference object");
ysr@3117 375 next_d = java_lang_ref_Reference::discovered(obj);
ysr@3117 376 if (TraceReferenceGC && PrintGCDetails) {
ysr@3117 377 gclog_or_tty->print_cr(" obj " INTPTR_FORMAT "/next_d " INTPTR_FORMAT,
hseigel@5784 378 (void *)obj, (void *)next_d);
ysr@3117 379 }
ysr@3117 380 assert(java_lang_ref_Reference::next(obj) == NULL,
ysr@3117 381 "Reference not active; should not be discovered");
ysr@3117 382 // Self-loop next, so as to make Ref not active.
pliden@6395 383 java_lang_ref_Reference::set_next_raw(obj, obj);
brutisso@6719 384 if (next_d != obj) {
brutisso@6719 385 oopDesc::bs()->write_ref_field(java_lang_ref_Reference::discovered_addr(obj), next_d);
brutisso@6719 386 } else {
brutisso@6719 387 // This is the last object.
brutisso@6719 388 // Swap refs_list into pending_list_addr and
ysr@3117 389 // set obj's discovered to what we read from pending_list_addr.
ysr@3117 390 oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr);
brutisso@6719 391 // Need post-barrier on pending_list_addr. See enqueue_discovered_ref_helper() above.
pliden@6395 392 java_lang_ref_Reference::set_discovered_raw(obj, old); // old may be NULL
pliden@6395 393 oopDesc::bs()->write_ref_field(java_lang_ref_Reference::discovered_addr(obj), old);
ysr@3117 394 }
duke@435 395 }
ysr@3117 396 } else { // Old behaviour
ysr@3117 397 // Walk down the list, copying the discovered field into
ysr@3117 398 // the next field and clearing the discovered field.
ysr@3117 399 while (obj != next_d) {
ysr@3117 400 obj = next_d;
ysr@3117 401 assert(obj->is_instanceRef(), "should be reference object");
ysr@3117 402 next_d = java_lang_ref_Reference::discovered(obj);
ysr@3117 403 if (TraceReferenceGC && PrintGCDetails) {
ysr@3117 404 gclog_or_tty->print_cr(" obj " INTPTR_FORMAT "/next_d " INTPTR_FORMAT,
hseigel@5784 405 (void *)obj, (void *)next_d);
ysr@3117 406 }
ysr@3117 407 assert(java_lang_ref_Reference::next(obj) == NULL,
ysr@3117 408 "The reference should not be enqueued");
ysr@3117 409 if (next_d == obj) { // obj is last
ysr@3117 410 // Swap refs_list into pendling_list_addr and
ysr@3117 411 // set obj's next to what we read from pending_list_addr.
ysr@3117 412 oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr);
ysr@3117 413 // Need oop_check on pending_list_addr above;
ysr@3117 414 // see special oop-check code at the end of
ysr@3117 415 // enqueue_discovered_reflists() further below.
ysr@3117 416 if (old == NULL) {
ysr@3117 417 // obj should be made to point to itself, since
ysr@3117 418 // pending list was empty.
ysr@3117 419 java_lang_ref_Reference::set_next(obj, obj);
ysr@3117 420 } else {
ysr@3117 421 java_lang_ref_Reference::set_next(obj, old);
ysr@3117 422 }
duke@435 423 } else {
ysr@3117 424 java_lang_ref_Reference::set_next(obj, next_d);
duke@435 425 }
ysr@3117 426 java_lang_ref_Reference::set_discovered(obj, (oop) NULL);
duke@435 427 }
duke@435 428 }
duke@435 429 }
duke@435 430
duke@435 431 // Parallel enqueue task
duke@435 432 class RefProcEnqueueTask: public AbstractRefProcTaskExecutor::EnqueueTask {
duke@435 433 public:
duke@435 434 RefProcEnqueueTask(ReferenceProcessor& ref_processor,
duke@435 435 DiscoveredList discovered_refs[],
coleenp@548 436 HeapWord* pending_list_addr,
duke@435 437 int n_queues)
duke@435 438 : EnqueueTask(ref_processor, discovered_refs,
stefank@3115 439 pending_list_addr, n_queues)
duke@435 440 { }
duke@435 441
coleenp@548 442 virtual void work(unsigned int work_id) {
ysr@2651 443 assert(work_id < (unsigned int)_ref_processor.max_num_q(), "Index out-of-bounds");
duke@435 444 // Simplest first cut: static partitioning.
duke@435 445 int index = work_id;
jmasa@2188 446 // The increment on "index" must correspond to the maximum number of queues
jmasa@2188 447 // (n_queues) with which that ReferenceProcessor was created. That
jmasa@2188 448 // is because of the "clever" way the discovered references lists were
ysr@2651 449 // allocated and are indexed into.
ysr@2651 450 assert(_n_queues == (int) _ref_processor.max_num_q(), "Different number not expected");
jmasa@2188 451 for (int j = 0;
johnc@3175 452 j < ReferenceProcessor::number_of_subclasses_of_ref();
jmasa@2188 453 j++, index += _n_queues) {
duke@435 454 _ref_processor.enqueue_discovered_reflist(
duke@435 455 _refs_lists[index], _pending_list_addr);
stefank@3115 456 _refs_lists[index].set_head(NULL);
duke@435 457 _refs_lists[index].set_length(0);
duke@435 458 }
duke@435 459 }
duke@435 460 };
duke@435 461
duke@435 462 // Enqueue references that are not made active again
coleenp@548 463 void ReferenceProcessor::enqueue_discovered_reflists(HeapWord* pending_list_addr,
duke@435 464 AbstractRefProcTaskExecutor* task_executor) {
duke@435 465 if (_processing_is_mt && task_executor != NULL) {
duke@435 466 // Parallel code
johnc@3210 467 RefProcEnqueueTask tsk(*this, _discovered_refs,
stefank@3115 468 pending_list_addr, _max_num_q);
duke@435 469 task_executor->execute(tsk);
duke@435 470 } else {
duke@435 471 // Serial code: call the parent class's implementation
jmasa@3357 472 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
johnc@3210 473 enqueue_discovered_reflist(_discovered_refs[i], pending_list_addr);
johnc@3210 474 _discovered_refs[i].set_head(NULL);
johnc@3210 475 _discovered_refs[i].set_length(0);
duke@435 476 }
duke@435 477 }
duke@435 478 }
duke@435 479
johnc@3175 480 void DiscoveredListIterator::load_ptrs(DEBUG_ONLY(bool allow_null_referent)) {
duke@435 481 _discovered_addr = java_lang_ref_Reference::discovered_addr(_ref);
coleenp@548 482 oop discovered = java_lang_ref_Reference::discovered(_ref);
coleenp@548 483 assert(_discovered_addr && discovered->is_oop_or_null(),
duke@435 484 "discovered field is bad");
coleenp@548 485 _next = discovered;
duke@435 486 _referent_addr = java_lang_ref_Reference::referent_addr(_ref);
coleenp@548 487 _referent = java_lang_ref_Reference::referent(_ref);
duke@435 488 assert(Universe::heap()->is_in_reserved_or_null(_referent),
duke@435 489 "Wrong oop found in java.lang.Reference object");
duke@435 490 assert(allow_null_referent ?
duke@435 491 _referent->is_oop_or_null()
duke@435 492 : _referent->is_oop(),
duke@435 493 "bad referent");
duke@435 494 }
duke@435 495
johnc@3175 496 void DiscoveredListIterator::remove() {
duke@435 497 assert(_ref->is_oop(), "Dropping a bad reference");
coleenp@548 498 oop_store_raw(_discovered_addr, NULL);
stefank@3115 499
coleenp@548 500 // First _prev_next ref actually points into DiscoveredList (gross).
stefank@3115 501 oop new_next;
stefank@3115 502 if (_next == _ref) {
stefank@3115 503 // At the end of the list, we should make _prev point to itself.
stefank@3115 504 // If _ref is the first ref, then _prev_next will be in the DiscoveredList,
stefank@3115 505 // and _prev will be NULL.
stefank@3115 506 new_next = _prev;
stefank@3115 507 } else {
stefank@3115 508 new_next = _next;
stefank@3115 509 }
pliden@6397 510 // Remove Reference object from discovered list. Note that G1 does not need a
pliden@6397 511 // pre-barrier here because we know the Reference has already been found/marked,
pliden@6397 512 // that's how it ended up in the discovered list in the first place.
pliden@6397 513 oop_store_raw(_prev_next, new_next);
duke@435 514 NOT_PRODUCT(_removed++);
ysr@887 515 _refs_list.dec_length(1);
duke@435 516 }
duke@435 517
johnc@3175 518 // Make the Reference object active again.
johnc@3175 519 void DiscoveredListIterator::make_active() {
brutisso@6719 520 // The pre barrier for G1 is probably just needed for the old
brutisso@6719 521 // reference processing behavior. Should we guard this with
brutisso@6719 522 // ReferenceProcessor::pending_list_uses_discovered_field() ?
johnc@3175 523 if (UseG1GC) {
johnc@3175 524 HeapWord* next_addr = java_lang_ref_Reference::next_addr(_ref);
johnc@3175 525 if (UseCompressedOops) {
pliden@6395 526 oopDesc::bs()->write_ref_field_pre((narrowOop*)next_addr, NULL);
johnc@3175 527 } else {
pliden@6395 528 oopDesc::bs()->write_ref_field_pre((oop*)next_addr, NULL);
johnc@3175 529 }
stefank@3115 530 }
brutisso@6719 531 java_lang_ref_Reference::set_next_raw(_ref, NULL);
johnc@3175 532 }
johnc@3175 533
johnc@3175 534 void DiscoveredListIterator::clear_referent() {
johnc@3175 535 oop_store_raw(_referent_addr, NULL);
duke@435 536 }
duke@435 537
duke@435 538 // NOTE: process_phase*() are largely similar, and at a high level
duke@435 539 // merely iterate over the extant list applying a predicate to
duke@435 540 // each of its elements and possibly removing that element from the
duke@435 541 // list and applying some further closures to that element.
duke@435 542 // We should consider the possibility of replacing these
duke@435 543 // process_phase*() methods by abstracting them into
duke@435 544 // a single general iterator invocation that receives appropriate
duke@435 545 // closures that accomplish this work.
duke@435 546
duke@435 547 // (SoftReferences only) Traverse the list and remove any SoftReferences whose
duke@435 548 // referents are not alive, but that should be kept alive for policy reasons.
duke@435 549 // Keep alive the transitive closure of all such referents.
duke@435 550 void
coleenp@548 551 ReferenceProcessor::process_phase1(DiscoveredList& refs_list,
duke@435 552 ReferencePolicy* policy,
duke@435 553 BoolObjectClosure* is_alive,
duke@435 554 OopClosure* keep_alive,
duke@435 555 VoidClosure* complete_gc) {
duke@435 556 assert(policy != NULL, "Must have a non-NULL policy");
brutisso@6719 557 DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
duke@435 558 // Decide which softly reachable refs should be kept alive.
duke@435 559 while (iter.has_next()) {
duke@435 560 iter.load_ptrs(DEBUG_ONLY(!discovery_is_atomic() /* allow_null_referent */));
duke@435 561 bool referent_is_dead = (iter.referent() != NULL) && !iter.is_referent_alive();
johnc@3188 562 if (referent_is_dead &&
johnc@3188 563 !policy->should_clear_reference(iter.obj(), _soft_ref_timestamp_clock)) {
duke@435 564 if (TraceReferenceGC) {
duke@435 565 gclog_or_tty->print_cr("Dropping reference (" INTPTR_FORMAT ": %s" ") by policy",
hseigel@5784 566 (void *)iter.obj(), iter.obj()->klass()->internal_name());
duke@435 567 }
ysr@887 568 // Remove Reference object from list
ysr@887 569 iter.remove();
duke@435 570 // Make the Reference object active again
duke@435 571 iter.make_active();
duke@435 572 // keep the referent around
duke@435 573 iter.make_referent_alive();
ysr@887 574 iter.move_to_next();
duke@435 575 } else {
duke@435 576 iter.next();
duke@435 577 }
duke@435 578 }
duke@435 579 // Close the reachable set
duke@435 580 complete_gc->do_void();
duke@435 581 NOT_PRODUCT(
duke@435 582 if (PrintGCDetails && TraceReferenceGC) {
jmasa@2188 583 gclog_or_tty->print_cr(" Dropped %d dead Refs out of %d "
ysr@3117 584 "discovered Refs by policy, from list " INTPTR_FORMAT,
jmasa@2188 585 iter.removed(), iter.processed(), (address)refs_list.head());
duke@435 586 }
duke@435 587 )
duke@435 588 }
duke@435 589
duke@435 590 // Traverse the list and remove any Refs that are not active, or
duke@435 591 // whose referents are either alive or NULL.
duke@435 592 void
coleenp@548 593 ReferenceProcessor::pp2_work(DiscoveredList& refs_list,
duke@435 594 BoolObjectClosure* is_alive,
coleenp@548 595 OopClosure* keep_alive) {
duke@435 596 assert(discovery_is_atomic(), "Error");
brutisso@6719 597 DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
duke@435 598 while (iter.has_next()) {
duke@435 599 iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */));
coleenp@548 600 DEBUG_ONLY(oop next = java_lang_ref_Reference::next(iter.obj());)
coleenp@548 601 assert(next == NULL, "Should not discover inactive Reference");
duke@435 602 if (iter.is_referent_alive()) {
duke@435 603 if (TraceReferenceGC) {
duke@435 604 gclog_or_tty->print_cr("Dropping strongly reachable reference (" INTPTR_FORMAT ": %s)",
hseigel@5784 605 (void *)iter.obj(), iter.obj()->klass()->internal_name());
duke@435 606 }
duke@435 607 // The referent is reachable after all.
ysr@887 608 // Remove Reference object from list.
ysr@887 609 iter.remove();
duke@435 610 // Update the referent pointer as necessary: Note that this
duke@435 611 // should not entail any recursive marking because the
duke@435 612 // referent must already have been traversed.
duke@435 613 iter.make_referent_alive();
ysr@887 614 iter.move_to_next();
duke@435 615 } else {
duke@435 616 iter.next();
duke@435 617 }
duke@435 618 }
duke@435 619 NOT_PRODUCT(
ysr@2651 620 if (PrintGCDetails && TraceReferenceGC && (iter.processed() > 0)) {
jmasa@2188 621 gclog_or_tty->print_cr(" Dropped %d active Refs out of %d "
jmasa@2188 622 "Refs in discovered list " INTPTR_FORMAT,
jmasa@2188 623 iter.removed(), iter.processed(), (address)refs_list.head());
duke@435 624 }
duke@435 625 )
duke@435 626 }
duke@435 627
duke@435 628 void
coleenp@548 629 ReferenceProcessor::pp2_work_concurrent_discovery(DiscoveredList& refs_list,
coleenp@548 630 BoolObjectClosure* is_alive,
coleenp@548 631 OopClosure* keep_alive,
coleenp@548 632 VoidClosure* complete_gc) {
duke@435 633 assert(!discovery_is_atomic(), "Error");
brutisso@6719 634 DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
duke@435 635 while (iter.has_next()) {
duke@435 636 iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */));
coleenp@548 637 HeapWord* next_addr = java_lang_ref_Reference::next_addr(iter.obj());
coleenp@548 638 oop next = java_lang_ref_Reference::next(iter.obj());
duke@435 639 if ((iter.referent() == NULL || iter.is_referent_alive() ||
coleenp@548 640 next != NULL)) {
coleenp@548 641 assert(next->is_oop_or_null(), "bad next field");
duke@435 642 // Remove Reference object from list
duke@435 643 iter.remove();
duke@435 644 // Trace the cohorts
duke@435 645 iter.make_referent_alive();
coleenp@548 646 if (UseCompressedOops) {
coleenp@548 647 keep_alive->do_oop((narrowOop*)next_addr);
coleenp@548 648 } else {
coleenp@548 649 keep_alive->do_oop((oop*)next_addr);
coleenp@548 650 }
ysr@887 651 iter.move_to_next();
duke@435 652 } else {
duke@435 653 iter.next();
duke@435 654 }
duke@435 655 }
duke@435 656 // Now close the newly reachable set
duke@435 657 complete_gc->do_void();
duke@435 658 NOT_PRODUCT(
ysr@2651 659 if (PrintGCDetails && TraceReferenceGC && (iter.processed() > 0)) {
jmasa@2188 660 gclog_or_tty->print_cr(" Dropped %d active Refs out of %d "
jmasa@2188 661 "Refs in discovered list " INTPTR_FORMAT,
jmasa@2188 662 iter.removed(), iter.processed(), (address)refs_list.head());
duke@435 663 }
duke@435 664 )
duke@435 665 }
duke@435 666
duke@435 667 // Traverse the list and process the referents, by either
coleenp@548 668 // clearing them or keeping them (and their reachable
duke@435 669 // closure) alive.
duke@435 670 void
coleenp@548 671 ReferenceProcessor::process_phase3(DiscoveredList& refs_list,
duke@435 672 bool clear_referent,
duke@435 673 BoolObjectClosure* is_alive,
duke@435 674 OopClosure* keep_alive,
duke@435 675 VoidClosure* complete_gc) {
jmasa@2188 676 ResourceMark rm;
brutisso@6719 677 DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
duke@435 678 while (iter.has_next()) {
duke@435 679 iter.update_discovered();
duke@435 680 iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */));
duke@435 681 if (clear_referent) {
duke@435 682 // NULL out referent pointer
duke@435 683 iter.clear_referent();
duke@435 684 } else {
duke@435 685 // keep the referent around
duke@435 686 iter.make_referent_alive();
duke@435 687 }
duke@435 688 if (TraceReferenceGC) {
duke@435 689 gclog_or_tty->print_cr("Adding %sreference (" INTPTR_FORMAT ": %s) as pending",
duke@435 690 clear_referent ? "cleared " : "",
hseigel@5784 691 (void *)iter.obj(), iter.obj()->klass()->internal_name());
duke@435 692 }
duke@435 693 assert(iter.obj()->is_oop(UseConcMarkSweepGC), "Adding a bad reference");
duke@435 694 iter.next();
duke@435 695 }
stefank@3115 696 // Remember to update the next pointer of the last ref.
duke@435 697 iter.update_discovered();
duke@435 698 // Close the reachable set
duke@435 699 complete_gc->do_void();
duke@435 700 }
duke@435 701
duke@435 702 void
stefank@3115 703 ReferenceProcessor::clear_discovered_references(DiscoveredList& refs_list) {
stefank@3115 704 oop obj = NULL;
stefank@3115 705 oop next = refs_list.head();
stefank@3115 706 while (next != obj) {
stefank@3115 707 obj = next;
stefank@3115 708 next = java_lang_ref_Reference::discovered(obj);
stefank@3115 709 java_lang_ref_Reference::set_discovered_raw(obj, NULL);
stefank@3115 710 }
stefank@3115 711 refs_list.set_head(NULL);
stefank@3115 712 refs_list.set_length(0);
stefank@3115 713 }
stefank@3115 714
stefank@3115 715 void
coleenp@548 716 ReferenceProcessor::abandon_partial_discovered_list(DiscoveredList& refs_list) {
stefank@3115 717 clear_discovered_references(refs_list);
duke@435 718 }
duke@435 719
ysr@777 720 void ReferenceProcessor::abandon_partial_discovery() {
ysr@777 721 // loop over the lists
jmasa@3357 722 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
jmasa@2188 723 if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) {
johnc@3175 724 gclog_or_tty->print_cr("\nAbandoning %s discovered list", list_name(i));
ysr@777 725 }
johnc@3210 726 abandon_partial_discovered_list(_discovered_refs[i]);
duke@435 727 }
duke@435 728 }
duke@435 729
duke@435 730 class RefProcPhase1Task: public AbstractRefProcTaskExecutor::ProcessTask {
duke@435 731 public:
duke@435 732 RefProcPhase1Task(ReferenceProcessor& ref_processor,
duke@435 733 DiscoveredList refs_lists[],
duke@435 734 ReferencePolicy* policy,
duke@435 735 bool marks_oops_alive)
duke@435 736 : ProcessTask(ref_processor, refs_lists, marks_oops_alive),
duke@435 737 _policy(policy)
duke@435 738 { }
duke@435 739 virtual void work(unsigned int i, BoolObjectClosure& is_alive,
duke@435 740 OopClosure& keep_alive,
duke@435 741 VoidClosure& complete_gc)
duke@435 742 {
jmasa@2188 743 Thread* thr = Thread::current();
jmasa@2188 744 int refs_list_index = ((WorkerThread*)thr)->id();
jmasa@2188 745 _ref_processor.process_phase1(_refs_lists[refs_list_index], _policy,
duke@435 746 &is_alive, &keep_alive, &complete_gc);
duke@435 747 }
duke@435 748 private:
duke@435 749 ReferencePolicy* _policy;
duke@435 750 };
duke@435 751
duke@435 752 class RefProcPhase2Task: public AbstractRefProcTaskExecutor::ProcessTask {
duke@435 753 public:
duke@435 754 RefProcPhase2Task(ReferenceProcessor& ref_processor,
duke@435 755 DiscoveredList refs_lists[],
duke@435 756 bool marks_oops_alive)
duke@435 757 : ProcessTask(ref_processor, refs_lists, marks_oops_alive)
duke@435 758 { }
duke@435 759 virtual void work(unsigned int i, BoolObjectClosure& is_alive,
duke@435 760 OopClosure& keep_alive,
duke@435 761 VoidClosure& complete_gc)
duke@435 762 {
duke@435 763 _ref_processor.process_phase2(_refs_lists[i],
duke@435 764 &is_alive, &keep_alive, &complete_gc);
duke@435 765 }
duke@435 766 };
duke@435 767
duke@435 768 class RefProcPhase3Task: public AbstractRefProcTaskExecutor::ProcessTask {
duke@435 769 public:
duke@435 770 RefProcPhase3Task(ReferenceProcessor& ref_processor,
duke@435 771 DiscoveredList refs_lists[],
duke@435 772 bool clear_referent,
duke@435 773 bool marks_oops_alive)
duke@435 774 : ProcessTask(ref_processor, refs_lists, marks_oops_alive),
duke@435 775 _clear_referent(clear_referent)
duke@435 776 { }
duke@435 777 virtual void work(unsigned int i, BoolObjectClosure& is_alive,
duke@435 778 OopClosure& keep_alive,
duke@435 779 VoidClosure& complete_gc)
duke@435 780 {
jmasa@2188 781 // Don't use "refs_list_index" calculated in this way because
jmasa@2188 782 // balance_queues() has moved the Ref's into the first n queues.
jmasa@2188 783 // Thread* thr = Thread::current();
jmasa@2188 784 // int refs_list_index = ((WorkerThread*)thr)->id();
jmasa@2188 785 // _ref_processor.process_phase3(_refs_lists[refs_list_index], _clear_referent,
duke@435 786 _ref_processor.process_phase3(_refs_lists[i], _clear_referent,
duke@435 787 &is_alive, &keep_alive, &complete_gc);
duke@435 788 }
duke@435 789 private:
duke@435 790 bool _clear_referent;
duke@435 791 };
duke@435 792
duke@435 793 // Balances reference queues.
jmasa@2188 794 // Move entries from all queues[0, 1, ..., _max_num_q-1] to
jmasa@2188 795 // queues[0, 1, ..., _num_q-1] because only the first _num_q
jmasa@2188 796 // corresponding to the active workers will be processed.
duke@435 797 void ReferenceProcessor::balance_queues(DiscoveredList ref_lists[])
duke@435 798 {
duke@435 799 // calculate total length
duke@435 800 size_t total_refs = 0;
jmasa@2188 801 if (TraceReferenceGC && PrintGCDetails) {
jmasa@2188 802 gclog_or_tty->print_cr("\nBalance ref_lists ");
jmasa@2188 803 }
jmasa@2188 804
jmasa@3357 805 for (uint i = 0; i < _max_num_q; ++i) {
duke@435 806 total_refs += ref_lists[i].length();
jmasa@2188 807 if (TraceReferenceGC && PrintGCDetails) {
jmasa@2188 808 gclog_or_tty->print("%d ", ref_lists[i].length());
jmasa@2188 809 }
jmasa@2188 810 }
jmasa@2188 811 if (TraceReferenceGC && PrintGCDetails) {
jmasa@2188 812 gclog_or_tty->print_cr(" = %d", total_refs);
duke@435 813 }
duke@435 814 size_t avg_refs = total_refs / _num_q + 1;
jmasa@3357 815 uint to_idx = 0;
jmasa@3357 816 for (uint from_idx = 0; from_idx < _max_num_q; from_idx++) {
jmasa@2188 817 bool move_all = false;
jmasa@2188 818 if (from_idx >= _num_q) {
jmasa@2188 819 move_all = ref_lists[from_idx].length() > 0;
jmasa@2188 820 }
jmasa@2188 821 while ((ref_lists[from_idx].length() > avg_refs) ||
jmasa@2188 822 move_all) {
duke@435 823 assert(to_idx < _num_q, "Sanity Check!");
duke@435 824 if (ref_lists[to_idx].length() < avg_refs) {
duke@435 825 // move superfluous refs
jmasa@2188 826 size_t refs_to_move;
jmasa@2188 827 // Move all the Ref's if the from queue will not be processed.
jmasa@2188 828 if (move_all) {
jmasa@2188 829 refs_to_move = MIN2(ref_lists[from_idx].length(),
jmasa@2188 830 avg_refs - ref_lists[to_idx].length());
jmasa@2188 831 } else {
jmasa@2188 832 refs_to_move = MIN2(ref_lists[from_idx].length() - avg_refs,
jmasa@2188 833 avg_refs - ref_lists[to_idx].length());
jmasa@2188 834 }
stefank@3115 835
stefank@3115 836 assert(refs_to_move > 0, "otherwise the code below will fail");
stefank@3115 837
duke@435 838 oop move_head = ref_lists[from_idx].head();
duke@435 839 oop move_tail = move_head;
duke@435 840 oop new_head = move_head;
duke@435 841 // find an element to split the list on
duke@435 842 for (size_t j = 0; j < refs_to_move; ++j) {
duke@435 843 move_tail = new_head;
coleenp@548 844 new_head = java_lang_ref_Reference::discovered(new_head);
duke@435 845 }
stefank@3115 846
stefank@3115 847 // Add the chain to the to list.
stefank@3115 848 if (ref_lists[to_idx].head() == NULL) {
stefank@3115 849 // to list is empty. Make a loop at the end.
brutisso@6719 850 java_lang_ref_Reference::set_discovered_raw(move_tail, move_tail);
stefank@3115 851 } else {
brutisso@6719 852 java_lang_ref_Reference::set_discovered_raw(move_tail, ref_lists[to_idx].head());
stefank@3115 853 }
duke@435 854 ref_lists[to_idx].set_head(move_head);
ysr@887 855 ref_lists[to_idx].inc_length(refs_to_move);
stefank@3115 856
stefank@3115 857 // Remove the chain from the from list.
stefank@3115 858 if (move_tail == new_head) {
stefank@3115 859 // We found the end of the from list.
stefank@3115 860 ref_lists[from_idx].set_head(NULL);
stefank@3115 861 } else {
stefank@3115 862 ref_lists[from_idx].set_head(new_head);
stefank@3115 863 }
ysr@887 864 ref_lists[from_idx].dec_length(refs_to_move);
jmasa@2188 865 if (ref_lists[from_idx].length() == 0) {
jmasa@2188 866 break;
jmasa@2188 867 }
duke@435 868 } else {
jmasa@2188 869 to_idx = (to_idx + 1) % _num_q;
duke@435 870 }
duke@435 871 }
duke@435 872 }
jmasa@2188 873 #ifdef ASSERT
jmasa@2188 874 size_t balanced_total_refs = 0;
jmasa@3357 875 for (uint i = 0; i < _max_num_q; ++i) {
jmasa@2188 876 balanced_total_refs += ref_lists[i].length();
jmasa@2188 877 if (TraceReferenceGC && PrintGCDetails) {
jmasa@2188 878 gclog_or_tty->print("%d ", ref_lists[i].length());
jmasa@2188 879 }
jmasa@2188 880 }
jmasa@2188 881 if (TraceReferenceGC && PrintGCDetails) {
jmasa@2188 882 gclog_or_tty->print_cr(" = %d", balanced_total_refs);
jmasa@2188 883 gclog_or_tty->flush();
jmasa@2188 884 }
jmasa@2188 885 assert(total_refs == balanced_total_refs, "Balancing was incomplete");
jmasa@2188 886 #endif
jmasa@2188 887 }
jmasa@2188 888
jmasa@2188 889 void ReferenceProcessor::balance_all_queues() {
jmasa@2188 890 balance_queues(_discoveredSoftRefs);
jmasa@2188 891 balance_queues(_discoveredWeakRefs);
jmasa@2188 892 balance_queues(_discoveredFinalRefs);
jmasa@2188 893 balance_queues(_discoveredPhantomRefs);
jmasa@7469 894 balance_queues(_discoveredCleanerRefs);
duke@435 895 }
duke@435 896
sla@5237 897 size_t
duke@435 898 ReferenceProcessor::process_discovered_reflist(
duke@435 899 DiscoveredList refs_lists[],
duke@435 900 ReferencePolicy* policy,
duke@435 901 bool clear_referent,
duke@435 902 BoolObjectClosure* is_alive,
duke@435 903 OopClosure* keep_alive,
duke@435 904 VoidClosure* complete_gc,
duke@435 905 AbstractRefProcTaskExecutor* task_executor)
duke@435 906 {
jmasa@2188 907 bool mt_processing = task_executor != NULL && _processing_is_mt;
jmasa@2188 908 // If discovery used MT and a dynamic number of GC threads, then
jmasa@2188 909 // the queues must be balanced for correctness if fewer than the
jmasa@2188 910 // maximum number of queues were used. The number of queue used
jmasa@2188 911 // during discovery may be different than the number to be used
jmasa@2188 912 // for processing so don't depend of _num_q < _max_num_q as part
jmasa@2188 913 // of the test.
jmasa@2188 914 bool must_balance = _discovery_is_mt;
jmasa@2188 915
jmasa@2188 916 if ((mt_processing && ParallelRefProcBalancingEnabled) ||
jmasa@2188 917 must_balance) {
duke@435 918 balance_queues(refs_lists);
duke@435 919 }
sla@5237 920
sla@5237 921 size_t total_list_count = total_count(refs_lists);
sla@5237 922
duke@435 923 if (PrintReferenceGC && PrintGCDetails) {
sla@5237 924 gclog_or_tty->print(", %u refs", total_list_count);
duke@435 925 }
duke@435 926
duke@435 927 // Phase 1 (soft refs only):
duke@435 928 // . Traverse the list and remove any SoftReferences whose
duke@435 929 // referents are not alive, but that should be kept alive for
duke@435 930 // policy reasons. Keep alive the transitive closure of all
duke@435 931 // such referents.
duke@435 932 if (policy != NULL) {
jmasa@2188 933 if (mt_processing) {
duke@435 934 RefProcPhase1Task phase1(*this, refs_lists, policy, true /*marks_oops_alive*/);
duke@435 935 task_executor->execute(phase1);
duke@435 936 } else {
jmasa@3357 937 for (uint i = 0; i < _max_num_q; i++) {
duke@435 938 process_phase1(refs_lists[i], policy,
duke@435 939 is_alive, keep_alive, complete_gc);
duke@435 940 }
duke@435 941 }
duke@435 942 } else { // policy == NULL
duke@435 943 assert(refs_lists != _discoveredSoftRefs,
duke@435 944 "Policy must be specified for soft references.");
duke@435 945 }
duke@435 946
duke@435 947 // Phase 2:
duke@435 948 // . Traverse the list and remove any refs whose referents are alive.
jmasa@2188 949 if (mt_processing) {
duke@435 950 RefProcPhase2Task phase2(*this, refs_lists, !discovery_is_atomic() /*marks_oops_alive*/);
duke@435 951 task_executor->execute(phase2);
duke@435 952 } else {
jmasa@3357 953 for (uint i = 0; i < _max_num_q; i++) {
duke@435 954 process_phase2(refs_lists[i], is_alive, keep_alive, complete_gc);
duke@435 955 }
duke@435 956 }
duke@435 957
duke@435 958 // Phase 3:
duke@435 959 // . Traverse the list and process referents as appropriate.
jmasa@2188 960 if (mt_processing) {
duke@435 961 RefProcPhase3Task phase3(*this, refs_lists, clear_referent, true /*marks_oops_alive*/);
duke@435 962 task_executor->execute(phase3);
duke@435 963 } else {
jmasa@3357 964 for (uint i = 0; i < _max_num_q; i++) {
duke@435 965 process_phase3(refs_lists[i], clear_referent,
duke@435 966 is_alive, keep_alive, complete_gc);
duke@435 967 }
duke@435 968 }
sla@5237 969
sla@5237 970 return total_list_count;
duke@435 971 }
duke@435 972
duke@435 973 void ReferenceProcessor::clean_up_discovered_references() {
duke@435 974 // loop over the lists
jmasa@3357 975 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
ysr@2651 976 if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) {
duke@435 977 gclog_or_tty->print_cr(
duke@435 978 "\nScrubbing %s discovered list of Null referents",
duke@435 979 list_name(i));
duke@435 980 }
johnc@3210 981 clean_up_discovered_reflist(_discovered_refs[i]);
duke@435 982 }
duke@435 983 }
duke@435 984
duke@435 985 void ReferenceProcessor::clean_up_discovered_reflist(DiscoveredList& refs_list) {
duke@435 986 assert(!discovery_is_atomic(), "Else why call this method?");
brutisso@6719 987 DiscoveredListIterator iter(refs_list, NULL, NULL);
duke@435 988 while (iter.has_next()) {
duke@435 989 iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */));
coleenp@548 990 oop next = java_lang_ref_Reference::next(iter.obj());
coleenp@548 991 assert(next->is_oop_or_null(), "bad next field");
duke@435 992 // If referent has been cleared or Reference is not active,
duke@435 993 // drop it.
coleenp@548 994 if (iter.referent() == NULL || next != NULL) {
duke@435 995 debug_only(
duke@435 996 if (PrintGCDetails && TraceReferenceGC) {
duke@435 997 gclog_or_tty->print_cr("clean_up_discovered_list: Dropping Reference: "
duke@435 998 INTPTR_FORMAT " with next field: " INTPTR_FORMAT
duke@435 999 " and referent: " INTPTR_FORMAT,
hseigel@5784 1000 (void *)iter.obj(), (void *)next, (void *)iter.referent());
duke@435 1001 }
duke@435 1002 )
duke@435 1003 // Remove Reference object from list
duke@435 1004 iter.remove();
ysr@887 1005 iter.move_to_next();
duke@435 1006 } else {
duke@435 1007 iter.next();
duke@435 1008 }
duke@435 1009 }
duke@435 1010 NOT_PRODUCT(
duke@435 1011 if (PrintGCDetails && TraceReferenceGC) {
duke@435 1012 gclog_or_tty->print(
duke@435 1013 " Removed %d Refs with NULL referents out of %d discovered Refs",
duke@435 1014 iter.removed(), iter.processed());
duke@435 1015 }
duke@435 1016 )
duke@435 1017 }
duke@435 1018
duke@435 1019 inline DiscoveredList* ReferenceProcessor::get_discovered_list(ReferenceType rt) {
jmasa@3357 1020 uint id = 0;
duke@435 1021 // Determine the queue index to use for this object.
duke@435 1022 if (_discovery_is_mt) {
duke@435 1023 // During a multi-threaded discovery phase,
duke@435 1024 // each thread saves to its "own" list.
duke@435 1025 Thread* thr = Thread::current();
johnc@2316 1026 id = thr->as_Worker_thread()->id();
duke@435 1027 } else {
duke@435 1028 // single-threaded discovery, we save in round-robin
duke@435 1029 // fashion to each of the lists.
duke@435 1030 if (_processing_is_mt) {
duke@435 1031 id = next_id();
duke@435 1032 }
duke@435 1033 }
jmasa@2188 1034 assert(0 <= id && id < _max_num_q, "Id is out-of-bounds (call Freud?)");
duke@435 1035
duke@435 1036 // Get the discovered queue to which we will add
duke@435 1037 DiscoveredList* list = NULL;
duke@435 1038 switch (rt) {
duke@435 1039 case REF_OTHER:
duke@435 1040 // Unknown reference type, no special treatment
duke@435 1041 break;
duke@435 1042 case REF_SOFT:
duke@435 1043 list = &_discoveredSoftRefs[id];
duke@435 1044 break;
duke@435 1045 case REF_WEAK:
duke@435 1046 list = &_discoveredWeakRefs[id];
duke@435 1047 break;
duke@435 1048 case REF_FINAL:
duke@435 1049 list = &_discoveredFinalRefs[id];
duke@435 1050 break;
duke@435 1051 case REF_PHANTOM:
duke@435 1052 list = &_discoveredPhantomRefs[id];
duke@435 1053 break;
jmasa@7469 1054 case REF_CLEANER:
jmasa@7469 1055 list = &_discoveredCleanerRefs[id];
jmasa@7469 1056 break;
duke@435 1057 case REF_NONE:
coleenp@4047 1058 // we should not reach here if we are an InstanceRefKlass
duke@435 1059 default:
duke@435 1060 ShouldNotReachHere();
duke@435 1061 }
jmasa@2188 1062 if (TraceReferenceGC && PrintGCDetails) {
johnc@2316 1063 gclog_or_tty->print_cr("Thread %d gets list " INTPTR_FORMAT, id, list);
jmasa@2188 1064 }
duke@435 1065 return list;
duke@435 1066 }
duke@435 1067
coleenp@548 1068 inline void
coleenp@548 1069 ReferenceProcessor::add_to_discovered_list_mt(DiscoveredList& refs_list,
coleenp@548 1070 oop obj,
coleenp@548 1071 HeapWord* discovered_addr) {
duke@435 1072 assert(_discovery_is_mt, "!_discovery_is_mt should have been handled by caller");
duke@435 1073 // First we must make sure this object is only enqueued once. CAS in a non null
duke@435 1074 // discovered_addr.
ysr@777 1075 oop current_head = refs_list.head();
stefank@3115 1076 // The last ref must have its discovered field pointing to itself.
stefank@3115 1077 oop next_discovered = (current_head != NULL) ? current_head : obj;
ysr@777 1078
stefank@3115 1079 oop retest = oopDesc::atomic_compare_exchange_oop(next_discovered, discovered_addr,
coleenp@548 1080 NULL);
duke@435 1081 if (retest == NULL) {
duke@435 1082 // This thread just won the right to enqueue the object.
ysr@3117 1083 // We have separate lists for enqueueing, so no synchronization
duke@435 1084 // is necessary.
coleenp@548 1085 refs_list.set_head(obj);
ysr@887 1086 refs_list.inc_length(1);
johnc@2316 1087
johnc@2316 1088 if (TraceReferenceGC) {
ysr@3117 1089 gclog_or_tty->print_cr("Discovered reference (mt) (" INTPTR_FORMAT ": %s)",
hseigel@5784 1090 (void *)obj, obj->klass()->internal_name());
johnc@2316 1091 }
duke@435 1092 } else {
duke@435 1093 // If retest was non NULL, another thread beat us to it:
duke@435 1094 // The reference has already been discovered...
duke@435 1095 if (TraceReferenceGC) {
ysr@3117 1096 gclog_or_tty->print_cr("Already discovered reference (" INTPTR_FORMAT ": %s)",
hseigel@5784 1097 (void *)obj, obj->klass()->internal_name());
duke@435 1098 }
duke@435 1099 }
duke@435 1100 }
duke@435 1101
ysr@2337 1102 #ifndef PRODUCT
ysr@2337 1103 // Non-atomic (i.e. concurrent) discovery might allow us
ysr@2337 1104 // to observe j.l.References with NULL referents, being those
ysr@2337 1105 // cleared concurrently by mutators during (or after) discovery.
ysr@2337 1106 void ReferenceProcessor::verify_referent(oop obj) {
ysr@2337 1107 bool da = discovery_is_atomic();
ysr@2337 1108 oop referent = java_lang_ref_Reference::referent(obj);
ysr@2337 1109 assert(da ? referent->is_oop() : referent->is_oop_or_null(),
ysr@2337 1110 err_msg("Bad referent " INTPTR_FORMAT " found in Reference "
ysr@2337 1111 INTPTR_FORMAT " during %satomic discovery ",
hseigel@5784 1112 (void *)referent, (void *)obj, da ? "" : "non-"));
ysr@2337 1113 }
ysr@2337 1114 #endif
ysr@2337 1115
duke@435 1116 // We mention two of several possible choices here:
duke@435 1117 // #0: if the reference object is not in the "originating generation"
duke@435 1118 // (or part of the heap being collected, indicated by our "span"
duke@435 1119 // we don't treat it specially (i.e. we scan it as we would
duke@435 1120 // a normal oop, treating its references as strong references).
ysr@3117 1121 // This means that references can't be discovered unless their
duke@435 1122 // referent is also in the same span. This is the simplest,
duke@435 1123 // most "local" and most conservative approach, albeit one
duke@435 1124 // that may cause weak references to be enqueued least promptly.
duke@435 1125 // We call this choice the "ReferenceBasedDiscovery" policy.
duke@435 1126 // #1: the reference object may be in any generation (span), but if
duke@435 1127 // the referent is in the generation (span) being currently collected
duke@435 1128 // then we can discover the reference object, provided
duke@435 1129 // the object has not already been discovered by
duke@435 1130 // a different concurrently running collector (as may be the
duke@435 1131 // case, for instance, if the reference object is in CMS and
duke@435 1132 // the referent in DefNewGeneration), and provided the processing
duke@435 1133 // of this reference object by the current collector will
duke@435 1134 // appear atomic to every other collector in the system.
duke@435 1135 // (Thus, for instance, a concurrent collector may not
duke@435 1136 // discover references in other generations even if the
duke@435 1137 // referent is in its own generation). This policy may,
duke@435 1138 // in certain cases, enqueue references somewhat sooner than
duke@435 1139 // might Policy #0 above, but at marginally increased cost
duke@435 1140 // and complexity in processing these references.
duke@435 1141 // We call this choice the "RefeferentBasedDiscovery" policy.
duke@435 1142 bool ReferenceProcessor::discover_reference(oop obj, ReferenceType rt) {
ysr@3117 1143 // Make sure we are discovering refs (rather than processing discovered refs).
duke@435 1144 if (!_discovering_refs || !RegisterReferences) {
duke@435 1145 return false;
duke@435 1146 }
ysr@3117 1147 // We only discover active references.
coleenp@548 1148 oop next = java_lang_ref_Reference::next(obj);
ysr@3117 1149 if (next != NULL) { // Ref is no longer active
duke@435 1150 return false;
duke@435 1151 }
duke@435 1152
duke@435 1153 HeapWord* obj_addr = (HeapWord*)obj;
duke@435 1154 if (RefDiscoveryPolicy == ReferenceBasedDiscovery &&
duke@435 1155 !_span.contains(obj_addr)) {
duke@435 1156 // Reference is not in the originating generation;
duke@435 1157 // don't treat it specially (i.e. we want to scan it as a normal
duke@435 1158 // object with strong references).
duke@435 1159 return false;
duke@435 1160 }
duke@435 1161
ysr@3117 1162 // We only discover references whose referents are not (yet)
ysr@3117 1163 // known to be strongly reachable.
duke@435 1164 if (is_alive_non_header() != NULL) {
ysr@2337 1165 verify_referent(obj);
ysr@2337 1166 if (is_alive_non_header()->do_object_b(java_lang_ref_Reference::referent(obj))) {
duke@435 1167 return false; // referent is reachable
duke@435 1168 }
duke@435 1169 }
ysr@888 1170 if (rt == REF_SOFT) {
ysr@888 1171 // For soft refs we can decide now if these are not
ysr@888 1172 // current candidates for clearing, in which case we
ysr@888 1173 // can mark through them now, rather than delaying that
ysr@888 1174 // to the reference-processing phase. Since all current
ysr@888 1175 // time-stamp policies advance the soft-ref clock only
ysr@888 1176 // at a major collection cycle, this is always currently
ysr@888 1177 // accurate.
johnc@3188 1178 if (!_current_soft_ref_policy->should_clear_reference(obj, _soft_ref_timestamp_clock)) {
ysr@888 1179 return false;
ysr@888 1180 }
ysr@888 1181 }
duke@435 1182
johnc@3175 1183 ResourceMark rm; // Needed for tracing.
johnc@3175 1184
ysr@777 1185 HeapWord* const discovered_addr = java_lang_ref_Reference::discovered_addr(obj);
ysr@777 1186 const oop discovered = java_lang_ref_Reference::discovered(obj);
coleenp@548 1187 assert(discovered->is_oop_or_null(), "bad discovered field");
coleenp@548 1188 if (discovered != NULL) {
duke@435 1189 // The reference has already been discovered...
duke@435 1190 if (TraceReferenceGC) {
ysr@3117 1191 gclog_or_tty->print_cr("Already discovered reference (" INTPTR_FORMAT ": %s)",
hseigel@5784 1192 (void *)obj, obj->klass()->internal_name());
duke@435 1193 }
duke@435 1194 if (RefDiscoveryPolicy == ReferentBasedDiscovery) {
duke@435 1195 // assumes that an object is not processed twice;
duke@435 1196 // if it's been already discovered it must be on another
duke@435 1197 // generation's discovered list; so we won't discover it.
duke@435 1198 return false;
duke@435 1199 } else {
duke@435 1200 assert(RefDiscoveryPolicy == ReferenceBasedDiscovery,
duke@435 1201 "Unrecognized policy");
duke@435 1202 // Check assumption that an object is not potentially
duke@435 1203 // discovered twice except by concurrent collectors that potentially
duke@435 1204 // trace the same Reference object twice.
johnc@2316 1205 assert(UseConcMarkSweepGC || UseG1GC,
johnc@2316 1206 "Only possible with a concurrent marking collector");
duke@435 1207 return true;
duke@435 1208 }
duke@435 1209 }
duke@435 1210
duke@435 1211 if (RefDiscoveryPolicy == ReferentBasedDiscovery) {
ysr@2337 1212 verify_referent(obj);
ysr@3117 1213 // Discover if and only if EITHER:
ysr@3117 1214 // .. reference is in our span, OR
ysr@3117 1215 // .. we are an atomic collector and referent is in our span
duke@435 1216 if (_span.contains(obj_addr) ||
ysr@2337 1217 (discovery_is_atomic() &&
ysr@2337 1218 _span.contains(java_lang_ref_Reference::referent(obj)))) {
duke@435 1219 // should_enqueue = true;
duke@435 1220 } else {
duke@435 1221 return false;
duke@435 1222 }
duke@435 1223 } else {
duke@435 1224 assert(RefDiscoveryPolicy == ReferenceBasedDiscovery &&
duke@435 1225 _span.contains(obj_addr), "code inconsistency");
duke@435 1226 }
duke@435 1227
duke@435 1228 // Get the right type of discovered queue head.
duke@435 1229 DiscoveredList* list = get_discovered_list(rt);
duke@435 1230 if (list == NULL) {
duke@435 1231 return false; // nothing special needs to be done
duke@435 1232 }
duke@435 1233
duke@435 1234 if (_discovery_is_mt) {
duke@435 1235 add_to_discovered_list_mt(*list, obj, discovered_addr);
duke@435 1236 } else {
brutisso@6719 1237 // We do a raw store here: the field will be visited later when processing
brutisso@6719 1238 // the discovered references.
ysr@777 1239 oop current_head = list->head();
stefank@3115 1240 // The last ref must have its discovered field pointing to itself.
stefank@3115 1241 oop next_discovered = (current_head != NULL) ? current_head : obj;
stefank@3115 1242
ysr@777 1243 assert(discovered == NULL, "control point invariant");
stefank@3115 1244 oop_store_raw(discovered_addr, next_discovered);
duke@435 1245 list->set_head(obj);
ysr@887 1246 list->inc_length(1);
duke@435 1247
johnc@2316 1248 if (TraceReferenceGC) {
ysr@3117 1249 gclog_or_tty->print_cr("Discovered reference (" INTPTR_FORMAT ": %s)",
hseigel@5784 1250 (void *)obj, obj->klass()->internal_name());
duke@435 1251 }
duke@435 1252 }
ysr@3117 1253 assert(obj->is_oop(), "Discovered a bad reference");
ysr@2337 1254 verify_referent(obj);
duke@435 1255 return true;
duke@435 1256 }
duke@435 1257
duke@435 1258 // Preclean the discovered references by removing those
duke@435 1259 // whose referents are alive, and by marking from those that
duke@435 1260 // are not active. These lists can be handled here
duke@435 1261 // in any order and, indeed, concurrently.
duke@435 1262 void ReferenceProcessor::preclean_discovered_references(
duke@435 1263 BoolObjectClosure* is_alive,
duke@435 1264 OopClosure* keep_alive,
duke@435 1265 VoidClosure* complete_gc,
sla@5237 1266 YieldClosure* yield,
brutisso@6904 1267 GCTimer* gc_timer,
brutisso@6904 1268 GCId gc_id) {
duke@435 1269
duke@435 1270 NOT_PRODUCT(verify_ok_to_handle_reflists());
duke@435 1271
duke@435 1272 // Soft references
duke@435 1273 {
sla@5237 1274 GCTraceTime tt("Preclean SoftReferences", PrintGCDetails && PrintReferenceGC,
brutisso@6904 1275 false, gc_timer, gc_id);
jmasa@3357 1276 for (uint i = 0; i < _max_num_q; i++) {
ysr@887 1277 if (yield->should_return()) {
ysr@887 1278 return;
ysr@887 1279 }
duke@435 1280 preclean_discovered_reflist(_discoveredSoftRefs[i], is_alive,
duke@435 1281 keep_alive, complete_gc, yield);
duke@435 1282 }
duke@435 1283 }
duke@435 1284
duke@435 1285 // Weak references
duke@435 1286 {
sla@5237 1287 GCTraceTime tt("Preclean WeakReferences", PrintGCDetails && PrintReferenceGC,
brutisso@6904 1288 false, gc_timer, gc_id);
jmasa@3357 1289 for (uint i = 0; i < _max_num_q; i++) {
ysr@887 1290 if (yield->should_return()) {
ysr@887 1291 return;
ysr@887 1292 }
duke@435 1293 preclean_discovered_reflist(_discoveredWeakRefs[i], is_alive,
duke@435 1294 keep_alive, complete_gc, yield);
duke@435 1295 }
duke@435 1296 }
duke@435 1297
duke@435 1298 // Final references
duke@435 1299 {
sla@5237 1300 GCTraceTime tt("Preclean FinalReferences", PrintGCDetails && PrintReferenceGC,
brutisso@6904 1301 false, gc_timer, gc_id);
jmasa@3357 1302 for (uint i = 0; i < _max_num_q; i++) {
ysr@887 1303 if (yield->should_return()) {
ysr@887 1304 return;
ysr@887 1305 }
duke@435 1306 preclean_discovered_reflist(_discoveredFinalRefs[i], is_alive,
duke@435 1307 keep_alive, complete_gc, yield);
duke@435 1308 }
duke@435 1309 }
duke@435 1310
duke@435 1311 // Phantom references
duke@435 1312 {
sla@5237 1313 GCTraceTime tt("Preclean PhantomReferences", PrintGCDetails && PrintReferenceGC,
brutisso@6904 1314 false, gc_timer, gc_id);
jmasa@3357 1315 for (uint i = 0; i < _max_num_q; i++) {
ysr@887 1316 if (yield->should_return()) {
ysr@887 1317 return;
ysr@887 1318 }
duke@435 1319 preclean_discovered_reflist(_discoveredPhantomRefs[i], is_alive,
duke@435 1320 keep_alive, complete_gc, yield);
duke@435 1321 }
jmasa@7469 1322
jmasa@7469 1323 // Cleaner references. Included in timing for phantom references. We
jmasa@7469 1324 // expect Cleaner references to be temporary, and don't want to deal with
jmasa@7469 1325 // possible incompatibilities arising from making it more visible.
jmasa@7469 1326 for (uint i = 0; i < _max_num_q; i++) {
jmasa@7469 1327 if (yield->should_return()) {
jmasa@7469 1328 return;
jmasa@7469 1329 }
jmasa@7469 1330 preclean_discovered_reflist(_discoveredCleanerRefs[i], is_alive,
jmasa@7469 1331 keep_alive, complete_gc, yield);
jmasa@7469 1332 }
duke@435 1333 }
duke@435 1334 }
duke@435 1335
duke@435 1336 // Walk the given discovered ref list, and remove all reference objects
duke@435 1337 // whose referents are still alive, whose referents are NULL or which
ysr@887 1338 // are not active (have a non-NULL next field). NOTE: When we are
ysr@887 1339 // thus precleaning the ref lists (which happens single-threaded today),
ysr@887 1340 // we do not disable refs discovery to honour the correct semantics of
ysr@887 1341 // java.lang.Reference. As a result, we need to be careful below
ysr@887 1342 // that ref removal steps interleave safely with ref discovery steps
ysr@887 1343 // (in this thread).
coleenp@548 1344 void
coleenp@548 1345 ReferenceProcessor::preclean_discovered_reflist(DiscoveredList& refs_list,
coleenp@548 1346 BoolObjectClosure* is_alive,
coleenp@548 1347 OopClosure* keep_alive,
coleenp@548 1348 VoidClosure* complete_gc,
coleenp@548 1349 YieldClosure* yield) {
brutisso@6719 1350 DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
duke@435 1351 while (iter.has_next()) {
duke@435 1352 iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */));
coleenp@548 1353 oop obj = iter.obj();
coleenp@548 1354 oop next = java_lang_ref_Reference::next(obj);
duke@435 1355 if (iter.referent() == NULL || iter.is_referent_alive() ||
coleenp@548 1356 next != NULL) {
duke@435 1357 // The referent has been cleared, or is alive, or the Reference is not
duke@435 1358 // active; we need to trace and mark its cohort.
duke@435 1359 if (TraceReferenceGC) {
duke@435 1360 gclog_or_tty->print_cr("Precleaning Reference (" INTPTR_FORMAT ": %s)",
hseigel@5784 1361 (void *)iter.obj(), iter.obj()->klass()->internal_name());
duke@435 1362 }
duke@435 1363 // Remove Reference object from list
duke@435 1364 iter.remove();
duke@435 1365 // Keep alive its cohort.
duke@435 1366 iter.make_referent_alive();
coleenp@548 1367 if (UseCompressedOops) {
coleenp@548 1368 narrowOop* next_addr = (narrowOop*)java_lang_ref_Reference::next_addr(obj);
coleenp@548 1369 keep_alive->do_oop(next_addr);
coleenp@548 1370 } else {
coleenp@548 1371 oop* next_addr = (oop*)java_lang_ref_Reference::next_addr(obj);
coleenp@548 1372 keep_alive->do_oop(next_addr);
coleenp@548 1373 }
ysr@887 1374 iter.move_to_next();
duke@435 1375 } else {
duke@435 1376 iter.next();
duke@435 1377 }
duke@435 1378 }
duke@435 1379 // Close the reachable set
duke@435 1380 complete_gc->do_void();
duke@435 1381
duke@435 1382 NOT_PRODUCT(
ysr@2651 1383 if (PrintGCDetails && PrintReferenceGC && (iter.processed() > 0)) {
jmasa@2188 1384 gclog_or_tty->print_cr(" Dropped %d Refs out of %d "
jmasa@2188 1385 "Refs in discovered list " INTPTR_FORMAT,
jmasa@2188 1386 iter.removed(), iter.processed(), (address)refs_list.head());
duke@435 1387 }
duke@435 1388 )
duke@435 1389 }
duke@435 1390
jmasa@3357 1391 const char* ReferenceProcessor::list_name(uint i) {
johnc@3175 1392 assert(i >= 0 && i <= _max_num_q * number_of_subclasses_of_ref(),
johnc@3175 1393 "Out of bounds index");
johnc@3175 1394
jmasa@2188 1395 int j = i / _max_num_q;
duke@435 1396 switch (j) {
duke@435 1397 case 0: return "SoftRef";
duke@435 1398 case 1: return "WeakRef";
duke@435 1399 case 2: return "FinalRef";
duke@435 1400 case 3: return "PhantomRef";
jmasa@7469 1401 case 4: return "CleanerRef";
duke@435 1402 }
duke@435 1403 ShouldNotReachHere();
duke@435 1404 return NULL;
duke@435 1405 }
duke@435 1406
duke@435 1407 #ifndef PRODUCT
duke@435 1408 void ReferenceProcessor::verify_ok_to_handle_reflists() {
duke@435 1409 // empty for now
duke@435 1410 }
duke@435 1411 #endif
duke@435 1412
duke@435 1413 #ifndef PRODUCT
duke@435 1414 void ReferenceProcessor::clear_discovered_references() {
duke@435 1415 guarantee(!_discovering_refs, "Discovering refs?");
jmasa@3357 1416 for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) {
johnc@3210 1417 clear_discovered_references(_discovered_refs[i]);
duke@435 1418 }
duke@435 1419 }
stefank@3115 1420
duke@435 1421 #endif // PRODUCT

mercurial