1 /* 2 * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 #include "gc/epsilon/epsilonHeap.hpp" 26 #include "gc/epsilon/epsilonMemoryPool.hpp" 27 #include "gc/epsilon/epsilonThreadLocalData.hpp" 28 #include "gc/shared/gcArguments.hpp" 29 #include "gc/shared/locationPrinter.inline.hpp" 30 #include "memory/allocation.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "memory/universe.hpp" 34 #include "runtime/globals.hpp" 35 36 jint EpsilonHeap::initialize() { 37 size_t align = HeapAlignment; 38 size_t init_byte_size = align_up(InitialHeapSize, align); 39 size_t max_byte_size = align_up(MaxHeapSize, align); 40 41 // Initialize backing storage 42 ReservedHeapSpace heap_rs = Universe::reserve_heap(max_byte_size, align); 43 _virtual_space.initialize(heap_rs, init_byte_size); 44 45 MemRegion committed_region((HeapWord*)_virtual_space.low(), (HeapWord*)_virtual_space.high()); 46 MemRegion reserved_region((HeapWord*)_virtual_space.low_boundary(), (HeapWord*)_virtual_space.high_boundary()); 47 48 initialize_reserved_region(heap_rs); 49 50 _space = new ContiguousSpace(); 51 _space->initialize(committed_region, /* clear_space = */ true, /* mangle_space = */ true); 52 53 // Precompute hot fields 54 _max_tlab_size = MIN2(CollectedHeap::max_tlab_size(), align_object_size(EpsilonMaxTLABSize / HeapWordSize)); 55 _step_counter_update = MIN2<size_t>(max_byte_size / 16, EpsilonUpdateCountersStep); 56 _step_heap_print = (EpsilonPrintHeapSteps == 0) ? SIZE_MAX : (max_byte_size / EpsilonPrintHeapSteps); 57 _decay_time_ns = (int64_t) EpsilonTLABDecayTime * NANOSECS_PER_MILLISEC; 58 59 // Enable monitoring 60 _monitoring_support = new EpsilonMonitoringSupport(this); 61 _last_counter_update = 0; 62 _last_heap_print = 0; 63 64 // Install barrier set 65 BarrierSet::set_barrier_set(new EpsilonBarrierSet()); 66 67 // All done, print out the configuration 68 if (init_byte_size != max_byte_size) { 69 log_info(gc)("Resizeable heap; starting at " SIZE_FORMAT "M, max: " SIZE_FORMAT "M, step: " SIZE_FORMAT "M", 70 init_byte_size / M, max_byte_size / M, EpsilonMinHeapExpand / M); 71 } else { 72 log_info(gc)("Non-resizeable heap; start/max: " SIZE_FORMAT "M", init_byte_size / M); 73 } 74 75 if (UseTLAB) { 76 log_info(gc)("Using TLAB allocation; max: " SIZE_FORMAT "K", _max_tlab_size * HeapWordSize / K); 77 if (EpsilonElasticTLAB) { 78 log_info(gc)("Elastic TLABs enabled; elasticity: %.2fx", EpsilonTLABElasticity); 79 } 80 if (EpsilonElasticTLABDecay) { 81 log_info(gc)("Elastic TLABs decay enabled; decay time: " SIZE_FORMAT "ms", EpsilonTLABDecayTime); 82 } 83 } else { 84 log_info(gc)("Not using TLAB allocation"); 85 } 86 87 return JNI_OK; 88 } 89 90 void EpsilonHeap::post_initialize() { 91 CollectedHeap::post_initialize(); 92 } 93 94 void EpsilonHeap::initialize_serviceability() { 95 _pool = new EpsilonMemoryPool(this); 96 _memory_manager.add_pool(_pool); 97 } 98 99 GrowableArray<GCMemoryManager*> EpsilonHeap::memory_managers() { 100 GrowableArray<GCMemoryManager*> memory_managers(1); 101 memory_managers.append(&_memory_manager); 102 return memory_managers; 103 } 104 105 GrowableArray<MemoryPool*> EpsilonHeap::memory_pools() { 106 GrowableArray<MemoryPool*> memory_pools(1); 107 memory_pools.append(_pool); 108 return memory_pools; 109 } 110 111 size_t EpsilonHeap::unsafe_max_tlab_alloc(Thread* thr) const { 112 // Return max allocatable TLAB size, and let allocation path figure out 113 // the actual allocation size. Note: result should be in bytes. 114 return _max_tlab_size * HeapWordSize; 115 } 116 117 EpsilonHeap* EpsilonHeap::heap() { 118 CollectedHeap* heap = Universe::heap(); 119 assert(heap != NULL, "Uninitialized access to EpsilonHeap::heap()"); 120 assert(heap->kind() == CollectedHeap::Epsilon, "Not an Epsilon heap"); 121 return (EpsilonHeap*)heap; 122 } 123 124 HeapWord* EpsilonHeap::allocate_work(size_t size) { 125 assert(is_object_aligned(size), "Allocation size should be aligned: " SIZE_FORMAT, size); 126 127 HeapWord* res = _space->par_allocate(size); 128 129 while (res == NULL) { 130 // Allocation failed, attempt expansion, and retry: 131 MutexLocker ml(Heap_lock); 132 133 size_t space_left = max_capacity() - capacity(); 134 size_t want_space = MAX2(size, EpsilonMinHeapExpand); 135 136 if (want_space < space_left) { 137 // Enough space to expand in bulk: 138 bool expand = _virtual_space.expand_by(want_space); 139 assert(expand, "Should be able to expand"); 140 } else if (size < space_left) { 141 // No space to expand in bulk, and this allocation is still possible, 142 // take all the remaining space: 143 bool expand = _virtual_space.expand_by(space_left); 144 assert(expand, "Should be able to expand"); 145 } else { 146 // No space left: 147 return NULL; 148 } 149 150 _space->set_end((HeapWord *) _virtual_space.high()); 151 res = _space->par_allocate(size); 152 } 153 154 size_t used = _space->used(); 155 156 // Allocation successful, update counters 157 { 158 size_t last = _last_counter_update; 159 if ((used - last >= _step_counter_update) && Atomic::cmpxchg(used, &_last_counter_update, last) == last) { 160 _monitoring_support->update_counters(); 161 } 162 } 163 164 // ...and print the occupancy line, if needed 165 { 166 size_t last = _last_heap_print; 167 if ((used - last >= _step_heap_print) && Atomic::cmpxchg(used, &_last_heap_print, last) == last) { 168 print_heap_info(used); 169 print_metaspace_info(); 170 } 171 } 172 173 assert(is_object_aligned(res), "Object should be aligned: " PTR_FORMAT, p2i(res)); 174 return res; 175 } 176 177 HeapWord* EpsilonHeap::allocate_new_tlab(size_t min_size, 178 size_t requested_size, 179 size_t* actual_size) { 180 Thread* thread = Thread::current(); 181 182 // Defaults in case elastic paths are not taken 183 bool fits = true; 184 size_t size = requested_size; 185 size_t ergo_tlab = requested_size; 186 int64_t time = 0; 187 188 if (EpsilonElasticTLAB) { 189 ergo_tlab = EpsilonThreadLocalData::ergo_tlab_size(thread); 190 191 if (EpsilonElasticTLABDecay) { 192 int64_t last_time = EpsilonThreadLocalData::last_tlab_time(thread); 193 time = (int64_t) os::javaTimeNanos(); 194 195 assert(last_time <= time, "time should be monotonic"); 196 197 // If the thread had not allocated recently, retract the ergonomic size. 198 // This conserves memory when the thread had initial burst of allocations, 199 // and then started allocating only sporadically. 200 if (last_time != 0 && (time - last_time > _decay_time_ns)) { 201 ergo_tlab = 0; 202 EpsilonThreadLocalData::set_ergo_tlab_size(thread, 0); 203 } 204 } 205 206 // If we can fit the allocation under current TLAB size, do so. 207 // Otherwise, we want to elastically increase the TLAB size. 208 fits = (requested_size <= ergo_tlab); 209 if (!fits) { 210 size = (size_t) (ergo_tlab * EpsilonTLABElasticity); 211 } 212 } 213 214 // Always honor boundaries 215 size = MAX2(min_size, MIN2(_max_tlab_size, size)); 216 217 // Always honor alignment 218 size = align_up(size, MinObjAlignment); 219 220 // Check that adjustments did not break local and global invariants 221 assert(is_object_aligned(size), 222 "Size honors object alignment: " SIZE_FORMAT, size); 223 assert(min_size <= size, 224 "Size honors min size: " SIZE_FORMAT " <= " SIZE_FORMAT, min_size, size); 225 assert(size <= _max_tlab_size, 226 "Size honors max size: " SIZE_FORMAT " <= " SIZE_FORMAT, size, _max_tlab_size); 227 assert(size <= CollectedHeap::max_tlab_size(), 228 "Size honors global max size: " SIZE_FORMAT " <= " SIZE_FORMAT, size, CollectedHeap::max_tlab_size()); 229 230 if (log_is_enabled(Trace, gc)) { 231 ResourceMark rm; 232 log_trace(gc)("TLAB size for \"%s\" (Requested: " SIZE_FORMAT "K, Min: " SIZE_FORMAT 233 "K, Max: " SIZE_FORMAT "K, Ergo: " SIZE_FORMAT "K) -> " SIZE_FORMAT "K", 234 thread->name(), 235 requested_size * HeapWordSize / K, 236 min_size * HeapWordSize / K, 237 _max_tlab_size * HeapWordSize / K, 238 ergo_tlab * HeapWordSize / K, 239 size * HeapWordSize / K); 240 } 241 242 // All prepared, let's do it! 243 HeapWord* res = allocate_work(size); 244 245 if (res != NULL) { 246 // Allocation successful 247 *actual_size = size; 248 if (EpsilonElasticTLABDecay) { 249 EpsilonThreadLocalData::set_last_tlab_time(thread, time); 250 } 251 if (EpsilonElasticTLAB && !fits) { 252 // If we requested expansion, this is our new ergonomic TLAB size 253 EpsilonThreadLocalData::set_ergo_tlab_size(thread, size); 254 } 255 } else { 256 // Allocation failed, reset ergonomics to try and fit smaller TLABs 257 if (EpsilonElasticTLAB) { 258 EpsilonThreadLocalData::set_ergo_tlab_size(thread, 0); 259 } 260 } 261 262 return res; 263 } 264 265 HeapWord* EpsilonHeap::mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded) { 266 *gc_overhead_limit_was_exceeded = false; 267 return allocate_work(size); 268 } 269 270 void EpsilonHeap::collect(GCCause::Cause cause) { 271 switch (cause) { 272 case GCCause::_metadata_GC_threshold: 273 case GCCause::_metadata_GC_clear_soft_refs: 274 // Receiving these causes means the VM itself entered the safepoint for metadata collection. 275 // While Epsilon does not do GC, it has to perform sizing adjustments, otherwise we would 276 // re-enter the safepoint again very soon. 277 278 assert(SafepointSynchronize::is_at_safepoint(), "Expected at safepoint"); 279 log_info(gc)("GC request for \"%s\" is handled", GCCause::to_string(cause)); 280 MetaspaceGC::compute_new_size(); 281 print_metaspace_info(); 282 break; 283 default: 284 log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); 285 } 286 _monitoring_support->update_counters(); 287 } 288 289 void EpsilonHeap::do_full_collection(bool clear_all_soft_refs) { 290 collect(gc_cause()); 291 } 292 293 void EpsilonHeap::object_iterate(ObjectClosure *cl) { 294 _space->object_iterate(cl); 295 } 296 297 void EpsilonHeap::print_on(outputStream *st) const { 298 st->print_cr("Epsilon Heap"); 299 300 // Cast away constness: 301 ((VirtualSpace)_virtual_space).print_on(st); 302 303 st->print_cr("Allocation space:"); 304 _space->print_on(st); 305 306 MetaspaceUtils::print_on(st); 307 } 308 309 bool EpsilonHeap::print_location(outputStream* st, void* addr) const { 310 return BlockLocationPrinter<EpsilonHeap>::print_location(st, addr); 311 } 312 313 void EpsilonHeap::print_tracing_info() const { 314 print_heap_info(used()); 315 print_metaspace_info(); 316 } 317 318 void EpsilonHeap::print_heap_info(size_t used) const { 319 size_t reserved = max_capacity(); 320 size_t committed = capacity(); 321 322 if (reserved != 0) { 323 log_info(gc)("Heap: " SIZE_FORMAT "%s reserved, " SIZE_FORMAT "%s (%.2f%%) committed, " 324 SIZE_FORMAT "%s (%.2f%%) used", 325 byte_size_in_proper_unit(reserved), proper_unit_for_byte_size(reserved), 326 byte_size_in_proper_unit(committed), proper_unit_for_byte_size(committed), 327 committed * 100.0 / reserved, 328 byte_size_in_proper_unit(used), proper_unit_for_byte_size(used), 329 used * 100.0 / reserved); 330 } else { 331 log_info(gc)("Heap: no reliable data"); 332 } 333 } 334 335 void EpsilonHeap::print_metaspace_info() const { 336 size_t reserved = MetaspaceUtils::reserved_bytes(); 337 size_t committed = MetaspaceUtils::committed_bytes(); 338 size_t used = MetaspaceUtils::used_bytes(); 339 340 if (reserved != 0) { 341 log_info(gc, metaspace)("Metaspace: " SIZE_FORMAT "%s reserved, " SIZE_FORMAT "%s (%.2f%%) committed, " 342 SIZE_FORMAT "%s (%.2f%%) used", 343 byte_size_in_proper_unit(reserved), proper_unit_for_byte_size(reserved), 344 byte_size_in_proper_unit(committed), proper_unit_for_byte_size(committed), 345 committed * 100.0 / reserved, 346 byte_size_in_proper_unit(used), proper_unit_for_byte_size(used), 347 used * 100.0 / reserved); 348 } else { 349 log_info(gc, metaspace)("Metaspace: no reliable data"); 350 } 351 }