src/share/vm/memory/referenceProcessor.cpp

Mon, 19 Dec 2011 10:02:05 -0800

author
johnc
date
Mon, 19 Dec 2011 10:02:05 -0800
changeset 3339
e7dead7e90af
parent 3210
bf2d2b8b1726
child 3357
441e946dc1af
permissions
-rw-r--r--

7117303: VM uses non-monotonic time source and complains that it is non-monotonic
Summary: Replaces calls to os::javaTimeMillis(), which does not (and cannot) guarantee monotonicity, in GC code to an equivalent expression that uses os::javaTimeNanos(). os::javaTimeNanos is guaranteed monotonically non-decreasing if the underlying platform provides a monotonic time source. Changes in OS files are to make use of the newly defined constants in globalDefinitions.hpp.
Reviewed-by: dholmes, ysr

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

mercurial