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_GC_SHENANDOAH_SHENANDOAHPACER_HPP
25#define SHARE_GC_SHENANDOAH_SHENANDOAHPACER_HPP
26
27#include "gc/shenandoah/shenandoahNumberSeq.hpp"
28#include "memory/allocation.hpp"
29
30class ShenandoahHeap;
31
32#define PACING_PROGRESS_UNINIT (-1)
33#define PACING_PROGRESS_ZERO ( 0)
34
35/**
36 * ShenandoahPacer provides allocation pacing mechanism.
37 *
38 * Currently it implements simple tax-and-spend pacing policy: GC threads provide
39 * credit, allocating thread spend the credit, or stall when credit is not available.
40 */
41class ShenandoahPacer : public CHeapObj<mtGC> {
42private:
43 ShenandoahHeap* _heap;
44 BinaryMagnitudeSeq _delays;
45 TruncatedSeq* _progress_history;
46
47 // Set once per phase
48 volatile intptr_t _epoch;
49 volatile double _tax_rate;
50
51 // Heavily updated, protect from accidental false sharing
52 DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile intptr_t));
53 volatile intptr_t _budget;
54 DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
55
56 // Heavily updated, protect from accidental false sharing
57 DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile intptr_t));
58 volatile intptr_t _progress;
59 DEFINE_PAD_MINUS_SIZE(3, DEFAULT_CACHE_LINE_SIZE, 0);
60
61public:
62 ShenandoahPacer(ShenandoahHeap* heap) :
63 _heap(heap),
64 _progress_history(new TruncatedSeq(5)),
65 _epoch(0),
66 _tax_rate(1),
67 _budget(0),
68 _progress(PACING_PROGRESS_UNINIT) {}
69
70 void setup_for_idle();
71 void setup_for_mark();
72 void setup_for_evac();
73 void setup_for_updaterefs();
74 void setup_for_traversal();
75
76 inline void report_mark(size_t words);
77 inline void report_evac(size_t words);
78 inline void report_updaterefs(size_t words);
79
80 inline void report_alloc(size_t words);
81
82 bool claim_for_alloc(size_t words, bool force);
83 void pace_for_alloc(size_t words);
84 void unpace_for_alloc(intptr_t epoch, size_t words);
85
86 intptr_t epoch();
87
88 void print_on(outputStream* out) const;
89
90private:
91 inline void report_internal(size_t words);
92 inline void report_progress_internal(size_t words);
93
94 void restart_with(size_t non_taxable_bytes, double tax_rate);
95
96 size_t update_and_get_progress_history();
97};
98
99#endif // SHARE_GC_SHENANDOAH_SHENANDOAHPACER_HPP
100