1 /*
2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHPACER_HPP
25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHPACER_HPP
26 
27 #include "gc/shenandoah/shenandoahNumberSeq.hpp"
28 #include "gc/shenandoah/shenandoahPadding.hpp"
29 #include "memory/allocation.hpp"
30 
31 class ShenandoahHeap;
32 
33 #define PACING_PROGRESS_UNINIT (-1)
34 #define PACING_PROGRESS_ZERO   ( 0)
35 
36 /**
37  * ShenandoahPacer provides allocation pacing mechanism.
38  *
39  * Currently it implements simple tax-and-spend pacing policy: GC threads provide
40  * credit, allocating thread spend the credit, or stall when credit is not available.
41  */
42 class ShenandoahPacer : public CHeapObj<mtGC> {
43 private:
44   ShenandoahHeap* _heap;
45   double _last_time;
46   TruncatedSeq* _progress_history;
47   Monitor* _wait_monitor;
48   ShenandoahSharedFlag _need_notify_waiters;
49 
50   // Set once per phase
51   volatile intptr_t _epoch;
52   volatile double _tax_rate;
53 
54   // Heavily updated, protect from accidental false sharing
55   shenandoah_padding(0);
56   volatile intptr_t _budget;
57   shenandoah_padding(1);
58 
59   // Heavily updated, protect from accidental false sharing
60   shenandoah_padding(2);
61   volatile intptr_t _progress;
62   shenandoah_padding(3);
63 
64 public:
ShenandoahPacer(ShenandoahHeap * heap)65   ShenandoahPacer(ShenandoahHeap* heap) :
66           _heap(heap),
67           _last_time(os::elapsedTime()),
68           _progress_history(new TruncatedSeq(5)),
69           _wait_monitor(new Monitor(Mutex::leaf, "_wait_monitor", true, Monitor::_safepoint_check_always)),
70           _epoch(0),
71           _tax_rate(1),
72           _budget(0),
73           _progress(PACING_PROGRESS_UNINIT) {}
74 
75   void setup_for_idle();
76   void setup_for_mark();
77   void setup_for_evac();
78   void setup_for_updaterefs();
79 
80   void setup_for_reset();
81   void setup_for_preclean();
82 
83   inline void report_mark(size_t words);
84   inline void report_evac(size_t words);
85   inline void report_updaterefs(size_t words);
86 
87   inline void report_alloc(size_t words);
88 
89   bool claim_for_alloc(size_t words, bool force);
90   void pace_for_alloc(size_t words);
91   void unpace_for_alloc(intptr_t epoch, size_t words);
92 
93   void notify_waiters();
94 
95   intptr_t epoch();
96 
97   void flush_stats_to_cycle();
98   void print_cycle_on(outputStream* out);
99 
100 private:
101   inline void report_internal(size_t words);
102   inline void report_progress_internal(size_t words);
103 
104   inline void add_budget(size_t words);
105   void restart_with(size_t non_taxable_bytes, double tax_rate);
106 
107   size_t update_and_get_progress_history();
108 
109   void wait(size_t time_ms);
110 };
111 
112 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHPACER_HPP
113