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 #ifndef SHARE_VM_GC_G1_G1MONITORINGSUPPORT_HPP 26 #define SHARE_VM_GC_G1_G1MONITORINGSUPPORT_HPP 27 28 #include "gc/shared/collectorCounters.hpp" 29 #include "gc/shared/generationCounters.hpp" 30 #include "services/memoryManager.hpp" 31 #include "services/memoryService.hpp" 32 #include "runtime/mutex.hpp" 33 34 class CollectorCounters; 35 class G1CollectedHeap; 36 class HSpaceCounters; 37 class MemoryPool; 38 39 // Class for monitoring logical spaces in G1. It provides data for 40 // both G1's jstat counters as well as G1's memory pools. 41 // 42 // G1 splits the heap into heap regions and each heap region belongs 43 // to one of the following categories: 44 // 45 // * eden : regions that have been allocated since the last GC 46 // * survivors : regions with objects that survived the last few GCs 47 // * old : long-lived non-humongous regions 48 // * humongous : humongous regions 49 // * archive : archive regions 50 // * free : free regions 51 // 52 // The combination of eden and survivor regions form the equivalent of 53 // the young generation in the other GCs. The combination of old, 54 // humongous, and archive regions form the equivalent of the old 55 // generation in the other GCs. Archive regions are permanently 56 // allocated during heap initialization, e.g., for CDS. Free regions 57 // do not have a good equivalent in the other GCs, given that they can 58 // be allocated as any of the other region types. 59 // 60 // The monitoring tools expect the heap to contain one or more 61 // generations (young, old) and each generation to contain one or 62 // more spaces (young has eden and survivor, old has old, humongous and 63 // archive). Given that G1 does not maintain those spaces physically 64 // (e.g., the set of non-contiguous eden regions can be considered as 65 // a "logical" space), we provide the illusion that those generations and 66 // spaces exist. In reality, each generation and space refers to a set 67 // of heap regions that are potentially non-contiguous. 68 // 69 // This class provides interfaces to access the min, current, and max 70 // capacity and current occupancy for each of G1's logical spaces and 71 // generations we expose to the monitoring tools. Also provided are 72 // counters for G1 concurrent collections and stop-the-world full heap 73 // collections. 74 // 75 // Below is a description of how the various sizes are calculated. 76 // 77 // * Current Capacity 78 // 79 // - heap_capacity = current heap capacity (e.g., current committed size) 80 // - young_gen_capacity = current max young gen target capacity 81 // (i.e., young gen target capacity + max allowed expansion capacity) 82 // - survivor_capacity = current survivor region capacity 83 // - eden_capacity = young_gen_capacity - survivor_capacity 84 // In legacy mode: 85 // - old_capacity = heap_capacity - young_gen_capacity 86 // Otherwise: 87 // - humongous_capacity = sum of humongous regions allocated 88 // - archive_capacity = sum of archive regions allocated 89 // - old_capacity = heap_capacity - young_gen_capacity - 90 // humongous_capacity - archive_capacity 91 // 92 // What we do in the above is to distribute the free regions among 93 // eden_capacity and old_capacity. 94 // 95 // * Occupancy 96 // 97 // - young_gen_committed = current young region capacity 98 // - survivor_used = survivor_capacity 99 // - eden_used = young_gen_used - survivor_used 100 // In legacy mode: 101 // - old_used = overall_used - young_gen_used 102 // Otherwise: 103 // - humongous_used = sum of humongous regions allocated 104 // - archive_used = sum of archive regions allocated 105 // - old_used = overall_used - young_gen_used - 106 // humongous_used - archive_used 107 // 108 // * Min Capacity 109 // 110 // We set this to 0 for all spaces. 111 // 112 // * Max Capacity 113 // 114 // For jstat, we set the max capacity of all spaces to heap_capacity, 115 // given that we don't always have a reasonable upper bound on how big 116 // each space can grow. For the memory pools, we make the max 117 // capacity undefined with the exception of the old memory pool for 118 // which we make the max capacity same as the max heap capacity. This 119 // allows users to sum memory pool max capacities to get something 120 // reasonable. 121 // 122 // We update all the above synchronously and we store the results in 123 // this singleton class so we can just read them out when needed. A subtle 124 // point is that all the above sizes must be recalculated when the old 125 // gen changes capacity (after a GC or after a humongous allocation) 126 // but only the eden occupancy changes when a new eden region is 127 // allocated. So, in the latter case we have minimal recalculation to 128 // do, but we don't both special-casing it because doing the full 129 // recalculation is quite fast. 130 131 class G1MonitoringSupport : public CHeapObj<mtGC> { 132 friend class VMStructs; 133 friend class G1MonitoringScope; 134 135 G1CollectedHeap* _g1h; 136 137 // java.lang.management MemoryManager and MemoryPool support 138 139 bool _use_legacy_monitoring; // For vmStructs and hsdb 140 141 GCMemoryManager _full_memory_manager; 142 // Legacy monitoring 143 GCMemoryManager _incremental_memory_manager; 144 // Default monitoring 145 GCMemoryManager _young_memory_manager; 146 GCMemoryManager _mixed_memory_manager; 147 GCMemoryManager _conc_memory_manager; 148 149 MemoryPool* _eden_space_pool; 150 MemoryPool* _survivor_space_pool; 151 MemoryPool* _old_space_pool; 152 MemoryPool* _archive_space_pool; 153 MemoryPool* _humongous_space_pool; 154 155 // jstat performance counters 156 // incremental collections both young and mixed 157 CollectorCounters* _incremental_collection_counters; 158 // full stop-the-world collections 159 CollectorCounters* _full_collection_counters; 160 // stop-the-world phases in G1 161 CollectorCounters* _conc_collection_counters; 162 // young collection set counters. The _eden_counters, 163 // _from_counters, and _to_counters are associated with 164 // this "generational" counter. 165 GenerationCounters* _young_gen_counters; 166 // old collection set counters. The _old_space_counters 167 // below are associated with this "generational" counter. 168 GenerationCounters* _old_gen_counters; 169 // Counters for the capacity and used for 170 // the whole heap 171 HSpaceCounters* _old_space_counters; 172 // the young collection 173 HSpaceCounters* _eden_space_counters; 174 // the survivor collection (only one, _to_counters, is actively used) 175 HSpaceCounters* _from_space_counters; 176 HSpaceCounters* _to_space_counters; 177 178 // When it's appropriate to recalculate the various sizes (at the 179 // end of a GC, when a new eden region is allocated, etc.) we store 180 // them here so that we can easily report them when needed and not 181 // have to recalculate them every time. 182 183 size_t _overall_committed; 184 size_t _overall_used; 185 186 size_t _young_gen_committed; 187 188 size_t _eden_space_committed; 189 size_t _eden_space_used; 190 size_t _survivor_space_committed; 191 size_t _survivor_space_used; 192 193 size_t _old_space_committed; 194 size_t _old_space_used; 195 size_t _archive_space_committed; 196 size_t _archive_space_used; 197 198 size_t _humongous_space_committed; 199 size_t _humongous_space_used; 200 201 // It returns x - y if x > y, 0 otherwise. 202 // As described in the comment above, some of the inputs to the 203 // calculations we have to do are obtained concurrently and hence 204 // may be inconsistent with each other. So, this provides a 205 // defensive way of performing the subtraction and avoids the value 206 // going negative (which would mean a very large result, given that 207 // the parameter are size_t). 208 static size_t subtract_up_to_zero(size_t x, size_t y) { 209 if (x > y) { 210 return x - y; 211 } else { 212 return 0; 213 } 214 } 215 216 // Recalculate all the space sizes. 217 void recalculate_sizes(); 218 219 public: 220 G1MonitoringSupport(G1CollectedHeap* g1h); 221 ~G1MonitoringSupport(); 222 223 void initialize_serviceability(); 224 225 GrowableArray<GCMemoryManager*> memory_managers(); 226 GrowableArray<MemoryPool*> memory_pools(); 227 228 bool use_legacy_monitoring() { return _use_legacy_monitoring; } 229 GCMemoryManager* conc_memory_manager() { return &_conc_memory_manager; } 230 231 // Unfortunately, the jstat tool assumes that no space has 0 232 // capacity. In our case, given that each space is logical, it's 233 // possible that no regions will be allocated to it, hence to have 0 234 // capacity (e.g., if there are no survivor regions, the survivor 235 // space has 0 capacity). The way we deal with this is to always pad 236 // each capacity value we report to jstat by a very small amount to 237 // make sure that it's never zero. Given that we sometimes have to 238 // report a capacity of a generation that contains several spaces 239 // (e.g., young gen includes one eden, two survivor spaces), the 240 // mult parameter is provided in order to adding the appropriate 241 // padding multiple times so that the capacities add up correctly. 242 static size_t pad_capacity(size_t size_bytes, size_t mult = 1) { 243 return size_bytes + MinObjAlignmentInBytes * mult; 244 } 245 246 // Recalculate all the space sizes from scratch and update 247 // all jstat counters accordingly. 248 void update_sizes(); 249 // Recalculate all the space sizes from scratch, but update 250 // just the eden size and jstat counters. 251 void update_eden_size(); 252 253 CollectorCounters* conc_collection_counters() { 254 return _conc_collection_counters; 255 } 256 257 // Monitoring support used by 258 // MemoryService 259 // jstat counters 260 // Tracing 261 // Values may not be consistent wrt to each other. 262 263 size_t young_gen_committed() { return _young_gen_committed; } 264 265 size_t eden_space_used() { return _eden_space_used; } 266 size_t survivor_space_used() { return _survivor_space_used; } 267 268 size_t old_gen_committed(); 269 size_t old_gen_used(); 270 271 size_t old_space_committed() { return _old_space_committed; } 272 size_t old_space_used() { return _old_space_used; } 273 size_t archive_space_used() { return _archive_space_used; } 274 275 size_t humongous_space_used() { return _humongous_space_used; } 276 277 // Monitoring support for MemoryPools. Values in the returned MemoryUsage are 278 // guaranteed to be consistent with each other. 279 MemoryUsage memory_usage(); 280 MemoryUsage eden_space_memory_usage(size_t initial_size, size_t max_size); 281 MemoryUsage survivor_space_memory_usage(size_t initial_size, size_t max_size); 282 MemoryUsage old_space_memory_usage(size_t initial_size, size_t max_size); 283 MemoryUsage archive_space_memory_usage(size_t initial_size, size_t max_size); 284 MemoryUsage humongous_space_memory_usage(size_t initial_size, size_t max_size); 285 }; 286 287 // TraceMemoryManagerStats for concurrent cycle. 288 class TraceConcMemoryManagerStats : public TraceMemoryManagerStats { 289 public: 290 enum Stage { 291 CycleStart, 292 Remark, 293 Cleanup, 294 CycleEnd 295 }; 296 TraceConcMemoryManagerStats(Stage stage, GCCause::Cause cause); 297 }; 298 299 // Scope object for java.lang.management support. 300 class G1MonitoringScope : public StackObj { 301 TraceCollectorStats _tcs; 302 TraceMemoryManagerStats _tms; 303 public: 304 G1MonitoringScope(G1MonitoringSupport* g1mm, bool full_gc, bool mixed_gc); 305 }; 306 307 #endif // SHARE_VM_GC_G1_G1MONITORINGSUPPORT_HPP