1 /*
  2  * Copyright Amazon.com Inc. 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_SHENANDOAH_SHENANDOAHCONTROLLER_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLLER_HPP
 27 
 28 #include "gc/shared/gcCause.hpp"
 29 #include "gc/shared/concurrentGCThread.hpp"
 30 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
 31 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 32 
 33 /**
 34  * This interface exposes methods necessary for the heap to interact
 35  * with the threads responsible for driving the collection cycle.
 36  */
 37 class ShenandoahController: public ConcurrentGCThread {
 38 private:
 39   ShenandoahSharedFlag _graceful_shutdown;
 40 
 41   shenandoah_padding(0);
 42   volatile size_t _allocs_seen;
 43   shenandoah_padding(1);
 44   volatile size_t _gc_id;
 45   shenandoah_padding(2);
 46 
 47 protected:
 48   ShenandoahSharedFlag _alloc_failure_gc;
 49   ShenandoahSharedFlag _humongous_alloc_failure_gc;
 50 
 51   // While we could have a single lock for these, it may risk unblocking
 52   // GC waiters when alloc failure GC cycle finishes. We want instead
 53   // to make complete explicit cycle for demanding customers.
 54   Monitor _alloc_failure_waiters_lock;
 55   Monitor _gc_waiters_lock;
 56 
 57 public:
 58   ShenandoahController():
 59     ConcurrentGCThread(),
 60     _allocs_seen(0),
 61     _gc_id(0),
 62     _alloc_failure_waiters_lock(Mutex::safepoint-2, "ShenandoahAllocFailureGC_lock", true),
 63     _gc_waiters_lock(Mutex::safepoint-2, "ShenandoahRequestedGC_lock", true)
 64   { }
 65 
 66   // Request a collection cycle. This handles "explicit" gc requests
 67   // like System.gc and "implicit" gc requests, like metaspace oom.
 68   virtual void request_gc(GCCause::Cause cause) = 0;
 69 
 70   // This cancels the collection cycle and has an option to block
 71   // until another cycle runs and clears the alloc failure gc flag.
 72   void handle_alloc_failure(ShenandoahAllocRequest& req, bool block);
 73 
 74   // Invoked for allocation failures during evacuation. This cancels
 75   // the collection cycle without blocking.
 76   void handle_alloc_failure_evac(size_t words);
 77 
 78   // Return true if setting the flag which indicates allocation failure succeeds.
 79   bool try_set_alloc_failure_gc(bool is_humongous);
 80 
 81   // Notify threads waiting for GC to complete.
 82   void notify_alloc_failure_waiters();
 83 
 84   // True if allocation failure flag has been set.
 85   bool is_alloc_failure_gc();
 86 
 87   // This is called for every allocation. The control thread accumulates
 88   // this value when idle. During the gc cycle, the control resets it
 89   // and reports it to the pacer.
 90   void pacing_notify_alloc(size_t words);
 91   size_t reset_allocs_seen();
 92 
 93   // These essentially allows to cancel a collection cycle for the
 94   // purpose of shutting down the JVM, without trying to start a degenerated
 95   // cycle.
 96   void prepare_for_graceful_shutdown();
 97   bool in_graceful_shutdown();
 98 
 99 
100   // Returns the internal gc count used by the control thread. Probably
101   // doesn't need to be exposed.
102   size_t get_gc_id();
103   void update_gc_id();
104 };
105 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLLER_HPP