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 #include "runtime/task.hpp"
 33 
 34 class ShenandoahHeap;
 35 class ShenandoahPacer;
 36 
 37 
 38 // Periodic task to notify blocked paced waiters.
 39 class ShenandoahPeriodicPacerNotifyTask : public PeriodicTask {
 40 private:
 41   ShenandoahPacer* const _pacer;
 42 public:
 43   explicit ShenandoahPeriodicPacerNotifyTask(ShenandoahPacer* pacer) :
 44     PeriodicTask(PeriodicTask::min_interval),
 45     _pacer(pacer) { }
 46 
 47   void task() override;
 48 };
 49 
 50 
 51 #define PACING_PROGRESS_UNINIT (-1)
 52 #define PACING_PROGRESS_ZERO   ( 0)
 53 
 54 /**
 55  * ShenandoahPacer provides allocation pacing mechanism.
 56  *
 57  * Currently it implements simple tax-and-spend pacing policy: GC threads provide
 58  * credit, allocating thread spend the credit, or stall when credit is not available.
 59  */
 60 class ShenandoahPacer : public CHeapObj<mtGC> {
 61 private:
 62   ShenandoahHeap* _heap;
 63   double _last_time;
 64   TruncatedSeq* _progress_history;
 65   Monitor* _wait_monitor;
 66   ShenandoahSharedFlag _need_notify_waiters;
 67   ShenandoahPeriodicPacerNotifyTask _notify_waiters_task;
 68 
 69   // Set once per phase
 70   volatile intptr_t _epoch;
 71   volatile double _tax_rate;
 72 
 73   // Heavily updated, protect from accidental false sharing
 74   shenandoah_padding(0);
 75   volatile intptr_t _budget;
 76   shenandoah_padding(1);
 77 
 78   // Heavily updated, protect from accidental false sharing
 79   shenandoah_padding(2);
 80   volatile intptr_t _progress;
 81   shenandoah_padding(3);
 82 
 83 public:
 84   explicit ShenandoahPacer(ShenandoahHeap* heap) :
 85           _heap(heap),
 86           _last_time(os::elapsedTime()),
 87           _progress_history(new TruncatedSeq(5)),
 88           _wait_monitor(new Monitor(Mutex::safepoint-1, "ShenandoahWaitMonitor_lock", true)),
 89           _notify_waiters_task(this),
 90           _epoch(0),
 91           _tax_rate(1),
 92           _budget(0),
 93           _progress(PACING_PROGRESS_UNINIT) {
 94     _notify_waiters_task.enroll();
 95   }
 96 
 97   void setup_for_idle();
 98   void setup_for_mark();
 99   void setup_for_evac();
100   void setup_for_updaterefs();
101 
102   void setup_for_reset();
103 
104   inline void report_mark(size_t words);
105   inline void report_evac(size_t words);
106   inline void report_updaterefs(size_t words);
107 
108   inline void report_alloc(size_t words);
109 
110   bool claim_for_alloc(size_t words, bool force);
111   void pace_for_alloc(size_t words);
112   void unpace_for_alloc(intptr_t epoch, size_t words);
113 
114   void notify_waiters();
115 
116   intptr_t epoch();
117 
118   void flush_stats_to_cycle();
119   void print_cycle_on(outputStream* out);
120 
121 private:
122   inline void report_internal(size_t words);
123   inline void report_progress_internal(size_t words);
124 
125   inline void add_budget(size_t words);
126   void restart_with(size_t non_taxable_bytes, double tax_rate);
127 
128   size_t update_and_get_progress_history();
129 
130   void wait(size_t time_ms);
131 };
132 
133 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHPACER_HPP