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_SHENANDOAHUNCOMMITTHREAD
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHUNCOMMITTHREAD
27 
28 #include "gc/shared/concurrentGCThread.hpp"
29 
30 class ShenandoahHeap;
31 
32 class ShenandoahUncommitThread : public ConcurrentGCThread {
33   ShenandoahHeap* const _heap;
34 
35   // Indicates that `SoftMaxHeapSize` has changed
36   ShenandoahSharedFlag _soft_max_changed;
37 
38   // Indicates that an explicit gc has been requested
39   ShenandoahSharedFlag _explicit_gc_requested;
40 
41   // Indicates whether it is safe to uncommit regions
42   ShenandoahSharedFlag _uncommit_allowed;
43 
44   // Indicates that regions are being actively uncommitted
45   ShenandoahSharedFlag _uncommit_in_progress;
46 
47   // This lock is used to coordinate allowing or forbidding regions to be uncommitted
48   Monitor _uncommit_lock;
49 
50   // True if there are regions to uncommit and uncommits are allowed
51   bool should_uncommit(double shrink_before, size_t shrink_until) const;
52 
53   // True if there are regions that have been empty for longer than ShenandoahUncommitDelay and the committed
54   // memory is higher than soft max capacity or minimum capacity
55   bool has_work(double shrink_before, size_t shrink_until) const;
56 
57   // Perform the work of uncommitting empty regions
58   void uncommit(double shrink_before, size_t shrink_until);
59 
60   // True if the control thread has allowed this thread to uncommit regions
61   bool is_uncommit_allowed() const;
62 
63   // Iterate over and uncommit eligible regions until committed heap falls below
64   // `shrink_until` bytes. A region is eligible for uncommit if the timestamp at which
65   // it was last made empty is before `shrink_before` seconds since jvm start.
66   // Returns the number of regions uncommitted. May be interrupted by `forbid_uncommit`.
67   size_t do_uncommit_work(double shrink_before, size_t shrink_until) const;
68 
69 public:
70   explicit ShenandoahUncommitThread(ShenandoahHeap* heap);
71 
72   // Periodically check for regions to uncommit
73   void run_service() override;
74 
75   // Wake up this thread and try to uncommit for changed soft max size
76   void notify_soft_max_changed();
77 
78   // Wake up this thread and try to uncommit for min heap size
79   void notify_explicit_gc_requested();
80 
81   // Wait for uncommit operations to stop, returns immediately if uncommit thread is idle
82   void forbid_uncommit();
83 
84   // Allows uncommit operations to happen, does not block
85   void allow_uncommit();
86 
87   // True if uncommit is in progress
88   bool is_uncommit_in_progress() const {
89     return _uncommit_in_progress.is_set();
90   }
91 protected:
92   // Interrupt and stop this thread
93   void stop_service() override;
94 };
95 
96 
97 #endif //SHARE_GC_SHENANDOAH_SHENANDOAHUNCOMMITTHREAD