Wed, 26 Jun 2013 16:58:37 +0200
8013590: NPG: Add a memory pool MXBean for Metaspace
Reviewed-by: jmasa, mgerdin
1 /*
2 * Copyright (c) 2000, 2012, 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 */
25 #ifndef SHARE_VM_MEMORY_SHAREDHEAP_HPP
26 #define SHARE_VM_MEMORY_SHAREDHEAP_HPP
28 #include "gc_interface/collectedHeap.hpp"
29 #include "memory/generation.hpp"
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.
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;
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_strong_roots() calls
73 // process_strong_roots(false, // no scoping; this is parallel code
74 // is_scavenging, so,
75 // &buf_scan_non_heap_roots,
76 // &eager_scan_code_roots);
77 // which delegates to SharedHeap::process_strong_roots() and uses
78 // SubTasksDone* _process_strong_tasks to claim tasks.
79 // process_strong_roots() calls
80 // rem_set()->younger_refs_iterate()
81 // to scan the card table and which eventually calls down into
82 // CardTableModRefBS::par_non_clean_card_iterate_work(). This method
83 // uses SequentialSubTasksDone* _pst to claim tasks.
84 // Both SubTasksDone and SequentialSubTasksDone call their method
85 // all_tasks_completed() to count the number of GC workers that have
86 // finished their work. That logic is "when all the workers are
87 // finished the tasks are finished".
88 //
89 // The pattern that appears in the code is to set _n_threads
90 // to a value > 1 before a task that you would like executed in parallel
91 // and then to set it to 0 after that task has completed. A value of
92 // 0 is a "special" value in set_n_threads() which translates to
93 // setting _n_threads to 1.
94 //
95 // Some code uses _n_terminiation to decide if work should be done in
96 // parallel. The notorious possibly_parallel_oops_do() in threads.cpp
97 // is an example of such code. Look for variable "is_par" for other
98 // examples.
99 //
100 // The active_workers is not reset to 0 after a parallel phase. It's
101 // value may be used in later phases and in one instance at least
102 // (the parallel remark) it has to be used (the parallel remark depends
103 // on the partitioning done in the previous parallel scavenge).
105 class SharedHeap : public CollectedHeap {
106 friend class VMStructs;
108 friend class VM_GC_Operation;
109 friend class VM_CGC_Operation;
111 private:
112 // For claiming strong_roots tasks.
113 SubTasksDone* _process_strong_tasks;
115 protected:
116 // There should be only a single instance of "SharedHeap" in a program.
117 // This is enforced with the protected constructor below, which will also
118 // set the static pointer "_sh" to that instance.
119 static SharedHeap* _sh;
121 // and the Gen Remembered Set, at least one good enough to scan the perm
122 // gen.
123 GenRemSet* _rem_set;
125 // A gc policy, controls global gc resource issues
126 CollectorPolicy *_collector_policy;
128 // See the discussion below, in the specification of the reader function
129 // for this variable.
130 int _strong_roots_parity;
132 // If we're doing parallel GC, use this gang of threads.
133 FlexibleWorkGang* _workers;
135 // Full initialization is done in a concrete subtype's "initialize"
136 // function.
137 SharedHeap(CollectorPolicy* policy_);
139 // Returns true if the calling thread holds the heap lock,
140 // or the calling thread is a par gc thread and the heap_lock is held
141 // by the vm thread doing a gc operation.
142 bool heap_lock_held_for_gc();
143 // True if the heap_lock is held by the a non-gc thread invoking a gc
144 // operation.
145 bool _thread_holds_heap_lock_for_gc;
147 public:
148 static SharedHeap* heap() { return _sh; }
150 void set_barrier_set(BarrierSet* bs);
151 SubTasksDone* process_strong_tasks() { return _process_strong_tasks; }
153 // Does operations required after initialization has been done.
154 virtual void post_initialize();
156 // Initialization of ("weak") reference processing support
157 virtual void ref_processing_init();
159 // This function returns the "GenRemSet" object that allows us to scan
160 // generations in a fully generational heap.
161 GenRemSet* rem_set() { return _rem_set; }
163 // Iteration functions.
164 void oop_iterate(ExtendedOopClosure* cl) = 0;
166 // Same as above, restricted to a memory region.
167 virtual void oop_iterate(MemRegion mr, ExtendedOopClosure* cl) = 0;
169 // Iterate over all objects allocated since the last collection, calling
170 // "cl->do_object" on each. The heap must have been initialized properly
171 // to support this function, or else this call will fail.
172 virtual void object_iterate_since_last_GC(ObjectClosure* cl) = 0;
174 // Iterate over all spaces in use in the heap, in an undefined order.
175 virtual void space_iterate(SpaceClosure* cl) = 0;
177 // A SharedHeap will contain some number of spaces. This finds the
178 // space whose reserved area contains the given address, or else returns
179 // NULL.
180 virtual Space* space_containing(const void* addr) const = 0;
182 bool no_gc_in_progress() { return !is_gc_active(); }
184 // Some collectors will perform "process_strong_roots" in parallel.
185 // Such a call will involve claiming some fine-grained tasks, such as
186 // scanning of threads. To make this process simpler, we provide the
187 // "strong_roots_parity()" method. Collectors that start parallel tasks
188 // whose threads invoke "process_strong_roots" must
189 // call "change_strong_roots_parity" in sequential code starting such a
190 // task. (This also means that a parallel thread may only call
191 // process_strong_roots once.)
192 //
193 // For calls to process_strong_roots by sequential code, the parity is
194 // updated automatically.
195 //
196 // The idea is that objects representing fine-grained tasks, such as
197 // threads, will contain a "parity" field. A task will is claimed in the
198 // current "process_strong_roots" call only if its parity field is the
199 // same as the "strong_roots_parity"; task claiming is accomplished by
200 // updating the parity field to the strong_roots_parity with a CAS.
201 //
202 // If the client meats this spec, then strong_roots_parity() will have
203 // the following properties:
204 // a) to return a different value than was returned before the last
205 // call to change_strong_roots_parity, and
206 // c) to never return a distinguished value (zero) with which such
207 // task-claiming variables may be initialized, to indicate "never
208 // claimed".
209 private:
210 void change_strong_roots_parity();
211 public:
212 int strong_roots_parity() { return _strong_roots_parity; }
214 // Call these in sequential code around process_strong_roots.
215 // strong_roots_prologue calls change_strong_roots_parity, if
216 // parallel tasks are enabled.
217 class StrongRootsScope : public MarkingCodeBlobClosure::MarkScope {
218 public:
219 StrongRootsScope(SharedHeap* outer, bool activate = true);
220 ~StrongRootsScope();
221 };
222 friend class StrongRootsScope;
224 enum ScanningOption {
225 SO_None = 0x0,
226 SO_AllClasses = 0x1,
227 SO_SystemClasses = 0x2,
228 SO_Strings = 0x4,
229 SO_CodeCache = 0x8
230 };
232 FlexibleWorkGang* workers() const { return _workers; }
234 // Invoke the "do_oop" method the closure "roots" on all root locations.
235 // The "so" argument determines which roots the closure is applied to:
236 // "SO_None" does none;
237 // "SO_AllClasses" applies the closure to all entries in the SystemDictionary;
238 // "SO_SystemClasses" to all the "system" classes and loaders;
239 // "SO_Strings" applies the closure to all entries in StringTable;
240 // "SO_CodeCache" applies the closure to all elements of the CodeCache.
241 void process_strong_roots(bool activate_scope,
242 bool is_scavenging,
243 ScanningOption so,
244 OopClosure* roots,
245 CodeBlobClosure* code_roots,
246 KlassClosure* klass_closure);
248 // Apply "blk" to all the weak roots of the system. These include
249 // JNI weak roots, the code cache, system dictionary, symbol table,
250 // string table.
251 void process_weak_roots(OopClosure* root_closure,
252 CodeBlobClosure* code_roots);
254 // The functions below are helper functions that a subclass of
255 // "SharedHeap" can use in the implementation of its virtual
256 // functions.
258 public:
260 // Do anything common to GC's.
261 virtual void gc_prologue(bool full) = 0;
262 virtual void gc_epilogue(bool full) = 0;
264 // Sets the number of parallel threads that will be doing tasks
265 // (such as process strong roots) subsequently.
266 virtual void set_par_threads(uint t);
268 int n_termination();
269 void set_n_termination(int t);
271 //
272 // New methods from CollectedHeap
273 //
275 // Some utilities.
276 void print_size_transition(outputStream* out,
277 size_t bytes_before,
278 size_t bytes_after,
279 size_t capacity);
280 };
282 #endif // SHARE_VM_MEMORY_SHAREDHEAP_HPP