src/share/vm/memory/sharedHeap.hpp

Wed, 15 Feb 2012 10:12:55 -0800

author
never
date
Wed, 15 Feb 2012 10:12:55 -0800
changeset 3571
09d00c18e323
parent 3357
441e946dc1af
child 4037
da91efe96a93
permissions
-rw-r--r--

7145537: minor tweaks to LogEvents
Reviewed-by: kvn, twisti

duke@435 1 /*
jmasa@2188 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. 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 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_MEMORY_SHAREDHEAP_HPP
stefank@2314 26 #define SHARE_VM_MEMORY_SHAREDHEAP_HPP
stefank@2314 27
stefank@2314 28 #include "gc_interface/collectedHeap.hpp"
stefank@2314 29 #include "memory/generation.hpp"
stefank@2314 30 #include "memory/permGen.hpp"
stefank@2314 31
duke@435 32 // A "SharedHeap" is an implementation of a java heap for HotSpot. This
duke@435 33 // is an abstract class: there may be many different kinds of heaps. This
duke@435 34 // class defines the functions that a heap must implement, and contains
duke@435 35 // infrastructure common to all heaps.
duke@435 36
duke@435 37 class PermGen;
duke@435 38 class Generation;
duke@435 39 class BarrierSet;
duke@435 40 class GenRemSet;
duke@435 41 class Space;
duke@435 42 class SpaceClosure;
duke@435 43 class OopClosure;
duke@435 44 class OopsInGenClosure;
duke@435 45 class ObjectClosure;
duke@435 46 class SubTasksDone;
duke@435 47 class WorkGang;
jmasa@2188 48 class FlexibleWorkGang;
duke@435 49 class CollectorPolicy;
duke@435 50 class KlassHandle;
duke@435 51
jmasa@3294 52 // Note on use of FlexibleWorkGang's for GC.
jmasa@3294 53 // There are three places where task completion is determined.
jmasa@3294 54 // In
jmasa@3294 55 // 1) ParallelTaskTerminator::offer_termination() where _n_threads
jmasa@3294 56 // must be set to the correct value so that count of workers that
jmasa@3294 57 // have offered termination will exactly match the number
jmasa@3294 58 // working on the task. Tasks such as those derived from GCTask
jmasa@3294 59 // use ParallelTaskTerminator's. Tasks that want load balancing
jmasa@3294 60 // by work stealing use this method to gauge completion.
jmasa@3294 61 // 2) SubTasksDone has a variable _n_threads that is used in
jmasa@3294 62 // all_tasks_completed() to determine completion. all_tasks_complete()
jmasa@3294 63 // counts the number of tasks that have been done and then reset
jmasa@3294 64 // the SubTasksDone so that it can be used again. When the number of
jmasa@3294 65 // tasks is set to the number of GC workers, then _n_threads must
jmasa@3294 66 // be set to the number of active GC workers. G1CollectedHeap,
jmasa@3294 67 // HRInto_G1RemSet, GenCollectedHeap and SharedHeap have SubTasksDone.
jmasa@3294 68 // This seems too many.
jmasa@3294 69 // 3) SequentialSubTasksDone has an _n_threads that is used in
jmasa@3294 70 // a way similar to SubTasksDone and has the same dependency on the
jmasa@3294 71 // number of active GC workers. CompactibleFreeListSpace and Space
jmasa@3294 72 // have SequentialSubTasksDone's.
jmasa@3294 73 // Example of using SubTasksDone and SequentialSubTasksDone
jmasa@3294 74 // G1CollectedHeap::g1_process_strong_roots() calls
jmasa@3294 75 // process_strong_roots(false, // no scoping; this is parallel code
jmasa@3294 76 // collecting_perm_gen, so,
jmasa@3294 77 // &buf_scan_non_heap_roots,
jmasa@3294 78 // &eager_scan_code_roots,
jmasa@3294 79 // &buf_scan_perm);
jmasa@3294 80 // which delegates to SharedHeap::process_strong_roots() and uses
jmasa@3294 81 // SubTasksDone* _process_strong_tasks to claim tasks.
jmasa@3294 82 // process_strong_roots() calls
jmasa@3294 83 // rem_set()->younger_refs_iterate(perm_gen(), perm_blk);
jmasa@3294 84 // to scan the card table and which eventually calls down into
jmasa@3294 85 // CardTableModRefBS::par_non_clean_card_iterate_work(). This method
jmasa@3294 86 // uses SequentialSubTasksDone* _pst to claim tasks.
jmasa@3294 87 // Both SubTasksDone and SequentialSubTasksDone call their method
jmasa@3294 88 // all_tasks_completed() to count the number of GC workers that have
jmasa@3294 89 // finished their work. That logic is "when all the workers are
jmasa@3294 90 // finished the tasks are finished".
jmasa@3294 91 //
jmasa@3294 92 // The pattern that appears in the code is to set _n_threads
jmasa@3294 93 // to a value > 1 before a task that you would like executed in parallel
jmasa@3294 94 // and then to set it to 0 after that task has completed. A value of
jmasa@3294 95 // 0 is a "special" value in set_n_threads() which translates to
jmasa@3294 96 // setting _n_threads to 1.
jmasa@3294 97 //
jmasa@3294 98 // Some code uses _n_terminiation to decide if work should be done in
jmasa@3294 99 // parallel. The notorious possibly_parallel_oops_do() in threads.cpp
jmasa@3294 100 // is an example of such code. Look for variable "is_par" for other
jmasa@3294 101 // examples.
jmasa@3294 102 //
jmasa@3294 103 // The active_workers is not reset to 0 after a parallel phase. It's
jmasa@3294 104 // value may be used in later phases and in one instance at least
jmasa@3294 105 // (the parallel remark) it has to be used (the parallel remark depends
jmasa@3294 106 // on the partitioning done in the previous parallel scavenge).
jmasa@3294 107
duke@435 108 class SharedHeap : public CollectedHeap {
duke@435 109 friend class VMStructs;
duke@435 110
ysr@777 111 friend class VM_GC_Operation;
ysr@777 112 friend class VM_CGC_Operation;
ysr@777 113
duke@435 114 private:
duke@435 115 // For claiming strong_roots tasks.
duke@435 116 SubTasksDone* _process_strong_tasks;
duke@435 117
duke@435 118 protected:
duke@435 119 // There should be only a single instance of "SharedHeap" in a program.
duke@435 120 // This is enforced with the protected constructor below, which will also
duke@435 121 // set the static pointer "_sh" to that instance.
duke@435 122 static SharedHeap* _sh;
duke@435 123
duke@435 124 // All heaps contain a "permanent generation." This is some ways
duke@435 125 // similar to a generation in a generational system, in other ways not.
duke@435 126 // See the "PermGen" class.
duke@435 127 PermGen* _perm_gen;
duke@435 128
duke@435 129 // and the Gen Remembered Set, at least one good enough to scan the perm
duke@435 130 // gen.
duke@435 131 GenRemSet* _rem_set;
duke@435 132
duke@435 133 // A gc policy, controls global gc resource issues
duke@435 134 CollectorPolicy *_collector_policy;
duke@435 135
duke@435 136 // See the discussion below, in the specification of the reader function
duke@435 137 // for this variable.
duke@435 138 int _strong_roots_parity;
duke@435 139
duke@435 140 // If we're doing parallel GC, use this gang of threads.
jmasa@2188 141 FlexibleWorkGang* _workers;
duke@435 142
duke@435 143 // Full initialization is done in a concrete subtype's "initialize"
duke@435 144 // function.
duke@435 145 SharedHeap(CollectorPolicy* policy_);
duke@435 146
ysr@777 147 // Returns true if the calling thread holds the heap lock,
ysr@777 148 // or the calling thread is a par gc thread and the heap_lock is held
ysr@777 149 // by the vm thread doing a gc operation.
ysr@777 150 bool heap_lock_held_for_gc();
ysr@777 151 // True if the heap_lock is held by the a non-gc thread invoking a gc
ysr@777 152 // operation.
ysr@777 153 bool _thread_holds_heap_lock_for_gc;
ysr@777 154
duke@435 155 public:
duke@435 156 static SharedHeap* heap() { return _sh; }
duke@435 157
duke@435 158 CollectorPolicy *collector_policy() const { return _collector_policy; }
duke@435 159
duke@435 160 void set_barrier_set(BarrierSet* bs);
jmasa@3294 161 SubTasksDone* process_strong_tasks() { return _process_strong_tasks; }
duke@435 162
duke@435 163 // Does operations required after initialization has been done.
duke@435 164 virtual void post_initialize();
duke@435 165
duke@435 166 // Initialization of ("weak") reference processing support
duke@435 167 virtual void ref_processing_init();
duke@435 168
duke@435 169 void set_perm(PermGen* perm_gen) { _perm_gen = perm_gen; }
duke@435 170
duke@435 171 // This function returns the "GenRemSet" object that allows us to scan
duke@435 172 // generations; at least the perm gen, possibly more in a fully
duke@435 173 // generational heap.
duke@435 174 GenRemSet* rem_set() { return _rem_set; }
duke@435 175
duke@435 176 // These function return the "permanent" generation, in which
duke@435 177 // reflective objects are allocated and stored. Two versions, the second
duke@435 178 // of which returns the view of the perm gen as a generation.
duke@435 179 PermGen* perm() const { return _perm_gen; }
duke@435 180 Generation* perm_gen() const { return _perm_gen->as_gen(); }
duke@435 181
duke@435 182 // Iteration functions.
duke@435 183 void oop_iterate(OopClosure* cl) = 0;
duke@435 184
duke@435 185 // Same as above, restricted to a memory region.
duke@435 186 virtual void oop_iterate(MemRegion mr, OopClosure* cl) = 0;
duke@435 187
duke@435 188 // Iterate over all objects allocated since the last collection, calling
duke@435 189 // "cl->do_object" on each. The heap must have been initialized properly
duke@435 190 // to support this function, or else this call will fail.
duke@435 191 virtual void object_iterate_since_last_GC(ObjectClosure* cl) = 0;
duke@435 192
duke@435 193 // Iterate over all spaces in use in the heap, in an undefined order.
duke@435 194 virtual void space_iterate(SpaceClosure* cl) = 0;
duke@435 195
duke@435 196 // A SharedHeap will contain some number of spaces. This finds the
duke@435 197 // space whose reserved area contains the given address, or else returns
duke@435 198 // NULL.
duke@435 199 virtual Space* space_containing(const void* addr) const = 0;
duke@435 200
duke@435 201 bool no_gc_in_progress() { return !is_gc_active(); }
duke@435 202
duke@435 203 // Some collectors will perform "process_strong_roots" in parallel.
duke@435 204 // Such a call will involve claiming some fine-grained tasks, such as
duke@435 205 // scanning of threads. To make this process simpler, we provide the
duke@435 206 // "strong_roots_parity()" method. Collectors that start parallel tasks
duke@435 207 // whose threads invoke "process_strong_roots" must
duke@435 208 // call "change_strong_roots_parity" in sequential code starting such a
duke@435 209 // task. (This also means that a parallel thread may only call
duke@435 210 // process_strong_roots once.)
duke@435 211 //
duke@435 212 // For calls to process_strong_roots by sequential code, the parity is
duke@435 213 // updated automatically.
duke@435 214 //
duke@435 215 // The idea is that objects representing fine-grained tasks, such as
duke@435 216 // threads, will contain a "parity" field. A task will is claimed in the
duke@435 217 // current "process_strong_roots" call only if its parity field is the
duke@435 218 // same as the "strong_roots_parity"; task claiming is accomplished by
duke@435 219 // updating the parity field to the strong_roots_parity with a CAS.
duke@435 220 //
duke@435 221 // If the client meats this spec, then strong_roots_parity() will have
duke@435 222 // the following properties:
duke@435 223 // a) to return a different value than was returned before the last
duke@435 224 // call to change_strong_roots_parity, and
duke@435 225 // c) to never return a distinguished value (zero) with which such
duke@435 226 // task-claiming variables may be initialized, to indicate "never
duke@435 227 // claimed".
jrose@1424 228 private:
duke@435 229 void change_strong_roots_parity();
jrose@1424 230 public:
duke@435 231 int strong_roots_parity() { return _strong_roots_parity; }
duke@435 232
jrose@1424 233 // Call these in sequential code around process_strong_roots.
jrose@1424 234 // strong_roots_prologue calls change_strong_roots_parity, if
jrose@1424 235 // parallel tasks are enabled.
jrose@1424 236 class StrongRootsScope : public MarkingCodeBlobClosure::MarkScope {
jrose@1424 237 public:
jrose@1424 238 StrongRootsScope(SharedHeap* outer, bool activate = true);
jrose@1424 239 ~StrongRootsScope();
jrose@1424 240 };
jrose@1424 241 friend class StrongRootsScope;
jrose@1424 242
duke@435 243 enum ScanningOption {
duke@435 244 SO_None = 0x0,
duke@435 245 SO_AllClasses = 0x1,
duke@435 246 SO_SystemClasses = 0x2,
ysr@2825 247 SO_Strings = 0x4,
ysr@2825 248 SO_CodeCache = 0x8
duke@435 249 };
duke@435 250
jmasa@2188 251 FlexibleWorkGang* workers() const { return _workers; }
duke@435 252
duke@435 253 // Invoke the "do_oop" method the closure "roots" on all root locations.
duke@435 254 // If "collecting_perm_gen" is false, then roots that may only contain
ysr@2825 255 // references to permGen objects are not scanned; instead, in that case,
ysr@2825 256 // the "perm_blk" closure is applied to all outgoing refs in the
duke@435 257 // permanent generation. The "so" argument determines which of roots
duke@435 258 // the closure is applied to:
duke@435 259 // "SO_None" does none;
duke@435 260 // "SO_AllClasses" applies the closure to all entries in the SystemDictionary;
duke@435 261 // "SO_SystemClasses" to all the "system" classes and loaders;
duke@435 262 // "SO_Strings" applies the closure to all entries in StringTable;
duke@435 263 // "SO_CodeCache" applies the closure to all elements of the CodeCache.
jrose@1424 264 void process_strong_roots(bool activate_scope,
jrose@1424 265 bool collecting_perm_gen,
duke@435 266 ScanningOption so,
duke@435 267 OopClosure* roots,
jrose@1424 268 CodeBlobClosure* code_roots,
duke@435 269 OopsInGenClosure* perm_blk);
duke@435 270
duke@435 271 // Apply "blk" to all the weak roots of the system. These include
duke@435 272 // JNI weak roots, the code cache, system dictionary, symbol table,
duke@435 273 // string table.
duke@435 274 void process_weak_roots(OopClosure* root_closure,
jrose@1424 275 CodeBlobClosure* code_roots,
duke@435 276 OopClosure* non_root_closure);
duke@435 277
duke@435 278 // The functions below are helper functions that a subclass of
duke@435 279 // "SharedHeap" can use in the implementation of its virtual
duke@435 280 // functions.
duke@435 281
ysr@777 282 public:
duke@435 283
duke@435 284 // Do anything common to GC's.
duke@435 285 virtual void gc_prologue(bool full) = 0;
duke@435 286 virtual void gc_epilogue(bool full) = 0;
duke@435 287
jmasa@3294 288 // Sets the number of parallel threads that will be doing tasks
jmasa@3294 289 // (such as process strong roots) subsequently.
jmasa@3357 290 virtual void set_par_threads(uint t);
jmasa@3294 291
jmasa@3294 292 int n_termination();
jmasa@3294 293 void set_n_termination(int t);
jmasa@3294 294
duke@435 295 //
duke@435 296 // New methods from CollectedHeap
duke@435 297 //
duke@435 298
duke@435 299 size_t permanent_capacity() const {
duke@435 300 assert(perm_gen(), "NULL perm gen");
duke@435 301 return perm_gen()->capacity();
duke@435 302 }
duke@435 303
duke@435 304 size_t permanent_used() const {
duke@435 305 assert(perm_gen(), "NULL perm gen");
duke@435 306 return perm_gen()->used();
duke@435 307 }
duke@435 308
duke@435 309 bool is_in_permanent(const void *p) const {
duke@435 310 assert(perm_gen(), "NULL perm gen");
duke@435 311 return perm_gen()->is_in_reserved(p);
duke@435 312 }
duke@435 313
duke@435 314 // Different from is_in_permanent in that is_in_permanent
duke@435 315 // only checks if p is in the reserved area of the heap
duke@435 316 // and this checks to see if it in the commited area.
duke@435 317 // This is typically used by things like the forte stackwalker
duke@435 318 // during verification of suspicious frame values.
duke@435 319 bool is_permanent(const void *p) const {
duke@435 320 assert(perm_gen(), "NULL perm gen");
duke@435 321 return perm_gen()->is_in(p);
duke@435 322 }
duke@435 323
duke@435 324 HeapWord* permanent_mem_allocate(size_t size) {
duke@435 325 assert(perm_gen(), "NULL perm gen");
duke@435 326 return _perm_gen->mem_allocate(size);
duke@435 327 }
duke@435 328
duke@435 329 void permanent_oop_iterate(OopClosure* cl) {
duke@435 330 assert(perm_gen(), "NULL perm gen");
duke@435 331 _perm_gen->oop_iterate(cl);
duke@435 332 }
duke@435 333
duke@435 334 void permanent_object_iterate(ObjectClosure* cl) {
duke@435 335 assert(perm_gen(), "NULL perm gen");
duke@435 336 _perm_gen->object_iterate(cl);
duke@435 337 }
duke@435 338
duke@435 339 // Some utilities.
ysr@777 340 void print_size_transition(outputStream* out,
ysr@777 341 size_t bytes_before,
duke@435 342 size_t bytes_after,
duke@435 343 size_t capacity);
duke@435 344 };
stefank@2314 345
stefank@2314 346 #endif // SHARE_VM_MEMORY_SHAREDHEAP_HPP

mercurial