1 /*
  2  * Copyright (c) 2018, 2019, Red Hat, Inc. 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_SHENANDOAHPACER_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHPACER_HPP
 27 
 28 #include "gc/shenandoah/shenandoahNumberSeq.hpp"
 29 #include "gc/shenandoah/shenandoahPadding.hpp"
 30 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 31 #include "memory/allocation.hpp"
 32 
 33 class ShenandoahHeap;
 34 
 35 #define PACING_PROGRESS_UNINIT (-1)
 36 #define PACING_PROGRESS_ZERO   ( 0)
 37 
 38 /**
 39  * ShenandoahPacer provides allocation pacing mechanism.
 40  *
 41  * Currently it implements simple tax-and-spend pacing policy: GC threads provide
 42  * credit, allocating thread spend the credit, or stall when credit is not available.
 43  */
 44 class ShenandoahPacer : public CHeapObj<mtGC> {
 45 private:
 46   ShenandoahHeap* _heap;
 47   double _last_time;
 48   TruncatedSeq* _progress_history;
 49   Monitor* _wait_monitor;
 50   ShenandoahSharedFlag _need_notify_waiters;
 51 
 52   // Set once per phase
 53   volatile intptr_t _epoch;
 54   volatile double _tax_rate;
 55 
 56   // Heavily updated, protect from accidental false sharing
 57   shenandoah_padding(0);
 58   volatile intptr_t _budget;
 59   shenandoah_padding(1);
 60 
 61   // Heavily updated, protect from accidental false sharing
 62   shenandoah_padding(2);
 63   volatile intptr_t _progress;
 64   shenandoah_padding(3);
 65 
 66 public:
 67   ShenandoahPacer(ShenandoahHeap* heap) :
 68           _heap(heap),
 69           _last_time(os::elapsedTime()),
 70           _progress_history(new TruncatedSeq(5)),
 71           _wait_monitor(new Monitor(Mutex::safepoint-1, "ShenandoahWaitMonitor_lock", true)),
 72           _epoch(0),
 73           _tax_rate(1),
 74           _budget(0),
 75           _progress(PACING_PROGRESS_UNINIT) {}
 76 
 77   void setup_for_idle();
 78   void setup_for_mark();
 79   void setup_for_evac();
 80   void setup_for_updaterefs();
 81 
 82   void setup_for_reset();
 83 
 84   inline void report_mark(size_t words);
 85   inline void report_evac(size_t words);
 86   inline void report_updaterefs(size_t words);
 87 
 88   inline void report_alloc(size_t words);
 89 
 90   bool claim_for_alloc(size_t words, bool force);
 91   void pace_for_alloc(size_t words);
 92   void unpace_for_alloc(intptr_t epoch, size_t words);
 93 
 94   void notify_waiters();
 95 
 96   intptr_t epoch();
 97 
 98   void flush_stats_to_cycle();
 99   void print_cycle_on(outputStream* out);
100 
101 private:
102   inline void report_internal(size_t words);
103   inline void report_progress_internal(size_t words);
104 
105   inline void add_budget(size_t words);
106   void restart_with(size_t non_taxable_bytes, double tax_rate);
107 
108   size_t update_and_get_progress_history();
109 
110   void wait(size_t time_ms);
111 };
112 
113 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHPACER_HPP