1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)sharedHeap.hpp       1.56 07/05/05 17:05:55 JVM"
   3 #endif
   4 /*
   5  * Copyright 2000-2006 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 // A "SharedHeap" is an implementation of a java heap for HotSpot.  This
  29 // is an abstract class: there may be many different kinds of heaps.  This
  30 // class defines the functions that a heap must implement, and contains
  31 // infrastructure common to all heaps.
  32 
  33 class PermGen;
  34 class Generation;
  35 class BarrierSet;
  36 class GenRemSet;
  37 class Space;
  38 class SpaceClosure;
  39 class OopClosure;
  40 class OopsInGenClosure;
  41 class ObjectClosure;
  42 class SubTasksDone;
  43 class WorkGang;
  44 class CollectorPolicy;
  45 class KlassHandle;
  46 
  47 class SharedHeap : public CollectedHeap {
  48   friend class VMStructs;
  49 
  50   friend class VM_GC_Operation;
  51   friend class VM_CGC_Operation;
  52 
  53 private:
  54   // For claiming strong_roots tasks.
  55   SubTasksDone* _process_strong_tasks;
  56 
  57 protected:
  58   // There should be only a single instance of "SharedHeap" in a program.
  59   // This is enforced with the protected constructor below, which will also
  60   // set the static pointer "_sh" to that instance.
  61   static SharedHeap* _sh;
  62 
  63   // All heaps contain a "permanent generation."  This is some ways
  64   // similar to a generation in a generational system, in other ways not.
  65   // See the "PermGen" class.
  66   PermGen* _perm_gen;
  67 
  68   // and the Gen Remembered Set, at least one good enough to scan the perm
  69   // gen.
  70   GenRemSet* _rem_set;
  71 
  72   // A gc policy, controls global gc resource issues
  73   CollectorPolicy *_collector_policy;
  74 
  75   // See the discussion below, in the specification of the reader function
  76   // for this variable.
  77   int _strong_roots_parity;
  78 
  79   // If we're doing parallel GC, use this gang of threads.
  80   WorkGang* _workers;
  81 
  82   // Number of parallel threads currently working on GC tasks.
  83   // O indicates use sequential code; 1 means use parallel code even with
  84   // only one thread, for performance testing purposes.
  85   int _n_par_threads;
  86 
  87   // Full initialization is done in a concrete subtype's "initialize"
  88   // function.
  89   SharedHeap(CollectorPolicy* policy_);
  90 
  91   // Returns true if the calling thread holds the heap lock,
  92   // or the calling thread is a par gc thread and the heap_lock is held
  93   // by the vm thread doing a gc operation.
  94   bool heap_lock_held_for_gc();
  95   // True if the heap_lock is held by the a non-gc thread invoking a gc
  96   // operation.
  97   bool _thread_holds_heap_lock_for_gc;
  98 
  99 public:
 100   static SharedHeap* heap() { return _sh; }
 101 
 102   CollectorPolicy *collector_policy() const { return _collector_policy; }
 103 
 104   void set_barrier_set(BarrierSet* bs);
 105 
 106   // Does operations required after initialization has been done.
 107   virtual void post_initialize();
 108 
 109   // Initialization of ("weak") reference processing support
 110   virtual void ref_processing_init();
 111 
 112   void set_perm(PermGen* perm_gen) { _perm_gen = perm_gen; }
 113 
 114   // This function returns the "GenRemSet" object that allows us to scan
 115   // generations; at least the perm gen, possibly more in a fully
 116   // generational heap.
 117   GenRemSet* rem_set() { return _rem_set; }
 118 
 119   // These function return the "permanent" generation, in which
 120   // reflective objects are allocated and stored.  Two versions, the second
 121   // of which returns the view of the perm gen as a generation.
 122   PermGen* perm() const { return _perm_gen; }
 123   Generation* perm_gen() const { return _perm_gen->as_gen(); }
 124 
 125   // Iteration functions.
 126   void oop_iterate(OopClosure* cl) = 0;
 127 
 128   // Same as above, restricted to a memory region.
 129   virtual void oop_iterate(MemRegion mr, OopClosure* cl) = 0;
 130 
 131   // Iterate over all objects allocated since the last collection, calling
 132   // "cl->do_object" on each.  The heap must have been initialized properly
 133   // to support this function, or else this call will fail.
 134   virtual void object_iterate_since_last_GC(ObjectClosure* cl) = 0;
 135 
 136   // Iterate over all spaces in use in the heap, in an undefined order.
 137   virtual void space_iterate(SpaceClosure* cl) = 0;
 138 
 139   // A SharedHeap will contain some number of spaces.  This finds the
 140   // space whose reserved area contains the given address, or else returns
 141   // NULL.
 142   virtual Space* space_containing(const void* addr) const = 0;
 143 
 144   bool no_gc_in_progress() { return !is_gc_active(); }
 145 
 146   // Some collectors will perform "process_strong_roots" in parallel.
 147   // Such a call will involve claiming some fine-grained tasks, such as
 148   // scanning of threads.  To make this process simpler, we provide the
 149   // "strong_roots_parity()" method.  Collectors that start parallel tasks
 150   // whose threads invoke "process_strong_roots" must 
 151   // call "change_strong_roots_parity" in sequential code starting such a
 152   // task.  (This also means that a parallel thread may only call
 153   // process_strong_roots once.)
 154   // 
 155   // For calls to process_strong_roots by sequential code, the parity is
 156   // updated automatically.
 157   // 
 158   // The idea is that objects representing fine-grained tasks, such as
 159   // threads, will contain a "parity" field.  A task will is claimed in the 
 160   // current "process_strong_roots" call only if its parity field is the
 161   // same as the "strong_roots_parity"; task claiming is accomplished by
 162   // updating the parity field to the strong_roots_parity with a CAS.
 163   // 
 164   // If the client meats this spec, then strong_roots_parity() will have
 165   // the following properties:
 166   //   a) to return a different value than was returned before the last
 167   //      call to change_strong_roots_parity, and
 168   //   c) to never return a distinguished value (zero) with which such
 169   //      task-claiming variables may be initialized, to indicate "never
 170   //      claimed".
 171   void change_strong_roots_parity();
 172   int strong_roots_parity() { return _strong_roots_parity; }
 173 
 174   enum ScanningOption {
 175     SO_None                = 0x0,
 176     SO_AllClasses          = 0x1,
 177     SO_SystemClasses       = 0x2,
 178     SO_Symbols             = 0x4,
 179     SO_Strings             = 0x8,
 180     SO_CodeCache           = 0x10
 181   };
 182 
 183   WorkGang* workers() const { return _workers; }
 184 
 185   // Sets the number of parallel threads that will be doing tasks
 186   // (such as process strong roots) subsequently.
 187   virtual void set_par_threads(int t);
 188 
 189   // Number of threads currently working on GC tasks.
 190   int n_par_threads() { return _n_par_threads; }
 191 
 192   // Invoke the "do_oop" method the closure "roots" on all root locations.
 193   // If "collecting_perm_gen" is false, then roots that may only contain
 194   // references to permGen objects are not scanned.  If true, the
 195   // "perm_gen" closure is applied to all older-to-younger refs in the
 196   // permanent generation.  The "so" argument determines which of roots
 197   // the closure is applied to:
 198   // "SO_None" does none;
 199   // "SO_AllClasses" applies the closure to all entries in the SystemDictionary;
 200   // "SO_SystemClasses" to all the "system" classes and loaders;
 201   // "SO_Symbols" applies the closure to all entries in SymbolsTable;
 202   // "SO_Strings" applies the closure to all entries in StringTable;
 203   // "SO_CodeCache" applies the closure to all elements of the CodeCache.
 204   void process_strong_roots(bool collecting_perm_gen,
 205                             ScanningOption so,
 206                             OopClosure* roots,
 207                             OopsInGenClosure* perm_blk);
 208 
 209   // Apply "blk" to all the weak roots of the system.  These include
 210   // JNI weak roots, the code cache, system dictionary, symbol table,
 211   // string table.
 212   void process_weak_roots(OopClosure* root_closure,
 213                           OopClosure* non_root_closure);
 214                           
 215 
 216   // Like CollectedHeap::collect, but assume that the caller holds the Heap_lock.
 217   virtual void collect_locked(GCCause::Cause cause) = 0;
 218 
 219   // The functions below are helper functions that a subclass of
 220   // "SharedHeap" can use in the implementation of its virtual
 221   // functions.
 222 
 223 public:
 224 
 225   // Do anything common to GC's.
 226   virtual void gc_prologue(bool full) = 0;
 227   virtual void gc_epilogue(bool full) = 0;
 228 
 229   //
 230   // New methods from CollectedHeap
 231   //
 232 
 233   size_t permanent_capacity() const {
 234     assert(perm_gen(), "NULL perm gen");
 235     return perm_gen()->capacity();
 236   }
 237 
 238   size_t permanent_used() const {
 239     assert(perm_gen(), "NULL perm gen");
 240     return perm_gen()->used();
 241   }
 242 
 243   bool is_in_permanent(const void *p) const {
 244     assert(perm_gen(), "NULL perm gen");
 245     return perm_gen()->is_in_reserved(p);
 246   }
 247 
 248   // Different from is_in_permanent in that is_in_permanent
 249   // only checks if p is in the reserved area of the heap
 250   // and this checks to see if it in the commited area. 
 251   // This is typically used by things like the forte stackwalker
 252   // during verification of suspicious frame values.
 253   bool is_permanent(const void *p) const {
 254     assert(perm_gen(), "NULL perm gen");
 255     return perm_gen()->is_in(p);
 256   }
 257 
 258   HeapWord* permanent_mem_allocate(size_t size) {
 259     assert(perm_gen(), "NULL perm gen");
 260     return _perm_gen->mem_allocate(size);
 261   }
 262 
 263   void permanent_oop_iterate(OopClosure* cl) {
 264     assert(perm_gen(), "NULL perm gen");
 265     _perm_gen->oop_iterate(cl);
 266   }
 267 
 268   void permanent_object_iterate(ObjectClosure* cl) {
 269     assert(perm_gen(), "NULL perm gen");
 270     _perm_gen->object_iterate(cl);
 271   }
 272 
 273   // Some utilities.
 274   void print_size_transition(outputStream* out,
 275                              size_t bytes_before,
 276                              size_t bytes_after,
 277                              size_t capacity);
 278 };
 279 
 280