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 "memory/allocation.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   BinaryMagnitudeSeq _delays;
  45   TruncatedSeq* _progress_history;
  46 
  47   volatile intptr_t _epoch;
  48   volatile jdouble _tax_rate;
  49 
  50   char _pad0[DEFAULT_CACHE_LINE_SIZE];
  51   volatile intptr_t _budget;
  52   char _pad1[DEFAULT_CACHE_LINE_SIZE];
  53   volatile intptr_t _progress;
  54   char _pad2[DEFAULT_CACHE_LINE_SIZE];
  55 
  56 public:
  57   ShenandoahPacer(ShenandoahHeap* heap) :
  58           _heap(heap),
  59           _progress_history(new TruncatedSeq(5)),
  60           _epoch(0),
  61           _tax_rate(1),
  62           _budget(0),
  63           _progress(PACING_PROGRESS_UNINIT) {
  64   }
  65 
  66   void setup_for_idle();
  67   void setup_for_mark();
  68   void setup_for_evac();
  69   void setup_for_updaterefs();
  70   void setup_for_traversal();
  71 
  72   inline void report_mark(size_t words);
  73   inline void report_evac(size_t words);
  74   inline void report_updaterefs(size_t words);
  75 
  76   inline void report_alloc(size_t words);
  77 
  78   bool claim_for_alloc(size_t words, bool force);
  79   void pace_for_alloc(size_t words);
  80   void unpace_for_alloc(intptr_t epoch, size_t words);
  81 
  82   intptr_t epoch();
  83 
  84   void print_on(outputStream* out) const;
  85 
  86 private:
  87   inline void report_internal(size_t words);
  88   inline void report_progress_internal(size_t words);
  89 
  90   void restart_with(jlong non_taxable_bytes, jdouble tax_rate);
  91 
  92   size_t update_and_get_progress_history();
  93 };
  94 
  95 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHPACER_HPP