src/share/vm/memory/referenceProcessor.cpp

Print this page
rev 2652 : 7085906: Replace the permgen allocated sentinelRef with a self-looped end
Summary: Remove the sentinelRef and let the last Reference in a discovered chain point back to itself.
Reviewed-by: TBD1, TBD2


  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "gc_interface/collectedHeap.hpp"
  29 #include "gc_interface/collectedHeap.inline.hpp"
  30 #include "memory/referencePolicy.hpp"
  31 #include "memory/referenceProcessor.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/java.hpp"
  34 #include "runtime/jniHandles.hpp"
  35 
  36 ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL;
  37 ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy      = NULL;
  38 oop              ReferenceProcessor::_sentinelRef = NULL;
  39 const int        subclasses_of_ref                = REF_PHANTOM - REF_OTHER;
  40 
  41 // List of discovered references.
  42 class DiscoveredList {
  43 public:
  44   DiscoveredList() : _len(0), _compressed_head(0), _oop_head(NULL) { }
  45   oop head() const     {
  46      return UseCompressedOops ?  oopDesc::decode_heap_oop_not_null(_compressed_head) :
  47                                 _oop_head;
  48   }
  49   HeapWord* adr_head() {
  50     return UseCompressedOops ? (HeapWord*)&_compressed_head :
  51                                (HeapWord*)&_oop_head;
  52   }
  53   void   set_head(oop o) {
  54     if (UseCompressedOops) {
  55       // Must compress the head ptr.
  56       _compressed_head = oopDesc::encode_heap_oop_not_null(o);
  57     } else {
  58       _oop_head = o;
  59     }
  60   }
  61   bool   empty() const          { return head() == ReferenceProcessor::sentinel_ref(); }
  62   size_t length()               { return _len; }
  63   void   set_length(size_t len) { _len = len;  }
  64   void   inc_length(size_t inc) { _len += inc; assert(_len > 0, "Error"); }
  65   void   dec_length(size_t dec) { _len -= dec; }
  66 private:
  67   // Set value depending on UseCompressedOops. This could be a template class
  68   // but then we have to fix all the instantiations and declarations that use this class.
  69   oop       _oop_head;
  70   narrowOop _compressed_head;
  71   size_t _len;
  72 };
  73 
  74 void referenceProcessor_init() {
  75   ReferenceProcessor::init_statics();
  76 }
  77 
  78 void ReferenceProcessor::init_statics() {
  79   assert(_sentinelRef == NULL, "should be initialized precisely once");
  80   EXCEPTION_MARK;
  81   _sentinelRef = instanceKlass::cast(
  82                     SystemDictionary::Reference_klass())->
  83                       allocate_permanent_instance(THREAD);
  84 
  85   // Initialize the master soft ref clock.
  86   java_lang_ref_SoftReference::set_clock(os::javaTimeMillis());
  87 
  88   if (HAS_PENDING_EXCEPTION) {
  89       Handle ex(THREAD, PENDING_EXCEPTION);
  90       vm_exit_during_initialization(ex);
  91   }
  92   assert(_sentinelRef != NULL && _sentinelRef->is_oop(),
  93          "Just constructed it!");
  94   _always_clear_soft_ref_policy = new AlwaysClearPolicy();
  95   _default_soft_ref_policy      = new COMPILER2_PRESENT(LRUMaxHeapPolicy())
  96                                       NOT_COMPILER2(LRUCurrentHeapPolicy());
  97   if (_always_clear_soft_ref_policy == NULL || _default_soft_ref_policy == NULL) {
  98     vm_exit_during_initialization("Could not allocate reference policy object");
  99   }
 100   guarantee(RefDiscoveryPolicy == ReferenceBasedDiscovery ||
 101             RefDiscoveryPolicy == ReferentBasedDiscovery,
 102             "Unrecongnized RefDiscoveryPolicy");
 103 }
 104 
 105 ReferenceProcessor::ReferenceProcessor(MemRegion span,
 106                                        bool      mt_processing,
 107                                        int       mt_processing_degree,
 108                                        bool      mt_discovery,
 109                                        int       mt_discovery_degree,
 110                                        bool      atomic_discovery,
 111                                        BoolObjectClosure* is_alive_non_header,
 112                                        bool      discovered_list_needs_barrier)  :
 113   _discovering_refs(false),
 114   _enqueuing_is_done(false),
 115   _is_alive_non_header(is_alive_non_header),
 116   _discovered_list_needs_barrier(discovered_list_needs_barrier),
 117   _bs(NULL),
 118   _processing_is_mt(mt_processing),
 119   _next_id(0)
 120 {
 121   _span = span;
 122   _discovery_is_atomic = atomic_discovery;
 123   _discovery_is_mt     = mt_discovery;
 124   _num_q               = MAX2(1, mt_processing_degree);
 125   _max_num_q           = MAX2(_num_q, mt_discovery_degree);
 126   _discoveredSoftRefs  = NEW_C_HEAP_ARRAY(DiscoveredList, _max_num_q * subclasses_of_ref);
 127   if (_discoveredSoftRefs == NULL) {
 128     vm_exit_during_initialization("Could not allocated RefProc Array");
 129   }
 130   _discoveredWeakRefs    = &_discoveredSoftRefs[_max_num_q];
 131   _discoveredFinalRefs   = &_discoveredWeakRefs[_max_num_q];
 132   _discoveredPhantomRefs = &_discoveredFinalRefs[_max_num_q];
 133   assert(sentinel_ref() != NULL, "_sentinelRef is NULL");
 134   // Initialized all entries to _sentinelRef
 135   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 136         _discoveredSoftRefs[i].set_head(sentinel_ref());
 137     _discoveredSoftRefs[i].set_length(0);
 138   }
 139   // If we do barreirs, cache a copy of the barrier set.
 140   if (discovered_list_needs_barrier) {
 141     _bs = Universe::heap()->barrier_set();
 142   }
 143   setup_policy(false /* default soft ref policy */);
 144 }
 145 
 146 #ifndef PRODUCT
 147 void ReferenceProcessor::verify_no_references_recorded() {
 148   guarantee(!_discovering_refs, "Discovering refs?");
 149   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 150     guarantee(_discoveredSoftRefs[i].empty(),
 151               "Found non-empty discovered list");
 152   }
 153 }
 154 #endif
 155 
 156 void ReferenceProcessor::weak_oops_do(OopClosure* f) {
 157   // Should this instead be
 158   // for (int i = 0; i < subclasses_of_ref; i++_ {
 159   //   for (int j = 0; j < _num_q; j++) {
 160   //     int index = i * _max_num_q + j;
 161   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 162     if (UseCompressedOops) {
 163       f->do_oop((narrowOop*)_discoveredSoftRefs[i].adr_head());
 164     } else {
 165       f->do_oop((oop*)_discoveredSoftRefs[i].adr_head());
 166     }
 167   }
 168 }
 169 
 170 void ReferenceProcessor::oops_do(OopClosure* f) {
 171   f->do_oop(adr_sentinel_ref());
 172 }
 173 
 174 void ReferenceProcessor::update_soft_ref_master_clock() {
 175   // Update (advance) the soft ref master clock field. This must be done
 176   // after processing the soft ref list.
 177   jlong now = os::javaTimeMillis();
 178   jlong clock = java_lang_ref_SoftReference::clock();
 179   NOT_PRODUCT(
 180   if (now < clock) {
 181     warning("time warp: %d to %d", clock, now);
 182   }
 183   )
 184   // In product mode, protect ourselves from system time being adjusted
 185   // externally and going backward; see note in the implementation of
 186   // GenCollectedHeap::time_since_last_gc() for the right way to fix
 187   // this uniformly throughout the VM; see bug-id 4741166. XXX
 188   if (now > clock) {
 189     java_lang_ref_SoftReference::set_clock(now);
 190   }
 191   // Else leave clock stalled at its old value until time progresses
 192   // past clock value.
 193 }


 266     void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
 267     int count() { return _count; }
 268   };
 269   CountHandleClosure global_handle_count;
 270   AlwaysAliveClosure always_alive;
 271   JNIHandles::weak_oops_do(&always_alive, &global_handle_count);
 272   return global_handle_count.count();
 273 }
 274 #endif
 275 
 276 void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive,
 277                                           OopClosure*        keep_alive,
 278                                           VoidClosure*       complete_gc) {
 279 #ifndef PRODUCT
 280   if (PrintGCDetails && PrintReferenceGC) {
 281     unsigned int count = count_jni_refs();
 282     gclog_or_tty->print(", %u refs", count);
 283   }
 284 #endif
 285   JNIHandles::weak_oops_do(is_alive, keep_alive);
 286   // Finally remember to keep sentinel around
 287   keep_alive->do_oop(adr_sentinel_ref());
 288   complete_gc->do_void();
 289 }
 290 
 291 
 292 template <class T>
 293 bool enqueue_discovered_ref_helper(ReferenceProcessor* ref,
 294                                    AbstractRefProcTaskExecutor* task_executor) {
 295 
 296   // Remember old value of pending references list
 297   T* pending_list_addr = (T*)java_lang_ref_Reference::pending_list_addr();
 298   T old_pending_list_value = *pending_list_addr;
 299 
 300   // Enqueue references that are not made active again, and
 301   // clear the decks for the next collection (cycle).
 302   ref->enqueue_discovered_reflists((HeapWord*)pending_list_addr, task_executor);
 303   // Do the oop-check on pending_list_addr missed in
 304   // enqueue_discovered_reflist. We should probably
 305   // do a raw oop_check so that future such idempotent
 306   // oop_stores relying on the oop-check side-effect
 307   // may be elided automatically and safely without


 317 
 318 bool ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor) {
 319   NOT_PRODUCT(verify_ok_to_handle_reflists());
 320   if (UseCompressedOops) {
 321     return enqueue_discovered_ref_helper<narrowOop>(this, task_executor);
 322   } else {
 323     return enqueue_discovered_ref_helper<oop>(this, task_executor);
 324   }
 325 }
 326 
 327 void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list,
 328                                                     HeapWord* pending_list_addr) {
 329   // Given a list of refs linked through the "discovered" field
 330   // (java.lang.ref.Reference.discovered) chain them through the
 331   // "next" field (java.lang.ref.Reference.next) and prepend
 332   // to the pending list.
 333   if (TraceReferenceGC && PrintGCDetails) {
 334     gclog_or_tty->print_cr("ReferenceProcessor::enqueue_discovered_reflist list "
 335                            INTPTR_FORMAT, (address)refs_list.head());
 336   }
 337   oop obj = refs_list.head();


 338   // Walk down the list, copying the discovered field into
 339   // the next field and clearing it (except for the last
 340   // non-sentinel object which is treated specially to avoid
 341   // confusion with an active reference).
 342   while (obj != sentinel_ref()) {
 343     assert(obj->is_instanceRef(), "should be reference object");
 344     oop next = java_lang_ref_Reference::discovered(obj);
 345     if (TraceReferenceGC && PrintGCDetails) {
 346       gclog_or_tty->print_cr("        obj " INTPTR_FORMAT "/next " INTPTR_FORMAT,
 347                              obj, next);
 348     }
 349     assert(java_lang_ref_Reference::next(obj) == NULL,
 350            "The reference should not be enqueued");
 351     if (next == sentinel_ref()) {  // obj is last
 352       // Swap refs_list into pendling_list_addr and
 353       // set obj's next to what we read from pending_list_addr.
 354       oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr);
 355       // Need oop_check on pending_list_addr above;
 356       // see special oop-check code at the end of
 357       // enqueue_discovered_reflists() further below.
 358       if (old == NULL) {
 359         // obj should be made to point to itself, since
 360         // pending list was empty.
 361         java_lang_ref_Reference::set_next(obj, obj);
 362       } else {
 363         java_lang_ref_Reference::set_next(obj, old);
 364       }
 365     } else {
 366       java_lang_ref_Reference::set_next(obj, next);
 367     }
 368     java_lang_ref_Reference::set_discovered(obj, (oop) NULL);
 369     obj = next;
 370   }
 371 }
 372 
 373 // Parallel enqueue task
 374 class RefProcEnqueueTask: public AbstractRefProcTaskExecutor::EnqueueTask {
 375 public:
 376   RefProcEnqueueTask(ReferenceProcessor& ref_processor,
 377                      DiscoveredList      discovered_refs[],
 378                      HeapWord*           pending_list_addr,
 379                      oop                 sentinel_ref,
 380                      int                 n_queues)
 381     : EnqueueTask(ref_processor, discovered_refs,
 382                   pending_list_addr, sentinel_ref, n_queues)
 383   { }
 384 
 385   virtual void work(unsigned int work_id) {
 386     assert(work_id < (unsigned int)_ref_processor.max_num_q(), "Index out-of-bounds");
 387     // Simplest first cut: static partitioning.
 388     int index = work_id;
 389     // The increment on "index" must correspond to the maximum number of queues
 390     // (n_queues) with which that ReferenceProcessor was created.  That
 391     // is because of the "clever" way the discovered references lists were
 392     // allocated and are indexed into.
 393     assert(_n_queues == (int) _ref_processor.max_num_q(), "Different number not expected");
 394     for (int j = 0;
 395          j < subclasses_of_ref;
 396          j++, index += _n_queues) {
 397       _ref_processor.enqueue_discovered_reflist(
 398         _refs_lists[index], _pending_list_addr);
 399       _refs_lists[index].set_head(_sentinel_ref);
 400       _refs_lists[index].set_length(0);
 401     }
 402   }
 403 };
 404 
 405 // Enqueue references that are not made active again
 406 void ReferenceProcessor::enqueue_discovered_reflists(HeapWord* pending_list_addr,
 407   AbstractRefProcTaskExecutor* task_executor) {
 408   if (_processing_is_mt && task_executor != NULL) {
 409     // Parallel code
 410     RefProcEnqueueTask tsk(*this, _discoveredSoftRefs,
 411                            pending_list_addr, sentinel_ref(), _max_num_q);
 412     task_executor->execute(tsk);
 413   } else {
 414     // Serial code: call the parent class's implementation
 415     for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 416       enqueue_discovered_reflist(_discoveredSoftRefs[i], pending_list_addr);
 417       _discoveredSoftRefs[i].set_head(sentinel_ref());
 418       _discoveredSoftRefs[i].set_length(0);
 419     }
 420   }
 421 }
 422 
 423 // Iterator for the list of discovered references.
 424 class DiscoveredListIterator {
 425 public:
 426   inline DiscoveredListIterator(DiscoveredList&    refs_list,
 427                                 OopClosure*        keep_alive,
 428                                 BoolObjectClosure* is_alive);
 429 
 430   // End Of List.
 431   inline bool has_next() const { return _next != ReferenceProcessor::sentinel_ref(); }
 432 
 433   // Get oop to the Reference object.
 434   inline oop obj() const { return _ref; }
 435 
 436   // Get oop to the referent object.
 437   inline oop referent() const { return _referent; }
 438 
 439   // Returns true if referent is alive.
 440   inline bool is_referent_alive() const;
 441 
 442   // Loads data for the current reference.
 443   // The "allow_null_referent" argument tells us to allow for the possibility
 444   // of a NULL referent in the discovered Reference object. This typically
 445   // happens in the case of concurrent collectors that may have done the
 446   // discovery concurrently, or interleaved, with mutator execution.
 447   inline void load_ptrs(DEBUG_ONLY(bool allow_null_referent));
 448 
 449   // Move to the next discovered reference.
 450   inline void next();
 451 
 452   // Remove the current reference from the list
 453   inline void remove();
 454 
 455   // Make the Reference object active again.
 456   inline void make_active() { java_lang_ref_Reference::set_next(_ref, NULL); }
 457 
 458   // Make the referent alive.
 459   inline void make_referent_alive() {
 460     if (UseCompressedOops) {
 461       _keep_alive->do_oop((narrowOop*)_referent_addr);
 462     } else {
 463       _keep_alive->do_oop((oop*)_referent_addr);
 464     }
 465   }
 466 
 467   // Update the discovered field.
 468   inline void update_discovered() {
 469     // First _prev_next ref actually points into DiscoveredList (gross).
 470     if (UseCompressedOops) {

 471       _keep_alive->do_oop((narrowOop*)_prev_next);

 472     } else {

 473       _keep_alive->do_oop((oop*)_prev_next);
 474     }
 475   }

 476 
 477   // NULL out referent pointer.
 478   inline void clear_referent() { oop_store_raw(_referent_addr, NULL); }
 479 
 480   // Statistics
 481   NOT_PRODUCT(
 482   inline size_t processed() const { return _processed; }
 483   inline size_t removed() const   { return _removed; }
 484   )
 485 
 486   inline void move_to_next();
 487 
 488 private:
 489   DiscoveredList&    _refs_list;
 490   HeapWord*          _prev_next;

 491   oop                _ref;
 492   HeapWord*          _discovered_addr;
 493   oop                _next;
 494   HeapWord*          _referent_addr;
 495   oop                _referent;
 496   OopClosure*        _keep_alive;
 497   BoolObjectClosure* _is_alive;
 498   DEBUG_ONLY(
 499   oop                _first_seen; // cyclic linked list check
 500   )
 501   NOT_PRODUCT(
 502   size_t             _processed;
 503   size_t             _removed;
 504   )
 505 };
 506 
 507 inline DiscoveredListIterator::DiscoveredListIterator(DiscoveredList&    refs_list,
 508                                                       OopClosure*        keep_alive,
 509                                                       BoolObjectClosure* is_alive)
 510   : _refs_list(refs_list),
 511     _prev_next(refs_list.adr_head()),

 512     _ref(refs_list.head()),
 513 #ifdef ASSERT
 514     _first_seen(refs_list.head()),
 515 #endif
 516 #ifndef PRODUCT
 517     _processed(0),
 518     _removed(0),
 519 #endif
 520     _next(refs_list.head()),
 521     _keep_alive(keep_alive),
 522     _is_alive(is_alive)
 523 { }
 524 
 525 inline bool DiscoveredListIterator::is_referent_alive() const {
 526   return _is_alive->do_object_b(_referent);
 527 }
 528 
 529 inline void DiscoveredListIterator::load_ptrs(DEBUG_ONLY(bool allow_null_referent)) {
 530   _discovered_addr = java_lang_ref_Reference::discovered_addr(_ref);
 531   oop discovered = java_lang_ref_Reference::discovered(_ref);
 532   assert(_discovered_addr && discovered->is_oop_or_null(),
 533          "discovered field is bad");
 534   _next = discovered;
 535   _referent_addr = java_lang_ref_Reference::referent_addr(_ref);
 536   _referent = java_lang_ref_Reference::referent(_ref);
 537   assert(Universe::heap()->is_in_reserved_or_null(_referent),
 538          "Wrong oop found in java.lang.Reference object");
 539   assert(allow_null_referent ?
 540              _referent->is_oop_or_null()
 541            : _referent->is_oop(),
 542          "bad referent");
 543 }
 544 
 545 inline void DiscoveredListIterator::next() {
 546   _prev_next = _discovered_addr;

 547   move_to_next();
 548 }
 549 
 550 inline void DiscoveredListIterator::remove() {
 551   assert(_ref->is_oop(), "Dropping a bad reference");
 552   oop_store_raw(_discovered_addr, NULL);

 553   // First _prev_next ref actually points into DiscoveredList (gross).










 554   if (UseCompressedOops) {
 555     // Remove Reference object from list.
 556     oopDesc::encode_store_heap_oop_not_null((narrowOop*)_prev_next, _next);
 557   } else {
 558     // Remove Reference object from list.
 559     oopDesc::store_heap_oop((oop*)_prev_next, _next);
 560   }
 561   NOT_PRODUCT(_removed++);
 562   _refs_list.dec_length(1);
 563 }
 564 
 565 inline void DiscoveredListIterator::move_to_next() {




 566   _ref = _next;

 567   assert(_ref != _first_seen, "cyclic ref_list found");
 568   NOT_PRODUCT(_processed++);
 569 }
 570 
 571 // NOTE: process_phase*() are largely similar, and at a high level
 572 // merely iterate over the extant list applying a predicate to
 573 // each of its elements and possibly removing that element from the
 574 // list and applying some further closures to that element.
 575 // We should consider the possibility of replacing these
 576 // process_phase*() methods by abstracting them into
 577 // a single general iterator invocation that receives appropriate
 578 // closures that accomplish this work.
 579 
 580 // (SoftReferences only) Traverse the list and remove any SoftReferences whose
 581 // referents are not alive, but that should be kept alive for policy reasons.
 582 // Keep alive the transitive closure of all such referents.
 583 void
 584 ReferenceProcessor::process_phase1(DiscoveredList&    refs_list,
 585                                    ReferencePolicy*   policy,
 586                                    BoolObjectClosure* is_alive,


 708   ResourceMark rm;
 709   DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
 710   while (iter.has_next()) {
 711     iter.update_discovered();
 712     iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */));
 713     if (clear_referent) {
 714       // NULL out referent pointer
 715       iter.clear_referent();
 716     } else {
 717       // keep the referent around
 718       iter.make_referent_alive();
 719     }
 720     if (TraceReferenceGC) {
 721       gclog_or_tty->print_cr("Adding %sreference (" INTPTR_FORMAT ": %s) as pending",
 722                              clear_referent ? "cleared " : "",
 723                              iter.obj(), iter.obj()->blueprint()->internal_name());
 724     }
 725     assert(iter.obj()->is_oop(UseConcMarkSweepGC), "Adding a bad reference");
 726     iter.next();
 727   }
 728   // Remember to keep sentinel pointer around
 729   iter.update_discovered();
 730   // Close the reachable set
 731   complete_gc->do_void();
 732 }
 733 
 734 void
 735 ReferenceProcessor::abandon_partial_discovered_list(DiscoveredList& refs_list) {
 736   oop obj = refs_list.head();
 737   while (obj != sentinel_ref()) {
 738     oop discovered = java_lang_ref_Reference::discovered(obj);


 739     java_lang_ref_Reference::set_discovered_raw(obj, NULL);
 740     obj = discovered;
 741   }
 742   refs_list.set_head(sentinel_ref());
 743   refs_list.set_length(0);
 744 }
 745 





 746 void ReferenceProcessor::abandon_partial_discovery() {
 747   // loop over the lists
 748   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 749     if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) {
 750       gclog_or_tty->print_cr("\nAbandoning %s discovered list",
 751                              list_name(i));
 752     }
 753     abandon_partial_discovered_list(_discoveredSoftRefs[i]);
 754   }
 755 }
 756 
 757 class RefProcPhase1Task: public AbstractRefProcTaskExecutor::ProcessTask {
 758 public:
 759   RefProcPhase1Task(ReferenceProcessor& ref_processor,
 760                     DiscoveredList      refs_lists[],
 761                     ReferencePolicy*    policy,
 762                     bool                marks_oops_alive)
 763     : ProcessTask(ref_processor, refs_lists, marks_oops_alive),
 764       _policy(policy)
 765   { }


 842   int to_idx = 0;
 843   for (int from_idx = 0; from_idx < _max_num_q; from_idx++) {
 844     bool move_all = false;
 845     if (from_idx >= _num_q) {
 846       move_all = ref_lists[from_idx].length() > 0;
 847     }
 848     while ((ref_lists[from_idx].length() > avg_refs) ||
 849            move_all) {
 850       assert(to_idx < _num_q, "Sanity Check!");
 851       if (ref_lists[to_idx].length() < avg_refs) {
 852         // move superfluous refs
 853         size_t refs_to_move;
 854         // Move all the Ref's if the from queue will not be processed.
 855         if (move_all) {
 856           refs_to_move = MIN2(ref_lists[from_idx].length(),
 857                               avg_refs - ref_lists[to_idx].length());
 858         } else {
 859           refs_to_move = MIN2(ref_lists[from_idx].length() - avg_refs,
 860                               avg_refs - ref_lists[to_idx].length());
 861         }



 862         oop move_head = ref_lists[from_idx].head();
 863         oop move_tail = move_head;
 864         oop new_head  = move_head;
 865         // find an element to split the list on
 866         for (size_t j = 0; j < refs_to_move; ++j) {
 867           move_tail = new_head;
 868           new_head = java_lang_ref_Reference::discovered(new_head);
 869         }






 870         java_lang_ref_Reference::set_discovered(move_tail, ref_lists[to_idx].head());

 871         ref_lists[to_idx].set_head(move_head);
 872         ref_lists[to_idx].inc_length(refs_to_move);






 873         ref_lists[from_idx].set_head(new_head);

 874         ref_lists[from_idx].dec_length(refs_to_move);
 875         if (ref_lists[from_idx].length() == 0) {
 876           break;
 877         }
 878       } else {
 879         to_idx = (to_idx + 1) % _num_q;
 880       }
 881     }
 882   }
 883 #ifdef ASSERT
 884   size_t balanced_total_refs = 0;
 885   for (int i = 0; i < _max_num_q; ++i) {
 886     balanced_total_refs += ref_lists[i].length();
 887     if (TraceReferenceGC && PrintGCDetails) {
 888       gclog_or_tty->print("%d ", ref_lists[i].length());
 889     }
 890   }
 891   if (TraceReferenceGC && PrintGCDetails) {
 892     gclog_or_tty->print_cr(" = %d", balanced_total_refs);
 893     gclog_or_tty->flush();


1065       break;
1066     case REF_NONE:
1067       // we should not reach here if we are an instanceRefKlass
1068     default:
1069       ShouldNotReachHere();
1070   }
1071   if (TraceReferenceGC && PrintGCDetails) {
1072     gclog_or_tty->print_cr("Thread %d gets list " INTPTR_FORMAT, id, list);
1073   }
1074   return list;
1075 }
1076 
1077 inline void
1078 ReferenceProcessor::add_to_discovered_list_mt(DiscoveredList& refs_list,
1079                                               oop             obj,
1080                                               HeapWord*       discovered_addr) {
1081   assert(_discovery_is_mt, "!_discovery_is_mt should have been handled by caller");
1082   // First we must make sure this object is only enqueued once. CAS in a non null
1083   // discovered_addr.
1084   oop current_head = refs_list.head();


1085 
1086   // Note: In the case of G1, this specific pre-barrier is strictly
1087   // not necessary because the only case we are interested in
1088   // here is when *discovered_addr is NULL (see the CAS further below),
1089   // so this will expand to nothing. As a result, we have manually
1090   // elided this out for G1, but left in the test for some future
1091   // collector that might have need for a pre-barrier here.
1092   if (_discovered_list_needs_barrier && !UseG1GC) {
1093     if (UseCompressedOops) {
1094       _bs->write_ref_field_pre((narrowOop*)discovered_addr, current_head);
1095     } else {
1096       _bs->write_ref_field_pre((oop*)discovered_addr, current_head);
1097     }
1098     guarantee(false, "Need to check non-G1 collector");
1099   }
1100   oop retest = oopDesc::atomic_compare_exchange_oop(current_head, discovered_addr,
1101                                                     NULL);
1102   if (retest == NULL) {
1103     // This thread just won the right to enqueue the object.
1104     // We have separate lists for enqueueing so no synchronization
1105     // is necessary.
1106     refs_list.set_head(obj);
1107     refs_list.inc_length(1);
1108     if (_discovered_list_needs_barrier) {
1109       _bs->write_ref_field((void*)discovered_addr, current_head);
1110     }
1111 
1112     if (TraceReferenceGC) {
1113       gclog_or_tty->print_cr("Enqueued reference (mt) (" INTPTR_FORMAT ": %s)",
1114                              obj, obj->blueprint()->internal_name());
1115     }
1116   } else {
1117     // If retest was non NULL, another thread beat us to it:
1118     // The reference has already been discovered...
1119     if (TraceReferenceGC) {
1120       gclog_or_tty->print_cr("Already enqueued reference (" INTPTR_FORMAT ": %s)",
1121                              obj, obj->blueprint()->internal_name());
1122     }
1123   }
1124 }
1125 
1126 #ifndef PRODUCT
1127 // Non-atomic (i.e. concurrent) discovery might allow us
1128 // to observe j.l.References with NULL referents, being those
1129 // cleared concurrently by mutators during (or after) discovery.


