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   GCIdMark gc_id_mark(_gc_id);
134   G1CollectedHeap* g1h = G1CollectedHeap::heap();
135   GCTraceCPUTime tcpu(g1h->concurrent_mark()->gc_tracer_cm());
136 
137   // GCTraceTime(...) only supports sub-phases, so a more verbose version
138   // is needed when we report the top-level pause phase.
139   GCTraceTimeLogger(Info, gc) logger(_message, GCCause::_no_gc, true);
140   GCTraceTimePauseTimer       timer(_message, g1h->concurrent_mark()->gc_timer_cm());
141   GCTraceTimeDriver           t(&logger, &timer);
142 
143   G1ConcGCMonitoringScope monitoring_scope(g1h->monitoring_support(), affects_memory_pools());
144   SvcGCMarker sgcm(SvcGCMarker::CONCURRENT);
145   IsSTWGCActiveMark x;
146 
147   work();
148 }
149 
150 bool VM_G1PauseConcurrent::doit_prologue() {
151   Heap_lock->lock();
152   G1CollectedHeap* g1h = G1CollectedHeap::heap();
153   if (g1h->is_shutting_down()) {
154     Heap_lock->unlock();
155     // JVM shutdown has started. Abort concurrent marking to ensure that any further
156     // concurrent VM operations will not try to start and interfere with the shutdown
157     // process.
158     g1h->concurrent_mark()->abort_marking_threads();
159     return false;
160   }
161   return true;
162 }
163 
164 void VM_G1PauseConcurrent::doit_epilogue() {
165   if (Universe::has_reference_pending_list()) {
166     Heap_lock->notify_all();
167   }
168   Heap_lock->unlock();
169 }
170 
171 void VM_G1PauseRemark::work() {
172   G1ConcurrentMark* cm = G1CollectedHeap::heap()->concurrent_mark();
173   cm->remark();
174 }
175 
176 void VM_G1PauseCleanup::work() {
177   G1ConcurrentMark* cm = G1CollectedHeap::heap()->concurrent_mark();
178   cm->cleanup();
179 }