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 "memory/allocation.hpp"
  31 
  32 class ShenandoahHeap;
  33 
  34 #define PACING_PROGRESS_UNINIT (-1)
  35 #define PACING_PROGRESS_ZERO   ( 0)
  36 
  37 /**
  38  * ShenandoahPacer provides allocation pacing mechanism.
  39  *
  40  * Currently it implements simple tax-and-spend pacing policy: GC threads provide
  41  * credit, allocating thread spend the credit, or stall when credit is not available.
  42  */
  43 class ShenandoahPacer : public CHeapObj<mtGC> {
  44 private:
  45   ShenandoahHeap* _heap;
  46   BinaryMagnitudeSeq _delays;
  47   TruncatedSeq* _progress_history;
  48 
  49   // Set once per phase
  50   volatile intptr_t _epoch;
  51   volatile double _tax_rate;
  52 
  53   // Heavily updated, protect from accidental false sharing
  54   shenandoah_padding(0);
  55   volatile intptr_t _budget;
  56   shenandoah_padding(1);
  57 
  58   // Heavily updated, protect from accidental false sharing
  59   shenandoah_padding(2);
  60   volatile intptr_t _progress;
  61   shenandoah_padding(3);
  62 
  63 public:
  64   ShenandoahPacer(ShenandoahHeap* heap) :
  65           _heap(heap),
  66           _progress_history(new TruncatedSeq(5)),
  67           _epoch(0),
  68           _tax_rate(1),
  69           _budget(0),
  70           _progress(PACING_PROGRESS_UNINIT) {}
  71 
  72   void setup_for_idle();
  73   void setup_for_mark();
  74   void setup_for_evac_update();
  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_update(size_t words);
  81 
  82   inline void report_alloc(size_t words);
  83 
  84   bool claim_for_alloc(size_t words, bool force);
  85   void pace_for_alloc(size_t words);
  86   void unpace_for_alloc(intptr_t epoch, size_t words);
  87 
  88   intptr_t epoch();
  89 
  90   void print_on(outputStream* out) const;
  91 
  92 private:
  93   inline void report_internal(size_t words);
  94   inline void report_progress_internal(size_t words);
  95 
  96   void restart_with(size_t non_taxable_bytes, double tax_rate);
  97 
  98   size_t update_and_get_progress_history();
  99 };
 100 
 101 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHPACER_HPP