1 /* 2 * Copyright (c) 2000, 2015, 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 31 // A "SharedHeap" is an implementation of a java heap for HotSpot. This 32 // is an abstract class: there may be many different kinds of heaps. This 33 // class defines the functions that a heap must implement, and contains 34 // infrastructure common to all heaps. 35 36 class Generation; 37 class BarrierSet; 38 class GenRemSet; 39 class Space; 40 class SpaceClosure; 41 class OopClosure; 42 class OopsInGenClosure; 43 class ObjectClosure; 44 class SubTasksDone; 45 class WorkGang; 46 class FlexibleWorkGang; 47 class CollectorPolicy; 48 class KlassClosure; 49 50 // Note on use of FlexibleWorkGang's for GC. 51 // There are three places where task completion is determined. 52 // In 53 // 1) ParallelTaskTerminator::offer_termination() where _n_threads 54 // must be set to the correct value so that count of workers that 55 // have offered termination will exactly match the number 56 // working on the task. Tasks such as those derived from GCTask 57 // use ParallelTaskTerminator's. Tasks that want load balancing 58 // by work stealing use this method to gauge completion. 59 // 2) SubTasksDone has a variable _n_threads that is used in 60 // all_tasks_completed() to determine completion. all_tasks_complete() 61 // counts the number of tasks that have been done and then reset 62 // the SubTasksDone so that it can be used again. When the number of 63 // tasks is set to the number of GC workers, then _n_threads must 64 // be set to the number of active GC workers. G1CollectedHeap, 65 // HRInto_G1RemSet, GenCollectedHeap and SharedHeap have SubTasksDone. 66 // This seems too many. 67 // 3) SequentialSubTasksDone has an _n_threads that is used in 68 // a way similar to SubTasksDone and has the same dependency on the 69 // number of active GC workers. CompactibleFreeListSpace and Space 70 // have SequentialSubTasksDone's. 71 // Example of using SubTasksDone and SequentialSubTasksDone 72 // G1CollectedHeap::g1_process_roots() 73 // to SharedHeap::process_roots() and uses 74 // SubTasksDone* _process_strong_tasks to claim tasks. 75 // process_roots() calls 76 // rem_set()->younger_refs_iterate() 77 // to scan the card table and which eventually calls down into 78 // CardTableModRefBS::par_non_clean_card_iterate_work(). This method 79 // uses SequentialSubTasksDone* _pst to claim tasks. 80 // Both SubTasksDone and SequentialSubTasksDone call their method 81 // all_tasks_completed() to count the number of GC workers that have 82 // finished their work. That logic is "when all the workers are 83 // finished the tasks are finished". 84 // 85 // The pattern that appears in the code is to set _n_threads 86 // to a value > 1 before a task that you would like executed in parallel 87 // and then to set it to 0 after that task has completed. A value of 88 // 0 is a "special" value in set_n_threads() which translates to 89 // setting _n_threads to 1. 90 // 91 // Some code uses _n_termination to decide if work should be done in 92 // parallel. The notorious possibly_parallel_oops_do() in threads.cpp 93 // is an example of such code. Look for variable "is_par" for other 94 // examples. 95 // 96 // The active_workers is not reset to 0 after a parallel phase. It's 97 // value may be used in later phases and in one instance at least 98 // (the parallel remark) it has to be used (the parallel remark depends 99 // on the partitioning done in the previous parallel scavenge). 100 101 class SharedHeap : public CollectedHeap { 102 friend class VMStructs; 103 104 friend class VM_GC_Operation; 105 friend class VM_CGC_Operation; 106 public: 107 // The set of potentially parallel tasks in root scanning. 108 enum SH_process_roots_tasks { 109 SH_PS_Threads_oops_do, 110 SH_PS_StringTable_oops_do, 111 SH_PS_Universe_oops_do, 112 SH_PS_JNIHandles_oops_do, 113 SH_PS_ObjectSynchronizer_oops_do, 114 SH_PS_FlatProfiler_oops_do, 115 SH_PS_Management_oops_do, 116 SH_PS_SystemDictionary_oops_do, 117 SH_PS_ClassLoaderDataGraph_oops_do, 118 SH_PS_jvmti_oops_do, 119 SH_PS_CodeCache_oops_do, 120 // Leave this one last. 121 SH_PS_NumElements 122 }; 123 124 static const char* ext_roots_task_str(uint task); 125 private: 126 // For claiming strong_roots tasks. 127 SubTasksDone* _process_strong_tasks; 128 129 protected: 130 // There should be only a single instance of "SharedHeap" in a program. 131 // This is enforced with the protected constructor below, which will also 132 // set the static pointer "_sh" to that instance. 133 static SharedHeap* _sh; 134 135 // A gc policy, controls global gc resource issues 136 CollectorPolicy *_collector_policy; 137 138 // See the discussion below, in the specification of the reader function 139 // for this variable. 140 int _strong_roots_parity; 141 142 // If we're doing parallel GC, use this gang of threads. 143 FlexibleWorkGang* _workers; 144 145 // Full initialization is done in a concrete subtype's "initialize" 146 // function. 147 SharedHeap(CollectorPolicy* policy_); 148 149 // Returns true if the calling thread holds the heap lock, 150 // or the calling thread is a par gc thread and the heap_lock is held 151 // by the vm thread doing a gc operation. 152 bool heap_lock_held_for_gc(); 153 // True if the heap_lock is held by the a non-gc thread invoking a gc 154 // operation. 155 bool _thread_holds_heap_lock_for_gc; 156 157 public: 158 static SharedHeap* heap() { return _sh; } 159 160 void set_barrier_set(BarrierSet* bs); 161 SubTasksDone* process_strong_tasks() { return _process_strong_tasks; } 162 163 // Does operations required after initialization has been done. 164 virtual void post_initialize(); 165 166 // Initialization of ("weak") reference processing support 167 virtual void ref_processing_init(); 168 169 // Iteration functions. 170 void oop_iterate(ExtendedOopClosure* cl) = 0; 171 172 // Iterate over all spaces in use in the heap, in an undefined order. 173 virtual void space_iterate(SpaceClosure* cl) = 0; 174 175 // A SharedHeap will contain some number of spaces. This finds the 176 // space whose reserved area contains the given address, or else returns 177 // NULL. 178 virtual Space* space_containing(const void* addr) const = 0; 179 180 bool no_gc_in_progress() { return !is_gc_active(); } 181 182 // Some collectors will perform "process_strong_roots" in parallel. 183 // Such a call will involve claiming some fine-grained tasks, such as 184 // scanning of threads. To make this process simpler, we provide the 185 // "strong_roots_parity()" method. Collectors that start parallel tasks 186 // whose threads invoke "process_strong_roots" must 187 // call "change_strong_roots_parity" in sequential code starting such a 188 // task. (This also means that a parallel thread may only call 189 // process_strong_roots once.) 190 // 191 // For calls to process_roots by sequential code, the parity is 192 // updated automatically. 193 // 194 // The idea is that objects representing fine-grained tasks, such as 195 // threads, will contain a "parity" field. A task will is claimed in the 196 // current "process_roots" call only if its parity field is the 197 // same as the "strong_roots_parity"; task claiming is accomplished by 198 // updating the parity field to the strong_roots_parity with a CAS. 199 // 200 // If the client meats this spec, then strong_roots_parity() will have 201 // the following properties: 202 // a) to return a different value than was returned before the last 203 // call to change_strong_roots_parity, and 204 // c) to never return a distinguished value (zero) with which such 205 // task-claiming variables may be initialized, to indicate "never 206 // claimed". 207 public: 208 int strong_roots_parity() { return _strong_roots_parity; } 209 210 // Call these in sequential code around process_roots. 211 // strong_roots_prologue calls change_strong_roots_parity, if 212 // parallel tasks are enabled. 213 class StrongRootsScope : public MarkingCodeBlobClosure::MarkScope { 214 // Used to implement the Thread work barrier. 215 static Monitor* _lock; 216 217 SharedHeap* _sh; 218 volatile jint _n_workers_done_with_threads; 219 220 public: 221 StrongRootsScope(SharedHeap* heap, bool activate = true); 222 ~StrongRootsScope(); 223 224 // Mark that this thread is done with the Threads work. 225 void mark_worker_done_with_threads(uint n_workers); 226 // Wait until all n_workers are done with the Threads work. 227 void wait_until_all_workers_done_with_threads(uint n_workers); 228 }; 229 friend class StrongRootsScope; 230 231 // The current active StrongRootScope 232 StrongRootsScope* _strong_roots_scope; 233 234 StrongRootsScope* active_strong_roots_scope() const; 235 236 private: 237 void register_strong_roots_scope(StrongRootsScope* scope); 238 void unregister_strong_roots_scope(StrongRootsScope* scope); 239 void change_strong_roots_parity(); 240 241 public: 242 enum ScanningOption { 243 SO_None = 0x0, 244 SO_AllCodeCache = 0x8, 245 SO_ScavengeCodeCache = 0x10 246 }; 247 248 FlexibleWorkGang* workers() const { return _workers; } 249 250 // Invoke the "do_oop" method the closure "roots" on all root locations. 251 // The "so" argument determines which roots the closure is applied to: 252 // "SO_None" does none; 253 // "SO_AllCodeCache" applies the closure to all elements of the CodeCache. 254 // "SO_ScavengeCodeCache" applies the closure to elements on the scavenge root list in the CodeCache. 255 void process_roots(bool activate_scope, 256 ScanningOption so, 257 OopClosure* strong_roots, 258 OopClosure* weak_roots, 259 CLDClosure* strong_cld_closure, 260 CLDClosure* weak_cld_closure, 261 CodeBlobClosure* code_roots, 262 PhaseTimeData* phase_durations = NULL); 263 void process_all_roots(bool activate_scope, 264 ScanningOption so, 265 OopClosure* roots, 266 CLDClosure* cld_closure, 267 CodeBlobClosure* code_roots, 268 PhaseTimeData* phase_durations = NULL); 269 void process_strong_roots(bool activate_scope, 270 ScanningOption so, 271 OopClosure* roots, 272 CLDClosure* cld_closure, 273 CodeBlobClosure* code_roots, 274 PhaseTimeData* phase_durations = NULL); 275 276 277 // Apply "root_closure" to the JNI weak roots.. 278 void process_weak_roots(OopClosure* root_closure); 279 280 // The functions below are helper functions that a subclass of 281 // "SharedHeap" can use in the implementation of its virtual 282 // functions. 283 284 public: 285 286 // Do anything common to GC's. 287 virtual void gc_prologue(bool full) = 0; 288 virtual void gc_epilogue(bool full) = 0; 289 290 // Sets the number of parallel threads that will be doing tasks 291 // (such as process roots) subsequently. 292 virtual void set_par_threads(uint t); 293 294 int n_termination(); 295 void set_n_termination(int t); 296 297 // 298 // New methods from CollectedHeap 299 // 300 301 // Some utilities. 302 void print_size_transition(outputStream* out, 303 size_t bytes_before, 304 size_t bytes_after, 305 size_t capacity); 306 }; 307 308 inline SharedHeap::ScanningOption operator|(SharedHeap::ScanningOption so0, SharedHeap::ScanningOption so1) { 309 return static_cast<SharedHeap::ScanningOption>(static_cast<int>(so0) | static_cast<int>(so1)); 310 } 311 312 #endif // SHARE_VM_MEMORY_SHAREDHEAP_HPP