src/share/vm/memory/referenceProcessor.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 9665
a8441ccaff15
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

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

mercurial