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 #include "precompiled.hpp"
  26 #include "gc/g1/g1CollectedHeap.inline.hpp"
  27 #include "gc/g1/g1MonitoringSupport.hpp"
  28 #include "gc/g1/g1Policy.hpp"
  29 #include "gc/shared/collectorCounters.hpp"
  30 #include "gc/shared/hSpaceCounters.hpp"
  31 #include "memory/metaspaceCounters.hpp"
  32 
  33 G1GenerationCounters::G1GenerationCounters(G1MonitoringSupport* g1mm,
  34                                            const char* name,
  35                                            int ordinal, int spaces,
  36                                            size_t min_capacity,
  37                                            size_t max_capacity,
  38                                            size_t curr_capacity)
  39   : GenerationCounters(name, ordinal, spaces, min_capacity,
  40                        max_capacity, curr_capacity), _g1mm(g1mm) { }
  41 
  42 // We pad the capacity three times given that the young generation
  43 // contains three spaces (eden and two survivors).
  44 G1YoungGenerationCounters::G1YoungGenerationCounters(G1MonitoringSupport* g1mm,
  45                                                      const char* name)
  46   : G1GenerationCounters(g1mm, name, 0 /* ordinal */, 3 /* spaces */,
  47                G1MonitoringSupport::pad_capacity(0, 3) /* min_capacity */,
  48                G1MonitoringSupport::pad_capacity(g1mm->young_gen_max(), 3),
  49                G1MonitoringSupport::pad_capacity(0, 3) /* curr_capacity */) {
  50   if (UsePerfData) {
  51     update_all();
  52   }
  53 }
  54 
  55 G1OldGenerationCounters::G1OldGenerationCounters(G1MonitoringSupport* g1mm,
  56                                                  const char* name)
  57   : G1GenerationCounters(g1mm, name, 1 /* ordinal */, 1 /* spaces */,
  58                G1MonitoringSupport::pad_capacity(0) /* min_capacity */,
  59                G1MonitoringSupport::pad_capacity(g1mm->old_gen_max()),
  60                G1MonitoringSupport::pad_capacity(0) /* curr_capacity */) {
  61   if (UsePerfData) {
  62     update_all();
  63   }
  64 }
  65 
  66 void G1YoungGenerationCounters::update_all() {
  67   size_t committed =
  68             G1MonitoringSupport::pad_capacity(_g1mm->young_gen_committed(), 3);
  69   _current_size->set_value(committed);
  70 }
  71 
  72 void G1OldGenerationCounters::update_all() {
  73   size_t committed =
  74             G1MonitoringSupport::pad_capacity(_g1mm->old_gen_committed());
  75   _current_size->set_value(committed);
  76 }
  77 
  78 G1MonitoringSupport::G1MonitoringSupport(G1CollectedHeap* g1h) :
  79   _g1h(g1h),
  80   _incremental_collection_counters(NULL),
  81   _full_collection_counters(NULL),
  82   _conc_collection_counters(NULL),
  83   _old_collection_counters(NULL),
  84   _old_space_counters(NULL),
  85   _young_collection_counters(NULL),
  86   _eden_counters(NULL),
  87   _from_counters(NULL),
  88   _to_counters(NULL),
  89 
  90   _overall_reserved(0),
  91   _overall_committed(0),    _overall_used(0),
  92   _young_region_num(0),
  93   _young_gen_committed(0),
  94   _eden_committed(0),       _eden_used(0),
  95   _survivor_committed(0),   _survivor_used(0),
  96   _old_committed(0),        _old_used(0) {
  97 
  98   _overall_reserved = g1h->max_capacity();
  99   recalculate_sizes();
 100 
 101   // Counters for GC collections
 102   //
 103   //  name "collector.0".  In a generational collector this would be the
 104   // young generation collection.
 105   _incremental_collection_counters =
 106     new CollectorCounters("G1 incremental collections", 0);
 107   //   name "collector.1".  In a generational collector this would be the
 108   // old generation collection.
 109   _full_collection_counters =
 110     new CollectorCounters("G1 stop-the-world full collections", 1);
 111   //   name "collector.2".  In a generational collector this would be the
 112   // STW phases in concurrent collection.
 113   _conc_collection_counters =
 114     new CollectorCounters("G1 stop-the-world phases", 2);
 115 
 116   // timer sampling for all counters supporting sampling only update the
 117   // used value.  See the take_sample() method.  G1 requires both used and
 118   // capacity updated so sampling is not currently used.  It might
 119   // be sufficient to update all counters in take_sample() even though
 120   // take_sample() only returns "used".  When sampling was used, there
 121   // were some anomolous values emitted which may have been the consequence
 122   // of not updating all values simultaneously (i.e., see the calculation done
 123   // in eden_space_used(), is it possible that the values used to
 124   // calculate either eden_used or survivor_used are being updated by
 125   // the collector when the sample is being done?).
 126   const bool sampled = false;
 127 
 128   // "Generation" and "Space" counters.
 129   //
 130   //  name "generation.1" This is logically the old generation in
 131   // generational GC terms.  The "1, 1" parameters are for
 132   // the n-th generation (=1) with 1 space.
 133   // Counters are created from minCapacity, maxCapacity, and capacity
 134   _old_collection_counters = new G1OldGenerationCounters(this, "old");
 135 
 136   //  name  "generation.1.space.0"
 137   // Counters are created from maxCapacity, capacity, initCapacity,
 138   // and used.
 139   _old_space_counters = new HSpaceCounters(_old_collection_counters->name_space(),
 140     "space", 0 /* ordinal */,
 141     pad_capacity(overall_reserved()) /* max_capacity */,
 142     pad_capacity(old_space_committed()) /* init_capacity */);
 143 
 144   //   Young collection set
 145   //  name "generation.0".  This is logically the young generation.
 146   //  The "0, 3" are parameters for the n-th generation (=0) with 3 spaces.
 147   // See  _old_collection_counters for additional counters
 148   _young_collection_counters = new G1YoungGenerationCounters(this, "young");
 149 
 150   const char* young_collection_name_space = _young_collection_counters->name_space();
 151 
 152   //  name "generation.0.space.0"
 153   // See _old_space_counters for additional counters
 154   _eden_counters = new HSpaceCounters(young_collection_name_space,
 155     "eden", 0 /* ordinal */,
 156     pad_capacity(overall_reserved()) /* max_capacity */,
 157     pad_capacity(eden_space_committed()) /* init_capacity */);
 158 
 159   //  name "generation.0.space.1"
 160   // See _old_space_counters for additional counters
 161   // Set the arguments to indicate that this survivor space is not used.
 162   _from_counters = new HSpaceCounters(young_collection_name_space,
 163     "s0", 1 /* ordinal */,
 164     pad_capacity(0) /* max_capacity */,
 165     pad_capacity(0) /* init_capacity */);
 166 
 167   //  name "generation.0.space.2"
 168   // See _old_space_counters for additional counters
 169   _to_counters = new HSpaceCounters(young_collection_name_space,
 170     "s1", 2 /* ordinal */,
 171     pad_capacity(overall_reserved()) /* max_capacity */,
 172     pad_capacity(survivor_space_committed()) /* init_capacity */);
 173 
 174   if (UsePerfData) {
 175     // Given that this survivor space is not used, we update it here
 176     // once to reflect that its used space is 0 so that we don't have to
 177     // worry about updating it again later.
 178     _from_counters->update_used(0);
 179   }
 180 }
 181 
 182 void G1MonitoringSupport::recalculate_sizes() {
 183   // Recalculate all the sizes from scratch. We assume that this is
 184   // called at a point where no concurrent updates to the various
 185   // values we read here are possible (i.e., at a STW phase at the end
 186   // of a GC).
 187 
 188   uint young_list_length = _g1h->young_regions_count();
 189   uint survivor_list_length = _g1h->survivor_regions_count();
 190   assert(young_list_length >= survivor_list_length, "invariant");
 191   uint eden_list_length = young_list_length - survivor_list_length;
 192   // Max length includes any potential extensions to the young gen
 193   // we'll do when the GC locker is active.
 194   uint young_list_max_length = _g1h->g1_policy()->young_list_max_length();
 195   assert(young_list_max_length >= survivor_list_length, "invariant");
 196   uint eden_list_max_length = young_list_max_length - survivor_list_length;
 197 
 198   _overall_used = _g1h->used_unlocked();
 199   _eden_used = (size_t) eden_list_length * HeapRegion::GrainBytes;
 200   _survivor_used = (size_t) survivor_list_length * HeapRegion::GrainBytes;
 201   _young_region_num = young_list_length;
 202   _old_used = subtract_up_to_zero(_overall_used, _eden_used + _survivor_used);
 203 
 204   // First calculate the committed sizes that can be calculated independently.
 205   _survivor_committed = _survivor_used;
 206   _old_committed = HeapRegion::align_up_to_region_byte_size(_old_used);
 207 
 208   // Next, start with the overall committed size.
 209   _overall_committed = _g1h->capacity();
 210   size_t committed = _overall_committed;
 211 
 212   // Remove the committed size we have calculated so far (for the
 213   // survivor and old space).
 214   assert(committed >= (_survivor_committed + _old_committed), "sanity");
 215   committed -= _survivor_committed + _old_committed;
 216 
 217   // Next, calculate and remove the committed size for the eden.
 218   _eden_committed = (size_t) eden_list_max_length * HeapRegion::GrainBytes;
 219   // Somewhat defensive: be robust in case there are inaccuracies in
 220   // the calculations
 221   _eden_committed = MIN2(_eden_committed, committed);
 222   committed -= _eden_committed;
 223 
 224   // Finally, give the rest to the old space...
 225   _old_committed += committed;
 226   // ..and calculate the young gen committed.
 227   _young_gen_committed = _eden_committed + _survivor_committed;
 228 
 229   assert(_overall_committed ==
 230          (_eden_committed + _survivor_committed + _old_committed),
 231          "the committed sizes should add up");
 232   // Somewhat defensive: cap the eden used size to make sure it
 233   // never exceeds the committed size.
 234   _eden_used = MIN2(_eden_used, _eden_committed);
 235   // _survivor_committed and _old_committed are calculated in terms of
 236   // the corresponding _*_used value, so the next two conditions
 237   // should hold.
 238   assert(_survivor_used <= _survivor_committed, "post-condition");
 239   assert(_old_used <= _old_committed, "post-condition");
 240 }
 241 
 242 void G1MonitoringSupport::recalculate_eden_size() {
 243   // When a new eden region is allocated, only the eden_used size is
 244   // affected (since we have recalculated everything else at the last GC).
 245 
 246   uint young_region_num = _g1h->young_regions_count();
 247   if (young_region_num > _young_region_num) {
 248     uint diff = young_region_num - _young_region_num;
 249     _eden_used += (size_t) diff * HeapRegion::GrainBytes;
 250     // Somewhat defensive: cap the eden used size to make sure it
 251     // never exceeds the committed size.
 252     _eden_used = MIN2(_eden_used, _eden_committed);
 253     _young_region_num = young_region_num;
 254   }
 255 }
 256 
 257 void G1MonitoringSupport::update_sizes() {
 258   recalculate_sizes();
 259   if (UsePerfData) {
 260     eden_counters()->update_capacity(pad_capacity(eden_space_committed()));
 261     eden_counters()->update_used(eden_space_used());
 262     // only the to survivor space (s1) is active, so we don't need to
 263     // update the counters for the from survivor space (s0)
 264     to_counters()->update_capacity(pad_capacity(survivor_space_committed()));
 265     to_counters()->update_used(survivor_space_used());
 266     old_space_counters()->update_capacity(pad_capacity(old_space_committed()));
 267     old_space_counters()->update_used(old_space_used());
 268     old_collection_counters()->update_all();
 269     young_collection_counters()->update_all();
 270     MetaspaceCounters::update_performance_counters();
 271     CompressedClassSpaceCounters::update_performance_counters();
 272   }
 273 }
 274 
 275 void G1MonitoringSupport::update_eden_size() {
 276   recalculate_eden_size();
 277   if (UsePerfData) {
 278     eden_counters()->update_used(eden_space_used());
 279   }
 280 }