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 #ifndef SHARE_GC_G1_G1CONCURRENTMARKTHREAD_HPP
 26 #define SHARE_GC_G1_G1CONCURRENTMARKTHREAD_HPP
 27 
 28 #include "gc/shared/concurrentGCThread.hpp"
 29 #include "runtime/atomic.hpp"
 30 
 31 class G1ConcurrentMark;
 32 class G1Policy;
 33 
 34 // The concurrent mark thread triggers the various steps of the concurrent marking
 35 // cycle, including various marking cleanup.
 36 //
 37 // The concurrent cycle may either be "Full" (i.e. include marking, rebuilding and
 38 // scrubbing, resetting for the next cycle) or "Undo", i.e. shortened to just the
 39 // reset part.
 40 class G1ConcurrentMarkThread: public ConcurrentGCThread {
 41   G1ConcurrentMark* _cm;
 42 
 43   enum ServiceState : uint {
 44     Idle,
 45     FullCycleMarking,
 46     FullCycleRebuildOrScrub,
 47     FullCycleResetForNextCycle,
 48     UndoCycleResetForNextCycle
 49   };
 50 
 51   Atomic<ServiceState> _state;
 52 
 53   ServiceState state() const { return _state.load_relaxed(); }
 54 
 55   // Returns whether we are in a "Full" cycle.
 56   bool is_in_full_concurrent_cycle() const;
 57 
 58   // Wait for next cycle. Returns the command passed over.
 59   bool wait_for_next_cycle();
 60 
 61   bool mark_loop_needs_restart() const;
 62 
 63   // Phases and subphases for the full concurrent cycle in order.
 64   //
 65   // All these methods return true if the cycle should be aborted.
 66   bool phase_clear_cld_claimed_marks();
 67   bool phase_scan_root_regions();
 68 
 69   bool phase_mark_loop();
 70   bool subphase_mark_from_roots();
 71   bool subphase_preclean();
 72   bool subphase_delay_to_keep_mmu_before_remark();
 73   bool subphase_remark();
 74 
 75   bool phase_rebuild_and_scrub();
 76   bool phase_delay_to_keep_mmu_before_cleanup();
 77   bool phase_cleanup();
 78   bool phase_clear_bitmap_for_next_mark();
 79 
 80   void concurrent_cycle_start();
 81 
 82   void concurrent_mark_cycle_do();
 83   void concurrent_undo_cycle_do();
 84 
 85   void concurrent_cycle_end(bool mark_cycle_completed);
 86 
 87   // Delay pauses to meet MMU.
 88   void delay_to_keep_mmu(bool remark);
 89   double mmu_delay_end(G1Policy* policy, bool remark);
 90 
 91   void run_service();
 92   void stop_service();
 93 
 94  public:
 95   // Constructor
 96   G1ConcurrentMarkThread(G1ConcurrentMark* cm);
 97 
 98   // Total cpu time used by all marking related threads (i.e. this thread and the
 99   // marking worker threads) in seconds.
100   double total_mark_cpu_time_s();
101   // Cpu time used by all marking worker threads in seconds.
102   double worker_threads_cpu_time_s();
103   // State management.
104   void set_idle();
105   void start_full_cycle();
106   void start_undo_cycle();
107 
108   void set_full_cycle_rebuild_and_scrub();
109   void set_full_cycle_reset_for_next_cycle();
110 
111   bool is_idle() const;
112   // Returns true from the moment a concurrent cycle is
113   // initiated (during the concurrent start pause when calling one of the
114   // start_*_cycle() methods) to the moment when the cycle completes.
115   bool is_in_progress() const;
116 
117   bool is_in_marking() const;
118   bool is_in_marking_or_rebuild() const;
119   bool is_in_reset_for_next_cycle() const;
120 
121   bool is_in_undo_cycle() const;
122 
123   // Update the perf data counter for concurrent mark.
124   void update_perf_counter_cpu_time();
125 };
126 
127 #endif // SHARE_GC_G1_G1CONCURRENTMARKTHREAD_HPP