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/shenandoahGC.hpp"
 32 #include "gc/shenandoah/shenandoahHeap.hpp"
 33 #include "gc/shenandoah/shenandoahPadding.hpp"
 34 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 35 
 36 class ShenandoahGenerationalControlThread: public ShenandoahController {
 37   friend class VMStructs;
 38 
 39 public:
 40   typedef enum {
 41     none,
 42     concurrent_normal,
 43     stw_degenerated,
 44     stw_full,
 45     bootstrapping_old,
 46     servicing_old
 47   } GCMode;
 48 
 49 private:
 50   Monitor _control_lock;
 51   Monitor _regulator_lock;
 52 
 53   ShenandoahSharedFlag _allow_old_preemption;
 54   ShenandoahSharedFlag _preemption_requested;
 55 
 56   GCCause::Cause  _requested_gc_cause;
 57   volatile ShenandoahGenerationType _requested_generation;
 58   ShenandoahGC::ShenandoahDegenPoint _degen_point;
 59   ShenandoahGeneration* _degen_generation;
 60 
 61   shenandoah_padding(0);
 62   volatile GCMode _mode;
 63   shenandoah_padding(1);
 64 
 65 public:
 66   ShenandoahGenerationalControlThread();
 67 
 68   void run_service() override;
 69   void stop_service() override;
 70 
 71   void request_gc(GCCause::Cause cause) override;
 72 
 73   // Return true if the request to start a concurrent GC for the given generation succeeded.
 74   bool request_concurrent_gc(ShenandoahGenerationType generation);
 75 
 76   GCMode gc_mode() {
 77     return _mode;
 78   }
 79 private:
 80 
 81   // Returns true if the cycle has been cancelled or degenerated.
 82   bool check_cancellation_or_degen(ShenandoahGC::ShenandoahDegenPoint point);
 83 
 84   // Returns true if the old generation marking completed (i.e., final mark executed for old generation).
 85   bool resume_concurrent_old_cycle(ShenandoahGeneration* generation, GCCause::Cause cause);
 86   void service_concurrent_cycle(ShenandoahGeneration* generation, GCCause::Cause cause, bool reset_old_bitmap_specially);
 87   void service_stw_full_cycle(GCCause::Cause cause);
 88   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahGC::ShenandoahDegenPoint point);
 89 
 90   void notify_gc_waiters();
 91 
 92   // Handle GC request.
 93   // Blocks until GC is over.
 94   void handle_requested_gc(GCCause::Cause cause);
 95 
 96   bool is_explicit_gc(GCCause::Cause cause) const;
 97   bool is_implicit_gc(GCCause::Cause cause) const;
 98 
 99   // Returns true if the old generation marking was interrupted to allow a young cycle.
100   bool preempt_old_marking(ShenandoahGenerationType generation);
101 
102   void process_phase_timings(const ShenandoahHeap* heap);
103 
104   void service_concurrent_normal_cycle(ShenandoahHeap* heap,
105                                        ShenandoahGenerationType generation,
106                                        GCCause::Cause cause);
107 
108   void service_concurrent_old_cycle(ShenandoahHeap* heap,
109                                     GCCause::Cause &cause);
110 
111   void set_gc_mode(GCMode new_mode);
112 
113   static const char* gc_mode_name(GCMode mode);
114 
115   void notify_control_thread();
116 
117   void service_concurrent_cycle(ShenandoahHeap* heap,
118                                 ShenandoahGeneration* generation,
119                                 GCCause::Cause &cause,
120                                 bool do_old_gc_bootstrap);
121 
122 };
123 
124 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALCONTROLTHREAD_HPP