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     // All allocations past TAMS are implicitly live, adjust the region data.
41     // Bitmaps/TAMS are swapped at this point, so we need to poll complete bitmap.
42     HeapWord *tams = _ctx->top_at_mark_start(r);
43     HeapWord *top = r->top();
44     if (top > tams) {
45       r->increase_live_data_alloc_words(pointer_delta(top, tams));
46     }
47 
48     // We are about to select the collection set, make sure it knows about
49     // current pinning status. Also, this allows trashing more regions that
50     // now have their pinning status dropped.
51     if (r->is_pinned()) {
52       if (r->pin_count() == 0) {
53         ShenandoahHeapLocker locker(_lock);
54         r->make_unpinned();
55       }
56     } else {
57       if (r->pin_count() > 0) {
58         ShenandoahHeapLocker locker(_lock);
59         r->make_pinned();
60       }
61     }
62 
63     if (ShenandoahHeap::heap()->mode()->is_generational()) {
64       // Allocations move the watermark when top moves, however compacting
65       // objects will sometimes lower top beneath the watermark, after which,
66       // attempts to read the watermark will assert out (watermark should not be
67       // higher than top). The right way™ to check for new allocations is to compare
68       // top with the TAMS as is done earlier in this function.
69       if (top > tams) {
70         // There have been allocations in this region since the start of the cycle.
71         // Any objects new to this region must not assimilate elevated age.
72         r->reset_age();
73       } else if (ShenandoahHeap::heap()->is_aging_cycle()) {
74         r->increment_age();
75       }
76     }
77 
78     // Remember limit for updating refs. It's guaranteed that we get no
79     // from-space-refs written from here on.
80     r->set_update_watermark_at_safepoint(r->top());
81   } else {
82     assert(!r->has_live(), "Region " SIZE_FORMAT " should have no live data", r->index());
83     assert(_ctx->top_at_mark_start(r) == r->top(),
84            "Region " SIZE_FORMAT " should have correct TAMS", r->index());
85   }
86 }
87 
88 ShenandoahCaptureUpdateWaterMarkForOld::ShenandoahCaptureUpdateWaterMarkForOld(ShenandoahMarkingContext* ctx) :
89   _ctx(ctx), _lock(ShenandoahHeap::heap()->lock()) {}
90 
91 void ShenandoahCaptureUpdateWaterMarkForOld::heap_region_do(ShenandoahHeapRegion* r) {
92   // Remember limit for updating refs. It's guaranteed that we get no from-space-refs written from here on.
93   r->set_update_watermark_at_safepoint(r->top());
94 }