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 inline static bool requires_humongous(size_t words) {
305 return words > ShenandoahHeapRegion::RegionSizeWords;
306 }
307
308 inline static size_t region_count() {
309 return ShenandoahHeapRegion::RegionCount;
310 }
311
312 inline static size_t region_size_bytes() {
313 return ShenandoahHeapRegion::RegionSizeBytes;
314 }
315
316 inline static size_t region_size_words() {
317 return ShenandoahHeapRegion::RegionSizeWords;
318 }
319
320 inline static size_t region_size_bytes_shift() {
321 return ShenandoahHeapRegion::RegionSizeBytesShift;
322 }
323
324 inline static size_t region_size_words_shift() {
325 return ShenandoahHeapRegion::RegionSizeWordsShift;
326 }
327
328 inline static size_t region_size_bytes_mask() {
329 return ShenandoahHeapRegion::RegionSizeBytesMask;
330 }
331
332 inline static size_t region_size_words_mask() {
333 return ShenandoahHeapRegion::RegionSizeWordsMask;
334 }
335
336 // Convert to jint with sanity checking
337 inline static jint region_size_bytes_jint() {
338 assert (ShenandoahHeapRegion::RegionSizeBytes <= (size_t)max_jint, "sanity");
339 return (jint)ShenandoahHeapRegion::RegionSizeBytes;
340 }
341
342 // Convert to jint with sanity checking
343 inline static jint region_size_words_jint() {
344 assert (ShenandoahHeapRegion::RegionSizeWords <= (size_t)max_jint, "sanity");
345 return (jint)ShenandoahHeapRegion::RegionSizeWords;
346 }
347
348 // Convert to jint with sanity checking
349 inline static jint region_size_bytes_shift_jint() {
350 assert (ShenandoahHeapRegion::RegionSizeBytesShift <= (size_t)max_jint, "sanity");
351 return (jint)ShenandoahHeapRegion::RegionSizeBytesShift;
352 }
353
354 // Convert to jint with sanity checking
355 inline static jint region_size_words_shift_jint() {
356 assert (ShenandoahHeapRegion::RegionSizeWordsShift <= (size_t)max_jint, "sanity");
357 return (jint)ShenandoahHeapRegion::RegionSizeWordsShift;
358 }
359
360 inline static size_t max_tlab_size_bytes() {
361 return ShenandoahHeapRegion::MaxTLABSizeBytes;
362 }
363
364 inline static size_t max_tlab_size_words() {
365 return ShenandoahHeapRegion::MaxTLABSizeWords;
366 }
367
368 inline size_t index() const {
369 return _index;
370 }
371
372 inline void save_top_before_promote();
373 inline HeapWord* get_top_before_promote() const { return _top_before_promoted; }
374
375 inline void set_promoted_in_place() {
376 _promoted_in_place = true;
377 }
378
379 // Returns true iff this region was promoted in place subsequent to the most recent start of concurrent old marking.
380 bool was_promoted_in_place() const {
381 return _promoted_in_place;
382 }
383 inline void restore_top_before_promote();
384 inline size_t garbage_before_padded_for_promote() const;
385
386 HeapWord* get_top_at_evac_start() const { return _top_at_evac_start; }
387 void record_top_at_evac_start() { _top_at_evac_start = _top; }
388
389 // Allocation (return nullptr if full)
390 inline HeapWord* allocate(size_t word_size, const ShenandoahAllocRequest& req);
391
392 // Allocate fill after top
393 inline HeapWord* allocate_fill(size_t word_size);
394
395 inline void clear_live_data();
396 void set_live_data(size_t s);
397
398 // Increase live data for newly allocated region
399 inline void increase_live_data_alloc_words(size_t s);
400
401 // Increase live data for region scanned with GC
402 inline void increase_live_data_gc_words(size_t s);
403
404 inline bool has_live() const;
405
406 // Represents the number of live bytes identified by most recent marking effort. Does not include the bytes
407 // above TAMS.
408 inline size_t get_live_data_bytes() const;
409
410 // Represents the number of live words identified by most recent marking effort. Does not include the words
411 // above TAMS.
412 inline size_t get_live_data_words() const;
413
414 inline size_t get_mixed_candidate_live_data_bytes() const;
415 inline size_t get_mixed_candidate_live_data_words() const;
416
417 inline void capture_mixed_candidate_garbage();
418
419 // Returns garbage by calculating difference between used and get_live_data_words. The value returned is only
420 // meaningful immediately following completion of marking. If there have been subsequent allocations in this region,
421 // use a different approach to determine garbage, such as (used() - get_mixed_candidate_live_data_bytes())
422 inline size_t garbage() const;
423
424 void print_on(outputStream* st) const;
425
426 void try_recycle_under_lock();
427
428 void try_recycle();
429
430 inline void begin_preemptible_coalesce_and_fill() {
431 _coalesce_and_fill_boundary = _bottom;
432 }
433
434 inline void end_preemptible_coalesce_and_fill() {
435 _coalesce_and_fill_boundary = _end;
436 }
437
438 inline void suspend_coalesce_and_fill(HeapWord* next_focus) {
439 _coalesce_and_fill_boundary = next_focus;
440 }
441
442 inline HeapWord* resume_coalesce_and_fill() {
443 return _coalesce_and_fill_boundary;
444 }
445
446 // Coalesce contiguous spans of garbage objects by filling header and registering start locations with remembered set.
447 // This is used by old-gen GC following concurrent marking to make old-gen HeapRegions parsable. Old regions must be
448 // parsable because the mark bitmap is not reliable during the concurrent old mark.
449 // Return true iff region is completely coalesced and filled. Returns false if cancelled before task is complete.
450 bool oop_coalesce_and_fill(bool cancellable);
451
452 // Invoke closure on every reference contained within the humongous object that spans this humongous
453 // region if the reference is contained within a DIRTY card and the reference is no more than words following
454 // start within the humongous object.
455 void oop_iterate_humongous_slice_dirty(OopIterateClosure* cl, HeapWord* start, size_t words, bool write_table) const;
456
457 // Invoke closure on every reference contained within the humongous object starting from start and
458 // ending at start + words.
459 void oop_iterate_humongous_slice_all(OopIterateClosure* cl, HeapWord* start, size_t words) const;
460
461 HeapWord* block_start(const void* p) const;
462 size_t block_size(const HeapWord* p) const;
463 bool block_is_obj(const HeapWord* p) const { return p < top(); }
464
465 // Find humongous start region that this region belongs to
466 ShenandoahHeapRegion* humongous_start_region() const;
467
468 HeapWord* top() const { return _top; }
469 void set_top(HeapWord* v) { _top = v; }
470
471 HeapWord* new_top() const { return _new_top; }
472 void set_new_top(HeapWord* v) { _new_top = v; }
473
474 HeapWord* bottom() const { return _bottom; }
475 HeapWord* end() const { return _end; }
476
477 size_t capacity() const { return byte_size(bottom(), end()); }
478 size_t used() const { return byte_size(bottom(), top()); }
479 size_t used_before_promote() const { return byte_size(bottom(), get_top_before_promote()); }
480 size_t free() const { return byte_size(top(), end()); }
481
482 // Does this region contain this address?
483 bool contains(HeapWord* p) const {
484 return (bottom() <= p) && (p < top());
485 }
486
487 inline void adjust_alloc_metadata(const ShenandoahAllocRequest &req, size_t);
488 void reset_alloc_metadata();
489 size_t get_shared_allocs() const;
490 size_t get_tlab_allocs() const;
491 size_t get_gclab_allocs() const;
492 size_t get_plab_allocs() const;
493
494 inline HeapWord* get_update_watermark() const;
495 inline void set_update_watermark(HeapWord* w);
496 inline void set_update_watermark_at_safepoint(HeapWord* w);
497
498 inline ShenandoahAffiliation affiliation() const;
499 inline const char* affiliation_name() const;
500
501 void set_affiliation(ShenandoahAffiliation new_affiliation);
502
503 // Region ageing and rejuvenation
504 uint age() const { return _age; }
505 CENSUS_NOISE(uint youth() const { return _youth; })
506
507 void increment_age() {
508 const uint max_age = markWord::max_age;
509 assert(_age <= max_age, "Error");
510 if (_age++ >= max_age) {
511 _age = max_age; // clamp
512 }
513 }
514
515 void reset_age() {
516 CENSUS_NOISE(_youth += _age;)
517 _age = 0;
518 }
519
520 CENSUS_NOISE(void clear_youth() { _youth = 0; })
521
522 inline bool need_bitmap_reset() const {
523 return _needs_bitmap_reset;
524 }
525
526 inline void set_needs_bitmap_reset() {
527 _needs_bitmap_reset = true;
528 }
529
530 inline void unset_needs_bitmap_reset() {
531 _needs_bitmap_reset = false;
532 }
533
534 // Self-forward accounting: set by an evacuating thread after it successfully
535 // installs a self-forward mark on an object in this region. Tested and cleared
536 // at the drain phase (degen/full GC entry) and again on region recycle.
537 bool has_self_forwards() const { return _has_self_forwards.is_set(); }
538 void set_has_self_forwards() { _has_self_forwards.set(); }
539 void clear_has_self_forwards() { _has_self_forwards.unset(); }
540
541 private:
542 void decrement_humongous_waste();
543 void do_commit();
544 void do_uncommit();
545
546 inline void internal_increase_live_data(size_t s);
547
548 void set_state(RegionState to);
549 };
550
551 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP