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