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_GENCOLLECTEDHEAP_HPP
26 #define SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP
27
28 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
29 #include "memory/collectorPolicy.hpp"
30 #include "memory/generation.hpp"
31 #include "memory/sharedHeap.hpp"
32
33 class SubTasksDone;
34
35 // A "GenCollectedHeap" is a SharedHeap that uses generational
36 // collection. It is represented with a sequence of Generation's.
37 class GenCollectedHeap : public SharedHeap {
38 friend class GenCollectorPolicy;
39 friend class Generation;
40 friend class DefNewGeneration;
41 friend class TenuredGeneration;
42 friend class ConcurrentMarkSweepGeneration;
43 friend class CMSCollector;
44 friend class GenMarkSweep;
45 friend class VM_GenCollectForAllocation;
46 friend class VM_GenCollectFull;
47 friend class VM_GenCollectFullConcurrent;
48 friend class VM_GC_HeapInspection;
49 friend class VM_HeapDumper;
50 friend class HeapInspection;
51 friend class GCCauseSetter;
52 friend class VMStructs;
53 public:
54 enum SomeConstants {
55 max_gens = 10
56 };
57
58 friend class VM_PopulateDumpSharedSpace;
59
60 protected:
61 // Fields:
62 static GenCollectedHeap* _gch;
63
64 private:
65 int _n_gens;
66 Generation* _gens[max_gens];
67 GenerationSpec** _gen_specs;
68
69 // The singleton Gen Remembered Set.
70 GenRemSet* _rem_set;
71
72 // The generational collector policy.
73 GenCollectorPolicy* _gen_policy;
74
75 // Indicates that the most recent previous incremental collection failed.
76 // The flag is cleared when an action is taken that might clear the
77 // condition that caused that incremental collection to fail.
78 bool _incremental_collection_failed;
79
80 // In support of ExplicitGCInvokesConcurrent functionality
81 unsigned int _full_collections_completed;
82
83 // Data structure for claiming the (potentially) parallel tasks in
84 // (gen-specific) roots processing.
85 SubTasksDone* _gen_process_roots_tasks;
86 SubTasksDone* gen_process_roots_tasks() { return _gen_process_roots_tasks; }
87
88 // In block contents verification, the number of header words to skip
89 NOT_PRODUCT(static size_t _skip_header_HeapWords;)
90
91 protected:
92 // Helper functions for allocation
93 HeapWord* attempt_allocation(size_t size,
94 bool is_tlab,
95 bool first_only);
96
97 // Helper function for two callbacks below.
98 // Considers collection of the first max_level+1 generations.
99 void do_collection(bool full,
100 bool clear_all_soft_refs,
101 size_t size,
102 bool is_tlab,
103 int max_level);
104
105 // Callback from VM_GenCollectForAllocation operation.
106 // This function does everything necessary/possible to satisfy an
107 // allocation request that failed in the youngest generation that should
108 // have handled it (including collection, expansion, etc.)
109 HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab);
110
111 // Callback from VM_GenCollectFull operation.
112 // Perform a full collection of the first max_level+1 generations.
113 virtual void do_full_collection(bool clear_all_soft_refs);
114 void do_full_collection(bool clear_all_soft_refs, int max_level);
115
116 // Does the "cause" of GC indicate that
117 // we absolutely __must__ clear soft refs?
118 bool must_clear_all_soft_refs();
119
120 public:
121 GenCollectedHeap(GenCollectorPolicy *policy);
122
123 GCStats* gc_stats(int level) const;
124
125 // Returns JNI_OK on success
126 virtual jint initialize();
127 char* allocate(size_t alignment, size_t* _total_reserved, ReservedSpace* heap_rs);
128
129 // Does operations required after initialization has been done.
130 void post_initialize();
131
132 // Initialize ("weak") refs processing support
133 virtual void ref_processing_init();
134
135 virtual CollectedHeap::Name kind() const {
136 return CollectedHeap::GenCollectedHeap;
137 }
138
139 // The generational collector policy.
140 GenCollectorPolicy* gen_policy() const { return _gen_policy; }
141 virtual CollectorPolicy* collector_policy() const { return (CollectorPolicy*) gen_policy(); }
142
143 // Adaptive size policy
144 virtual AdaptiveSizePolicy* size_policy() {
145 return gen_policy()->size_policy();
146 }
147
148 // Return the (conservative) maximum heap alignment
149 static size_t conservative_max_heap_alignment() {
150 return Generation::GenGrain;
151 }
152
153 size_t capacity() const;
154 size_t used() const;
155
156 // Save the "used_region" for generations level and lower.
157 void save_used_regions(int level);
158
159 size_t max_capacity() const;
160
290
291 // Ensure parsability: override
292 virtual void ensure_parsability(bool retire_tlabs);
293
294 // Time in ms since the longest time a collector ran in
295 // in any generation.
296 virtual jlong millis_since_last_gc();
297
298 // Total number of full collections completed.
299 unsigned int total_full_collections_completed() {
300 assert(_full_collections_completed <= _total_full_collections,
301 "Can't complete more collections than were started");
302 return _full_collections_completed;
303 }
304
305 // Update above counter, as appropriate, at the end of a stop-world GC cycle
306 unsigned int update_full_collections_completed();
307 // Update above counter, as appropriate, at the end of a concurrent GC cycle
308 unsigned int update_full_collections_completed(unsigned int count);
309
310 // Update "time of last gc" for all constituent generations
311 // to "now".
312 void update_time_of_last_gc(jlong now) {
313 for (int i = 0; i < _n_gens; i++) {
314 _gens[i]->update_time_of_last_gc(now);
315 }
316 }
317
318 // Update the gc statistics for each generation.
319 // "level" is the level of the latest collection.
320 void update_gc_stats(int current_level, bool full) {
321 for (int i = 0; i < _n_gens; i++) {
322 _gens[i]->update_gc_stats(current_level, full);
323 }
324 }
325
326 // Override.
327 bool no_gc_in_progress() { return !is_gc_active(); }
328
329 // Override.
330 void prepare_for_verify();
331
332 // Override.
333 void verify(bool silent, VerifyOption option);
334
335 // Override.
336 virtual void print_on(outputStream* st) const;
337 virtual void print_gc_threads_on(outputStream* st) const;
338 virtual void gc_threads_do(ThreadClosure* tc) const;
339 virtual void print_tracing_info() const;
340 virtual void print_on_error(outputStream* st) const;
341
342 // PrintGC, PrintGCDetails support
343 void print_heap_change(size_t prev_used) const;
347 // functions.
348
349 class GenClosure : public StackObj {
350 public:
351 virtual void do_generation(Generation* gen) = 0;
352 };
353
354 // Apply "cl.do_generation" to all generations in the heap
355 // If "old_to_young" determines the order.
356 void generation_iterate(GenClosure* cl, bool old_to_young);
357
358 void space_iterate(SpaceClosure* cl);
359
360 // Return "true" if all generations have reached the
361 // maximal committed limit that they can reach, without a garbage
362 // collection.
363 virtual bool is_maximal_no_gc() const;
364
365 // Return the generation before "gen".
366 Generation* prev_gen(Generation* gen) const {
367 int l = gen->level();
368 guarantee(l > 0, "Out of bounds");
369 return _gens[l-1];
370 }
371
372 // Return the generation after "gen".
373 Generation* next_gen(Generation* gen) const {
374 int l = gen->level() + 1;
375 guarantee(l < _n_gens, "Out of bounds");
376 return _gens[l];
377 }
378
379 Generation* get_gen(int i) const {
380 guarantee(i >= 0 && i < _n_gens, "Out of bounds");
381 return _gens[i];
382 }
383
384 int n_gens() const {
385 assert(_n_gens == gen_policy()->number_of_generations(), "Sanity");
386 return _n_gens;
387 }
388
389 // This function returns the "GenRemSet" object that allows us to scan
390 // generations in a fully generational heap.
391 GenRemSet* rem_set() { return _rem_set; }
392
393 // Convenience function to be used in situations where the heap type can be
394 // asserted to be this type.
395 static GenCollectedHeap* heap();
396
397 void set_par_threads(uint t);
398
399 // Invoke the "do_oop" method of one of the closures "not_older_gens"
400 // or "older_gens" on root locations for the generation at
401 // "level". (The "older_gens" closure is used for scanning references
|
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_GENCOLLECTEDHEAP_HPP
26 #define SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP
27
28 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
29 #include "memory/collectorPolicy.hpp"
30 #include "memory/generation.hpp"
31 #include "memory/sharedHeap.hpp"
32
33 class SubTasksDone;
34
35 // A "GenCollectedHeap" is a SharedHeap that uses generational
36 // collection. It has two generations, young and old.
37 class GenCollectedHeap : public SharedHeap {
38 friend class GenCollectorPolicy;
39 friend class Generation;
40 friend class DefNewGeneration;
41 friend class TenuredGeneration;
42 friend class ConcurrentMarkSweepGeneration;
43 friend class CMSCollector;
44 friend class GenMarkSweep;
45 friend class VM_GenCollectForAllocation;
46 friend class VM_GenCollectFull;
47 friend class VM_GenCollectFullConcurrent;
48 friend class VM_GC_HeapInspection;
49 friend class VM_HeapDumper;
50 friend class HeapInspection;
51 friend class GCCauseSetter;
52 friend class VMStructs;
53 public:
54 enum SomeConstants {
55 max_gens = 10
56 };
57
58 friend class VM_PopulateDumpSharedSpace;
59
60 protected:
61 // Fields:
62 static GenCollectedHeap* _gch;
63
64 private:
65 int _n_gens;
66
67 Generation* _young_gen;
68 Generation* _old_gen;
69
70 GenerationSpec** _gen_specs;
71
72 // The singleton Gen Remembered Set.
73 GenRemSet* _rem_set;
74
75 // The generational collector policy.
76 GenCollectorPolicy* _gen_policy;
77
78 // Indicates that the most recent previous incremental collection failed.
79 // The flag is cleared when an action is taken that might clear the
80 // condition that caused that incremental collection to fail.
81 bool _incremental_collection_failed;
82
83 // In support of ExplicitGCInvokesConcurrent functionality
84 unsigned int _full_collections_completed;
85
86 // Data structure for claiming the (potentially) parallel tasks in
87 // (gen-specific) roots processing.
88 SubTasksDone* _gen_process_roots_tasks;
89 SubTasksDone* gen_process_roots_tasks() { return _gen_process_roots_tasks; }
90
91 // Collects the given generation.
92 void collect_generation(Generation* gen, bool full, size_t size, bool is_tlab,
93 bool run_verification, bool clear_soft_refs,
94 bool restore_marks_for_biased_locking);
95
96 // In block contents verification, the number of header words to skip
97 NOT_PRODUCT(static size_t _skip_header_HeapWords;)
98
99 protected:
100 // Helper functions for allocation
101 HeapWord* attempt_allocation(size_t size,
102 bool is_tlab,
103 bool first_only);
104
105 // Helper function for two callbacks below.
106 // Considers collection of the first max_level+1 generations.
107 void do_collection(bool full,
108 bool clear_all_soft_refs,
109 size_t size,
110 bool is_tlab,
111 int max_level);
112
113 // Callback from VM_GenCollectForAllocation operation.
114 // This function does everything necessary/possible to satisfy an
115 // allocation request that failed in the youngest generation that should
116 // have handled it (including collection, expansion, etc.)
117 HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab);
118
119 // Callback from VM_GenCollectFull operation.
120 // Perform a full collection of the first max_level+1 generations.
121 virtual void do_full_collection(bool clear_all_soft_refs);
122 void do_full_collection(bool clear_all_soft_refs, int max_level);
123
124 // Does the "cause" of GC indicate that
125 // we absolutely __must__ clear soft refs?
126 bool must_clear_all_soft_refs();
127
128 public:
129 GenCollectedHeap(GenCollectorPolicy *policy);
130
131 GCStats* gc_stats(int level) const;
132
133 // Returns JNI_OK on success
134 virtual jint initialize();
135
136 char* allocate(size_t alignment, size_t* _total_reserved, ReservedSpace* heap_rs);
137
138 // Does operations required after initialization has been done.
139 void post_initialize();
140
141 // Initialize ("weak") refs processing support
142 virtual void ref_processing_init();
143
144 virtual CollectedHeap::Name kind() const {
145 return CollectedHeap::GenCollectedHeap;
146 }
147
148 Generation* young_gen() { return _young_gen; }
149 Generation* old_gen() { return _old_gen; }
150
151 // The generational collector policy.
152 GenCollectorPolicy* gen_policy() const { return _gen_policy; }
153
154 virtual CollectorPolicy* collector_policy() const { return (CollectorPolicy*) gen_policy(); }
155
156 // Adaptive size policy
157 virtual AdaptiveSizePolicy* size_policy() {
158 return gen_policy()->size_policy();
159 }
160
161 // Return the (conservative) maximum heap alignment
162 static size_t conservative_max_heap_alignment() {
163 return Generation::GenGrain;
164 }
165
166 size_t capacity() const;
167 size_t used() const;
168
169 // Save the "used_region" for generations level and lower.
170 void save_used_regions(int level);
171
172 size_t max_capacity() const;
173
303
304 // Ensure parsability: override
305 virtual void ensure_parsability(bool retire_tlabs);
306
307 // Time in ms since the longest time a collector ran in
308 // in any generation.
309 virtual jlong millis_since_last_gc();
310
311 // Total number of full collections completed.
312 unsigned int total_full_collections_completed() {
313 assert(_full_collections_completed <= _total_full_collections,
314 "Can't complete more collections than were started");
315 return _full_collections_completed;
316 }
317
318 // Update above counter, as appropriate, at the end of a stop-world GC cycle
319 unsigned int update_full_collections_completed();
320 // Update above counter, as appropriate, at the end of a concurrent GC cycle
321 unsigned int update_full_collections_completed(unsigned int count);
322
323 // Update "time of last gc" for all generations to "now".
324 void update_time_of_last_gc(jlong now) {
325 _young_gen->update_time_of_last_gc(now);
326 _old_gen->update_time_of_last_gc(now);
327 }
328
329 // Update the gc statistics for each generation.
330 // "level" is the level of the latest collection.
331 void update_gc_stats(int current_level, bool full) {
332 _young_gen->update_gc_stats(current_level, full);
333 _old_gen->update_gc_stats(current_level, full);
334 }
335
336 // Override.
337 bool no_gc_in_progress() { return !is_gc_active(); }
338
339 // Override.
340 void prepare_for_verify();
341
342 // Override.
343 void verify(bool silent, VerifyOption option);
344
345 // Override.
346 virtual void print_on(outputStream* st) const;
347 virtual void print_gc_threads_on(outputStream* st) const;
348 virtual void gc_threads_do(ThreadClosure* tc) const;
349 virtual void print_tracing_info() const;
350 virtual void print_on_error(outputStream* st) const;
351
352 // PrintGC, PrintGCDetails support
353 void print_heap_change(size_t prev_used) const;
357 // functions.
358
359 class GenClosure : public StackObj {
360 public:
361 virtual void do_generation(Generation* gen) = 0;
362 };
363
364 // Apply "cl.do_generation" to all generations in the heap
365 // If "old_to_young" determines the order.
366 void generation_iterate(GenClosure* cl, bool old_to_young);
367
368 void space_iterate(SpaceClosure* cl);
369
370 // Return "true" if all generations have reached the
371 // maximal committed limit that they can reach, without a garbage
372 // collection.
373 virtual bool is_maximal_no_gc() const;
374
375 // Return the generation before "gen".
376 Generation* prev_gen(Generation* gen) const {
377 guarantee(gen->level() == 1, "Out of bounds");
378 return _young_gen;
379 }
380
381 // Return the generation after "gen".
382 Generation* next_gen(Generation* gen) const {
383 guarantee(gen->level() == 0, "Out of bounds");
384 return _old_gen;
385 }
386
387 Generation* get_gen(int i) const {
388 guarantee(i >= 0 && i < _n_gens, "Out of bounds");
389 if (i == 0) {
390 return _young_gen;
391 } else {
392 return _old_gen;
393 }
394 }
395
396 int n_gens() const {
397 assert(_n_gens == gen_policy()->number_of_generations(), "Sanity");
398 return _n_gens;
399 }
400
401 // This function returns the "GenRemSet" object that allows us to scan
402 // generations in a fully generational heap.
403 GenRemSet* rem_set() { return _rem_set; }
404
405 // Convenience function to be used in situations where the heap type can be
406 // asserted to be this type.
407 static GenCollectedHeap* heap();
408
409 void set_par_threads(uint t);
410
411 // Invoke the "do_oop" method of one of the closures "not_older_gens"
412 // or "older_gens" on root locations for the generation at
413 // "level". (The "older_gens" closure is used for scanning references
|