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   shenandoah_padding(0);
40   volatile size_t _allocs_seen;
41   shenandoah_padding(1);
42   // A monotonically increasing GC count.
43   volatile size_t _gc_id;
44   shenandoah_padding(2);
45 
46 protected:
47   // While we could have a single lock for these, it may risk unblocking
48   // GC waiters when alloc failure GC cycle finishes. We want instead
49   // to make complete explicit cycle for demanding customers.
50   Monitor _alloc_failure_waiters_lock;
51   Monitor _gc_waiters_lock;
52 
53   // Increments the internal GC count.
54   void update_gc_id();
55 
56 public:
57   ShenandoahController():
58     _allocs_seen(0),
59     _gc_id(0),
60     _alloc_failure_waiters_lock(Mutex::safepoint-2, "ShenandoahAllocFailureGC_lock", true),
61     _gc_waiters_lock(Mutex::safepoint-2, "ShenandoahRequestedGC_lock", true)
62   { }
63 
64   // Request a collection cycle. This handles "explicit" gc requests
65   // like System.gc and "implicit" gc requests, like metaspace oom.
66   virtual void request_gc(GCCause::Cause cause) = 0;
67 
68   // This cancels the collection cycle and has an option to block
69   // until another cycle completes successfully.
70   void handle_alloc_failure(const ShenandoahAllocRequest& req, bool block);
71 
72   // Invoked for allocation failures during evacuation. This cancels
73   // the collection cycle without blocking.
74   void handle_alloc_failure_evac(size_t words);
75 
76   // Notify threads waiting for GC to complete.
77   void notify_alloc_failure_waiters();
78 
79   // This is called for every allocation. The control thread accumulates
80   // this value when idle. During the gc cycle, the control resets it
81   // and reports it to the pacer.
82   void pacing_notify_alloc(size_t words);
83 
84   // Zeros out the number of allocations seen since the last GC cycle.
85   size_t reset_allocs_seen();
86 
87   // Return the value of a monotonic increasing GC count, maintained by the control thread.
88   size_t get_gc_id();
89 };
90 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLLER_HPP