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