src/share/vm/memory/referenceProcessor.cpp

Thu, 22 May 2014 15:52:41 -0400

author
drchase
date
Thu, 22 May 2014 15:52:41 -0400
changeset 6680
78bbf4d43a14
parent 6397
d60ecdb2773e
child 6719
8e20ef014b08
permissions
-rw-r--r--

8037816: Fix for 8036122 breaks build with Xcode5/clang
8043029: Change 8037816 breaks HS build with older GCC versions which don't support diagnostic pragmas
8043164: Format warning in traceStream.hpp
Summary: Backport of main fix + two corrections, enables clang compilation, turns on format attributes, corrects/mutes warnings
Reviewed-by: kvn, coleenp, iveresov, twisti

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

mercurial