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