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