1 /*
 2  * Copyright (c) 2021, Red Hat, Inc. 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 
27 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
28 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
29 #include "gc/shenandoah/shenandoahMarkClosures.hpp"
30 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
31 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
32 
33 
34 ShenandoahFinalMarkUpdateRegionStateClosure::ShenandoahFinalMarkUpdateRegionStateClosure(
35   ShenandoahMarkingContext *ctx) :
36   _ctx(ctx), _lock(ShenandoahHeap::heap()->lock()) {}
37 
38 void ShenandoahFinalMarkUpdateRegionStateClosure::heap_region_do(ShenandoahHeapRegion* r) {
39   if (r->is_active()) {
40     if (_ctx != nullptr) {
41       // _ctx may be null when this closure is used to sync only the pin status
42       // update the watermark of old regions. For old regions we cannot reset
43       // the TAMS because we rely on that to keep promoted objects alive after
44       // old marking is complete.
45 
46       // All allocations past TAMS are implicitly live, adjust the region data.
47       // Bitmaps/TAMS are swapped at this point, so we need to poll complete bitmap.
48       HeapWord *tams = _ctx->top_at_mark_start(r);
49       HeapWord *top = r->top();
50       if (top > tams) {
51         r->increase_live_data_alloc_words(pointer_delta(top, tams));
52       }
53     }
54 
55     // We are about to select the collection set, make sure it knows about
56     // current pinning status. Also, this allows trashing more regions that
57     // now have their pinning status dropped.
58     if (r->is_pinned()) {
59       if (r->pin_count() == 0) {
60         ShenandoahHeapLocker locker(_lock);
61         r->make_unpinned();
62       }
63     } else {
64       if (r->pin_count() > 0) {
65         ShenandoahHeapLocker locker(_lock);
66         r->make_pinned();
67       }
68     }
69 
70     // Remember limit for updating refs. It's guaranteed that we get no
71     // from-space-refs written from here on.
72     r->set_update_watermark_at_safepoint(r->top());
73   } else {
74     assert(!r->has_live(), "Region " SIZE_FORMAT " should have no live data", r->index());
75     assert(_ctx == nullptr || _ctx->top_at_mark_start(r) == r->top(),
76              "Region " SIZE_FORMAT " should have correct TAMS", r->index());
77   }
78 }