1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)referenceProcessor.hpp       1.43 07/05/05 17:05:54 JVM"
   3 #endif
   4 /*
   5  * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // ReferenceProcessor class encapsulates the per-"collector" processing
  29 // of "weak" references for GC. The interface is useful for supporting
  30 // a generational abstraction, in particular when there are multiple
  31 // generations that are being independently collected -- possibly
  32 // concurrently and/or incrementally.  Note, however, that the
  33 // ReferenceProcessor class abstracts away from a generational setting
  34 // by using only a heap interval (called "span" below), thus allowing
  35 // its use in a straightforward manner in a general, non-generational
  36 // setting.
  37 //
  38 // The basic idea is that each ReferenceProcessor object concerns
  39 // itself with ("weak") reference processing in a specific "span"
  40 // of the heap of interest to a specific collector. Currently,
  41 // the span is a convex interval of the heap, but, efficiency
  42 // apart, there seems to be no reason it couldn't be extended
  43 // (with appropriate modifications) to any "non-convex interval".
  44 
  45 // forward references
  46 class ReferencePolicy;
  47 class AbstractRefProcTaskExecutor;
  48 class DiscoveredList;
  49 
  50 class ReferenceProcessor : public CHeapObj {
  51  friend class DiscoveredList;
  52  friend class DiscoveredListIterator;
  53  protected:
  54   // End of list marker
  55   static oop  _sentinelRef;
  56   MemRegion   _span; // (right-open) interval of heap
  57                      // subject to wkref discovery
  58   bool        _discovering_refs;      // true when discovery enabled
  59   bool        _discovery_is_atomic;   // if discovery is atomic wrt
  60                                       // other collectors in configuration
  61   bool        _discovery_is_mt;       // true if reference discovery is MT.
  62   bool        _enqueuing_is_done;     // true if all weak references enqueued
  63   bool        _processing_is_mt;      // true during phases when
  64                                       // reference processing is MT.
  65   int         _next_id;               // round-robin counter in
  66                                       // support of work distribution
  67 
  68   // For collectors that do not keep GC marking information
  69   // in the object header, this field holds a closure that
  70   // helps the reference processor determine the reachability
  71   // of an oop (the field is currently initialized to NULL for
  72   // all collectors but the CMS collector).
  73   BoolObjectClosure* _is_alive_non_header;
  74 
  75   // The discovered ref lists themselves
  76   int             _num_q;       // the MT'ness degree of the queues below
  77   DiscoveredList* _discoveredSoftRefs; // pointer to array of oops
  78   DiscoveredList* _discoveredWeakRefs;
  79   DiscoveredList* _discoveredFinalRefs;
  80   DiscoveredList* _discoveredPhantomRefs;
  81 
  82  public:
  83   int  num_q()                           { return _num_q; }
  84   DiscoveredList* discovered_soft_refs() { return _discoveredSoftRefs; }
  85   static oop* sentinel_ref()             { return &_sentinelRef; }
  86 
  87  public:
  88   // Process references with a certain reachability level.
  89   void process_discovered_reflist(DiscoveredList               refs_lists[],
  90                                   ReferencePolicy*             policy,
  91                                   bool                         clear_referent,
  92                                   BoolObjectClosure*           is_alive,
  93                                   OopClosure*                  keep_alive,
  94                                   VoidClosure*                 complete_gc,
  95                                   AbstractRefProcTaskExecutor* task_executor);
  96                                           
  97   void process_phaseJNI(BoolObjectClosure* is_alive,
  98                         OopClosure*        keep_alive,
  99                         VoidClosure*       complete_gc);
 100 
 101   // Work methods used by the method process_discovered_reflist
 102   // Phase1: keep alive all those referents that are otherwise
 103   // dead but which must be kept alive by policy (and their closure).
 104   void process_phase1(DiscoveredList&     refs_list_addr,
 105                       ReferencePolicy*    policy,
 106                       BoolObjectClosure*  is_alive,
 107                       OopClosure*         keep_alive,
 108                       VoidClosure*        complete_gc);
 109   // Phase2: remove all those references whose referents are
 110   // reachable.
 111   inline void process_phase2(DiscoveredList&    refs_list_addr,
 112                              BoolObjectClosure* is_alive,
 113                              OopClosure*        keep_alive, 
 114                              VoidClosure*       complete_gc) {
 115     if (discovery_is_atomic()) {
 116       // complete_gc is ignored in this case for this phase
 117       pp2_work(refs_list_addr, is_alive, keep_alive);
 118     } else {
 119       assert(complete_gc != NULL, "Error");
 120       pp2_work_concurrent_discovery(refs_list_addr, is_alive,
 121                                     keep_alive, complete_gc);
 122     }
 123   }
 124   // Work methods in support of process_phase2
 125   void pp2_work(DiscoveredList&    refs_list_addr,
 126                 BoolObjectClosure* is_alive,
 127                 OopClosure*        keep_alive);
 128   void pp2_work_concurrent_discovery(
 129                 DiscoveredList&    refs_list_addr,
 130                 BoolObjectClosure* is_alive,
 131                 OopClosure*        keep_alive,
 132                 VoidClosure*       complete_gc);
 133   // Phase3: process the referents by either clearing them
 134   // or keeping them alive (and their closure)
 135   void process_phase3(DiscoveredList&    refs_list_addr,
 136                       bool               clear_referent,
 137                       BoolObjectClosure* is_alive,
 138                       OopClosure*        keep_alive,
 139                       VoidClosure*       complete_gc);
 140 
 141   // Enqueue references with a certain reachability level
 142   void enqueue_discovered_reflist(DiscoveredList& refs_list, oop* pending_list_addr);
 143                                   
 144   // "Preclean" all the discovered reference lists
 145   // by removing references with strongly reachable referents.
 146   // The first argument is a predicate on an oop that indicates
 147   // its (strong) reachability and the second is a closure that
 148   // may be used to incrementalize or abort the precleaning process.
 149   // The caller is responsible for taking care of potential
 150   // interference with concurrent operations on these lists
 151   // (or predicates involved) by other threads. Currently
 152   // only used by the CMS collector.
 153   void preclean_discovered_references(BoolObjectClosure* is_alive,
 154                                       OopClosure*        keep_alive,
 155                                       VoidClosure*       complete_gc,
 156                                       YieldClosure*      yield);
 157                                       
 158   // Delete entries in the discovered lists that have
 159   // either a null referent or are not active. Such
 160   // Reference objects can result from the clearing
 161   // or enqueueing of Reference objects concurrent
 162   // with their discovery by a (concurrent) collector.
 163   // For a definition of "active" see java.lang.ref.Reference;
 164   // Refs are born active, become inactive when enqueued,
 165   // and never become active again. The state of being
 166   // active is encoded as follows: A Ref is active 
 167   // if and only if its "next" field is NULL.
 168   void clean_up_discovered_references();
 169   void clean_up_discovered_reflist(DiscoveredList& refs_list);
 170 
 171   // Returns the name of the discovered reference list 
 172   // occupying the i / _num_q slot.
 173   const char* list_name(int i);
 174 
 175  protected:
 176   // "Preclean" the given discovered reference list
 177   // by removing references with strongly reachable referents.
 178   // Currently used in support of CMS only.
 179   void preclean_discovered_reflist(DiscoveredList&    refs_list,
 180                                    BoolObjectClosure* is_alive,
 181                                    OopClosure*        keep_alive,
 182                                    VoidClosure*       complete_gc,
 183                                    YieldClosure*      yield);
 184                                    
 185   void enqueue_discovered_reflists(oop* pending_list_addr, AbstractRefProcTaskExecutor* task_executor);
 186   int next_id() {
 187     int id = _next_id;
 188     if (++_next_id == _num_q) {
 189       _next_id = 0;
 190     }
 191     return id;
 192   }
 193   DiscoveredList* get_discovered_list(ReferenceType rt);
 194   inline void add_to_discovered_list_mt(DiscoveredList& refs_list, oop obj,
 195                                         oop* discovered_addr);
 196   void verify_ok_to_handle_reflists() PRODUCT_RETURN;
 197 
 198   void abandon_partial_discovered_list(DiscoveredList& refs_list);
 199   void abandon_partial_discovered_list_arr(DiscoveredList refs_lists[]);
 200 
 201   // Calculate the number of jni handles.
 202   unsigned int count_jni_refs();
 203   
 204   // Balances reference queues.
 205   void balance_queues(DiscoveredList ref_lists[]);
 206   
 207   // Update (advance) the soft ref master clock field.
 208   void update_soft_ref_master_clock();
 209   
 210  public:
 211   // constructor
 212   ReferenceProcessor():
 213     _span((HeapWord*)NULL, (HeapWord*)NULL),
 214     _discoveredSoftRefs(NULL),  _discoveredWeakRefs(NULL),
 215     _discoveredFinalRefs(NULL), _discoveredPhantomRefs(NULL),
 216     _discovering_refs(false),
 217     _discovery_is_atomic(true),
 218     _enqueuing_is_done(false),
 219     _discovery_is_mt(false),
 220     _is_alive_non_header(NULL),
 221     _num_q(0),
 222     _processing_is_mt(false),
 223     _next_id(0)
 224   {}
 225 
 226   ReferenceProcessor(MemRegion span, bool atomic_discovery,
 227                      bool mt_discovery, int mt_degree = 1, 
 228                      bool mt_processing = false);
 229    
 230   // Allocates and initializes a reference processor.
 231   static ReferenceProcessor* create_ref_processor(
 232     MemRegion          span, 
 233     bool               atomic_discovery,
 234     bool               mt_discovery,
 235     BoolObjectClosure* is_alive_non_header = NULL,
 236     int                parallel_gc_threads = 1, 
 237     bool               mt_processing = false);
 238 
 239   // RefDiscoveryPolicy values
 240   enum {
 241     ReferenceBasedDiscovery = 0,
 242     ReferentBasedDiscovery  = 1
 243   };
 244 
 245   static void init_statics();
 246 
 247  public:
 248   // get and set "is_alive_non_header" field
 249   BoolObjectClosure* is_alive_non_header() {
 250     return _is_alive_non_header;
 251   }
 252   void set_is_alive_non_header(BoolObjectClosure* is_alive_non_header) {
 253     _is_alive_non_header = is_alive_non_header;
 254   }
 255 
 256   // get and set span
 257   MemRegion span()                   { return _span; }
 258   void      set_span(MemRegion span) { _span = span; }
 259 
 260   // start and stop weak ref discovery
 261   void enable_discovery()   { _discovering_refs = true;  }
 262   void disable_discovery()  { _discovering_refs = false; }
 263   bool discovery_enabled()  { return _discovering_refs;  }
 264 
 265   // whether discovery is atomic wrt other collectors
 266   bool discovery_is_atomic() const { return _discovery_is_atomic; }
 267   void set_atomic_discovery(bool atomic) { _discovery_is_atomic = atomic; }
 268 
 269   // whether discovery is done by multiple threads same-old-timeously
 270   bool discovery_is_mt() const { return _discovery_is_mt; }
 271   void set_mt_discovery(bool mt) { _discovery_is_mt = mt; }
 272 
 273   // Whether we are in a phase when _processing_ is MT.
 274   bool processing_is_mt() const { return _processing_is_mt; }
 275   void set_mt_processing(bool mt) { _processing_is_mt = mt; }
 276 
 277   // whether all enqueuing of weak references is complete
 278   bool enqueuing_is_done()  { return _enqueuing_is_done; }
 279   void set_enqueuing_is_done(bool v) { _enqueuing_is_done = v; }
 280 
 281   // iterate over oops
 282   void weak_oops_do(OopClosure* f);       // weak roots
 283   static void oops_do(OopClosure* f);     // strong root(s)
 284 
 285   // Discover a Reference object, using appropriate discovery criteria
 286   bool discover_reference(oop obj, ReferenceType rt);
 287 
 288   // Process references found during GC (called by the garbage collector)
 289   void process_discovered_references(ReferencePolicy*             policy,
 290                                      BoolObjectClosure*           is_alive,
 291                                      OopClosure*                  keep_alive,
 292                                      VoidClosure*                 complete_gc,
 293                                      AbstractRefProcTaskExecutor* task_executor);
 294 
 295  public:
 296   // Enqueue references at end of GC (called by the garbage collector)
 297   bool enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor = NULL);
 298 
 299   // debugging
 300   void verify_no_references_recorded() PRODUCT_RETURN;
 301   static void verify();
 302 
 303   // clear the discovered lists (unlinking each entry).
 304   void clear_discovered_references() PRODUCT_RETURN;
 305 };
 306 
 307 // A utility class to disable reference discovery in
 308 // the scope which contains it, for given ReferenceProcessor.
 309 class NoRefDiscovery: StackObj {
 310  private:
 311   ReferenceProcessor* _rp;
 312   bool _was_discovering_refs;
 313  public:
 314   NoRefDiscovery(ReferenceProcessor* rp) : _rp(rp) {
 315     if (_was_discovering_refs = _rp->discovery_enabled()) {
 316       _rp->disable_discovery();
 317     }
 318   }
 319 
 320   ~NoRefDiscovery() {
 321     if (_was_discovering_refs) {
 322       _rp->enable_discovery();
 323     }
 324   }
 325 };
 326 
 327 
 328 // A utility class to temporarily mutate the span of the
 329 // given ReferenceProcessor in the scope that contains it.
 330 class ReferenceProcessorSpanMutator: StackObj {
 331  private:
 332   ReferenceProcessor* _rp;
 333   MemRegion           _saved_span;
 334 
 335  public:
 336   ReferenceProcessorSpanMutator(ReferenceProcessor* rp,
 337                                 MemRegion span):
 338     _rp(rp) {
 339     _saved_span = _rp->span();
 340     _rp->set_span(span);
 341   }
 342 
 343   ~ReferenceProcessorSpanMutator() {
 344     _rp->set_span(_saved_span);
 345   }
 346 };
 347 
 348 // A utility class to temporarily change the MT'ness of
 349 // reference discovery for the given ReferenceProcessor
 350 // in the scope that contains it.
 351 class ReferenceProcessorMTMutator: StackObj {
 352  private:
 353   ReferenceProcessor* _rp;
 354   bool                _saved_mt;
 355 
 356  public:
 357   ReferenceProcessorMTMutator(ReferenceProcessor* rp,
 358                               bool mt):
 359     _rp(rp) {
 360     _saved_mt = _rp->discovery_is_mt();
 361     _rp->set_mt_discovery(mt);
 362   }
 363 
 364   ~ReferenceProcessorMTMutator() {
 365     _rp->set_mt_discovery(_saved_mt);
 366   }
 367 };
 368 
 369 
 370 // A utility class to temporarily change the disposition
 371 // of the "is_alive_non_header" closure field of the
 372 // given ReferenceProcessor in the scope that contains it.
 373 class ReferenceProcessorIsAliveMutator: StackObj {
 374  private:
 375   ReferenceProcessor* _rp;
 376   BoolObjectClosure*  _saved_cl;
 377 
 378  public:
 379   ReferenceProcessorIsAliveMutator(ReferenceProcessor* rp,
 380                                    BoolObjectClosure*  cl):
 381     _rp(rp) {
 382     _saved_cl = _rp->is_alive_non_header();
 383     _rp->set_is_alive_non_header(cl);
 384   }
 385 
 386   ~ReferenceProcessorIsAliveMutator() {
 387     _rp->set_is_alive_non_header(_saved_cl);
 388   }
 389 };
 390 
 391 // A utility class to temporarily change the disposition
 392 // of the "discovery_is_atomic" field of the
 393 // given ReferenceProcessor in the scope that contains it.
 394 class ReferenceProcessorAtomicMutator: StackObj {
 395  private:
 396   ReferenceProcessor* _rp;
 397   bool                _saved_atomic_discovery;
 398 
 399  public:
 400   ReferenceProcessorAtomicMutator(ReferenceProcessor* rp,
 401                                   bool atomic):
 402     _rp(rp) {
 403     _saved_atomic_discovery = _rp->discovery_is_atomic();
 404     _rp->set_atomic_discovery(atomic);
 405   }
 406 
 407   ~ReferenceProcessorAtomicMutator() {
 408     _rp->set_atomic_discovery(_saved_atomic_discovery);
 409   }
 410 };
 411 
 412 
 413 // A utility class to temporarily change the MT processing
 414 // disposition of the given ReferenceProcessor instance
 415 // in the scope that contains it.
 416 class ReferenceProcessorMTProcMutator: StackObj {
 417  private:
 418   ReferenceProcessor* _rp;
 419   bool  _saved_mt;
 420 
 421  public:
 422   ReferenceProcessorMTProcMutator(ReferenceProcessor* rp,
 423                                   bool mt):
 424     _rp(rp) {
 425     _saved_mt = _rp->processing_is_mt();
 426     _rp->set_mt_processing(mt);
 427   }
 428 
 429   ~ReferenceProcessorMTProcMutator() {
 430     _rp->set_mt_processing(_saved_mt);
 431   }
 432 };
 433 
 434 
 435 // This class is an interface used to implement task execution for the
 436 // reference processing.
 437 class AbstractRefProcTaskExecutor {
 438 public:
 439 
 440   // Abstract tasks to execute.
 441   class ProcessTask;
 442   class EnqueueTask;
 443   
 444   // Executes a task using worker threads.  
 445   virtual void execute(ProcessTask& task) = 0;
 446   virtual void execute(EnqueueTask& task) = 0;
 447   
 448   // Switch to single threaded mode.
 449   virtual void set_single_threaded_mode() { };
 450 };
 451 
 452 // Abstract reference processing task to execute.
 453 class AbstractRefProcTaskExecutor::ProcessTask {
 454 protected:
 455   ProcessTask(ReferenceProcessor& ref_processor,
 456               DiscoveredList      refs_lists[],
 457               bool                marks_oops_alive)
 458     : _ref_processor(ref_processor),
 459       _refs_lists(refs_lists),
 460       _marks_oops_alive(marks_oops_alive)
 461   { }
 462     
 463 public:
 464   virtual void work(unsigned int work_id, BoolObjectClosure& is_alive,
 465                     OopClosure& keep_alive,
 466                     VoidClosure& complete_gc) = 0;
 467     
 468   // Returns true if a task marks some oops as alive.
 469   bool marks_oops_alive() const
 470   { return _marks_oops_alive; }
 471     
 472 protected:
 473   ReferenceProcessor& _ref_processor;
 474   DiscoveredList*     _refs_lists;
 475   const bool          _marks_oops_alive;
 476 };
 477 
 478 // Abstract reference processing task to execute.
 479 class AbstractRefProcTaskExecutor::EnqueueTask {
 480 protected:
 481   EnqueueTask(ReferenceProcessor& ref_processor,
 482               DiscoveredList      refs_lists[],
 483               oop*                pending_list_addr,
 484               oop                 sentinel_ref, 
 485               int                 n_queues)
 486     : _ref_processor(ref_processor),
 487       _refs_lists(refs_lists),
 488       _pending_list_addr(pending_list_addr),
 489       _sentinel_ref(sentinel_ref),
 490       _n_queues(n_queues)
 491   { }
 492     
 493 public:
 494   virtual void work(unsigned int work_id) = 0;
 495     
 496 protected:
 497   ReferenceProcessor& _ref_processor;
 498   DiscoveredList*     _refs_lists;
 499   oop*                _pending_list_addr;
 500   oop                 _sentinel_ref; 
 501   int                 _n_queues;
 502 };
 503