1 /*
2 * Copyright (c) 2013, 2019, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP
27 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP
28
29 #include "gc/shared/gc_globals.hpp"
30 #include "gc/shared/spaceDecorator.hpp"
31 #include "gc/shenandoah/shenandoahAffiliation.hpp"
32 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
33 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
34 #include "gc/shenandoah/shenandoahAsserts.hpp"
35 #include "gc/shenandoah/shenandoahHeap.hpp"
36 #include "gc/shenandoah/shenandoahPadding.hpp"
37 #include "runtime/atomic.hpp"
38 #include "utilities/sizes.hpp"
39
40 class VMStructs;
41 class ShenandoahHeapRegionStateConstant;
42
43 class ShenandoahHeapRegion {
44 friend class VMStructs;
45 friend class ShenandoahHeapRegionStateConstant;
46 private:
47
48 /*
49 Region state is described by a state machine. Transitions are guarded by
50 heap lock, which allows changing the state of several regions atomically.
51 Region states can be logically aggregated in groups.
52
53 "Empty":
54 .................................................................
55 . .
56 . .
57 . Uncommitted <------- Committed <------------------------\
58 . | | . |
59 . \---------v-----------/ . |
60 . | . |
61 .........................|....................................... |
62 | |
63 "Active": | |
64 .........................|....................................... |
65 . | . |
66 . /-----------------^-------------------\ . |
67 . | | . |
68 . v v "Humongous": . |
69 . Regular ---\-----\ ..................O................ . |
70 . | ^ | | . | . . |
71 . | | | | . *---------\ . . |
72 . v | | | . v v . . |
73 . Pinned Cset | . HStart <--> H/Start H/Cont . . |
74 . ^ / | | . Pinned v | . . |
75 . | / | | . *<--------/ . . |
76 . | v | | . | . . |
77 . CsetPinned | | ..................O................ . |
78 . | | | . |
79 . \-----\---v-------------------/ . |
80 . | . |
81 .........................|....................................... |
82 | |
83 "Trash": | |
84 .........................|....................................... |
85 . | . |
86 . v . |
87 . Trash ---------------------------------------/
88 . .
89 . .
90 .................................................................
91
92 Transition from "Empty" to "Active" is first allocation. It can go from {Uncommitted, Committed}
93 to {Regular, "Humongous"}. The allocation may happen in Regular regions too, but not in Humongous.
94
95 Transition from "Active" to "Trash" is reclamation. It can go from CSet during the normal cycle,
96 and from {Regular, "Humongous"} for immediate reclamation. The existence of Trash state allows
97 quick reclamation without actual cleaning up.
98
99 Transition from "Trash" to "Empty" is recycling. It cleans up the regions and corresponding metadata.
100 Can be done asynchronously and in bulk.
101
102 Note how internal transitions disallow logic bugs:
103 a) No region can go Empty, unless properly reclaimed/recycled;
104 b) No region can go Uncommitted, unless reclaimed/recycled first;
105 c) Only Regular regions can go to CSet;
106 d) Pinned cannot go Trash, thus it could never be reclaimed until unpinned;
107 e) Pinned cannot go CSet, thus it never moves;
108 f) Humongous cannot be used for regular allocations;
109 g) Humongous cannot go CSet, thus it never moves;
110 h) Humongous start can go pinned, and thus can be protected from moves (humongous continuations should
111 follow associated humongous starts, not pinnable/movable by themselves);
112 i) Empty cannot go Trash, avoiding useless work;
113 j) ...
114 */
115
116 enum RegionState {
117 _empty_uncommitted, // region is empty and has memory uncommitted
118 _empty_committed, // region is empty and has memory committed
119 _regular, // region is for regular allocations
120 _humongous_start, // region is the humongous start
121 _humongous_cont, // region is the humongous continuation
122 _pinned_humongous_start, // region is both humongous start and pinned
123 _cset, // region is in collection set
124 _pinned, // region is pinned
125 _pinned_cset, // region is pinned and in cset (evac failure path)
126 _trash, // region contains only trash
127 _REGION_STATES_NUM // last
128 };
129
130 public:
131 static const char* region_state_to_string(RegionState s) {
132 switch (s) {
133 case _empty_uncommitted: return "Empty Uncommitted";
134 case _empty_committed: return "Empty Committed";
135 case _regular: return "Regular";
136 case _humongous_start: return "Humongous Start";
137 case _humongous_cont: return "Humongous Continuation";
138 case _pinned_humongous_start: return "Humongous Start, Pinned";
139 case _cset: return "Collection Set";
140 case _pinned: return "Pinned";
141 case _pinned_cset: return "Collection Set, Pinned";
142 case _trash: return "Trash";
143 default:
144 ShouldNotReachHere();
145 return "";
146 }
147 }
148
149 private:
150 // This method protects from accidental changes in enum order:
151 int region_state_to_ordinal(RegionState s) const {
152 switch (s) {
153 case _empty_uncommitted: return 0;
154 case _empty_committed: return 1;
155 case _regular: return 2;
156 case _humongous_start: return 3;
157 case _humongous_cont: return 4;
158 case _cset: return 5;
159 case _pinned: return 6;
160 case _trash: return 7;
161 case _pinned_cset: return 8;
162 case _pinned_humongous_start: return 9;
163 default:
164 ShouldNotReachHere();
165 return -1;
166 }
167 }
168
169 void report_illegal_transition(const char* method);
170 void recycle_internal();
171
172 public:
173 static int region_states_num() {
174 return _REGION_STATES_NUM;
175 }
176
177 // Allowed transitions from the outside code:
178 void make_regular_allocation(ShenandoahAffiliation affiliation);
179 void make_affiliated_maybe();
180 void make_regular_bypass();
181 void make_humongous_start();
182 void make_humongous_cont();
183 void make_humongous_start_bypass(ShenandoahAffiliation affiliation);
184 void make_humongous_cont_bypass(ShenandoahAffiliation affiliation);
185 void make_pinned();
186 void make_unpinned();
187 void make_cset();
188 void make_trash();
189 void make_trash_immediate();
190 void make_empty();
191 void make_uncommitted();
192 void make_committed_bypass();
193
194 // Primitive state predicates
195 bool is_empty_uncommitted() const { return state() == _empty_uncommitted; }
196 bool is_empty_committed() const { return state() == _empty_committed; }
197 bool is_regular() const { return state() == _regular; }
198 bool is_humongous_continuation() const { return state() == _humongous_cont; }
199 bool is_regular_pinned() const { return state() == _pinned; }
200 bool is_trash() const { return is_trash(state()); }
201
202 // Derived state predicates (boolean combinations of individual states)
203 bool static is_trash(RegionState state) { return state == _trash; }
204 bool static is_empty_state(RegionState state) { return state == _empty_committed || state == _empty_uncommitted; }
205 bool static is_humongous_start_state(RegionState state) { return state == _humongous_start || state == _pinned_humongous_start; }
206 bool is_empty_or_trash() const { auto cur_state = state(); return is_empty_state(cur_state) || cur_state == _trash; }
207 bool is_empty() const { return is_empty_state(this->state()); }
208 bool is_active() const { auto cur_state = state(); return !is_empty_state(cur_state) && cur_state != _trash; }
209 bool is_humongous_start() const { return is_humongous_start_state(state()); }
210 bool is_humongous() const { auto cur_state = state(); return is_humongous_start_state(cur_state) || cur_state == _humongous_cont; }
211 bool is_committed() const { return !is_empty_uncommitted(); }
212 bool is_cset() const { auto cur_state = state(); return cur_state == _cset || cur_state == _pinned_cset; }
213 bool is_pinned() const { auto cur_state = state(); return cur_state == _pinned || cur_state == _pinned_cset || cur_state == _pinned_humongous_start; }
214 bool is_regular_or_regular_pinned() const { auto cur_state = state(); return cur_state == _regular || cur_state == _pinned; }
215
216 inline bool is_young() const;
217 inline bool is_old() const;
218 inline bool is_affiliated() const;
219
220 // Macro-properties:
221 bool is_alloc_allowed() const { auto cur_state = state(); return is_empty_state(cur_state) || cur_state == _regular || cur_state == _pinned; }
222 bool is_stw_move_allowed() const { auto cur_state = state(); return cur_state == _regular || cur_state == _cset || (ShenandoahHumongousMoves && cur_state == _humongous_start); }
223
224 RegionState state() const { return _state.load_acquire(); }
225 int state_ordinal() const { return region_state_to_ordinal(state()); }
226
227 void record_pin();
228 void record_unpin();
229 size_t pin_count() const;
230
231 private:
232 static size_t RegionCount;
233 static size_t RegionSizeBytes;
234 static size_t RegionSizeWords;
235 static size_t RegionSizeBytesShift;
236 static size_t RegionSizeWordsShift;
237 static size_t RegionSizeBytesMask;
238 static size_t RegionSizeWordsMask;
239 static size_t MaxTLABSizeBytes;
240 static size_t MaxTLABSizeWords;
241
242 // Never updated fields
243 size_t const _index;
244 HeapWord* const _bottom;
245 HeapWord* const _end;
246
247 // Rarely updated fields
248 HeapWord* _new_top;
249 double _empty_time;
250
251 HeapWord* _top_before_promoted;
252 HeapWord* _top_at_evac_start;
253
254 // Seldom updated fields
255 Atomic<RegionState> _state;
256 HeapWord* _coalesce_and_fill_boundary; // for old regions not selected as collection set candidates.
257
258 // Frequently updated fields
259 HeapWord* _top;
260
261 size_t _tlab_allocs;
262 size_t _gclab_allocs;
263 size_t _plab_allocs;
264
265 Atomic<size_t> _live_data;
266 Atomic<size_t> _critical_pins;
267
268 size_t _mixed_candidate_garbage_words;
269
270 Atomic<HeapWord*> _update_watermark;
271
272 uint _age;
273 bool _promoted_in_place;
274 CENSUS_NOISE(uint _youth;) // tracks epochs of retrograde ageing (rejuvenation)
275
276 ShenandoahSharedFlag _recycling; // Used to indicate that the region is being recycled; see try_recycle*().
277
278 // Set when an evacuation failure self-forwarded at least one object in this
279 // region. The drain at degen/full GC entry scans flagged regions and CAS-
280 // clears the self_fwd bits. Safety-net reset on region recycle.
281 ShenandoahSharedFlag _has_self_forwards;
282
283 bool _needs_bitmap_reset;
284
285 public:
286 ShenandoahHeapRegion(HeapWord* start, size_t index, bool committed);
287
288 // Absolute minimums and maximums we should not ever break.
289 static const size_t MIN_NUM_REGIONS = 10;
290 static const size_t MIN_REGION_SIZE = 256*K;
291 static const size_t MAX_REGION_SIZE = 32*M;
292
293 // Return adjusted max heap size
294 static size_t setup_sizes(size_t max_heap_size);
295
296 double empty_time() {
297 return _empty_time;
298 }
299
300 inline static size_t required_regions(size_t bytes) {
301 return (bytes + ShenandoahHeapRegion::region_size_bytes() - 1) >> ShenandoahHeapRegion::region_size_bytes_shift();
302 }
303
304 // Whether an allocation of the given word size must occupy a contiguous run of
305 // humongous regions rather than fitting in a single region.
306 //
307 // may_expand_for_hash must be set only when 'words' is the *base* size of a
308 // freshly allocated, not-yet-hashed object that could later grow by one word
309 // when an identity hash-code is injected as it is copied during GC (see
310 // oopDesc::copy_size). In that case we account for that potential growth when
311 // deciding whether the object needs the humongous path, so an object whose
312 // expanded form would overflow a region is routed to humongous from the start.
313 // It must be false for every size that is already final:
314 // - the live size of an existing object (obj->size()), which already includes
315 // the hash word once the object has been expanded by a prior GC copy;
316 // - an evacuation/promotion copy size (copy_size()), which is already expanded;
317 // - a raw LAB buffer (TLAB/GCLAB/PLAB), which never grows.
318 inline static bool requires_humongous(size_t words, bool may_expand_for_hash) {
319 if (UseCompactObjectHeaders && may_expand_for_hash) {
320 words = oopDesc::hash_expanded_size(words);
321 }
322 return words > ShenandoahHeapRegion::RegionSizeWords;
323 }
324
325 inline static size_t region_count() {
326 return ShenandoahHeapRegion::RegionCount;
327 }
328
329 inline static size_t region_size_bytes() {
330 return ShenandoahHeapRegion::RegionSizeBytes;
331 }
332
333 inline static size_t region_size_words() {
334 return ShenandoahHeapRegion::RegionSizeWords;
335 }
336
337 inline static size_t region_size_bytes_shift() {
338 return ShenandoahHeapRegion::RegionSizeBytesShift;
339 }
340
341 inline static size_t region_size_words_shift() {
342 return ShenandoahHeapRegion::RegionSizeWordsShift;
343 }
344
345 inline static size_t region_size_bytes_mask() {
346 return ShenandoahHeapRegion::RegionSizeBytesMask;
347 }
348
349 inline static size_t region_size_words_mask() {
350 return ShenandoahHeapRegion::RegionSizeWordsMask;
351 }
352
353 // Convert to jint with sanity checking
354 inline static jint region_size_bytes_jint() {
355 assert (ShenandoahHeapRegion::RegionSizeBytes <= (size_t)max_jint, "sanity");
356 return (jint)ShenandoahHeapRegion::RegionSizeBytes;
357 }
358
359 // Convert to jint with sanity checking
360 inline static jint region_size_words_jint() {
361 assert (ShenandoahHeapRegion::RegionSizeWords <= (size_t)max_jint, "sanity");
362 return (jint)ShenandoahHeapRegion::RegionSizeWords;
363 }
364
365 // Convert to jint with sanity checking
366 inline static jint region_size_bytes_shift_jint() {
367 assert (ShenandoahHeapRegion::RegionSizeBytesShift <= (size_t)max_jint, "sanity");
368 return (jint)ShenandoahHeapRegion::RegionSizeBytesShift;
369 }
370
371 // Convert to jint with sanity checking
372 inline static jint region_size_words_shift_jint() {
373 assert (ShenandoahHeapRegion::RegionSizeWordsShift <= (size_t)max_jint, "sanity");
374 return (jint)ShenandoahHeapRegion::RegionSizeWordsShift;
375 }
376
377 inline static size_t max_tlab_size_bytes() {
378 return ShenandoahHeapRegion::MaxTLABSizeBytes;
379 }
380
381 inline static size_t max_tlab_size_words() {
382 return ShenandoahHeapRegion::MaxTLABSizeWords;
383 }
384
385 inline size_t index() const {
386 return _index;
387 }
388
389 inline void save_top_before_promote();
390 inline HeapWord* get_top_before_promote() const { return _top_before_promoted; }
391
392 inline void set_promoted_in_place() {
393 _promoted_in_place = true;
394 }
395
396 // Returns true iff this region was promoted in place subsequent to the most recent start of concurrent old marking.
397 bool was_promoted_in_place() const {
398 return _promoted_in_place;
399 }
400 inline void restore_top_before_promote();
401 inline size_t garbage_before_padded_for_promote() const;
402
403 HeapWord* get_top_at_evac_start() const { return _top_at_evac_start; }
404 void record_top_at_evac_start() { _top_at_evac_start = _top; }
405
406 // Allocation (return nullptr if full)
407 inline HeapWord* allocate(size_t word_size, const ShenandoahAllocRequest& req);
408
409 // Allocate fill after top
410 inline HeapWord* allocate_fill(size_t word_size);
411
412 inline void clear_live_data();
413 void set_live_data(size_t s);
414
415 // Increase live data for newly allocated region
416 inline void increase_live_data_alloc_words(size_t s);
417
418 // Increase live data for region scanned with GC
419 inline void increase_live_data_gc_words(size_t s);
420
421 inline bool has_live() const;
422
423 // Represents the number of live bytes identified by most recent marking effort. Does not include the bytes
424 // above TAMS.
425 inline size_t get_live_data_bytes() const;
426
427 // Represents the number of live words identified by most recent marking effort. Does not include the words
428 // above TAMS.
429 inline size_t get_live_data_words() const;
430
431 inline size_t get_mixed_candidate_live_data_bytes() const;
432 inline size_t get_mixed_candidate_live_data_words() const;
433
434 inline void capture_mixed_candidate_garbage();
435
436 // Returns garbage by calculating difference between used and get_live_data_words. The value returned is only
437 // meaningful immediately following completion of marking. If there have been subsequent allocations in this region,
438 // use a different approach to determine garbage, such as (used() - get_mixed_candidate_live_data_bytes())
439 inline size_t garbage() const;
440
441 void print_on(outputStream* st) const;
442
443 void try_recycle_under_lock();
444
445 void try_recycle();
446
447 inline void begin_preemptible_coalesce_and_fill() {
448 _coalesce_and_fill_boundary = _bottom;
449 }
450
451 inline void end_preemptible_coalesce_and_fill() {
452 _coalesce_and_fill_boundary = _end;
453 }
454
455 inline void suspend_coalesce_and_fill(HeapWord* next_focus) {
456 _coalesce_and_fill_boundary = next_focus;
457 }
458
459 inline HeapWord* resume_coalesce_and_fill() {
460 return _coalesce_and_fill_boundary;
461 }
462
463 // Coalesce contiguous spans of garbage objects by filling header and registering start locations with remembered set.
464 // This is used by old-gen GC following concurrent marking to make old-gen HeapRegions parsable. Old regions must be
465 // parsable because the mark bitmap is not reliable during the concurrent old mark.
466 // Return true iff region is completely coalesced and filled. Returns false if cancelled before task is complete.
467 bool oop_coalesce_and_fill(bool cancellable);
468
469 // Invoke closure on every reference contained within the humongous object that spans this humongous
470 // region if the reference is contained within a DIRTY card and the reference is no more than words following
471 // start within the humongous object.
472 void oop_iterate_humongous_slice_dirty(OopIterateClosure* cl, HeapWord* start, size_t words, bool write_table) const;
473
474 // Invoke closure on every reference contained within the humongous object starting from start and
475 // ending at start + words.
476 void oop_iterate_humongous_slice_all(OopIterateClosure* cl, HeapWord* start, size_t words) const;
477
478 HeapWord* block_start(const void* p) const;
479 size_t block_size(const HeapWord* p) const;
480 bool block_is_obj(const HeapWord* p) const { return p < top(); }
481
482 // Find humongous start region that this region belongs to
483 ShenandoahHeapRegion* humongous_start_region() const;
484
485 HeapWord* top() const { return _top; }
486 void set_top(HeapWord* v) { _top = v; }
487
488 HeapWord* new_top() const { return _new_top; }
489 void set_new_top(HeapWord* v) { _new_top = v; }
490
491 HeapWord* bottom() const { return _bottom; }
492 HeapWord* end() const { return _end; }
493
494 size_t capacity() const { return byte_size(bottom(), end()); }
495 size_t used() const { return byte_size(bottom(), top()); }
496 size_t used_before_promote() const { return byte_size(bottom(), get_top_before_promote()); }
497 size_t free() const { return byte_size(top(), end()); }
498
499 // Does this region contain this address?
500 bool contains(HeapWord* p) const {
501 return (bottom() <= p) && (p < top());
502 }
503
504 inline void adjust_alloc_metadata(const ShenandoahAllocRequest &req, size_t);
505 void reset_alloc_metadata();
506 size_t get_shared_allocs() const;
507 size_t get_tlab_allocs() const;
508 size_t get_gclab_allocs() const;
509 size_t get_plab_allocs() const;
510
511 inline HeapWord* get_update_watermark() const;
512 inline void set_update_watermark(HeapWord* w);
513 inline void set_update_watermark_at_safepoint(HeapWord* w);
514
515 inline ShenandoahAffiliation affiliation() const;
516 inline const char* affiliation_name() const;
517
518 void set_affiliation(ShenandoahAffiliation new_affiliation);
519
520 // Region ageing and rejuvenation
521 uint age() const { return _age; }
522 CENSUS_NOISE(uint youth() const { return _youth; })
523
524 void increment_age() {
525 const uint max_age = markWord::max_age;
526 assert(_age <= max_age, "Error");
527 if (_age++ >= max_age) {
528 _age = max_age; // clamp
529 }
530 }
531
532 void reset_age() {
533 CENSUS_NOISE(_youth += _age;)
534 _age = 0;
535 }
536
537 CENSUS_NOISE(void clear_youth() { _youth = 0; })
538
539 inline bool need_bitmap_reset() const {
540 return _needs_bitmap_reset;
541 }
542
543 inline void set_needs_bitmap_reset() {
544 _needs_bitmap_reset = true;
545 }
546
547 inline void unset_needs_bitmap_reset() {
548 _needs_bitmap_reset = false;
549 }
550
551 // Self-forward accounting: set by an evacuating thread after it successfully
552 // installs a self-forward mark on an object in this region. Tested and cleared
553 // at the drain phase (degen/full GC entry) and again on region recycle.
554 bool has_self_forwards() const { return _has_self_forwards.is_set(); }
555 void set_has_self_forwards() { _has_self_forwards.set(); }
556 void clear_has_self_forwards() { _has_self_forwards.unset(); }
557
558 private:
559 void decrement_humongous_waste();
560 void do_commit();
561 void do_uncommit();
562
563 inline void internal_increase_live_data(size_t s);
564
565 void set_state(RegionState to);
566 };
567
568 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP