1 /*
   2  * Copyright (c) 2011, 2018, 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_G1MONITORINGSUPPORT_HPP
  26 #define SHARE_VM_GC_G1_G1MONITORINGSUPPORT_HPP
  27 
  28 #include "gc/shared/generationCounters.hpp"
  29 
  30 class CollectorCounters;
  31 class G1CollectedHeap;
  32 class HSpaceCounters;
  33 
  34 // Class for monitoring logical spaces in G1. It provides data for
  35 // both G1's jstat counters as well as G1's memory pools.
  36 //
  37 // G1 splits the heap into heap regions and each heap region belongs
  38 // to one of the following categories:
  39 //
  40 // * eden      : regions that have been allocated since the last GC
  41 // * survivors : regions with objects that survived the last few GCs
  42 // * old       : long-lived non-humongous regions
  43 // * humongous : humongous regions
  44 // * free      : free regions
  45 //
  46 // The combination of eden and survivor regions form the equivalent of
  47 // the young generation in the other GCs. The combination of old and
  48 // humongous regions form the equivalent of the old generation in the
  49 // other GCs. Free regions do not have a good equivalent in the other
  50 // GCs given that they can be allocated as any of the other region types.
  51 //
  52 // The monitoring tools expect the heap to contain a number of
  53 // generations (young, old, perm) and each generation to contain a
  54 // number of spaces (young: eden, survivors, old). Given that G1 does
  55 // not maintain those spaces physically (e.g., the set of
  56 // non-contiguous eden regions can be considered as a "logical"
  57 // space), we'll provide the illusion that those generations and
  58 // spaces exist. In reality, each generation and space refers to a set
  59 // of heap regions that are potentially non-contiguous.
  60 //
  61 // This class provides interfaces to access the min, current, and max
  62 // capacity and current occupancy for each of G1's logical spaces and
  63 // generations we expose to the monitoring tools. Also provided are
  64 // counters for G1 concurrent collections and stop-the-world full heap
  65 // collections.
  66 //
  67 // Below is a description of how the various sizes are calculated.
  68 //
  69 // * Current Capacity
  70 //
  71 //    - heap_capacity = current heap capacity (e.g., current committed size)
  72 //    - young_gen_capacity = current max young gen target capacity
  73 //          (i.e., young gen target capacity + max allowed expansion capacity)
  74 //    - survivor_capacity = current survivor region capacity
  75 //    - eden_capacity = young_gen_capacity - survivor_capacity
  76 //    - old_capacity = heap_capacity - young_gen_capacity
  77 //
  78 //    What we do in the above is to distribute the free regions among
  79 //    eden_capacity and old_capacity.
  80 //
  81 // * Occupancy
  82 //
  83 //    - young_gen_used = current young region capacity
  84 //    - survivor_used = survivor_capacity
  85 //    - eden_used = young_gen_used - survivor_used
  86 //    - old_used = overall_used - young_gen_used
  87 //
  88 //    Unfortunately, we currently only keep track of the number of
  89 //    currently allocated young and survivor regions + the overall used
  90 //    bytes in the heap, so the above can be a little inaccurate.
  91 //
  92 // * Min Capacity
  93 //
  94 //    We set this to 0 for all spaces.
  95 //
  96 // * Max Capacity
  97 //
  98 //    For jstat, we set the max capacity of all spaces to heap_capacity,
  99 //    given that we don't always have a reasonable upper bound on how big
 100 //    each space can grow. For the memory pools, we make the max
 101 //    capacity undefined with the exception of the old memory pool for
 102 //    which we make the max capacity same as the max heap capacity.
 103 //
 104 // If we had more accurate occupancy / capacity information per
 105 // region set the above calculations would be greatly simplified and
 106 // be made more accurate.
 107 //
 108 // We update all the above synchronously and we store the results in
 109 // fields so that we just read said fields when needed. A subtle point
 110 // is that all the above sizes need to be recalculated when the old
 111 // gen changes capacity (after a GC or after a humongous allocation)
 112 // but only the eden occupancy changes when a new eden region is
 113 // allocated. So, in the latter case we have minimal recalculation to
 114 // do which is important as we want to keep the eden region allocation
 115 // path as low-overhead as possible.
 116 
 117 class G1MonitoringSupport : public CHeapObj<mtGC> {
 118   friend class VMStructs;
 119 
 120   G1CollectedHeap* _g1h;
 121 
 122   // jstat performance counters
 123   //  incremental collections both young and mixed
 124   CollectorCounters*   _incremental_collection_counters;
 125   //  full stop-the-world collections
 126   CollectorCounters*   _full_collection_counters;
 127   //  stop-the-world phases in G1
 128   CollectorCounters*   _conc_collection_counters;
 129   //  young collection set counters.  The _eden_counters,
 130   // _from_counters, and _to_counters are associated with
 131   // this "generational" counter.
 132   GenerationCounters*  _young_collection_counters;
 133   //  old collection set counters. The _old_space_counters
 134   // below are associated with this "generational" counter.
 135   GenerationCounters*  _old_collection_counters;
 136   // Counters for the capacity and used for
 137   //   the whole heap
 138   HSpaceCounters*      _old_space_counters;
 139   //   the young collection
 140   HSpaceCounters*      _eden_counters;
 141   //   the survivor collection (only one, _to_counters, is actively used)
 142   HSpaceCounters*      _from_counters;
 143   HSpaceCounters*      _to_counters;
 144 
 145   // When it's appropriate to recalculate the various sizes (at the
 146   // end of a GC, when a new eden region is allocated, etc.) we store
 147   // them here so that we can easily report them when needed and not
 148   // have to recalculate them every time.
 149 
 150   size_t _overall_reserved;
 151   size_t _overall_committed;
 152   size_t _overall_used;
 153 
 154   uint   _young_region_num;
 155   size_t _young_gen_committed;
 156   size_t _eden_committed;
 157   size_t _eden_used;
 158   size_t _survivor_committed;
 159   size_t _survivor_used;
 160 
 161   size_t _old_committed;
 162   size_t _old_used;
 163 
 164   G1CollectedHeap* g1h() { return _g1h; }
 165 
 166   // It returns x - y if x > y, 0 otherwise.
 167   // As described in the comment above, some of the inputs to the
 168   // calculations we have to do are obtained concurrently and hence
 169   // may be inconsistent with each other. So, this provides a
 170   // defensive way of performing the subtraction and avoids the value
 171   // going negative (which would mean a very large result, given that
 172   // the parameter are size_t).
 173   static size_t subtract_up_to_zero(size_t x, size_t y) {
 174     if (x > y) {
 175       return x - y;
 176     } else {
 177       return 0;
 178     }
 179   }
 180 
 181   // Recalculate all the sizes.
 182   void recalculate_sizes();
 183   // Recalculate only what's necessary when a new eden region is allocated.
 184   void recalculate_eden_size();
 185 
 186  public:
 187   G1MonitoringSupport(G1CollectedHeap* g1h);
 188 
 189   // Unfortunately, the jstat tool assumes that no space has 0
 190   // capacity. In our case, given that each space is logical, it's
 191   // possible that no regions will be allocated to it, hence to have 0
 192   // capacity (e.g., if there are no survivor regions, the survivor
 193   // space has 0 capacity). The way we deal with this is to always pad
 194   // each capacity value we report to jstat by a very small amount to
 195   // make sure that it's never zero. Given that we sometimes have to
 196   // report a capacity of a generation that contains several spaces
 197   // (e.g., young gen includes one eden, two survivor spaces), the
 198   // mult parameter is provided in order to adding the appropriate
 199   // padding multiple times so that the capacities add up correctly.
 200   static size_t pad_capacity(size_t size_bytes, size_t mult = 1) {
 201     return size_bytes + MinObjAlignmentInBytes * mult;
 202   }
 203 
 204   // Recalculate all the sizes from scratch and update all the jstat
 205   // counters accordingly.
 206   void update_sizes();
 207   // Recalculate only what's necessary when a new eden region is
 208   // allocated and update any jstat counters that need to be updated.
 209   void update_eden_size();
 210 
 211   CollectorCounters* incremental_collection_counters() {
 212     return _incremental_collection_counters;
 213   }
 214   CollectorCounters* full_collection_counters() {
 215     return _full_collection_counters;
 216   }
 217   CollectorCounters* conc_collection_counters() {
 218     return _conc_collection_counters;
 219   }
 220   GenerationCounters* young_collection_counters() {
 221     return _young_collection_counters;
 222   }
 223   GenerationCounters* old_collection_counters() {
 224     return _old_collection_counters;
 225   }
 226   HSpaceCounters*      old_space_counters() { return _old_space_counters; }
 227   HSpaceCounters*      eden_counters() { return _eden_counters; }
 228   HSpaceCounters*      from_counters() { return _from_counters; }
 229   HSpaceCounters*      to_counters() { return _to_counters; }
 230 
 231   // Monitoring support used by
 232   //   MemoryService
 233   //   jstat counters
 234   //   Tracing
 235 
 236   size_t overall_reserved()           { return _overall_reserved;     }
 237   size_t overall_committed()          { return _overall_committed;    }
 238   size_t overall_used()               { return _overall_used;         }
 239 
 240   size_t young_gen_committed()        { return _young_gen_committed;  }
 241   size_t young_gen_max()              { return overall_reserved();    }
 242   size_t eden_space_committed()       { return _eden_committed;       }
 243   size_t eden_space_used()            { return _eden_used;            }
 244   size_t survivor_space_committed()   { return _survivor_committed;   }
 245   size_t survivor_space_used()        { return _survivor_used;        }
 246 
 247   size_t old_gen_committed()          { return old_space_committed(); }
 248   size_t old_gen_max()                { return overall_reserved();    }
 249   size_t old_space_committed()        { return _old_committed;        }
 250   size_t old_space_used()             { return _old_used;             }
 251 };
 252 
 253 class G1GenerationCounters: public GenerationCounters {
 254 protected:
 255   G1MonitoringSupport* _g1mm;
 256 
 257 public:
 258   G1GenerationCounters(G1MonitoringSupport* g1mm,
 259                        const char* name, int ordinal, int spaces,
 260                        size_t min_capacity, size_t max_capacity,
 261                        size_t curr_capacity);
 262 };
 263 
 264 class G1YoungGenerationCounters: public G1GenerationCounters {
 265 public:
 266   G1YoungGenerationCounters(G1MonitoringSupport* g1mm, const char* name);
 267   virtual void update_all();
 268 };
 269 
 270 class G1OldGenerationCounters: public G1GenerationCounters {
 271 public:
 272   G1OldGenerationCounters(G1MonitoringSupport* g1mm, const char* name);
 273   virtual void update_all();
 274 };
 275 
 276 #endif // SHARE_VM_GC_G1_G1MONITORINGSUPPORT_HPP
--- EOF ---