src/share/vm/memory/referenceProcessor.cpp

Tue, 15 Mar 2016 10:11:02 +0100

author
stefank
date
Tue, 15 Mar 2016 10:11:02 +0100
changeset 9665
a8441ccaff15
parent 7741
35c7330b68e2
child 9703
2fdf635bcf28
child 9858
b985cbb00e68
permissions
-rw-r--r--

8151539: Remove duplicate AlwaysTrueClosures
Reviewed-by: tschatzl, mgerdin, kbarrett, drwhite

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

mercurial