1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)vmGCOperations.hpp   1.14 07/05/29 09:44:12 JVM"
   3 #endif
   4 /*
   5  * Copyright 2005-2008 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 // The following class hierarchy represents
  29 // a set of operations (VM_Operation) related to GC.
  30 //
  31 //  VM_Operation
  32 //      VM_GC_Operation 
  33 //          VM_GC_HeapInspection
  34 //          VM_GenCollectForAllocation
  35 //          VM_GenCollectFull
  36 //          VM_GenCollectFullConcurrent
  37 //          VM_ParallelGCFailedAllocation
  38 //          VM_ParallelGCFailedPermanentAllocation
  39 //          VM_ParallelGCSystemGC
  40 //  VM_GC_Operation
  41 //   - implements methods common to all classes in the hierarchy:
  42 //     prevents multiple gc requests and manages lock on heap;
  43 //     
  44 //  VM_GC_HeapInspection
  45 //   - prints class histogram on SIGBREAK if PrintClassHistogram
  46 //     is specified; and also the attach "inspectheap" operation
  47 //
  48 //  VM_GenCollectForAllocation
  49 //  VM_GenCollectForPermanentAllocation
  50 //  VM_ParallelGCFailedAllocation
  51 //  VM_ParallelGCFailedPermanentAllocation
  52 //   - this operation is invoked when allocation is failed;
  53 //     operation performs garbage collection and tries to
  54 //     allocate afterwards;
  55 //     
  56 //  VM_GenCollectFull
  57 //  VM_GenCollectFullConcurrent
  58 //  VM_ParallelGCSystemGC
  59 //   - these operations preform full collection of heaps of 
  60 //     different kind
  61 //  
  62 
  63 class VM_GC_Operation: public VM_Operation {
  64  protected:
  65   BasicLock     _pending_list_basic_lock; // for refs pending list notification (PLL)
  66   unsigned int  _gc_count_before;         // gc count before acquiring PLL
  67   unsigned int  _full_gc_count_before;    // full gc count before acquiring PLL
  68   bool          _full;                    // whether a "full" collection
  69   bool          _prologue_succeeded;      // whether doit_prologue succeeded
  70   GCCause::Cause _gc_cause;                // the putative cause for this gc op
  71   bool          _gc_locked;               // will be set if gc was locked
  72 
  73   virtual bool skip_operation() const;
  74 
  75   // java.lang.ref.Reference support
  76   void acquire_pending_list_lock();
  77   void release_and_notify_pending_list_lock();
  78 
  79  public:
  80   VM_GC_Operation(unsigned int gc_count_before,
  81                   unsigned int full_gc_count_before = 0,
  82                   bool full = false) {
  83     _full = full;
  84     _prologue_succeeded = false;
  85     _gc_count_before    = gc_count_before;
  86 
  87     // A subclass constructor will likely overwrite the following
  88     _gc_cause           = GCCause::_no_cause_specified;
  89 
  90     _gc_locked = false;
  91 
  92     if (full) {
  93       _full_gc_count_before = full_gc_count_before;
  94     }
  95   }
  96   ~VM_GC_Operation() {}
  97   
  98   // Acquire the reference synchronization lock
  99   virtual bool doit_prologue();
 100   // Do notifyAll (if needed) and release held lock
 101   virtual void doit_epilogue();
 102 
 103   virtual bool allow_nested_vm_operations() const  { return true; }
 104   bool prologue_succeeded() const { return _prologue_succeeded; }
 105 
 106   void set_gc_locked() { _gc_locked = true; }
 107   bool gc_locked() const  { return _gc_locked; }
 108 
 109   static void notify_gc_begin(bool full = false);
 110   static void notify_gc_end();
 111 };
 112 
 113 
 114 class VM_GC_HeapInspection: public VM_GC_Operation {
 115  private:
 116   outputStream* _out;
 117   bool _full_gc;
 118  public:
 119   VM_GC_HeapInspection(outputStream* out, bool request_full_gc) :
 120     VM_GC_Operation(0 /* total collections,      dummy, ignored */,
 121                     0 /* total full collections, dummy, ignored */,
 122                     request_full_gc) {
 123     _out = out;
 124     _full_gc = request_full_gc;
 125   }
 126 
 127   ~VM_GC_HeapInspection() {}
 128   virtual VMOp_Type type() const { return VMOp_GC_HeapInspection; }
 129   virtual bool skip_operation() const;
 130   virtual bool doit_prologue();
 131   virtual void doit();
 132 };
 133 
 134 
 135 class VM_GenCollectForAllocation: public VM_GC_Operation {
 136  private:
 137   HeapWord*   _res;
 138   size_t      _size;                       // size of object to be allocated.
 139   bool        _tlab;                       // alloc is of a tlab.
 140  public:
 141   VM_GenCollectForAllocation(size_t size,
 142                              bool tlab,
 143                              unsigned int gc_count_before)
 144     : VM_GC_Operation(gc_count_before),
 145       _size(size),
 146       _tlab(tlab) {
 147     _res = NULL;
 148   }
 149   ~VM_GenCollectForAllocation()  {}
 150   virtual VMOp_Type type() const { return VMOp_GenCollectForAllocation; }    
 151   virtual void doit();
 152   HeapWord* result() const       { return _res; }
 153 };
 154 
 155 
 156 // VM operation to invoke a collection of the heap as a
 157 // GenCollectedHeap heap.
 158 class VM_GenCollectFull: public VM_GC_Operation {
 159  private:
 160   int _max_level;
 161  public:
 162   VM_GenCollectFull(unsigned int gc_count_before,
 163                     unsigned int full_gc_count_before,
 164                     GCCause::Cause gc_cause,
 165                       int max_level) 
 166     : VM_GC_Operation(gc_count_before, full_gc_count_before, true /* full */),
 167       _max_level(max_level)
 168   { _gc_cause = gc_cause; }
 169   ~VM_GenCollectFull() {}
 170   virtual VMOp_Type type() const { return VMOp_GenCollectFull; }
 171   virtual void doit();
 172 };
 173 
 174 class VM_GenCollectForPermanentAllocation: public VM_GC_Operation {
 175  private:
 176   HeapWord*   _res;
 177   size_t      _size;                       // size of object to be allocated
 178  public:
 179   VM_GenCollectForPermanentAllocation(size_t size,
 180                                       unsigned int gc_count_before,
 181                                       unsigned int full_gc_count_before,
 182                                       GCCause::Cause gc_cause)
 183     : VM_GC_Operation(gc_count_before, full_gc_count_before, true),
 184       _size(size) {
 185     _res = NULL;
 186     _gc_cause = gc_cause;
 187   }
 188   ~VM_GenCollectForPermanentAllocation()  {}
 189   virtual VMOp_Type type() const { return VMOp_GenCollectForPermanentAllocation; }
 190   virtual void doit();
 191   HeapWord* result() const       { return _res; }
 192 };