src/share/vm/memory/referenceProcessor.cpp

Thu, 26 Sep 2013 10:25:02 -0400

author
hseigel
date
Thu, 26 Sep 2013 10:25:02 -0400
changeset 5784
190899198332
parent 5237
f2110083203d
child 6395
a258f8cb530f
permissions
-rw-r--r--

7195622: CheckUnhandledOops has limited usefulness now
Summary: Enable CHECK_UNHANDLED_OOPS in fastdebug builds across all supported platforms.
Reviewed-by: coleenp, hseigel, dholmes, stefank, twisti, ihse, rdurbin
Contributed-by: lois.foltan@oracle.com

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

mercurial