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