src/share/vm/memory/referenceProcessor.cpp

Fri, 07 Sep 2012 12:04:16 -0400

author
coleenp
date
Fri, 07 Sep 2012 12:04:16 -0400
changeset 4047
aed758eda82a
parent 4037
da91efe96a93
child 5159
001ec9515f84
permissions
-rw-r--r--

7195833: NPG: Rename instanceClassLoaderKlass, instanceRefKlass and instanceMirrorKlass
Summary: Simple renaming to be consistent with instanceKlass->InstanceKlass renaming
Reviewed-by: stefank, jmasa

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

mercurial