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_implementation/shenandoah/shenandoahNumberSeq.hpp"
  28 #include "gc_implementation/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   volatile intptr_t _epoch;
  51   volatile jdouble _tax_rate;
  52 
  53   shenandoah_padding(0);
  54   volatile intptr_t _budget;
  55   shenandoah_padding(1);
  56   volatile intptr_t _progress;
  57   shenandoah_padding(2);
  58 
  59 public:
  60   ShenandoahPacer(ShenandoahHeap* heap) :
  61           _heap(heap),
  62           _last_time(os::elapsedTime()),
  63           _progress_history(new TruncatedSeq(5)),
  64           _wait_monitor(new Monitor(Mutex::leaf, "_wait_monitor", true)),
  65           _epoch(0),
  66           _tax_rate(1),
  67           _budget(0),
  68           _progress(PACING_PROGRESS_UNINIT) {
  69   }
  70 
  71   void setup_for_idle();
  72   void setup_for_mark();
  73   void setup_for_evac();
  74   void setup_for_updaterefs();
  75 
  76   void setup_for_reset();
  77   void setup_for_preclean();
  78 
  79   inline void report_mark(size_t words);
  80   inline void report_evac(size_t words);
  81   inline void report_updaterefs(size_t words);
  82 
  83   inline void report_alloc(size_t words);
  84 
  85   bool claim_for_alloc(size_t words, bool force);
  86   void pace_for_alloc(size_t words);
  87   void unpace_for_alloc(intptr_t epoch, size_t words);
  88 
  89   void notify_waiters();
  90 
  91   intptr_t epoch();
  92 
  93   void flush_stats_to_cycle();
  94   void print_cycle_on(outputStream* out);
  95 
  96 private:
  97   inline void report_internal(size_t words);
  98   inline void report_progress_internal(size_t words);
  99 
 100   inline void add_budget(size_t words);
 101   void restart_with(jlong non_taxable_bytes, jdouble tax_rate);
 102 
 103   size_t update_and_get_progress_history();
 104 
 105   void wait(size_t time_ms);
 106 };
 107 
 108 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHPACER_HPP