1 /*
2 * Copyright (c) 2015, 2026, 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 #include "gc/z/zGeneration.inline.hpp"
25 #include "gc/z/zHeap.inline.hpp"
26 #include "gc/z/zLiveMap.inline.hpp"
27 #include "gc/z/zStat.hpp"
28 #include "gc/z/zUtils.hpp"
29 #include "logging/log.hpp"
30 #include "utilities/debug.hpp"
31 #include "utilities/powerOfTwo.hpp"
32 #include "utilities/spinYield.hpp"
33
34 static const ZStatCounter ZCounterMarkSeqNumResetContention("Contention", "Mark SeqNum Reset Contention", ZStatUnitOpsPerSecond);
35 static const ZStatCounter ZCounterMarkSegmentResetContention("Contention", "Mark Segment Reset Contention", ZStatUnitOpsPerSecond);
36
37 ZLiveMap::ZLiveMap(uint32_t object_max_count)
38 : _segment_size((object_max_count == 1 ? 1u : (object_max_count / NumSegments)) * BitsPerObject),
39 _segment_shift(log2i_exact(_segment_size)),
40 _seqnum(0),
41 _live_objects(0),
42 _live_bytes(0),
43 _will_expand_objects(0),
44 _segment_live_bits(0),
45 _segment_claim_bits(0),
46 _bitmap(0) {}
47
48 void ZLiveMap::initialize_bitmap() {
49 if (_bitmap.size() == 0) {
50 _bitmap.initialize(size_t(_segment_size) * size_t(NumSegments), false /* clear */);
51 }
52 }
53
54 void ZLiveMap::reset(ZGenerationId id) {
55 ZGeneration* const generation = ZGeneration::generation(id);
56 const uint32_t seqnum_initializing = (uint32_t)-1;
57 bool contention = false;
58
59 SpinYield yielder(0, 0, 1000);
60
61 // Multiple threads can enter here, make sure only one of them
62 // resets the marking information while the others busy wait.
63 for (uint32_t seqnum = _seqnum.load_acquire();
64 seqnum != generation->seqnum();
65 seqnum = _seqnum.load_acquire()) {
66
67 if (seqnum != seqnum_initializing) {
68 // No one has claimed initialization of the livemap yet
69 if (_seqnum.compare_set(seqnum, seqnum_initializing)) {
70 // This thread claimed the initialization
71
72 // Reset marking information
73 _live_bytes.store_relaxed(0u);
74 _live_objects.store_relaxed(0u);
75 _will_expand_objects.store_relaxed(0u);
76
77 // Clear segment claimed/live bits
78 segment_live_bits().clear();
79 segment_claim_bits().clear();
80
81 // We lazily initialize the bitmap the first time the page is marked, i.e.
82 // a bit is about to be set for the first time.
83 initialize_bitmap();
84
85 assert(_seqnum.load_relaxed() == seqnum_initializing, "Invalid");
86
87 // Make sure the newly reset marking information is ordered
88 // before the update of the page seqnum, such that when the
89 // up-to-date seqnum is load acquired, the bit maps will not
90 // contain stale information.
91 _seqnum.release_store(generation->seqnum());
92 break;
93 }
94 }
95
96 // Mark reset contention
97 if (!contention) {
98 // Count contention once
99 ZStatInc(ZCounterMarkSeqNumResetContention);
100 contention = true;
101
102 log_trace(gc)("Mark seqnum reset contention, thread: " PTR_FORMAT " (%s), map: " PTR_FORMAT,
103 p2i(Thread::current()), ZUtils::thread_name(), p2i(this));
104 }
105
106 // "Yield" to allow the thread that's resetting the livemap to finish
107 yielder.wait();
108 }
109 }
110
111 void ZLiveMap::reset_segment(BitMap::idx_t segment) {
112 bool contention = false;
113
114 if (!claim_segment(segment)) {
115 // Already claimed, wait for live bit to be set
116 while (!is_segment_live(segment)) {
117 // Mark reset contention
118 if (!contention) {
119 // Count contention once
120 ZStatInc(ZCounterMarkSegmentResetContention);
121 contention = true;
122
123 log_trace(gc)("Mark segment reset contention, thread: " PTR_FORMAT " (%s), map: " PTR_FORMAT ", segment: %zu",
124 p2i(Thread::current()), ZUtils::thread_name(), p2i(this), segment);
125 }
126 }
127
128 // Segment is live
129 return;
130 }
131
132 // Segment claimed, clear it
133 const BitMap::idx_t start_index = segment_start(segment);
134 const BitMap::idx_t end_index = segment_end(segment);
135 if (_segment_size / BitsPerWord >= 32) {
136 _bitmap.clear_large_range(start_index, end_index);
137 } else {
138 _bitmap.clear_range(start_index, end_index);
139 }
140
141 // Set live bit
142 const bool success = set_segment_live(segment);
143 assert(success, "Should never fail");
144 }