src/share/vm/memory/referenceProcessor.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7741
35c7330b68e2
parent 7535
7ae4e26cb1e0
child 9703
2fdf635bcf28
permissions
-rw-r--r--

merge

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

mercurial