1245     }
1246   } else {
1247     assert(RefDiscoveryPolicy == ReferenceBasedDiscovery &&
1248            _span.contains(obj_addr), "code inconsistency");
1249   }
1250 
1251   // Get the right type of discovered queue head.
1252   DiscoveredList* list = get_discovered_list(rt);
1253   if (list == NULL) {
1254     return false;   // nothing special needs to be done
1255   }
1256 
1257   if (_discovery_is_mt) {
1258     add_to_discovered_list_mt(*list, obj, discovered_addr);
1259   } else {
1260     // If "_discovered_list_needs_barrier", we do write barriers when
1261     // updating the discovered reference list.  Otherwise, we do a raw store
1262     // here: the field will be visited later when processing the discovered
1263     // references.
1264     oop current_head = list->head();



1265     // As in the case further above, since we are over-writing a NULL
1266     // pre-value, we can safely elide the pre-barrier here for the case of G1.
1267     assert(discovered == NULL, "control point invariant");
1268     if (_discovered_list_needs_barrier && !UseG1GC) { // safe to elide for G1
1269       if (UseCompressedOops) {
1270         _bs->write_ref_field_pre((narrowOop*)discovered_addr, current_head);
1271       } else {
1272         _bs->write_ref_field_pre((oop*)discovered_addr, current_head);
1273       }
1274       guarantee(false, "Need to check non-G1 collector");
1275     }
1276     oop_store_raw(discovered_addr, current_head);
1277     if (_discovered_list_needs_barrier) {
1278       _bs->write_ref_field((void*)discovered_addr, current_head);
1279     }
1280     list->set_head(obj);
1281     list->inc_length(1);
1282 
1283     if (TraceReferenceGC) {
1284       gclog_or_tty->print_cr("Enqueued reference (" INTPTR_FORMAT ": %s)",
1285                                 obj, obj->blueprint()->internal_name());
1286     }
1287   }
1288   assert(obj->is_oop(), "Enqueued a bad reference");
1289   verify_referent(obj);
1290   return true;
1291 }
1292 
1293 // Preclean the discovered references by removing those
1294 // whose referents are alive, and by marking from those that
1295 // are not active. These lists can be handled here
1296 // in any order and, indeed, concurrently.
1297 void ReferenceProcessor::preclean_discovered_references(
1298   BoolObjectClosure* is_alive,


1420 
1421 const char* ReferenceProcessor::list_name(int i) {
1422    assert(i >= 0 && i <= _max_num_q * subclasses_of_ref, "Out of bounds index");
1423    int j = i / _max_num_q;
1424    switch (j) {
1425      case 0: return "SoftRef";
1426      case 1: return "WeakRef";
1427      case 2: return "FinalRef";
1428      case 3: return "PhantomRef";
1429    }
1430    ShouldNotReachHere();
1431    return NULL;
1432 }
1433 
1434 #ifndef PRODUCT
1435 void ReferenceProcessor::verify_ok_to_handle_reflists() {
1436   // empty for now
1437 }
1438 #endif
1439 
1440 void ReferenceProcessor::verify() {
1441   guarantee(sentinel_ref() != NULL && sentinel_ref()->is_oop(), "Lost _sentinelRef");
1442 }
1443 
1444 #ifndef PRODUCT
1445 void ReferenceProcessor::clear_discovered_references() {
1446   guarantee(!_discovering_refs, "Discovering refs?");
1447   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
1448     oop obj = _discoveredSoftRefs[i].head();
1449     while (obj != sentinel_ref()) {
1450       oop next = java_lang_ref_Reference::discovered(obj);
1451       java_lang_ref_Reference::set_discovered(obj, (oop) NULL);
1452       obj = next;
1453     }
1454     _discoveredSoftRefs[i].set_head(sentinel_ref());
1455     _discoveredSoftRefs[i].set_length(0);
1456   }
1457 }

