1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   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 "memory/allocation.hpp"
  28 #include "utilities/numberSeq.hpp"
  29 
  30 class 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  */
  41 class ShenandoahPacer : public CHeapObj<mtGC> {
  42 private:
  43   ShenandoahHeap* _heap;
  44   volatile intptr_t _budget;
  45   volatile jdouble _tax_rate;
  46   BinaryMagnitudeSeq _delays;
  47 
  48   volatile intptr_t _progress;
  49   TruncatedSeq* _progress_history;
  50   volatile intptr_t _epoch;
  51 
  52 public:
  53   ShenandoahPacer(ShenandoahHeap* heap) :
  54           _heap(heap), _budget(0), _tax_rate(1),
  55           _progress(PACING_PROGRESS_UNINIT),
  56           _progress_history(new TruncatedSeq(5)),
  57           _epoch(0) {
  58   }
  59 
  60   void setup_for_idle();
  61   void setup_for_mark();
  62   void setup_for_evac();
  63   void setup_for_updaterefs();
  64 
  65   inline void report_mark(size_t words);
  66   inline void report_evac(size_t words);
  67   inline void report_updaterefs(size_t words);
  68 
  69   inline void report_alloc(size_t words);
  70 
  71   bool claim_for_alloc(size_t words, bool force);
  72   void pace_for_alloc(size_t words);
  73   void unpace_for_alloc(intptr_t epoch, size_t words);
  74 
  75   intptr_t epoch();
  76 
  77   void print_on(outputStream* out) const;
  78 
  79 private:
  80   inline void report_internal(size_t words);
  81   inline void report_progress_internal(size_t words);
  82 
  83   void restart_with(jlong non_taxable_bytes, jdouble tax_rate);
  84 
  85   size_t update_and_get_progress_history();
  86 };
  87 
  88 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHPACER_HPP