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