1458 #endif // PRODUCT


  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "gc_interface/collectedHeap.hpp"
  29 #include "gc_interface/collectedHeap.inline.hpp"
  30 #include "memory/referencePolicy.hpp"
  31 #include "memory/referenceProcessor.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/java.hpp"
  34 #include "runtime/jniHandles.hpp"
  35 
  36 ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL;
  37 ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy      = NULL;

  38 const int        subclasses_of_ref                = REF_PHANTOM - REF_OTHER;
  39 
  40 // List of discovered references.
  41 class DiscoveredList {
  42 public:
  43   DiscoveredList() : _len(0), _compressed_head(0), _oop_head(NULL) { }
  44   oop head() const     {
  45      return UseCompressedOops ?  oopDesc::decode_heap_oop(_compressed_head) :
  46                                 _oop_head;
  47   }
  48   HeapWord* adr_head() {
  49     return UseCompressedOops ? (HeapWord*)&_compressed_head :
  50                                (HeapWord*)&_oop_head;
  51   }
  52   void   set_head(oop o) {
  53     if (UseCompressedOops) {
  54       // Must compress the head ptr.
  55       _compressed_head = oopDesc::encode_heap_oop(o);
  56     } else {
  57       _oop_head = o;
  58     }
  59   }
  60   bool   empty() const          { return head() == NULL; }
  61   size_t length()               { return _len; }
  62   void   set_length(size_t len) { _len = len;  }
  63   void   inc_length(size_t inc) { _len += inc; assert(_len > 0, "Error"); }
  64   void   dec_length(size_t dec) { _len -= dec; }
  65 private:
  66   // Set value depending on UseCompressedOops. This could be a template class
  67   // but then we have to fix all the instantiations and declarations that use this class.
  68   oop       _oop_head;
  69   narrowOop _compressed_head;
  70   size_t _len;
  71 };
  72 
  73 void referenceProcessor_init() {
  74   ReferenceProcessor::init_statics();
  75 }
  76 
  77 void ReferenceProcessor::init_statics() {






  78   // Initialize the master soft ref clock.
  79   java_lang_ref_SoftReference::set_clock(os::javaTimeMillis());
  80 






  81   _always_clear_soft_ref_policy = new AlwaysClearPolicy();
  82   _default_soft_ref_policy      = new COMPILER2_PRESENT(LRUMaxHeapPolicy())
  83                                       NOT_COMPILER2(LRUCurrentHeapPolicy());
  84   if (_always_clear_soft_ref_policy == NULL || _default_soft_ref_policy == NULL) {
  85     vm_exit_during_initialization("Could not allocate reference policy object");
  86   }
  87   guarantee(RefDiscoveryPolicy == ReferenceBasedDiscovery ||
  88             RefDiscoveryPolicy == ReferentBasedDiscovery,
  89             "Unrecongnized RefDiscoveryPolicy");
  90 }
  91 
  92 ReferenceProcessor::ReferenceProcessor(MemRegion span,
  93                                        bool      mt_processing,
  94                                        int       mt_processing_degree,
  95                                        bool      mt_discovery,
  96                                        int       mt_discovery_degree,
  97                                        bool      atomic_discovery,
  98                                        BoolObjectClosure* is_alive_non_header,
  99                                        bool      discovered_list_needs_barrier)  :
 100   _discovering_refs(false),
 101   _enqueuing_is_done(false),
 102   _is_alive_non_header(is_alive_non_header),
 103   _discovered_list_needs_barrier(discovered_list_needs_barrier),
 104   _bs(NULL),
 105   _processing_is_mt(mt_processing),
 106   _next_id(0)
 107 {
 108   _span = span;
 109   _discovery_is_atomic = atomic_discovery;
 110   _discovery_is_mt     = mt_discovery;
 111   _num_q               = MAX2(1, mt_processing_degree);
 112   _max_num_q           = MAX2(_num_q, mt_discovery_degree);
 113   _discoveredSoftRefs  = NEW_C_HEAP_ARRAY(DiscoveredList, _max_num_q * subclasses_of_ref);
 114   if (_discoveredSoftRefs == NULL) {
 115     vm_exit_during_initialization("Could not allocated RefProc Array");
 116   }
 117   _discoveredWeakRefs    = &_discoveredSoftRefs[_max_num_q];
 118   _discoveredFinalRefs   = &_discoveredWeakRefs[_max_num_q];
 119   _discoveredPhantomRefs = &_discoveredFinalRefs[_max_num_q];
 120   // Initialized all entries to NULL

 121   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 122     _discoveredSoftRefs[i].set_head(NULL);
 123     _discoveredSoftRefs[i].set_length(0);
 124   }
 125   // If we do barreirs, cache a copy of the barrier set.
 126   if (discovered_list_needs_barrier) {
 127     _bs = Universe::heap()->barrier_set();
 128   }
 129   setup_policy(false /* default soft ref policy */);
 130 }
 131 
 132 #ifndef PRODUCT
 133 void ReferenceProcessor::verify_no_references_recorded() {
 134   guarantee(!_discovering_refs, "Discovering refs?");
 135   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 136     guarantee(_discoveredSoftRefs[i].empty(),
 137               "Found non-empty discovered list");
 138   }
 139 }
 140 #endif
 141 
 142 void ReferenceProcessor::weak_oops_do(OopClosure* f) {
 143   // Should this instead be
 144   // for (int i = 0; i < subclasses_of_ref; i++_ {
 145   //   for (int j = 0; j < _num_q; j++) {
 146   //     int index = i * _max_num_q + j;
 147   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 148     if (UseCompressedOops) {
 149       f->do_oop((narrowOop*)_discoveredSoftRefs[i].adr_head());
 150     } else {
 151       f->do_oop((oop*)_discoveredSoftRefs[i].adr_head());
 152     }
 153   }
 154 }
 155 




 156 void ReferenceProcessor::update_soft_ref_master_clock() {
 157   // Update (advance) the soft ref master clock field. This must be done
 158   // after processing the soft ref list.
 159   jlong now = os::javaTimeMillis();
 160   jlong clock = java_lang_ref_SoftReference::clock();
 161   NOT_PRODUCT(
 162   if (now < clock) {
 163     warning("time warp: %d to %d", clock, now);
 164   }
 165   )
 166   // In product mode, protect ourselves from system time being adjusted
 167   // externally and going backward; see note in the implementation of
 168   // GenCollectedHeap::time_since_last_gc() for the right way to fix
 169   // this uniformly throughout the VM; see bug-id 4741166. XXX
 170   if (now > clock) {
 171     java_lang_ref_SoftReference::set_clock(now);
 172   }
 173   // Else leave clock stalled at its old value until time progresses
 174   // past clock value.
 175 }


 248     void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
 249     int count() { return _count; }
 250   };
 251   CountHandleClosure global_handle_count;
 252   AlwaysAliveClosure always_alive;
 253   JNIHandles::weak_oops_do(&always_alive, &global_handle_count);
 254   return global_handle_count.count();
 255 }
 256 #endif
 257 
 258 void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive,
 259                                           OopClosure*        keep_alive,
 260                                           VoidClosure*       complete_gc) {
 261 #ifndef PRODUCT
 262   if (PrintGCDetails && PrintReferenceGC) {
 263     unsigned int count = count_jni_refs();
 264     gclog_or_tty->print(", %u refs", count);
 265   }
 266 #endif
 267   JNIHandles::weak_oops_do(is_alive, keep_alive);


 268   complete_gc->do_void();
 269 }
 270 
 271 
 272 template <class T>
 273 bool enqueue_discovered_ref_helper(ReferenceProcessor* ref,
 274                                    AbstractRefProcTaskExecutor* task_executor) {
 275 
 276   // Remember old value of pending references list
 277   T* pending_list_addr = (T*)java_lang_ref_Reference::pending_list_addr();
 278   T old_pending_list_value = *pending_list_addr;
 279 
 280   // Enqueue references that are not made active again, and
 281   // clear the decks for the next collection (cycle).
 282   ref->enqueue_discovered_reflists((HeapWord*)pending_list_addr, task_executor);
 283   // Do the oop-check on pending_list_addr missed in
 284   // enqueue_discovered_reflist. We should probably
 285   // do a raw oop_check so that future such idempotent
 286   // oop_stores relying on the oop-check side-effect
 287   // may be elided automatically and safely without


 297 
 298 bool ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor) {
 299   NOT_PRODUCT(verify_ok_to_handle_reflists());
 300   if (UseCompressedOops) {
 301     return enqueue_discovered_ref_helper<narrowOop>(this, task_executor);
 302   } else {
 303     return enqueue_discovered_ref_helper<oop>(this, task_executor);
 304   }
 305 }
 306 
 307 void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list,
 308                                                     HeapWord* pending_list_addr) {
 309   // Given a list of refs linked through the "discovered" field
 310   // (java.lang.ref.Reference.discovered) chain them through the
 311   // "next" field (java.lang.ref.Reference.next) and prepend
 312   // to the pending list.
 313   if (TraceReferenceGC && PrintGCDetails) {
 314     gclog_or_tty->print_cr("ReferenceProcessor::enqueue_discovered_reflist list "
 315                            INTPTR_FORMAT, (address)refs_list.head());
 316   }
 317 
 318   oop obj = NULL;
 319   oop next = refs_list.head();
 320   // Walk down the list, copying the discovered field into
 321   // the next field and clearing it.
 322   while (obj != next) {
 323     obj = next;

 324     assert(obj->is_instanceRef(), "should be reference object");
 325     next = java_lang_ref_Reference::discovered(obj);
 326     if (TraceReferenceGC && PrintGCDetails) {
 327       gclog_or_tty->print_cr("        obj " INTPTR_FORMAT "/next " INTPTR_FORMAT,
 328                              obj, next);
 329     }
 330     assert(java_lang_ref_Reference::next(obj) == NULL,
 331            "The reference should not be enqueued");
 332     if (next == obj) {  // obj is last
 333       // Swap refs_list into pendling_list_addr and
 334       // set obj's next to what we read from pending_list_addr.
 335       oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr);
 336       // Need oop_check on pending_list_addr above;
 337       // see special oop-check code at the end of
 338       // enqueue_discovered_reflists() further below.
 339       if (old == NULL) {
 340         // obj should be made to point to itself, since
 341         // pending list was empty.
 342         java_lang_ref_Reference::set_next(obj, obj);
 343       } else {
 344         java_lang_ref_Reference::set_next(obj, old);
 345       }
 346     } else {
 347       java_lang_ref_Reference::set_next(obj, next);
 348     }
 349     java_lang_ref_Reference::set_discovered(obj, (oop) NULL);

 350   }
 351 }
 352 
 353 // Parallel enqueue task
 354 class RefProcEnqueueTask: public AbstractRefProcTaskExecutor::EnqueueTask {
 355 public:
 356   RefProcEnqueueTask(ReferenceProcessor& ref_processor,
 357                      DiscoveredList      discovered_refs[],
 358                      HeapWord*           pending_list_addr,

 359                      int                 n_queues)
 360     : EnqueueTask(ref_processor, discovered_refs,
 361                   pending_list_addr, n_queues)
 362   { }
 363 
 364   virtual void work(unsigned int work_id) {
 365     assert(work_id < (unsigned int)_ref_processor.max_num_q(), "Index out-of-bounds");
 366     // Simplest first cut: static partitioning.
 367     int index = work_id;
 368     // The increment on "index" must correspond to the maximum number of queues
 369     // (n_queues) with which that ReferenceProcessor was created.  That
 370     // is because of the "clever" way the discovered references lists were
 371     // allocated and are indexed into.
 372     assert(_n_queues == (int) _ref_processor.max_num_q(), "Different number not expected");
 373     for (int j = 0;
 374          j < subclasses_of_ref;
 375          j++, index += _n_queues) {
 376       _ref_processor.enqueue_discovered_reflist(
 377         _refs_lists[index], _pending_list_addr);
 378       _refs_lists[index].set_head(NULL);
 379       _refs_lists[index].set_length(0);
 380     }
 381   }
 382 };
 383 
 384 // Enqueue references that are not made active again
 385 void ReferenceProcessor::enqueue_discovered_reflists(HeapWord* pending_list_addr,
 386   AbstractRefProcTaskExecutor* task_executor) {
 387   if (_processing_is_mt && task_executor != NULL) {
 388     // Parallel code
 389     RefProcEnqueueTask tsk(*this, _discoveredSoftRefs,
 390                            pending_list_addr, _max_num_q);
 391     task_executor->execute(tsk);
 392   } else {
 393     // Serial code: call the parent class's implementation
 394     for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 395       enqueue_discovered_reflist(_discoveredSoftRefs[i], pending_list_addr);
 396       _discoveredSoftRefs[i].set_head(NULL);
 397       _discoveredSoftRefs[i].set_length(0);
 398     }
 399   }
 400 }
 401 
 402 // Iterator for the list of discovered references.
 403 class DiscoveredListIterator {
 404 public:
 405   inline DiscoveredListIterator(DiscoveredList&    refs_list,
 406                                 OopClosure*        keep_alive,
 407                                 BoolObjectClosure* is_alive);
 408 
 409   // End Of List.
 410   inline bool has_next() const { return _ref != NULL; }
 411 
 412   // Get oop to the Reference object.
 413   inline oop obj() const { return _ref; }
 414 
 415   // Get oop to the referent object.
 416   inline oop referent() const { return _referent; }
 417 
 418   // Returns true if referent is alive.
 419   inline bool is_referent_alive() const;
 420 
 421   // Loads data for the current reference.
 422   // The "allow_null_referent" argument tells us to allow for the possibility
 423   // of a NULL referent in the discovered Reference object. This typically
 424   // happens in the case of concurrent collectors that may have done the
 425   // discovery concurrently, or interleaved, with mutator execution.
 426   inline void load_ptrs(DEBUG_ONLY(bool allow_null_referent));
 427 
 428   // Move to the next discovered reference.
 429   inline void next();
 430 
 431   // Remove the current reference from the list
 432   inline void remove();
 433 
 434   // Make the Reference object active again.
 435   inline void make_active() { java_lang_ref_Reference::set_next(_ref, NULL); }
 436 
 437   // Make the referent alive.
 438   inline void make_referent_alive() {
 439     if (UseCompressedOops) {
 440       _keep_alive->do_oop((narrowOop*)_referent_addr);
 441     } else {
 442       _keep_alive->do_oop((oop*)_referent_addr);
 443     }
 444   }
 445 
 446   // Update the discovered field.
 447   inline void update_discovered() {
 448     // First _prev_next ref actually points into DiscoveredList (gross).
 449     if (UseCompressedOops) {
 450       if (!oopDesc::is_null(*(narrowOop*)_prev_next)) {
 451         _keep_alive->do_oop((narrowOop*)_prev_next);
 452       }
 453     } else {
 454       if (!oopDesc::is_null(*(oop*)_prev_next)) {
 455         _keep_alive->do_oop((oop*)_prev_next);
 456       }
 457     }
 458   }
 459 
 460   // NULL out referent pointer.
 461   inline void clear_referent() { oop_store_raw(_referent_addr, NULL); }
 462 
 463   // Statistics
 464   NOT_PRODUCT(
 465   inline size_t processed() const { return _processed; }
 466   inline size_t removed() const   { return _removed; }
 467   )
 468 
 469   inline void move_to_next();
 470 
 471 private:
 472   DiscoveredList&    _refs_list;
 473   HeapWord*          _prev_next;
 474   oop                _prev;
 475   oop                _ref;
 476   HeapWord*          _discovered_addr;
 477   oop                _next;
 478   HeapWord*          _referent_addr;
 479   oop                _referent;
 480   OopClosure*        _keep_alive;
 481   BoolObjectClosure* _is_alive;
 482   DEBUG_ONLY(
 483   oop                _first_seen; // cyclic linked list check
 484   )
 485   NOT_PRODUCT(
 486   size_t             _processed;
 487   size_t             _removed;
 488   )
 489 };
 490 
 491 inline DiscoveredListIterator::DiscoveredListIterator(DiscoveredList&    refs_list,
 492                                                       OopClosure*        keep_alive,
 493                                                       BoolObjectClosure* is_alive)
 494   : _refs_list(refs_list),
 495     _prev_next(refs_list.adr_head()),
 496     _prev(NULL),
 497     _ref(refs_list.head()),
 498 #ifdef ASSERT
 499     _first_seen(refs_list.head()),
 500 #endif
 501 #ifndef PRODUCT
 502     _processed(0),
 503     _removed(0),
 504 #endif
 505     _next(NULL),
 506     _keep_alive(keep_alive),
 507     _is_alive(is_alive)
 508 { }
 509 
 510 inline bool DiscoveredListIterator::is_referent_alive() const {
 511   return _is_alive->do_object_b(_referent);
 512 }
 513 
 514 inline void DiscoveredListIterator::load_ptrs(DEBUG_ONLY(bool allow_null_referent)) {
 515   _discovered_addr = java_lang_ref_Reference::discovered_addr(_ref);
 516   oop discovered = java_lang_ref_Reference::discovered(_ref);
 517   assert(_discovered_addr && discovered->is_oop_or_null(),
 518          "discovered field is bad");
 519   _next = discovered;
 520   _referent_addr = java_lang_ref_Reference::referent_addr(_ref);
 521   _referent = java_lang_ref_Reference::referent(_ref);
 522   assert(Universe::heap()->is_in_reserved_or_null(_referent),
 523          "Wrong oop found in java.lang.Reference object");
 524   assert(allow_null_referent ?
 525              _referent->is_oop_or_null()
 526            : _referent->is_oop(),
 527          "bad referent");
 528 }
 529 
 530 inline void DiscoveredListIterator::next() {
 531   _prev_next = _discovered_addr;
 532   _prev = _ref;
 533   move_to_next();
 534 }
 535 
 536 inline void DiscoveredListIterator::remove() {
 537   assert(_ref->is_oop(), "Dropping a bad reference");
 538   oop_store_raw(_discovered_addr, NULL);
 539 
 540   // First _prev_next ref actually points into DiscoveredList (gross).
 541   oop new_next;
 542   if (_next == _ref) {
 543     // At the end of the list, we should make _prev point to itself.
 544     // If _ref is the first ref, then _prev_next will be in the DiscoveredList,
 545     // and _prev will be NULL.
 546     new_next = _prev;
 547   } else {
 548     new_next = _next;
 549   }
 550 
 551   if (UseCompressedOops) {
 552     // Remove Reference object from list.
 553     oopDesc::encode_store_heap_oop((narrowOop*)_prev_next, new_next);
 554   } else {
 555     // Remove Reference object from list.
 556     oopDesc::store_heap_oop((oop*)_prev_next, new_next);
 557   }
 558   NOT_PRODUCT(_removed++);
 559   _refs_list.dec_length(1);
 560 }
 561 
 562 inline void DiscoveredListIterator::move_to_next() {
 563   if (_ref == _next) {
 564     // End of the list.
 565     _ref = NULL;
 566   } else {
 567     _ref = _next;
 568   }
 569   assert(_ref != _first_seen, "cyclic ref_list found");
 570   NOT_PRODUCT(_processed++);
 571 }
 572 
 573 // NOTE: process_phase*() are largely similar, and at a high level
 574 // merely iterate over the extant list applying a predicate to
 575 // each of its elements and possibly removing that element from the
 576 // list and applying some further closures to that element.
 577 // We should consider the possibility of replacing these
 578 // process_phase*() methods by abstracting them into
 579 // a single general iterator invocation that receives appropriate
 580 // closures that accomplish this work.
 581 
 582 // (SoftReferences only) Traverse the list and remove any SoftReferences whose
 583 // referents are not alive, but that should be kept alive for policy reasons.
 584 // Keep alive the transitive closure of all such referents.
 585 void
 586 ReferenceProcessor::process_phase1(DiscoveredList&    refs_list,
 587                                    ReferencePolicy*   policy,
 588                                    BoolObjectClosure* is_alive,


 710   ResourceMark rm;
 711   DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
 712   while (iter.has_next()) {
 713     iter.update_discovered();
 714     iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */));
 715     if (clear_referent) {
 716       // NULL out referent pointer
 717       iter.clear_referent();
 718     } else {
 719       // keep the referent around
 720       iter.make_referent_alive();
 721     }
 722     if (TraceReferenceGC) {
 723       gclog_or_tty->print_cr("Adding %sreference (" INTPTR_FORMAT ": %s) as pending",
 724                              clear_referent ? "cleared " : "",
 725                              iter.obj(), iter.obj()->blueprint()->internal_name());
 726     }
 727     assert(iter.obj()->is_oop(UseConcMarkSweepGC), "Adding a bad reference");
 728     iter.next();
 729   }
 730   // Remember to update the next pointer of the last ref.
 731   iter.update_discovered();
 732   // Close the reachable set
 733   complete_gc->do_void();
 734 }
 735 
 736 void
 737 ReferenceProcessor::clear_discovered_references(DiscoveredList& refs_list) {
 738   oop obj = NULL;
 739   oop next = refs_list.head();
 740   while (next != obj) {
 741     obj = next;
 742     next = java_lang_ref_Reference::discovered(obj);
 743     java_lang_ref_Reference::set_discovered_raw(obj, NULL);

 744   }
 745   refs_list.set_head(NULL);
 746   refs_list.set_length(0);
 747 }
 748 
 749 void
 750 ReferenceProcessor::abandon_partial_discovered_list(DiscoveredList& refs_list) {
 751   clear_discovered_references(refs_list);
 752 }
 753 
 754 void ReferenceProcessor::abandon_partial_discovery() {
 755   // loop over the lists
 756   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
 757     if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) {
 758       gclog_or_tty->print_cr("\nAbandoning %s discovered list",
 759                              list_name(i));
 760     }
 761     abandon_partial_discovered_list(_discoveredSoftRefs[i]);
 762   }
 763 }
 764 
 765 class RefProcPhase1Task: public AbstractRefProcTaskExecutor::ProcessTask {
 766 public:
 767   RefProcPhase1Task(ReferenceProcessor& ref_processor,
 768                     DiscoveredList      refs_lists[],
 769                     ReferencePolicy*    policy,
 770                     bool                marks_oops_alive)
 771     : ProcessTask(ref_processor, refs_lists, marks_oops_alive),
 772       _policy(policy)
 773   { }


 850   int to_idx = 0;
 851   for (int from_idx = 0; from_idx < _max_num_q; from_idx++) {
 852     bool move_all = false;
 853     if (from_idx >= _num_q) {
 854       move_all = ref_lists[from_idx].length() > 0;
 855     }
 856     while ((ref_lists[from_idx].length() > avg_refs) ||
 857            move_all) {
 858       assert(to_idx < _num_q, "Sanity Check!");
 859       if (ref_lists[to_idx].length() < avg_refs) {
 860         // move superfluous refs
 861         size_t refs_to_move;
 862         // Move all the Ref's if the from queue will not be processed.
 863         if (move_all) {
 864           refs_to_move = MIN2(ref_lists[from_idx].length(),
 865                               avg_refs - ref_lists[to_idx].length());
 866         } else {
 867           refs_to_move = MIN2(ref_lists[from_idx].length() - avg_refs,
 868                               avg_refs - ref_lists[to_idx].length());
 869         }
 870 
 871         assert(refs_to_move > 0, "otherwise the code below will fail");
 872 
 873         oop move_head = ref_lists[from_idx].head();
 874         oop move_tail = move_head;
 875         oop new_head  = move_head;
 876         // find an element to split the list on
 877         for (size_t j = 0; j < refs_to_move; ++j) {
 878           move_tail = new_head;
 879           new_head = java_lang_ref_Reference::discovered(new_head);
 880         }
 881 
 882         // Add the chain to the to list.
 883         if (ref_lists[to_idx].head() == NULL) {
 884           // to list is empty. Make a loop at the end.
 885           java_lang_ref_Reference::set_discovered(move_tail, move_tail);
 886         } else {
 887           java_lang_ref_Reference::set_discovered(move_tail, ref_lists[to_idx].head());
 888         }
 889         ref_lists[to_idx].set_head(move_head);
 890         ref_lists[to_idx].inc_length(refs_to_move);
 891 
 892         // Remove the chain from the from list.
 893         if (move_tail == new_head) {
 894           // We found the end of the from list.
 895           ref_lists[from_idx].set_head(NULL);
 896         } else {
 897           ref_lists[from_idx].set_head(new_head);
 898         }
 899         ref_lists[from_idx].dec_length(refs_to_move);
 900         if (ref_lists[from_idx].length() == 0) {
 901           break;
 902         }
 903       } else {
 904         to_idx = (to_idx + 1) % _num_q;
 905       }
 906     }
 907   }
 908 #ifdef ASSERT
 909   size_t balanced_total_refs = 0;
 910   for (int i = 0; i < _max_num_q; ++i) {
 911     balanced_total_refs += ref_lists[i].length();
 912     if (TraceReferenceGC && PrintGCDetails) {
 913       gclog_or_tty->print("%d ", ref_lists[i].length());
 914     }
 915   }
 916   if (TraceReferenceGC && PrintGCDetails) {
 917     gclog_or_tty->print_cr(" = %d", balanced_total_refs);
 918     gclog_or_tty->flush();


1090       break;
1091     case REF_NONE:
1092       // we should not reach here if we are an instanceRefKlass
1093     default:
1094       ShouldNotReachHere();
1095   }
1096   if (TraceReferenceGC && PrintGCDetails) {
1097     gclog_or_tty->print_cr("Thread %d gets list " INTPTR_FORMAT, id, list);
1098   }
1099   return list;
1100 }
1101 
1102 inline void
1103 ReferenceProcessor::add_to_discovered_list_mt(DiscoveredList& refs_list,
1104                                               oop             obj,
1105                                               HeapWord*       discovered_addr) {
1106   assert(_discovery_is_mt, "!_discovery_is_mt should have been handled by caller");
1107   // First we must make sure this object is only enqueued once. CAS in a non null
1108   // discovered_addr.
1109   oop current_head = refs_list.head();
1110   // The last ref must have its discovered field pointing to itself.
1111   oop next_discovered = (current_head != NULL) ? current_head : obj;
1112 
1113   // Note: In the case of G1, this specific pre-barrier is strictly
1114   // not necessary because the only case we are interested in
1115   // here is when *discovered_addr is NULL (see the CAS further below),
1116   // so this will expand to nothing. As a result, we have manually
1117   // elided this out for G1, but left in the test for some future
1118   // collector that might have need for a pre-barrier here.
1119   if (_discovered_list_needs_barrier && !UseG1GC) {
1120     if (UseCompressedOops) {
1121       _bs->write_ref_field_pre((narrowOop*)discovered_addr, next_discovered);
1122     } else {
1123       _bs->write_ref_field_pre((oop*)discovered_addr, next_discovered);
1124     }
1125     guarantee(false, "Need to check non-G1 collector");
1126   }
1127   oop retest = oopDesc::atomic_compare_exchange_oop(next_discovered, discovered_addr,
1128                                                     NULL);
1129   if (retest == NULL) {
1130     // This thread just won the right to enqueue the object.
1131     // We have separate lists for enqueueing so no synchronization
1132     // is necessary.
1133     refs_list.set_head(obj);
1134     refs_list.inc_length(1);
1135     if (_discovered_list_needs_barrier) {
1136       _bs->write_ref_field((void*)discovered_addr, next_discovered);
1137     }
1138 
1139     if (TraceReferenceGC) {
1140       gclog_or_tty->print_cr("Enqueued reference (mt) (" INTPTR_FORMAT ": %s)",
1141                              obj, obj->blueprint()->internal_name());
1142     }
1143   } else {
1144     // If retest was non NULL, another thread beat us to it:
1145     // The reference has already been discovered...
1146     if (TraceReferenceGC) {
1147       gclog_or_tty->print_cr("Already enqueued reference (" INTPTR_FORMAT ": %s)",
1148                              obj, obj->blueprint()->internal_name());
1149     }
1150   }
1151 }
1152 
1153 #ifndef PRODUCT
1154 // Non-atomic (i.e. concurrent) discovery might allow us
1155 // to observe j.l.References with NULL referents, being those
1156 // cleared concurrently by mutators during (or after) discovery.


