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/g1/g1MemoryPool.hpp"
  30 #include "gc/shared/collectorCounters.hpp"
  31 #include "gc/shared/hSpaceCounters.hpp"
  32 #include "memory/metaspaceCounters.hpp"
  33 #include "services/memoryPool.hpp"
  34 
  35 class G1GenerationCounters : public GenerationCounters {
  36 protected:
  37   G1MonitoringSupport* _g1mm;
  38 
  39 public:
  40   G1GenerationCounters(G1MonitoringSupport* g1mm,
  41                        const char* name, int ordinal, int spaces,
  42                        size_t min_capacity, size_t max_capacity,
  43                        size_t curr_capacity)
  44   : GenerationCounters(name, ordinal, spaces, min_capacity,
  45                        max_capacity, curr_capacity), _g1mm(g1mm) { }
  46 };
  47 
  48 class G1YoungGenerationCounters : public G1GenerationCounters {
  49 public:
  50   // We pad the capacity three times given that the young generation
  51   // contains three spaces (eden and two survivors).
  52   G1YoungGenerationCounters(G1MonitoringSupport* g1mm, const char* name, size_t max_size)
  53   : G1GenerationCounters(g1mm, name, 0 /* ordinal */, 3 /* spaces */,
  54                          G1MonitoringSupport::pad_capacity(0, 3) /* min_capacity */,
  55                          G1MonitoringSupport::pad_capacity(max_size, 3),
  56                          G1MonitoringSupport::pad_capacity(0, 3) /* curr_capacity */) {
  57     if (UsePerfData) {
  58       update_all();
  59     }
  60   }
  61 
  62   virtual void update_all() {
  63     size_t committed =
  64               G1MonitoringSupport::pad_capacity(_g1mm->young_gen_committed(), 3);
  65     _current_size->set_value(committed);
  66   }
  67 };
  68 
  69 class G1OldGenerationCounters : public G1GenerationCounters {
  70 public:
  71   G1OldGenerationCounters(G1MonitoringSupport* g1mm, const char* name, size_t max_size)
  72   : G1GenerationCounters(g1mm, name, 1 /* ordinal */, 1 /* spaces */,
  73                          G1MonitoringSupport::pad_capacity(0) /* min_capacity */,
  74                          G1MonitoringSupport::pad_capacity(max_size),
  75                          G1MonitoringSupport::pad_capacity(0) /* curr_capacity */) {
  76     if (UsePerfData) {
  77       update_all();
  78     }
  79   }
  80 
  81   virtual void update_all() {
  82     size_t committed =
  83               G1MonitoringSupport::pad_capacity(_g1mm->old_gen_committed());
  84     _current_size->set_value(committed);
  85   }
  86 };
  87 
  88 G1MonitoringSupport::G1MonitoringSupport(G1CollectedHeap* g1h) :
  89   _g1h(g1h),
  90   _update_mutex(Mutex::leaf, "G1 Monitoring Support Lock", true, Monitor::_safepoint_check_never),
  91   _incremental_memory_manager("G1 Young Generation", "end of minor GC"),
  92   _full_gc_memory_manager("G1 Old Generation", "end of major GC"),
  93   _eden_pool(NULL),
  94   _survivor_pool(NULL),
  95   _old_pool(NULL),
  96   _incremental_collection_counters(NULL),
  97   _full_collection_counters(NULL),
  98   _conc_collection_counters(NULL),
  99   _young_gen_counters(NULL),
 100   _old_gen_counters(NULL),
 101   _old_space_counters(NULL),
 102   _eden_space_counters(NULL),
 103   _from_space_counters(NULL),
 104   _to_space_counters(NULL),
 105 
 106   _overall_committed(0),
 107   _overall_used(0),
 108   _young_gen_committed(0),
 109   _old_gen_committed(0),
 110 
 111   _eden_space_committed(0),
 112   _eden_space_used(0),
 113   _survivor_space_committed(0),
 114   _survivor_space_used(0),
 115   _old_gen_used(0) {
 116 
 117   recalculate_sizes();
 118 
 119   // Counters for garbage collections
 120   //
 121   //  name "collector.0".  In a generational collector this would be the
 122   // young generation collection.
 123   _incremental_collection_counters =
 124     new CollectorCounters("G1 incremental collections", 0);
 125   //   name "collector.1".  In a generational collector this would be the
 126   // old generation collection.
 127   _full_collection_counters =
 128     new CollectorCounters("G1 stop-the-world full collections", 1);
 129   //   name "collector.2".  In a generational collector this would be the
 130   // STW phases in concurrent collection.
 131   _conc_collection_counters =
 132     new CollectorCounters("G1 stop-the-world phases", 2);
 133 
 134   // "Generation" and "Space" counters.
 135   //
 136   //  name "generation.1" This is logically the old generation in
 137   // generational GC terms.  The "1, 1" parameters are for
 138   // the n-th generation (=1) with 1 space.
 139   // Counters are created from minCapacity, maxCapacity, and capacity
 140   _old_gen_counters = new G1OldGenerationCounters(this, "old", _g1h->max_capacity());
 141 
 142   //  name  "generation.1.space.0"
 143   // Counters are created from maxCapacity, capacity, initCapacity,
 144   // and used.
 145   _old_space_counters = new HSpaceCounters(_old_gen_counters->name_space(),
 146     "space", 0 /* ordinal */,
 147     pad_capacity(g1h->max_capacity()) /* max_capacity */,
 148     pad_capacity(_old_gen_committed) /* init_capacity */);
 149 
 150   //   Young collection set
 151   //  name "generation.0".  This is logically the young generation.
 152   //  The "0, 3" are parameters for the n-th generation (=0) with 3 spaces.
 153   // See  _old_collection_counters for additional counters
 154   _young_gen_counters = new G1YoungGenerationCounters(this, "young", _g1h->max_capacity());
 155 
 156   const char* young_collection_name_space = _young_gen_counters->name_space();
 157 
 158   //  name "generation.0.space.0"
 159   // See _old_space_counters for additional counters
 160   _eden_space_counters = new HSpaceCounters(young_collection_name_space,
 161     "eden", 0 /* ordinal */,
 162     pad_capacity(g1h->max_capacity()) /* max_capacity */,
 163     pad_capacity(_eden_space_committed) /* init_capacity */);
 164 
 165   //  name "generation.0.space.1"
 166   // See _old_space_counters for additional counters
 167   // Set the arguments to indicate that this survivor space is not used.
 168   _from_space_counters = new HSpaceCounters(young_collection_name_space,
 169     "s0", 1 /* ordinal */,
 170     pad_capacity(0) /* max_capacity */,
 171     pad_capacity(0) /* init_capacity */);
 172   // Given that this survivor space is not used, we update it here
 173   // once to reflect that its used space is 0 so that we don't have to
 174   // worry about updating it again later.
 175   _from_space_counters->update_used(0);
 176 
 177   //  name "generation.0.space.2"
 178   // See _old_space_counters for additional counters
 179   _to_space_counters = new HSpaceCounters(young_collection_name_space,
 180     "s1", 2 /* ordinal */,
 181     pad_capacity(g1h->max_capacity()) /* max_capacity */,
 182     pad_capacity(_survivor_space_committed) /* init_capacity */);
 183 }
 184 
 185 G1MonitoringSupport::~G1MonitoringSupport() {
 186   delete _eden_pool;
 187   delete _survivor_pool;
 188   delete _old_pool;
 189 }
 190 
 191 void G1MonitoringSupport::initialize_serviceability() {
 192   _eden_pool = new G1EdenPool(_g1h, _eden_space_committed);
 193   _survivor_pool = new G1SurvivorPool(_g1h, _survivor_space_committed);
 194   _old_pool = new G1OldGenPool(_g1h, _old_gen_committed, _g1h->max_capacity());
 195 
 196   _full_gc_memory_manager.add_pool(_eden_pool);
 197   _full_gc_memory_manager.add_pool(_survivor_pool);
 198   _full_gc_memory_manager.add_pool(_old_pool);
 199 
 200   _incremental_memory_manager.add_pool(_eden_pool);
 201   _incremental_memory_manager.add_pool(_survivor_pool);
 202   _incremental_memory_manager.add_pool(_old_pool, false /* always_affected_by_gc */);
 203 }
 204 
 205 MemoryUsage G1MonitoringSupport::memory_usage() {
 206   MutexLockerEx x(&_update_mutex, Mutex::_no_safepoint_check_flag);
 207   return MemoryUsage(InitialHeapSize, _overall_used, _overall_committed, _g1h->max_capacity());
 208 }
 209 
 210 GrowableArray<GCMemoryManager*> G1MonitoringSupport::memory_managers() {
 211   GrowableArray<GCMemoryManager*> memory_managers(2);
 212   memory_managers.append(&_incremental_memory_manager);
 213   memory_managers.append(&_full_gc_memory_manager);
 214   return memory_managers;
 215 }
 216 
 217 GrowableArray<MemoryPool*> G1MonitoringSupport::memory_pools() {
 218   GrowableArray<MemoryPool*> memory_pools(3);
 219   memory_pools.append(_eden_pool);
 220   memory_pools.append(_survivor_pool);
 221   memory_pools.append(_old_pool);
 222   return memory_pools;
 223 }
 224 
 225 void G1MonitoringSupport::recalculate_sizes() {
 226   assert_heap_locked_or_at_safepoint(true);
 227 
 228   MutexLockerEx x(&_update_mutex, Mutex::_no_safepoint_check_flag);
 229   // Recalculate all the sizes from scratch.
 230 
 231   uint young_list_length = _g1h->young_regions_count();
 232   uint survivor_list_length = _g1h->survivor_regions_count();
 233   assert(young_list_length >= survivor_list_length, "invariant");
 234   uint eden_list_length = young_list_length - survivor_list_length;
 235   // Max length includes any potential extensions to the young gen
 236   // we'll do when the GC locker is active.
 237   uint young_list_max_length = _g1h->g1_policy()->young_list_max_length();
 238   assert(young_list_max_length >= survivor_list_length, "invariant");
 239   uint eden_list_max_length = young_list_max_length - survivor_list_length;
 240 
 241   _overall_used = _g1h->used_unlocked();
 242   _eden_space_used = (size_t) eden_list_length * HeapRegion::GrainBytes;
 243   _survivor_space_used = (size_t) survivor_list_length * HeapRegion::GrainBytes;
 244   _old_gen_used = subtract_up_to_zero(_overall_used, _eden_space_used + _survivor_space_used);
 245 
 246   // First calculate the committed sizes that can be calculated independently.
 247   _survivor_space_committed = _survivor_space_used;
 248   _old_gen_committed = HeapRegion::align_up_to_region_byte_size(_old_gen_used);
 249 
 250   // Next, start with the overall committed size.
 251   _overall_committed = _g1h->capacity();
 252   size_t committed = _overall_committed;
 253 
 254   // Remove the committed size we have calculated so far (for the
 255   // survivor and old space).
 256   assert(committed >= (_survivor_space_committed + _old_gen_committed), "sanity");
 257   committed -= _survivor_space_committed + _old_gen_committed;
 258 
 259   // Next, calculate and remove the committed size for the eden.
 260   _eden_space_committed = (size_t) eden_list_max_length * HeapRegion::GrainBytes;
 261   // Somewhat defensive: be robust in case there are inaccuracies in
 262   // the calculations
 263   _eden_space_committed = MIN2(_eden_space_committed, committed);
 264   committed -= _eden_space_committed;
 265 
 266   // Finally, give the rest to the old space...
 267   _old_gen_committed += committed;
 268   // ..and calculate the young gen committed.
 269   _young_gen_committed = _eden_space_committed + _survivor_space_committed;
 270 
 271   assert(_overall_committed ==
 272          (_eden_space_committed + _survivor_space_committed + _old_gen_committed),
 273          "the committed sizes should add up");
 274   // Somewhat defensive: cap the eden used size to make sure it
 275   // never exceeds the committed size.
 276   _eden_space_used = MIN2(_eden_space_used, _eden_space_committed);
 277   // _survivor_committed and _old_committed are calculated in terms of
 278   // the corresponding _*_used value, so the next two conditions
 279   // should hold.
 280   assert(_survivor_space_used <= _survivor_space_committed, "post-condition");
 281   assert(_old_gen_used <= _old_gen_committed, "post-condition");
 282 }
 283 
 284 void G1MonitoringSupport::update_sizes() {
 285   recalculate_sizes();
 286   if (UsePerfData) {
 287     _eden_space_counters->update_capacity(pad_capacity(_eden_space_committed));
 288     _eden_space_counters->update_used(_eden_space_used);
 289    // only the "to" survivor space is active, so we don't need to
 290     // update the counters for the "from" survivor space
 291     _to_space_counters->update_capacity(pad_capacity(_survivor_space_committed));
 292     _to_space_counters->update_used(_survivor_space_used);
 293     _old_space_counters->update_capacity(pad_capacity(_old_gen_committed));
 294     _old_space_counters->update_used(_old_gen_used);
 295 
 296     _young_gen_counters->update_all();
 297     _old_gen_counters->update_all();
 298 
 299     MetaspaceCounters::update_performance_counters();
 300     CompressedClassSpaceCounters::update_performance_counters();
 301   }
 302 }
 303 
 304 void G1MonitoringSupport::update_eden_size() {
 305   // Recalculate everything - this should be fast enough and we are sure that we do not
 306   // miss anything.
 307   recalculate_sizes();
 308   if (UsePerfData) {
 309     _eden_space_counters->update_used(_eden_space_used);
 310   }
 311 }
 312 
 313 MemoryUsage G1MonitoringSupport::eden_space_memory_usage(size_t initial_size, size_t max_size) {
 314   MutexLockerEx x(&_update_mutex, Mutex::_no_safepoint_check_flag);
 315 
 316   return MemoryUsage(initial_size,
 317                      _eden_space_used,
 318                      _eden_space_committed,
 319                      max_size);
 320 }
 321 
 322 MemoryUsage G1MonitoringSupport::survivor_space_memory_usage(size_t initial_size, size_t max_size) {
 323   MutexLockerEx x(&_update_mutex, Mutex::_no_safepoint_check_flag);
 324 
 325   return MemoryUsage(initial_size,
 326                      _survivor_space_used,
 327                      _survivor_space_committed,
 328                      max_size);
 329 }
 330 
 331 MemoryUsage G1MonitoringSupport::old_gen_memory_usage(size_t initial_size, size_t max_size) {
 332   MutexLockerEx x(&_update_mutex, Mutex::_no_safepoint_check_flag);
 333 
 334   return MemoryUsage(initial_size,
 335                      _old_gen_used,
 336                      _old_gen_committed,
 337                      max_size);
 338 }
 339 
 340 G1MonitoringScope::G1MonitoringScope(G1MonitoringSupport* g1mm, bool full_gc, bool all_memory_pools_affected) :
 341   _tcs(full_gc ? g1mm->_full_collection_counters : g1mm->_incremental_collection_counters),
 342   _tms(full_gc ? &g1mm->_full_gc_memory_manager : &g1mm->_incremental_memory_manager,
 343        G1CollectedHeap::heap()->gc_cause(), all_memory_pools_affected) {
 344 }