src/share/vm/memory/referenceProcessor.hpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
   1 /*
   2  * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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 #ifndef SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
  26 #define SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
  27 
  28 #include "memory/referencePolicy.hpp"


  29 #include "oops/instanceRefKlass.hpp"
  30 


  31 // ReferenceProcessor class encapsulates the per-"collector" processing
  32 // of java.lang.Reference objects for GC. The interface is useful for supporting
  33 // a generational abstraction, in particular when there are multiple
  34 // generations that are being independently collected -- possibly
  35 // concurrently and/or incrementally.  Note, however, that the
  36 // ReferenceProcessor class abstracts away from a generational setting
  37 // by using only a heap interval (called "span" below), thus allowing
  38 // its use in a straightforward manner in a general, non-generational
  39 // setting.
  40 //
  41 // The basic idea is that each ReferenceProcessor object concerns
  42 // itself with ("weak") reference processing in a specific "span"
  43 // of the heap of interest to a specific collector. Currently,
  44 // the span is a convex interval of the heap, but, efficiency
  45 // apart, there seems to be no reason it couldn't be extended
  46 // (with appropriate modifications) to any "non-convex interval".
  47 
  48 // forward references
  49 class ReferencePolicy;
  50 class AbstractRefProcTaskExecutor;


 187 
 188   // Statistics
 189   NOT_PRODUCT(
 190   inline size_t processed() const { return _processed; }
 191   inline size_t removed() const   { return _removed; }
 192   )
 193 
 194   inline void move_to_next() {
 195     if (_ref == _next) {
 196       // End of the list.
 197       _ref = NULL;
 198     } else {
 199       _ref = _next;
 200     }
 201     assert(_ref != _first_seen, "cyclic ref_list found");
 202     NOT_PRODUCT(_processed++);
 203   }
 204 };
 205 
 206 class ReferenceProcessor : public CHeapObj<mtGC> {




 207  protected:
 208   // Compatibility with pre-4965777 JDK's
 209   static bool _pending_list_uses_discovered_field;
 210 
 211   // The SoftReference master timestamp clock
 212   static jlong _soft_ref_timestamp_clock;
 213 
 214   MemRegion   _span;                    // (right-open) interval of heap
 215                                         // subject to wkref discovery
 216 
 217   bool        _discovering_refs;        // true when discovery enabled
 218   bool        _discovery_is_atomic;     // if discovery is atomic wrt
 219                                         // other collectors in configuration
 220   bool        _discovery_is_mt;         // true if reference discovery is MT.
 221 
 222   // If true, setting "next" field of a discovered refs list requires
 223   // write barrier(s).  (Must be true if used in a collector in which
 224   // elements of a discovered list may be moved during discovery: for
 225   // example, a collector like Garbage-First that moves objects during a
 226   // long-term concurrent marking phase that does weak reference


 265   DiscoveredList* _discoveredFinalRefs;
 266   DiscoveredList* _discoveredPhantomRefs;
 267 
 268  public:
 269   static int number_of_subclasses_of_ref() { return (REF_PHANTOM - REF_OTHER); }
 270 
 271   uint num_q()                             { return _num_q; }
 272   uint max_num_q()                         { return _max_num_q; }
 273   void set_active_mt_degree(uint v)        { _num_q = v; }
 274 
 275   DiscoveredList* discovered_refs()        { return _discovered_refs; }
 276 
 277   ReferencePolicy* setup_policy(bool always_clear) {
 278     _current_soft_ref_policy = always_clear ?
 279       _always_clear_soft_ref_policy : _default_soft_ref_policy;
 280     _current_soft_ref_policy->setup();   // snapshot the policy threshold
 281     return _current_soft_ref_policy;
 282   }
 283 
 284   // Process references with a certain reachability level.
 285   void process_discovered_reflist(DiscoveredList               refs_lists[],
 286                                   ReferencePolicy*             policy,
 287                                   bool                         clear_referent,
 288                                   BoolObjectClosure*           is_alive,
 289                                   OopClosure*                  keep_alive,
 290                                   VoidClosure*                 complete_gc,
 291                                   AbstractRefProcTaskExecutor* task_executor);
 292 
 293   void process_phaseJNI(BoolObjectClosure* is_alive,
 294                         OopClosure*        keep_alive,
 295                         VoidClosure*       complete_gc);
 296 
 297   // Work methods used by the method process_discovered_reflist
 298   // Phase1: keep alive all those referents that are otherwise
 299   // dead but which must be kept alive by policy (and their closure).
 300   void process_phase1(DiscoveredList&     refs_list,
 301                       ReferencePolicy*    policy,
 302                       BoolObjectClosure*  is_alive,
 303                       OopClosure*         keep_alive,
 304                       VoidClosure*        complete_gc);
 305   // Phase2: remove all those references whose referents are


 332                       bool               clear_referent,
 333                       BoolObjectClosure* is_alive,
 334                       OopClosure*        keep_alive,
 335                       VoidClosure*       complete_gc);
 336 
 337   // Enqueue references with a certain reachability level
 338   void enqueue_discovered_reflist(DiscoveredList& refs_list, HeapWord* pending_list_addr);
 339 
 340   // "Preclean" all the discovered reference lists
 341   // by removing references with strongly reachable referents.
 342   // The first argument is a predicate on an oop that indicates
 343   // its (strong) reachability and the second is a closure that
 344   // may be used to incrementalize or abort the precleaning process.
 345   // The caller is responsible for taking care of potential
 346   // interference with concurrent operations on these lists
 347   // (or predicates involved) by other threads. Currently
 348   // only used by the CMS collector.
 349   void preclean_discovered_references(BoolObjectClosure* is_alive,
 350                                       OopClosure*        keep_alive,
 351                                       VoidClosure*       complete_gc,
 352                                       YieldClosure*      yield);

 353 
 354   // Delete entries in the discovered lists that have
 355   // either a null referent or are not active. Such
 356   // Reference objects can result from the clearing
 357   // or enqueueing of Reference objects concurrent
 358   // with their discovery by a (concurrent) collector.
 359   // For a definition of "active" see java.lang.ref.Reference;
 360   // Refs are born active, become inactive when enqueued,
 361   // and never become active again. The state of being
 362   // active is encoded as follows: A Ref is active
 363   // if and only if its "next" field is NULL.
 364   void clean_up_discovered_references();
 365   void clean_up_discovered_reflist(DiscoveredList& refs_list);
 366 
 367   // Returns the name of the discovered reference list
 368   // occupying the i / _num_q slot.
 369   const char* list_name(uint i);
 370 
 371   void enqueue_discovered_reflists(HeapWord* pending_list_addr, AbstractRefProcTaskExecutor* task_executor);
 372 


 483 
 484   // Whether we are in a phase when _processing_ is MT.
 485   bool processing_is_mt() const { return _processing_is_mt; }
 486   void set_mt_processing(bool mt) { _processing_is_mt = mt; }
 487 
 488   // whether all enqueuing of weak references is complete
 489   bool enqueuing_is_done()  { return _enqueuing_is_done; }
 490   void set_enqueuing_is_done(bool v) { _enqueuing_is_done = v; }
 491 
 492   // iterate over oops
 493   void weak_oops_do(OopClosure* f);       // weak roots
 494 
 495   // Balance each of the discovered lists.
 496   void balance_all_queues();
 497   void verify_list(DiscoveredList& ref_list);
 498 
 499   // Discover a Reference object, using appropriate discovery criteria
 500   bool discover_reference(oop obj, ReferenceType rt);
 501 
 502   // Process references found during GC (called by the garbage collector)
 503   void process_discovered_references(BoolObjectClosure*           is_alive,

 504                                      OopClosure*                  keep_alive,
 505                                      VoidClosure*                 complete_gc,
 506                                      AbstractRefProcTaskExecutor* task_executor);

 507 
 508  public:
 509   // Enqueue references at end of GC (called by the garbage collector)
 510   bool enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor = NULL);
 511 
 512   // If a discovery is in process that is being superceded, abandon it: all
 513   // the discovered lists will be empty, and all the objects on them will
 514   // have NULL discovered fields.  Must be called only at a safepoint.
 515   void abandon_partial_discovery();
 516 
 517   // debugging
 518   void verify_no_references_recorded() PRODUCT_RETURN;
 519   void verify_referent(oop obj)        PRODUCT_RETURN;
 520 
 521   // clear the discovered lists (unlinking each entry).
 522   void clear_discovered_references() PRODUCT_RETURN;
 523 };
 524 
 525 // A utility class to disable reference discovery in
 526 // the scope which contains it, for given ReferenceProcessor.
 527 class NoRefDiscovery: StackObj {
 528  private:


   1 /*
   2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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 #ifndef SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
  26 #define SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
  27 
  28 #include "memory/referencePolicy.hpp"
  29 #include "memory/referenceProcessorStats.hpp"
  30 #include "memory/referenceType.hpp"
  31 #include "oops/instanceRefKlass.hpp"
  32 
  33 class GCTimer;
  34 
  35 // ReferenceProcessor class encapsulates the per-"collector" processing
  36 // of java.lang.Reference objects for GC. The interface is useful for supporting
  37 // a generational abstraction, in particular when there are multiple
  38 // generations that are being independently collected -- possibly
  39 // concurrently and/or incrementally.  Note, however, that the
  40 // ReferenceProcessor class abstracts away from a generational setting
  41 // by using only a heap interval (called "span" below), thus allowing
  42 // its use in a straightforward manner in a general, non-generational
  43 // setting.
  44 //
  45 // The basic idea is that each ReferenceProcessor object concerns
  46 // itself with ("weak") reference processing in a specific "span"
  47 // of the heap of interest to a specific collector. Currently,
  48 // the span is a convex interval of the heap, but, efficiency
  49 // apart, there seems to be no reason it couldn't be extended
  50 // (with appropriate modifications) to any "non-convex interval".
  51 
  52 // forward references
  53 class ReferencePolicy;
  54 class AbstractRefProcTaskExecutor;


 191 
 192   // Statistics
 193   NOT_PRODUCT(
 194   inline size_t processed() const { return _processed; }
 195   inline size_t removed() const   { return _removed; }
 196   )
 197 
 198   inline void move_to_next() {
 199     if (_ref == _next) {
 200       // End of the list.
 201       _ref = NULL;
 202     } else {
 203       _ref = _next;
 204     }
 205     assert(_ref != _first_seen, "cyclic ref_list found");
 206     NOT_PRODUCT(_processed++);
 207   }
 208 };
 209 
 210 class ReferenceProcessor : public CHeapObj<mtGC> {
 211 
 212  private:
 213   size_t total_count(DiscoveredList lists[]);
 214 
 215  protected:
 216   // Compatibility with pre-4965777 JDK's
 217   static bool _pending_list_uses_discovered_field;
 218 
 219   // The SoftReference master timestamp clock
 220   static jlong _soft_ref_timestamp_clock;
 221 
 222   MemRegion   _span;                    // (right-open) interval of heap
 223                                         // subject to wkref discovery
 224 
 225   bool        _discovering_refs;        // true when discovery enabled
 226   bool        _discovery_is_atomic;     // if discovery is atomic wrt
 227                                         // other collectors in configuration
 228   bool        _discovery_is_mt;         // true if reference discovery is MT.
 229 
 230   // If true, setting "next" field of a discovered refs list requires
 231   // write barrier(s).  (Must be true if used in a collector in which
 232   // elements of a discovered list may be moved during discovery: for
 233   // example, a collector like Garbage-First that moves objects during a
 234   // long-term concurrent marking phase that does weak reference


 273   DiscoveredList* _discoveredFinalRefs;
 274   DiscoveredList* _discoveredPhantomRefs;
 275 
 276  public:
 277   static int number_of_subclasses_of_ref() { return (REF_PHANTOM - REF_OTHER); }
 278 
 279   uint num_q()                             { return _num_q; }
 280   uint max_num_q()                         { return _max_num_q; }
 281   void set_active_mt_degree(uint v)        { _num_q = v; }
 282 
 283   DiscoveredList* discovered_refs()        { return _discovered_refs; }
 284 
 285   ReferencePolicy* setup_policy(bool always_clear) {
 286     _current_soft_ref_policy = always_clear ?
 287       _always_clear_soft_ref_policy : _default_soft_ref_policy;
 288     _current_soft_ref_policy->setup();   // snapshot the policy threshold
 289     return _current_soft_ref_policy;
 290   }
 291 
 292   // Process references with a certain reachability level.
 293   size_t process_discovered_reflist(DiscoveredList               refs_lists[],
 294                                     ReferencePolicy*             policy,
 295                                     bool                         clear_referent,
 296                                     BoolObjectClosure*           is_alive,
 297                                     OopClosure*                  keep_alive,
 298                                     VoidClosure*                 complete_gc,
 299                                     AbstractRefProcTaskExecutor* task_executor);
 300 
 301   void process_phaseJNI(BoolObjectClosure* is_alive,
 302                         OopClosure*        keep_alive,
 303                         VoidClosure*       complete_gc);
 304 
 305   // Work methods used by the method process_discovered_reflist
 306   // Phase1: keep alive all those referents that are otherwise
 307   // dead but which must be kept alive by policy (and their closure).
 308   void process_phase1(DiscoveredList&     refs_list,
 309                       ReferencePolicy*    policy,
 310                       BoolObjectClosure*  is_alive,
 311                       OopClosure*         keep_alive,
 312                       VoidClosure*        complete_gc);
 313   // Phase2: remove all those references whose referents are


 340                       bool               clear_referent,
 341                       BoolObjectClosure* is_alive,
 342                       OopClosure*        keep_alive,
 343                       VoidClosure*       complete_gc);
 344 
 345   // Enqueue references with a certain reachability level
 346   void enqueue_discovered_reflist(DiscoveredList& refs_list, HeapWord* pending_list_addr);
 347 
 348   // "Preclean" all the discovered reference lists
 349   // by removing references with strongly reachable referents.
 350   // The first argument is a predicate on an oop that indicates
 351   // its (strong) reachability and the second is a closure that
 352   // may be used to incrementalize or abort the precleaning process.
 353   // The caller is responsible for taking care of potential
 354   // interference with concurrent operations on these lists
 355   // (or predicates involved) by other threads. Currently
 356   // only used by the CMS collector.
 357   void preclean_discovered_references(BoolObjectClosure* is_alive,
 358                                       OopClosure*        keep_alive,
 359                                       VoidClosure*       complete_gc,
 360                                       YieldClosure*      yield,
 361                                       GCTimer*           gc_timer);
 362 
 363   // Delete entries in the discovered lists that have
 364   // either a null referent or are not active. Such
 365   // Reference objects can result from the clearing
 366   // or enqueueing of Reference objects concurrent
 367   // with their discovery by a (concurrent) collector.
 368   // For a definition of "active" see java.lang.ref.Reference;
 369   // Refs are born active, become inactive when enqueued,
 370   // and never become active again. The state of being
 371   // active is encoded as follows: A Ref is active
 372   // if and only if its "next" field is NULL.
 373   void clean_up_discovered_references();
 374   void clean_up_discovered_reflist(DiscoveredList& refs_list);
 375 
 376   // Returns the name of the discovered reference list
 377   // occupying the i / _num_q slot.
 378   const char* list_name(uint i);
 379 
 380   void enqueue_discovered_reflists(HeapWord* pending_list_addr, AbstractRefProcTaskExecutor* task_executor);
 381 


 492 
 493   // Whether we are in a phase when _processing_ is MT.
 494   bool processing_is_mt() const { return _processing_is_mt; }
 495   void set_mt_processing(bool mt) { _processing_is_mt = mt; }
 496 
 497   // whether all enqueuing of weak references is complete
 498   bool enqueuing_is_done()  { return _enqueuing_is_done; }
 499   void set_enqueuing_is_done(bool v) { _enqueuing_is_done = v; }
 500 
 501   // iterate over oops
 502   void weak_oops_do(OopClosure* f);       // weak roots
 503 
 504   // Balance each of the discovered lists.
 505   void balance_all_queues();
 506   void verify_list(DiscoveredList& ref_list);
 507 
 508   // Discover a Reference object, using appropriate discovery criteria
 509   bool discover_reference(oop obj, ReferenceType rt);
 510 
 511   // Process references found during GC (called by the garbage collector)
 512   ReferenceProcessorStats
 513   process_discovered_references(BoolObjectClosure*           is_alive,
 514                                 OopClosure*                  keep_alive,
 515                                 VoidClosure*                 complete_gc,
 516                                 AbstractRefProcTaskExecutor* task_executor,
 517                                 GCTimer *gc_timer);
 518 

 519   // Enqueue references at end of GC (called by the garbage collector)
 520   bool enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor = NULL);
 521 
 522   // If a discovery is in process that is being superceded, abandon it: all
 523   // the discovered lists will be empty, and all the objects on them will
 524   // have NULL discovered fields.  Must be called only at a safepoint.
 525   void abandon_partial_discovery();
 526 
 527   // debugging
 528   void verify_no_references_recorded() PRODUCT_RETURN;
 529   void verify_referent(oop obj)        PRODUCT_RETURN;
 530 
 531   // clear the discovered lists (unlinking each entry).
 532   void clear_discovered_references() PRODUCT_RETURN;
 533 };
 534 
 535 // A utility class to disable reference discovery in
 536 // the scope which contains it, for given ReferenceProcessor.
 537 class NoRefDiscovery: StackObj {
 538  private: