src/share/vm/memory/referenceProcessor.cpp

Fri, 10 Jan 2014 09:53:53 +0100

author
pliden
date
Fri, 10 Jan 2014 09:53:53 +0100
changeset 6395
a258f8cb530f
parent 5784
190899198332
child 6397
d60ecdb2773e
permissions
-rw-r--r--

8029255: G1: Reference processing should not enqueue references on the shared SATB queue
Reviewed-by: brutisso, tschatzl

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

mercurial