1272     }
1273   } else {
1274     assert(RefDiscoveryPolicy == ReferenceBasedDiscovery &&
1275            _span.contains(obj_addr), "code inconsistency");
1276   }
1277 
1278   // Get the right type of discovered queue head.
1279   DiscoveredList* list = get_discovered_list(rt);
1280   if (list == NULL) {
1281     return false;   // nothing special needs to be done
1282   }
1283 
1284   if (_discovery_is_mt) {
1285     add_to_discovered_list_mt(*list, obj, discovered_addr);
1286   } else {
1287     // If "_discovered_list_needs_barrier", we do write barriers when
1288     // updating the discovered reference list.  Otherwise, we do a raw store
1289     // here: the field will be visited later when processing the discovered
1290     // references.
1291     oop current_head = list->head();
1292     // The last ref must have its discovered field pointing to itself.
1293     oop next_discovered = (current_head != NULL) ? current_head : obj;
1294 
1295     // As in the case further above, since we are over-writing a NULL
1296     // pre-value, we can safely elide the pre-barrier here for the case of G1.
1297     assert(discovered == NULL, "control point invariant");
1298     if (_discovered_list_needs_barrier && !UseG1GC) { // safe to elide for G1
1299       if (UseCompressedOops) {
1300         _bs->write_ref_field_pre((narrowOop*)discovered_addr, next_discovered);
1301       } else {
1302         _bs->write_ref_field_pre((oop*)discovered_addr, next_discovered);
1303       }
1304       guarantee(false, "Need to check non-G1 collector");
1305     }
1306     oop_store_raw(discovered_addr, next_discovered);
1307     if (_discovered_list_needs_barrier) {
1308       _bs->write_ref_field((void*)discovered_addr, next_discovered);
1309     }
1310     list->set_head(obj);
1311     list->inc_length(1);
1312 
1313     if (TraceReferenceGC) {
1314       gclog_or_tty->print_cr("Enqueued reference (" INTPTR_FORMAT ": %s)",
1315                                 obj, obj->blueprint()->internal_name());
1316     }
1317   }
1318   assert(obj->is_oop(), "Enqueued a bad reference");
1319   verify_referent(obj);
1320   return true;
1321 }
1322 
1323 // Preclean the discovered references by removing those
1324 // whose referents are alive, and by marking from those that
1325 // are not active. These lists can be handled here
1326 // in any order and, indeed, concurrently.
1327 void ReferenceProcessor::preclean_discovered_references(
1328   BoolObjectClosure* is_alive,


1450 
1451 const char* ReferenceProcessor::list_name(int i) {
1452    assert(i >= 0 && i <= _max_num_q * subclasses_of_ref, "Out of bounds index");
1453    int j = i / _max_num_q;
1454    switch (j) {
1455      case 0: return "SoftRef";
1456      case 1: return "WeakRef";
1457      case 2: return "FinalRef";
1458      case 3: return "PhantomRef";
1459    }
1460    ShouldNotReachHere();
1461    return NULL;
1462 }
1463 
1464 #ifndef PRODUCT
1465 void ReferenceProcessor::verify_ok_to_handle_reflists() {
1466   // empty for now
1467 }
1468 #endif
1469 




1470 #ifndef PRODUCT
1471 void ReferenceProcessor::clear_discovered_references() {
1472   guarantee(!_discovering_refs, "Discovering refs?");
1473   for (int i = 0; i < _max_num_q * subclasses_of_ref; i++) {
1474     clear_discovered_references(_discoveredSoftRefs[i]);







1475   }
1476 }
1477 
1478 #endif // PRODUCT