1 /*
  2  * Copyright (c) 2001, 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 
 25 #include "gc/g1/g1CollectedHeap.inline.hpp"
 26 #include "gc/g1/g1CollectorState.inline.hpp"
 27 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
 28 #include "gc/g1/g1Policy.hpp"
 29 #include "gc/g1/g1Trace.hpp"
 30 #include "gc/g1/g1VMOperations.hpp"
 31 #include "gc/shared/concurrentGCBreakpoints.hpp"
 32 #include "gc/shared/gcCause.hpp"
 33 #include "gc/shared/gcId.hpp"
 34 #include "gc/shared/gcTimer.hpp"
 35 #include "gc/shared/gcTraceTime.inline.hpp"
 36 #include "gc/shared/isGCActiveMark.hpp"
 37 #include "memory/universe.hpp"
 38 #include "runtime/interfaceSupport.inline.hpp"
 39 
 40 bool VM_G1CollectFull::skip_operation() const {
 41   // There is a race between the periodic collection task's checks for
 42   // wanting a collection and processing its request.  A collection in that
 43   // gap should cancel the request.
 44   if ((_gc_cause == GCCause::_g1_periodic_collection) &&
 45       (G1CollectedHeap::heap()->total_collections() != _gc_count_before)) {
 46     return true;
 47   }
 48   return VM_GC_Operation::skip_operation();
 49 }
 50 
 51 void VM_G1CollectFull::doit() {
 52   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 53   GCCauseSetter x(g1h, _gc_cause);
 54   bool clear_all_soft_refs = _gc_cause == GCCause::_metadata_GC_clear_soft_refs ||
 55                              _gc_cause == GCCause::_wb_full_gc;
 56   g1h->do_full_collection(size_t(0) /* allocation_word_size */,
 57                           clear_all_soft_refs,
 58                           false /* do_maximal_compaction */);
 59 }
 60 
 61 VM_G1TryInitiateConcMark::VM_G1TryInitiateConcMark(size_t allocation_word_size,
 62                                                    uint gc_count_before,
 63                                                    GCCause::Cause gc_cause) :
 64   VM_GC_Collect_Operation(gc_count_before, gc_cause),
 65   _word_size(allocation_word_size),
 66   _transient_failure(false),
 67   _mark_in_progress(false),
 68   _cycle_already_in_progress(false),
 69   _whitebox_attached(false),
 70   _gc_succeeded(false)
 71 {}
 72 
 73 bool VM_G1TryInitiateConcMark::doit_prologue() {
 74   bool result = VM_GC_Operation::doit_prologue();
 75   // The prologue can fail for a couple of reasons. The first is that another GC
 76   // got scheduled and prevented the scheduling of the concurrent start GC.
 77   // In this case we want to retry the GC so that the concurrent start pause is
 78   // actually scheduled.
 79   if (!result) _transient_failure = true;
 80   return result;
 81 }
 82 
 83 void VM_G1TryInitiateConcMark::doit() {
 84   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 85 
 86   GCCauseSetter x(g1h, _gc_cause);
 87 
 88   G1CollectorState* state = g1h->collector_state();
 89   _mark_in_progress = state->is_in_marking();
 90   _cycle_already_in_progress =  state->is_in_concurrent_cycle();
 91 
 92   if (!g1h->policy()->force_concurrent_start_if_outside_cycle(_gc_cause)) {
 93     // Failure to force the next GC pause to be a concurrent start indicates
 94     // there is already a concurrent marking cycle in progress. Flags to indicate
 95     // that were already set, so return immediately.
 96   } else if ((_gc_cause != GCCause::_wb_breakpoint) &&
 97              ConcurrentGCBreakpoints::is_controlled()) {
 98     // WhiteBox wants to be in control of concurrent cycles, so don't try to
 99     // start one.  This check is after the force_concurrent_start_xxx so that a
100     // request will be remembered for a later partial collection, even though
101     // we've rejected this request.
102     _whitebox_attached = true;
103   } else {
104     g1h->do_collection_pause_at_safepoint(_word_size);
105     _gc_succeeded = true;
106   }
107 }
108 
109 VM_G1CollectForAllocation::VM_G1CollectForAllocation(size_t word_size,
110                                                      uint gc_count_before,
111                                                      GCCause::Cause gc_cause) :
112   VM_CollectForAllocation(word_size, gc_count_before, gc_cause) {}
113 
114 void VM_G1CollectForAllocation::doit() {
115   G1CollectedHeap* g1h = G1CollectedHeap::heap();
116   GCCauseSetter x(g1h, _gc_cause);
117   // Try a partial collection of some kind.
118   g1h->do_collection_pause_at_safepoint(_word_size);
119 
120   if (_word_size > 0) {
121     // An allocation had been requested. Do it, eventually trying a stronger
122     // kind of GC.
123     _result = g1h->satisfy_failed_allocation(_word_size);
124   } else if (g1h->should_upgrade_to_full_gc()) {
125     // There has been a request to perform a GC to free some space. We have no
126     // information on how much memory has been asked for. In case there are
127     // absolutely no regions left to allocate into, do a full compaction.
128     g1h->upgrade_to_full_collection();
129   }
130 }
131 
132 void VM_G1PauseConcurrent::doit() {
133   G1CollectedHeap* g1h = G1CollectedHeap::heap();
134   if (_is_shutting_down) {
135     g1h->concurrent_mark()->shutdown_concurrent_cycle();
136     return;
137   }
138 
139   GCIdMark gc_id_mark(_gc_id);
140   GCTraceCPUTime tcpu(g1h->concurrent_mark()->gc_tracer_cm());
141 
142   // GCTraceTime(...) only supports sub-phases, so a more verbose version
143   // is needed when we report the top-level pause phase.
144   GCTraceTimeLogger(Info, gc) logger(_message, GCCause::_no_gc, true);
145   GCTraceTimePauseTimer       timer(_message, g1h->concurrent_mark()->gc_timer_cm());
146   GCTraceTimeDriver           t(&logger, &timer);
147 
148   G1ConcGCMonitoringScope monitoring_scope(g1h->monitoring_support(), affects_memory_pools());
149   SvcGCMarker sgcm(SvcGCMarker::CONCURRENT);
150   IsSTWGCActiveMark x;
151 
152   work();
153 }
154 
155 bool VM_G1PauseConcurrent::doit_prologue() {
156   Heap_lock->lock();
157   G1CollectedHeap* g1h = G1CollectedHeap::heap();
158   _is_shutting_down = g1h->is_shutting_down();
159   if (_is_shutting_down && !g1h->concurrent_mark()->shutdown_cleanup_needed()) {
160     Heap_lock->unlock();
161     return false;
162   }
163   return true;
164 }
165 
166 void VM_G1PauseConcurrent::doit_epilogue() {
167   if (Universe::has_reference_pending_list()) {
168     Heap_lock->notify_all();
169   }
170   Heap_lock->unlock();
171 }
172 
173 void VM_G1PauseRemark::work() {
174   G1ConcurrentMark* cm = G1CollectedHeap::heap()->concurrent_mark();
175   cm->remark();
176 }
177 
178 void VM_G1PauseCleanup::work() {
179   G1ConcurrentMark* cm = G1CollectedHeap::heap()->concurrent_mark();
180   cm->cleanup();
181 }
182 
183 bool VM_G1StopMarking::doit_prologue() {
184   G1CollectedHeap* g1h = G1CollectedHeap::heap();
185 #ifdef ASSERT
186   {
187     MutexLocker ml(Heap_lock);
188     assert(g1h->is_shutting_down(), "must be");
189   }
190 #endif
191   return g1h->concurrent_mark()->shutdown_cleanup_needed();
192 }
193 
194 void VM_G1StopMarking::doit() {
195   G1ConcurrentMark* cm = G1CollectedHeap::heap()->concurrent_mark();
196   cm->shutdown_concurrent_cycle();
197 }