1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. 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_VM_GC_G1_G1IHOPCONTROL_HPP
  26 #define SHARE_VM_GC_G1_G1IHOPCONTROL_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/numberSeq.hpp"
  30 
  31 class G1Predictions;
  32 
  33 // Base class for algorithms that calculate the heap occupancy at which
  34 // concurrent marking should start. This heap usage threshold should be relative
  35 // to old gen size.
  36 class G1IHOPControl : public CHeapObj<mtGC> {
  37  protected:
  38   // The initial IHOP value relative to the target occupancy.
  39   double _initial_ihop_percent;
  40   // The target maximum occupancy of the heap.
  41   size_t _target_occupancy;
  42 
  43   // Most recent complete mutator allocation period in seconds.
  44   double _last_allocation_time_s;
  45   // Amount of bytes allocated during _last_allocation_time_s.
  46   size_t _last_allocated_bytes;
  47 
  48   // Initialize an instance with the initial IHOP value in percent and the target
  49   // occupancy. The target occupancy is the number of bytes when marking should
  50   // be finished and reclaim started.
  51   G1IHOPControl(double initial_ihop_percent, size_t target_occupancy);
  52 
  53   // Most recent time from the end of the initial mark to the start of the first
  54   // mixed gc.
  55   virtual double last_marking_length_s() const = 0;
  56  public:
  57   virtual ~G1IHOPControl() { }
  58 
  59   // Get the current non-young occupancy at which concurrent marking should start.
  60   virtual size_t get_conc_mark_start_threshold() = 0;
  61 
  62   // Update information about time during which allocations in the Java heap occurred,
  63   // how large these allocations were in bytes, and an additional buffer.
  64   // The allocations should contain any amount of space made unusable for further
  65   // allocation, e.g. any waste caused by TLAB allocation, space at the end of
  66   // humongous objects that can not be used for allocation, etc.
  67   // Together with the target occupancy, this additional buffer should contain the
  68   // difference between old gen size and total heap size at the start of reclamation,
  69   // and space required for that reclamation.
  70   virtual void update_allocation_info(double allocation_time_s, size_t allocated_bytes, size_t additional_buffer_size);
  71   // Update the time spent in the mutator beginning from the end of initial mark to
  72   // the first mixed gc.
  73   virtual void update_marking_length(double marking_length_s) = 0;
  74 
  75   virtual void print();
  76 };
  77 
  78 // The returned concurrent mark starting occupancy threshold is a fixed value
  79 // relative to the maximum heap size.
  80 class G1StaticIHOPControl : public G1IHOPControl {
  81   // Most recent mutator time between the end of initial mark to the start of the
  82   // first mixed gc.
  83   double _last_marking_length_s;
  84  protected:
  85   double last_marking_length_s() const { return _last_marking_length_s; }
  86  public:
  87   G1StaticIHOPControl(double ihop_percent, size_t target_occupancy);
  88 
  89   size_t get_conc_mark_start_threshold() { return (size_t) (_initial_ihop_percent * _target_occupancy / 100.0); }
  90 
  91   virtual void update_marking_length(double marking_length_s) {
  92    assert(marking_length_s > 0.0, "Marking length must be larger than zero but is %.3f", marking_length_s);
  93     _last_marking_length_s = marking_length_s;
  94   }
  95 
  96 #ifndef PRODUCT
  97   static void test();
  98 #endif
  99 };
 100 
 101 // This algorithm tries to return a concurrent mark starting occupancy value that
 102 // makes sure that during marking the given target occupancy is never exceeded,
 103 // based on predictions of current allocation rate and time periods between
 104 // initial mark and the first mixed gc.
 105 class G1AdaptiveIHOPControl : public G1IHOPControl {
 106   size_t _heap_reserve_percent; // Percentage of maximum heap capacity we should avoid to touch
 107   size_t _heap_waste_percent;   // Percentage of free heap that should be considered as waste.
 108 
 109   const G1Predictions * _predictor;
 110 
 111   TruncatedSeq _marking_times_s;
 112   TruncatedSeq _allocation_rate_s;
 113 
 114   size_t _last_allocation_bytes; // Most recent mutator allocation since last GC.
 115   // The most recent unrestrained size of the young gen. This is used as an additional
 116   // factor in the calculation of the threshold, as the threshold is based on
 117   // non-young gen occupancy at the end of GC. For the IHOP threshold, we need to
 118   // consider the young gen size during that time too.
 119   // Since we cannot know what young gen sizes are used in the future, we will just
 120   // use the current one. We expect that this one will be one with a fairly large size,
 121   // as there is no marking or mixed gc that could impact its size too much.
 122   size_t _last_unrestrained_young_size;
 123 
 124   bool have_enough_data_for_prediction() const;
 125 
 126   // The "actual" target threshold the algorithm wants to keep during and at the
 127   // end of marking. This is typically lower than the requested threshold, as the
 128   // algorithm needs to consider restrictions by the environment.
 129   size_t actual_target_threshold() const;
 130  protected:
 131   virtual double last_marking_length_s() const { return _marking_times_s.last(); }
 132  public:
 133   G1AdaptiveIHOPControl(double ihop_percent,
 134                         size_t initial_target_occupancy,
 135                         G1Predictions const* predictor,
 136                         size_t heap_reserve_percent, // The percentage of total heap capacity that should not be tapped into.
 137                         size_t heap_waste_percent);  // The percentage of the free space in the heap that we think is not usable for allocation.
 138 
 139   virtual size_t get_conc_mark_start_threshold();
 140 
 141   virtual void update_allocation_info(double allocation_time_s, size_t allocated_bytes, size_t additional_buffer_size);
 142   virtual void update_marking_length(double marking_length_s);
 143 
 144   virtual void print();
 145 #ifndef PRODUCT
 146   static void test();
 147 #endif
 148 };
 149 
 150 #endif // SHARE_VM_GC_G1_G1IHOPCONTROL_HPP