src/share/vm/memory/referenceProcessor.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial