1 /*
2 * Copyright (c) 1997, 2017, 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 #include "precompiled.hpp"
26 #include "gc/shared/blockOffsetTable.inline.hpp"
27 #include "gc/shared/cardTableRS.hpp"
28 #include "gc/shared/collectedHeap.inline.hpp"
29 #include "gc/shared/gcLocker.hpp"
30 #include "gc/shared/gcTimer.hpp"
31 #include "gc/shared/gcTrace.hpp"
32 #include "gc/shared/genCollectedHeap.hpp"
33 #include "gc/shared/genOopClosures.hpp"
34 #include "gc/shared/genOopClosures.inline.hpp"
35 #include "gc/shared/generation.hpp"
36 #include "gc/shared/space.inline.hpp"
37 #include "gc/shared/spaceDecorator.hpp"
38 #include "logging/log.hpp"
39 #include "memory/allocation.inline.hpp"
40 #include "oops/oop.inline.hpp"
41 #include "runtime/java.hpp"
42 #include "utilities/copy.hpp"
43 #include "utilities/events.hpp"
44
Generation(ReservedSpace rs,size_t initial_size)45 Generation::Generation(ReservedSpace rs, size_t initial_size) :
46 _ref_processor(NULL),
47 _gc_manager(NULL) {
48 if (!_virtual_space.initialize(rs, initial_size)) {
49 vm_exit_during_initialization("Could not reserve enough space for "
50 "object heap");
51 }
52 // Mangle all of the the initial generation.
53 if (ZapUnusedHeapArea) {
54 MemRegion mangle_region((HeapWord*)_virtual_space.low(),
55 (HeapWord*)_virtual_space.high());
56 SpaceMangler::mangle_region(mangle_region);
57 }
58 _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(),
59 (HeapWord*)_virtual_space.high_boundary());
60 }
61
initial_size()62 size_t Generation::initial_size() {
63 GenCollectedHeap* gch = GenCollectedHeap::heap();
64 if (gch->is_young_gen(this)) {
65 return gch->young_gen_spec()->init_size();
66 }
67 return gch->old_gen_spec()->init_size();
68 }
69
70 // This is for CMS. It returns stable monotonic used space size.
71 // Remove this when CMS is removed.
used_stable() const72 size_t Generation::used_stable() const {
73 return used();
74 }
75
max_capacity() const76 size_t Generation::max_capacity() const {
77 return reserved().byte_size();
78 }
79
80 // By default we get a single threaded default reference processor;
81 // generations needing multi-threaded refs processing or discovery override this method.
ref_processor_init()82 void Generation::ref_processor_init() {
83 assert(_ref_processor == NULL, "a reference processor already exists");
84 assert(!_reserved.is_empty(), "empty generation?");
85 _span_based_discoverer.set_span(_reserved);
86 _ref_processor = new ReferenceProcessor(&_span_based_discoverer); // a vanilla reference processor
87 if (_ref_processor == NULL) {
88 vm_exit_during_initialization("Could not allocate ReferenceProcessor object");
89 }
90 }
91
print() const92 void Generation::print() const { print_on(tty); }
93
print_on(outputStream * st) const94 void Generation::print_on(outputStream* st) const {
95 st->print(" %-20s", name());
96 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
97 capacity()/K, used()/K);
98 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
99 p2i(_virtual_space.low_boundary()),
100 p2i(_virtual_space.high()),
101 p2i(_virtual_space.high_boundary()));
102 }
103
print_summary_info_on(outputStream * st)104 void Generation::print_summary_info_on(outputStream* st) {
105 StatRecord* sr = stat_record();
106 double time = sr->accumulated_time.seconds();
107 st->print_cr("Accumulated %s generation GC time %3.7f secs, "
108 "%u GC's, avg GC time %3.7f",
109 GenCollectedHeap::heap()->is_young_gen(this) ? "young" : "old" ,
110 time,
111 sr->invocations,
112 sr->invocations > 0 ? time / sr->invocations : 0.0);
113 }
114
115 // Utility iterator classes
116
117 class GenerationIsInReservedClosure : public SpaceClosure {
118 public:
119 const void* _p;
120 Space* sp;
do_space(Space * s)121 virtual void do_space(Space* s) {
122 if (sp == NULL) {
123 if (s->is_in_reserved(_p)) sp = s;
124 }
125 }
GenerationIsInReservedClosure(const void * p)126 GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {}
127 };
128
129 class GenerationIsInClosure : public SpaceClosure {
130 public:
131 const void* _p;
132 Space* sp;
do_space(Space * s)133 virtual void do_space(Space* s) {
134 if (sp == NULL) {
135 if (s->is_in(_p)) sp = s;
136 }
137 }
GenerationIsInClosure(const void * p)138 GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {}
139 };
140
is_in(const void * p) const141 bool Generation::is_in(const void* p) const {
142 GenerationIsInClosure blk(p);
143 ((Generation*)this)->space_iterate(&blk);
144 return blk.sp != NULL;
145 }
146
max_contiguous_available() const147 size_t Generation::max_contiguous_available() const {
148 // The largest number of contiguous free words in this or any higher generation.
149 size_t avail = contiguous_available();
150 size_t old_avail = 0;
151 if (GenCollectedHeap::heap()->is_young_gen(this)) {
152 old_avail = GenCollectedHeap::heap()->old_gen()->contiguous_available();
153 }
154 return MAX2(avail, old_avail);
155 }
156
promotion_attempt_is_safe(size_t max_promotion_in_bytes) const157 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
158 size_t available = max_contiguous_available();
159 bool res = (available >= max_promotion_in_bytes);
160 log_trace(gc)("Generation: promo attempt is%s safe: available(" SIZE_FORMAT ") %s max_promo(" SIZE_FORMAT ")",
161 res? "":" not", available, res? ">=":"<", max_promotion_in_bytes);
162 return res;
163 }
164
165 // Ignores "ref" and calls allocate().
promote(oop obj,size_t obj_size)166 oop Generation::promote(oop obj, size_t obj_size) {
167 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
168
169 #ifndef PRODUCT
170 if (GenCollectedHeap::heap()->promotion_should_fail()) {
171 return NULL;
172 }
173 #endif // #ifndef PRODUCT
174
175 HeapWord* result = allocate(obj_size, false);
176 if (result != NULL) {
177 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
178 return oop(result);
179 } else {
180 GenCollectedHeap* gch = GenCollectedHeap::heap();
181 return gch->handle_failed_promotion(this, obj, obj_size);
182 }
183 }
184
par_promote(int thread_num,oop obj,markOop m,size_t word_sz)185 oop Generation::par_promote(int thread_num,
186 oop obj, markOop m, size_t word_sz) {
187 // Could do a bad general impl here that gets a lock. But no.
188 ShouldNotCallThis();
189 return NULL;
190 }
191
space_containing(const void * p) const192 Space* Generation::space_containing(const void* p) const {
193 GenerationIsInReservedClosure blk(p);
194 // Cast away const
195 ((Generation*)this)->space_iterate(&blk);
196 return blk.sp;
197 }
198
199 // Some of these are mediocre general implementations. Should be
200 // overridden to get better performance.
201
202 class GenerationBlockStartClosure : public SpaceClosure {
203 public:
204 const void* _p;
205 HeapWord* _start;
do_space(Space * s)206 virtual void do_space(Space* s) {
207 if (_start == NULL && s->is_in_reserved(_p)) {
208 _start = s->block_start(_p);
209 }
210 }
GenerationBlockStartClosure(const void * p)211 GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; }
212 };
213
block_start(const void * p) const214 HeapWord* Generation::block_start(const void* p) const {
215 GenerationBlockStartClosure blk(p);
216 // Cast away const
217 ((Generation*)this)->space_iterate(&blk);
218 return blk._start;
219 }
220
221 class GenerationBlockSizeClosure : public SpaceClosure {
222 public:
223 const HeapWord* _p;
224 size_t size;
do_space(Space * s)225 virtual void do_space(Space* s) {
226 if (size == 0 && s->is_in_reserved(_p)) {
227 size = s->block_size(_p);
228 }
229 }
GenerationBlockSizeClosure(const HeapWord * p)230 GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; }
231 };
232
block_size(const HeapWord * p) const233 size_t Generation::block_size(const HeapWord* p) const {
234 GenerationBlockSizeClosure blk(p);
235 // Cast away const
236 ((Generation*)this)->space_iterate(&blk);
237 assert(blk.size > 0, "seems reasonable");
238 return blk.size;
239 }
240
241 class GenerationBlockIsObjClosure : public SpaceClosure {
242 public:
243 const HeapWord* _p;
244 bool is_obj;
do_space(Space * s)245 virtual void do_space(Space* s) {
246 if (!is_obj && s->is_in_reserved(_p)) {
247 is_obj |= s->block_is_obj(_p);
248 }
249 }
GenerationBlockIsObjClosure(const HeapWord * p)250 GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; }
251 };
252
block_is_obj(const HeapWord * p) const253 bool Generation::block_is_obj(const HeapWord* p) const {
254 GenerationBlockIsObjClosure blk(p);
255 // Cast away const
256 ((Generation*)this)->space_iterate(&blk);
257 return blk.is_obj;
258 }
259
260 class GenerationOopIterateClosure : public SpaceClosure {
261 public:
262 OopIterateClosure* _cl;
do_space(Space * s)263 virtual void do_space(Space* s) {
264 s->oop_iterate(_cl);
265 }
GenerationOopIterateClosure(OopIterateClosure * cl)266 GenerationOopIterateClosure(OopIterateClosure* cl) :
267 _cl(cl) {}
268 };
269
oop_iterate(OopIterateClosure * cl)270 void Generation::oop_iterate(OopIterateClosure* cl) {
271 GenerationOopIterateClosure blk(cl);
272 space_iterate(&blk);
273 }
274
younger_refs_in_space_iterate(Space * sp,OopsInGenClosure * cl,uint n_threads)275 void Generation::younger_refs_in_space_iterate(Space* sp,
276 OopsInGenClosure* cl,
277 uint n_threads) {
278 CardTableRS* rs = GenCollectedHeap::heap()->rem_set();
279 rs->younger_refs_in_space_iterate(sp, cl, n_threads);
280 }
281
282 class GenerationObjIterateClosure : public SpaceClosure {
283 private:
284 ObjectClosure* _cl;
285 public:
do_space(Space * s)286 virtual void do_space(Space* s) {
287 s->object_iterate(_cl);
288 }
GenerationObjIterateClosure(ObjectClosure * cl)289 GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
290 };
291
object_iterate(ObjectClosure * cl)292 void Generation::object_iterate(ObjectClosure* cl) {
293 GenerationObjIterateClosure blk(cl);
294 space_iterate(&blk);
295 }
296
297 class GenerationSafeObjIterateClosure : public SpaceClosure {
298 private:
299 ObjectClosure* _cl;
300 public:
do_space(Space * s)301 virtual void do_space(Space* s) {
302 s->safe_object_iterate(_cl);
303 }
GenerationSafeObjIterateClosure(ObjectClosure * cl)304 GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
305 };
306
safe_object_iterate(ObjectClosure * cl)307 void Generation::safe_object_iterate(ObjectClosure* cl) {
308 GenerationSafeObjIterateClosure blk(cl);
309 space_iterate(&blk);
310 }
311
312 #if INCLUDE_SERIALGC
313
prepare_for_compaction(CompactPoint * cp)314 void Generation::prepare_for_compaction(CompactPoint* cp) {
315 // Generic implementation, can be specialized
316 CompactibleSpace* space = first_compaction_space();
317 while (space != NULL) {
318 space->prepare_for_compaction(cp);
319 space = space->next_compaction_space();
320 }
321 }
322
323 class AdjustPointersClosure: public SpaceClosure {
324 public:
do_space(Space * sp)325 void do_space(Space* sp) {
326 sp->adjust_pointers();
327 }
328 };
329
adjust_pointers()330 void Generation::adjust_pointers() {
331 // Note that this is done over all spaces, not just the compactible
332 // ones.
333 AdjustPointersClosure blk;
334 space_iterate(&blk, true);
335 }
336
compact()337 void Generation::compact() {
338 CompactibleSpace* sp = first_compaction_space();
339 while (sp != NULL) {
340 sp->compact();
341 sp = sp->next_compaction_space();
342 }
343 }
344
345 #endif // INCLUDE_SERIALGC
346