1 /*
  2  * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALCONTROLTHREAD_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALCONTROLTHREAD_HPP
 28 
 29 #include "gc/shared/gcCause.hpp"
 30 #include "gc/shenandoah/shenandoahController.hpp"
 31 #include "gc/shenandoah/shenandoahGenerationType.hpp"
 32 #include "gc/shenandoah/shenandoahGC.hpp"
 33 #include "gc/shenandoah/shenandoahPadding.hpp"
 34 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 35 
 36 class ShenandoahOldGeneration;
 37 class ShenandoahGeneration;
 38 class ShenandoahGenerationalHeap;
 39 class ShenandoahHeap;
 40 
 41 class ShenandoahGenerationalControlThread: public ShenandoahController {
 42   friend class VMStructs;
 43 
 44 public:
 45   typedef enum {
 46     none,
 47     concurrent_normal,
 48     stw_degenerated,
 49     stw_full,
 50     bootstrapping_old,
 51     servicing_old
 52   } GCMode;
 53 
 54 private:
 55   Monitor _control_lock;
 56   Monitor _regulator_lock;
 57 
 58   ShenandoahSharedFlag _allow_old_preemption;
 59   ShenandoahSharedFlag _preemption_requested;
 60 
 61   GCCause::Cause  _requested_gc_cause;
 62   volatile ShenandoahGenerationType _requested_generation;
 63   ShenandoahGC::ShenandoahDegenPoint _degen_point;
 64   ShenandoahGeneration* _degen_generation;
 65 
 66   shenandoah_padding(0);
 67   volatile GCMode _mode;
 68   shenandoah_padding(1);
 69 
 70 public:
 71   ShenandoahGenerationalControlThread();
 72 
 73   void run_service() override;
 74   void stop_service() override;
 75 
 76   void request_gc(GCCause::Cause cause) override;
 77 
 78   // Return true if the request to start a concurrent GC for the given generation succeeded.
 79   bool request_concurrent_gc(ShenandoahGenerationType generation);
 80 
 81   GCMode gc_mode() {
 82     return _mode;
 83   }
 84 private:
 85 
 86   // Returns true if the cycle has been cancelled or degenerated.
 87   bool check_cancellation_or_degen(ShenandoahGC::ShenandoahDegenPoint point);
 88 
 89   // Returns true if the old generation marking completed (i.e., final mark executed for old generation).
 90   bool resume_concurrent_old_cycle(ShenandoahOldGeneration* generation, GCCause::Cause cause);
 91   void service_concurrent_cycle(ShenandoahGeneration* generation, GCCause::Cause cause, bool reset_old_bitmap_specially);
 92   void service_stw_full_cycle(GCCause::Cause cause);
 93   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahGC::ShenandoahDegenPoint point);
 94 
 95   void notify_gc_waiters();
 96 
 97   // Handle GC request.
 98   // Blocks until GC is over.
 99   void handle_requested_gc(GCCause::Cause cause);
100 
101   bool is_explicit_gc(GCCause::Cause cause) const;
102   bool is_implicit_gc(GCCause::Cause cause) const;
103 
104   // Returns true if the old generation marking was interrupted to allow a young cycle.
105   bool preempt_old_marking(ShenandoahGenerationType generation);
106 
107   void process_phase_timings(const ShenandoahGenerationalHeap* heap);
108 
109   void service_concurrent_normal_cycle(ShenandoahGenerationalHeap* heap,
110                                        ShenandoahGenerationType generation,
111                                        GCCause::Cause cause);
112 
113   void service_concurrent_old_cycle(ShenandoahGenerationalHeap* heap,
114                                     GCCause::Cause &cause);
115 
116   void set_gc_mode(GCMode new_mode);
117 
118   static const char* gc_mode_name(GCMode mode);
119 
120   void notify_control_thread();
121 
122   void service_concurrent_cycle(ShenandoahHeap* heap,
123                                 ShenandoahGeneration* generation,
124                                 GCCause::Cause &cause,
125                                 bool do_old_gc_bootstrap);
126 
127 };
128 
129 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALCONTROLTHREAD_HPP