1 /*
   2  * Copyright (c) 2011, 2019, 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/hSpaceCounters.hpp"
  31 #include "memory/metaspaceCounters.hpp"
  32 #include "runtime/mutexLocker.inline.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   _incremental_memory_manager("G1 Young Generation", "end of minor GC"),
  91   _full_gc_memory_manager("G1 Old Generation", "end of major GC"),
  92   _eden_space_pool(NULL),
  93   _survivor_space_pool(NULL),
  94   _old_gen_pool(NULL),
  95   _incremental_collection_counters(NULL),
  96   _full_collection_counters(NULL),
  97   _conc_collection_counters(NULL),
  98   _young_gen_counters(NULL),
  99   _old_gen_counters(NULL),
 100   _old_space_counters(NULL),
 101   _eden_space_counters(NULL),
 102   _from_space_counters(NULL),
 103   _to_space_counters(NULL),
 104 
 105   _overall_committed(0),
 106   _overall_used(0),
 107   _young_gen_committed(0),
 108   _old_gen_committed(0),
 109 
 110   _eden_space_committed(0),
 111   _eden_space_used(0),
 112   _survivor_space_committed(0),
 113   _survivor_space_used(0),
 114   _old_gen_used(0) {
 115 
 116   recalculate_sizes();
 117 
 118   // Counters for garbage collections
 119   //
 120   //  name "collector.0".  In a generational collector this would be the
 121   // young generation collection.
 122   _incremental_collection_counters =
 123     new CollectorCounters("G1 young collection pauses", 0);
 124   //   name "collector.1".  In a generational collector this would be the
 125   // old generation collection.
 126   _full_collection_counters =
 127     new CollectorCounters("G1 full collection pauses", 1);
 128   //   name "collector.2".  In a generational collector this would be the
 129   // STW phases in concurrent collection.
 130   _conc_collection_counters =
 131     new CollectorCounters("G1 concurrent cycle pauses", 2);
 132 
 133   // "Generation" and "Space" counters.
 134   //
 135   //  name "generation.1" This is logically the old generation in
 136   // generational GC terms.  The "1, 1" parameters are for
 137   // the n-th generation (=1) with 1 space.
 138   // Counters are created from minCapacity, maxCapacity, and capacity
 139   _old_gen_counters = new G1OldGenerationCounters(this, "old", _g1h->max_capacity());
 140 
 141   //  name  "generation.1.space.0"
 142   // Counters are created from maxCapacity, capacity, initCapacity,
 143   // and used.
 144   _old_space_counters = new HSpaceCounters(_old_gen_counters->name_space(),
 145     "space", 0 /* ordinal */,
 146     pad_capacity(g1h->max_capacity()) /* max_capacity */,
 147     pad_capacity(_old_gen_committed) /* init_capacity */);
 148 
 149   //   Young collection set
 150   //  name "generation.0".  This is logically the young generation.
 151   //  The "0, 3" are parameters for the n-th generation (=0) with 3 spaces.
 152   // See  _old_collection_counters for additional counters
 153   _young_gen_counters = new G1YoungGenerationCounters(this, "young", _g1h->max_capacity());
 154 
 155   const char* young_collection_name_space = _young_gen_counters->name_space();
 156 
 157   //  name "generation.0.space.0"
 158   // See _old_space_counters for additional counters
 159   _eden_space_counters = new HSpaceCounters(young_collection_name_space,
 160     "eden", 0 /* ordinal */,
 161     pad_capacity(g1h->max_capacity()) /* max_capacity */,
 162     pad_capacity(_eden_space_committed) /* init_capacity */);
 163 
 164   //  name "generation.0.space.1"
 165   // See _old_space_counters for additional counters
 166   // Set the arguments to indicate that this survivor space is not used.
 167   _from_space_counters = new HSpaceCounters(young_collection_name_space,
 168     "s0", 1 /* ordinal */,
 169     pad_capacity(0) /* max_capacity */,
 170     pad_capacity(0) /* init_capacity */);
 171   // Given that this survivor space is not used, we update it here
 172   // once to reflect that its used space is 0 so that we don't have to
 173   // worry about updating it again later.
 174   if (UsePerfData) {
 175     _from_space_counters->update_used(0);
 176   }
 177 
 178   //  name "generation.0.space.2"
 179   // See _old_space_counters for additional counters
 180   _to_space_counters = new HSpaceCounters(young_collection_name_space,
 181     "s1", 2 /* ordinal */,
 182     pad_capacity(g1h->max_capacity()) /* max_capacity */,
 183     pad_capacity(_survivor_space_committed) /* init_capacity */);
 184 }
 185 
 186 G1MonitoringSupport::~G1MonitoringSupport() {
 187   delete _eden_space_pool;
 188   delete _survivor_space_pool;
 189   delete _old_gen_pool;
 190 }
 191 
 192 void G1MonitoringSupport::initialize_serviceability() {
 193   _eden_space_pool = new G1EdenPool(_g1h, _eden_space_committed);
 194   _survivor_space_pool = new G1SurvivorPool(_g1h, _survivor_space_committed);
 195   _old_gen_pool = new G1OldGenPool(_g1h, _old_gen_committed, _g1h->max_capacity());
 196 
 197   _full_gc_memory_manager.add_pool(_eden_space_pool);
 198   _full_gc_memory_manager.add_pool(_survivor_space_pool);
 199   _full_gc_memory_manager.add_pool(_old_gen_pool);
 200 
 201   _incremental_memory_manager.add_pool(_eden_space_pool);
 202   _incremental_memory_manager.add_pool(_survivor_space_pool);
 203   _incremental_memory_manager.add_pool(_old_gen_pool, false /* always_affected_by_gc */);
 204 }
 205 
 206 MemoryUsage G1MonitoringSupport::memory_usage() {
 207   MutexLocker x(MonitoringSupport_lock, Mutex::_no_safepoint_check_flag);
 208   return MemoryUsage(InitialHeapSize, _overall_used, _overall_committed, _g1h->max_capacity());
 209 }
 210 
 211 GrowableArray<GCMemoryManager*> G1MonitoringSupport::memory_managers() {
 212   GrowableArray<GCMemoryManager*> memory_managers(2);
 213   memory_managers.append(&_incremental_memory_manager);
 214   memory_managers.append(&_full_gc_memory_manager);
 215   return memory_managers;
 216 }
 217 
 218 GrowableArray<MemoryPool*> G1MonitoringSupport::memory_pools() {
 219   GrowableArray<MemoryPool*> memory_pools(3);
 220   memory_pools.append(_eden_space_pool);
 221   memory_pools.append(_survivor_space_pool);
 222   memory_pools.append(_old_gen_pool);
 223   return memory_pools;
 224 }
 225 
 226 void G1MonitoringSupport::recalculate_sizes() {
 227   assert_heap_locked_or_at_safepoint(true);
 228 
 229   MutexLocker x(MonitoringSupport_lock, Mutex::_no_safepoint_check_flag);
 230   // Recalculate all the sizes from scratch.
 231 
 232   // This never includes used bytes of current allocating heap region.
 233   _overall_used = _g1h->used_unlocked();
 234   _eden_space_used = _g1h->eden_regions_used_bytes();
 235   _survivor_space_used = _g1h->survivor_regions_used_bytes();
 236 
 237   // _overall_used and _eden_space_used are obtained concurrently so
 238   // may be inconsistent with each other. To prevent _old_gen_used going negative,
 239   // use smaller value to substract.
 240   _old_gen_used = _overall_used - MIN2(_overall_used, _eden_space_used + _survivor_space_used);
 241 
 242   uint survivor_list_length = _g1h->survivor_regions_count();
 243   // Max length includes any potential extensions to the young gen
 244   // we'll do when the GC locker is active.
 245   uint young_list_max_length = _g1h->policy()->young_list_max_length();
 246   assert(young_list_max_length >= survivor_list_length, "invariant");
 247   uint eden_list_max_length = young_list_max_length - survivor_list_length;
 248 
 249   // First calculate the committed sizes that can be calculated independently.
 250   _survivor_space_committed = survivor_list_length * HeapRegion::GrainBytes;
 251   _old_gen_committed = HeapRegion::align_up_to_region_byte_size(_old_gen_used);
 252 
 253   // Next, start with the overall committed size.
 254   _overall_committed = _g1h->capacity();
 255   size_t committed = _overall_committed;
 256 
 257   // Remove the committed size we have calculated so far (for the
 258   // survivor and old space).
 259   assert(committed >= (_survivor_space_committed + _old_gen_committed), "sanity");
 260   committed -= _survivor_space_committed + _old_gen_committed;
 261 
 262   // Next, calculate and remove the committed size for the eden.
 263   _eden_space_committed = (size_t) eden_list_max_length * HeapRegion::GrainBytes;
 264   // Somewhat defensive: be robust in case there are inaccuracies in
 265   // the calculations
 266   _eden_space_committed = MIN2(_eden_space_committed, committed);
 267   committed -= _eden_space_committed;
 268 
 269   // Finally, give the rest to the old space...
 270   _old_gen_committed += committed;
 271   // ..and calculate the young gen committed.
 272   _young_gen_committed = _eden_space_committed + _survivor_space_committed;
 273 
 274   assert(_overall_committed ==
 275          (_eden_space_committed + _survivor_space_committed + _old_gen_committed),
 276          "the committed sizes should add up");
 277   // Somewhat defensive: cap the eden used size to make sure it
 278   // never exceeds the committed size.
 279   _eden_space_used = MIN2(_eden_space_used, _eden_space_committed);
 280   // _survivor_space_used is calculated during a safepoint and _survivor_space_committed
 281   // is calculated from survivor region count * heap region size.
 282   assert(_survivor_space_used <= _survivor_space_committed, "Survivor used bytes(" SIZE_FORMAT
 283          ") should be less than or equal to survivor committed(" SIZE_FORMAT ")",
 284          _survivor_space_used, _survivor_space_committed);
 285   // _old_gen_committed is calculated in terms of _old_gen_used value.
 286   assert(_old_gen_used <= _old_gen_committed, "Old gen used bytes(" SIZE_FORMAT
 287          ") should be less than or equal to old gen committed(" SIZE_FORMAT ")",
 288          _old_gen_used, _old_gen_committed);
 289 }
 290 
 291 void G1MonitoringSupport::update_sizes() {
 292   recalculate_sizes();
 293   if (UsePerfData) {
 294     _eden_space_counters->update_capacity(pad_capacity(_eden_space_committed));
 295     _eden_space_counters->update_used(_eden_space_used);
 296    // only the "to" survivor space is active, so we don't need to
 297     // update the counters for the "from" survivor space
 298     _to_space_counters->update_capacity(pad_capacity(_survivor_space_committed));
 299     _to_space_counters->update_used(_survivor_space_used);
 300     _old_space_counters->update_capacity(pad_capacity(_old_gen_committed));
 301     _old_space_counters->update_used(_old_gen_used);
 302 
 303     _young_gen_counters->update_all();
 304     _old_gen_counters->update_all();
 305 
 306     MetaspaceCounters::update_performance_counters();
 307     CompressedClassSpaceCounters::update_performance_counters();
 308   }
 309 }
 310 
 311 void G1MonitoringSupport::update_eden_size() {
 312   // Recalculate everything - this should be fast enough and we are sure that we do not
 313   // miss anything.
 314   recalculate_sizes();
 315   if (UsePerfData) {
 316     _eden_space_counters->update_used(_eden_space_used);
 317   }
 318 }
 319 
 320 MemoryUsage G1MonitoringSupport::eden_space_memory_usage(size_t initial_size, size_t max_size) {
 321   MutexLocker x(MonitoringSupport_lock, Mutex::_no_safepoint_check_flag);
 322 
 323   return MemoryUsage(initial_size,
 324                      _eden_space_used,
 325                      _eden_space_committed,
 326                      max_size);
 327 }
 328 
 329 MemoryUsage G1MonitoringSupport::survivor_space_memory_usage(size_t initial_size, size_t max_size) {
 330   MutexLocker x(MonitoringSupport_lock, Mutex::_no_safepoint_check_flag);
 331 
 332   return MemoryUsage(initial_size,
 333                      _survivor_space_used,
 334                      _survivor_space_committed,
 335                      max_size);
 336 }
 337 
 338 MemoryUsage G1MonitoringSupport::old_gen_memory_usage(size_t initial_size, size_t max_size) {
 339   MutexLocker x(MonitoringSupport_lock, Mutex::_no_safepoint_check_flag);
 340 
 341   return MemoryUsage(initial_size,
 342                      _old_gen_used,
 343                      _old_gen_committed,
 344                      max_size);
 345 }
 346 
 347 G1MonitoringScope::G1MonitoringScope(G1MonitoringSupport* g1mm, bool full_gc, bool all_memory_pools_affected) :
 348   _tcs(full_gc ? g1mm->_full_collection_counters : g1mm->_incremental_collection_counters),
 349   _tms(full_gc ? &g1mm->_full_gc_memory_manager : &g1mm->_incremental_memory_manager,
 350        G1CollectedHeap::heap()->gc_cause(), all_memory_pools_affected) {
 351 }