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 // * free : free regions 50 // 51 // The combination of eden and survivor regions form the equivalent of 52 // the young generation in the other GCs. The combination of old and 53 // humongous regions form the equivalent of the old generation in the 54 // other GCs. Free regions do not have a good equivalent in the other 55 // GCs given that they can be allocated as any of the other region types. 56 // 57 // The monitoring tools expect the heap to contain a number of 58 // generations (young, old, perm) and each generation to contain a 59 // number of spaces (young: eden, survivors, old). Given that G1 does 60 // not maintain those spaces physically (e.g., the set of 61 // non-contiguous eden regions can be considered as a "logical" 62 // space), we'll provide the illusion that those generations and 63 // spaces exist. In reality, each generation and space refers to a set 64 // of heap regions that are potentially non-contiguous. 65 // 66 // This class provides interfaces to access the min, current, and max 67 // capacity and current occupancy for each of G1's logical spaces and 68 // generations we expose to the monitoring tools. Also provided are 69 // counters for G1 concurrent collections and stop-the-world full heap 70 // collections. 71 // 72 // Below is a description of how the various sizes are calculated. 73 // 74 // * Current Capacity 75 // 76 // - heap_capacity = current heap capacity (e.g., current committed size) 77 // - young_gen_capacity = current max young gen target capacity 78 // (i.e., young gen target capacity + max allowed expansion capacity) 79 // - survivor_capacity = current survivor region capacity 80 // - eden_capacity = young_gen_capacity - survivor_capacity 81 // - old_capacity = heap_capacity - young_gen_capacity 82 // 83 // What we do in the above is to distribute the free regions among 84 // eden_capacity and old_capacity. 85 // 86 // * Occupancy 87 // 88 // - young_gen_used = current young region capacity 89 // - survivor_used = survivor_capacity 90 // - eden_used = young_gen_used - survivor_used 91 // - old_used = overall_used - young_gen_used 92 // 93 // Unfortunately, we currently only keep track of the number of 94 // currently allocated young and survivor regions + the overall used 95 // bytes in the heap, so the above can be a little inaccurate. 96 // 97 // * Min Capacity 98 // 99 // We set this to 0 for all spaces. 100 // 101 // * Max Capacity 102 // 103 // For jstat, we set the max capacity of all spaces to heap_capacity, 104 // given that we don't always have a reasonable upper bound on how big 105 // each space can grow. For the memory pools, we make the max 106 // capacity undefined with the exception of the old memory pool for 107 // which we make the max capacity same as the max heap capacity. 108 // 109 // If we had more accurate occupancy / capacity information per 110 // region set the above calculations would be greatly simplified and 111 // be made more accurate. 112 // 113 // We update all the above synchronously and we store the results in 114 // fields so that we just read said fields when needed. A subtle point 115 // is that all the above sizes need to be recalculated when the old 116 // gen changes capacity (after a GC or after a humongous allocation) 117 // but only the eden occupancy changes when a new eden region is 118 // allocated. So, in the latter case we have minimal recalculation to 119 // do which is important as we want to keep the eden region allocation 120 // path as low-overhead as possible. 121 122 class G1MonitoringSupport : public CHeapObj<mtGC> { 123 friend class VMStructs; 124 friend class G1MonitoringScope; 125 126 G1CollectedHeap* _g1h; 127 128 // java.lang.management MemoryManager and MemoryPool support 129 GCMemoryManager _incremental_memory_manager; 130 GCMemoryManager _full_gc_memory_manager; 131 132 MemoryPool* _eden_space_pool; 133 MemoryPool* _survivor_space_pool; 134 MemoryPool* _old_gen_pool; 135 136 // jstat performance counters 137 // incremental collections both young and mixed 138 CollectorCounters* _incremental_collection_counters; 139 // full stop-the-world collections 140 CollectorCounters* _full_collection_counters; 141 // stop-the-world phases in G1 142 CollectorCounters* _conc_collection_counters; 143 // young collection set counters. The _eden_counters, 144 // _from_counters, and _to_counters are associated with 145 // this "generational" counter. 146 GenerationCounters* _young_gen_counters; 147 // old collection set counters. The _old_space_counters 148 // below are associated with this "generational" counter. 149 GenerationCounters* _old_gen_counters; 150 // Counters for the capacity and used for 151 // the whole heap 152 HSpaceCounters* _old_space_counters; 153 // the young collection 154 HSpaceCounters* _eden_space_counters; 155 // the survivor collection (only one, _to_counters, is actively used) 156 HSpaceCounters* _from_space_counters; 157 HSpaceCounters* _to_space_counters; 158 159 // When it's appropriate to recalculate the various sizes (at the 160 // end of a GC, when a new eden region is allocated, etc.) we store 161 // them here so that we can easily report them when needed and not 162 // have to recalculate them every time. 163 164 size_t _overall_committed; 165 size_t _overall_used; 166 167 size_t _young_gen_committed; 168 size_t _old_gen_committed; 169 170 size_t _eden_space_committed; 171 size_t _eden_space_used; 172 size_t _survivor_space_committed; 173 size_t _survivor_space_used; 174 175 size_t _old_gen_used; 176 177 // It returns x - y if x > y, 0 otherwise. 178 // As described in the comment above, some of the inputs to the 179 // calculations we have to do are obtained concurrently and hence 180 // may be inconsistent with each other. So, this provides a 181 // defensive way of performing the subtraction and avoids the value 182 // going negative (which would mean a very large result, given that 183 // the parameter are size_t). 184 static size_t subtract_up_to_zero(size_t x, size_t y) { 185 if (x > y) { 186 return x - y; 187 } else { 188 return 0; 189 } 190 } 191 192 // Recalculate all the sizes. 193 void recalculate_sizes(); 194 195 void recalculate_eden_size(); 196 197 public: 198 G1MonitoringSupport(G1CollectedHeap* g1h); 199 ~G1MonitoringSupport(); 200 201 void initialize_serviceability(); 202 203 MemoryUsage memory_usage(); 204 GrowableArray<GCMemoryManager*> memory_managers(); 205 GrowableArray<MemoryPool*> memory_pools(); 206 207 // Unfortunately, the jstat tool assumes that no space has 0 208 // capacity. In our case, given that each space is logical, it's 209 // possible that no regions will be allocated to it, hence to have 0 210 // capacity (e.g., if there are no survivor regions, the survivor 211 // space has 0 capacity). The way we deal with this is to always pad 212 // each capacity value we report to jstat by a very small amount to 213 // make sure that it's never zero. Given that we sometimes have to 214 // report a capacity of a generation that contains several spaces 215 // (e.g., young gen includes one eden, two survivor spaces), the 216 // mult parameter is provided in order to adding the appropriate 217 // padding multiple times so that the capacities add up correctly. 218 static size_t pad_capacity(size_t size_bytes, size_t mult = 1) { 219 return size_bytes + MinObjAlignmentInBytes * mult; 220 } 221 222 // Recalculate all the sizes from scratch and update all the jstat 223 // counters accordingly. 224 void update_sizes(); 225 226 void update_eden_size(); 227 228 CollectorCounters* conc_collection_counters() { 229 return _conc_collection_counters; 230 } 231 232 // Monitoring support used by 233 // MemoryService 234 // jstat counters 235 // Tracing 236 // Values may not be consistent wrt to each other. 237 238 size_t young_gen_committed() { return _young_gen_committed; } 239 240 size_t eden_space_used() { return _eden_space_used; } 241 size_t survivor_space_used() { return _survivor_space_used; } 242 243 size_t old_gen_committed() { return _old_gen_committed; } 244 size_t old_gen_used() { return _old_gen_used; } 245 246 // Monitoring support for MemoryPools. Values in the returned MemoryUsage are 247 // guaranteed to be consistent with each other. 248 MemoryUsage eden_space_memory_usage(size_t initial_size, size_t max_size); 249 MemoryUsage survivor_space_memory_usage(size_t initial_size, size_t max_size); 250 251 MemoryUsage old_gen_memory_usage(size_t initial_size, size_t max_size); 252 }; 253 254 // Scope object for java.lang.management support. 255 class G1MonitoringScope : public StackObj { 256 TraceCollectorStats _tcs; 257 TraceMemoryManagerStats _tms; 258 public: 259 G1MonitoringScope(G1MonitoringSupport* g1mm, bool full_gc, bool all_memory_pools_affected); 260 }; 261 262 #endif // SHARE_VM_GC_G1_G1MONITORINGSUPPORT_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 |