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 "memory/allocation.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "memory/resourceArea.hpp" 30 31 jint EpsilonHeap::initialize() { 32 size_t init_byte_size = _policy->initial_heap_byte_size(); 33 size_t max_byte_size = _policy->max_heap_byte_size(); 34 size_t align = _policy->heap_alignment(); 35 36 ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size, align); 37 _virtual_space.initialize(heap_rs, init_byte_size); 38 39 MemRegion committed_region((HeapWord*)_virtual_space.low(), (HeapWord*)_virtual_space.high()); 40 MemRegion reserved_region((HeapWord*)_virtual_space.low_boundary(), (HeapWord*)_virtual_space.high_boundary()); 41 42 initialize_reserved_region(reserved_region.start(), reserved_region.end()); 43 44 _space = new ContiguousSpace(); 45 _space->initialize(committed_region, true, true); 46 47 BarrierSet::set_barrier_set(new EpsilonBarrierSet()); 48 49 _max_tlab_size = MIN2(CollectedHeap::max_tlab_size(), EpsilonMaxTLABSize / HeapWordSize); 50 51 _monitoring_support = new EpsilonMonitoringSupport(this); 52 _last_counter_update = 0; 53 _last_heap_print = 0; 54 55 _step_counter_update = MIN2<size_t>(max_byte_size / 16, EpsilonUpdateCountersStep); 56 _step_heap_print = (EpsilonPrintHeapStep == 0) ? SIZE_MAX : (max_byte_size / EpsilonPrintHeapStep); 57 58 if (init_byte_size != max_byte_size) { 59 log_info(gc)("Initialized with " SIZE_FORMAT "M heap, resizeable to up to " SIZE_FORMAT "M heap with " SIZE_FORMAT "M steps", 60 init_byte_size / M, max_byte_size / M, EpsilonMinHeapExpand / M); 61 } else { 62 log_info(gc)("Initialized with " SIZE_FORMAT "M non-resizeable heap", init_byte_size / M); 63 } 64 if (UseTLAB) { 65 log_info(gc)("Using TLAB allocation; min: " SIZE_FORMAT "K, max: " SIZE_FORMAT "K", 66 ThreadLocalAllocBuffer::min_size() * HeapWordSize / K, 67 _max_tlab_size*HeapWordSize / K); 68 } else { 69 log_info(gc)("Not using TLAB allocation"); 70 } 71 72 return JNI_OK; 73 } 74 75 void EpsilonHeap::post_initialize() { 76 CollectedHeap::post_initialize(); 77 } 78 79 void EpsilonHeap::initialize_serviceability() { 80 _pool = new EpsilonMemoryPool(this); 81 _memory_manager.add_pool(_pool); 82 } 83 84 GrowableArray<GCMemoryManager*> EpsilonHeap::memory_managers() { 85 GrowableArray<GCMemoryManager*> memory_managers(1); 86 memory_managers.append(&_memory_manager); 87 return memory_managers; 88 } 89 90 GrowableArray<MemoryPool*> EpsilonHeap::memory_pools() { 91 GrowableArray<MemoryPool*> memory_pools(1); 92 memory_pools.append(_pool); 93 return memory_pools; 94 } 95 96 size_t EpsilonHeap::unsafe_max_tlab_alloc(Thread* thr) const { 97 // This is the only way we can control TLAB sizes without having safepoints. 98 // Implement exponential expansion within [MinTLABSize; _max_tlab_size], based 99 // on previously "used" TLAB size. 100 101 size_t size = MIN2(_max_tlab_size * HeapWordSize, MAX2(MinTLABSize, thr->tlab().used() * HeapWordSize * 2)); 102 103 if (log_is_enabled(Trace, gc)) { 104 ResourceMark rm; 105 log_trace(gc)( 106 "Selecting TLAB size for \"%s\" (Desired: " SIZE_FORMAT "K, Used: " SIZE_FORMAT "K) -> " SIZE_FORMAT "K", 107 Thread::current()->name(), 108 thr->tlab().desired_size() * HeapWordSize / K, 109 thr->tlab().used() * HeapWordSize / K, 110 size / K); 111 } 112 113 return size; 114 } 115 116 EpsilonHeap* EpsilonHeap::heap() { 117 CollectedHeap* heap = Universe::heap(); 118 assert(heap != NULL, "Uninitialized access to EpsilonHeap::heap()"); 119 assert(heap->kind() == CollectedHeap::Epsilon, "Not an Epsilon heap"); 120 return (EpsilonHeap*)heap; 121 } 122 123 HeapWord* EpsilonHeap::allocate_work(size_t size) { 124 HeapWord* res = _space->par_allocate(size); 125 126 while (res == NULL) { 127 // Allocation failed, attempt expansion, and retry: 128 MutexLockerEx ml(Heap_lock); 129 130 size_t space_left = max_capacity() - capacity(); 131 size_t want_space = MAX2(size, EpsilonMinHeapExpand); 132 133 if (want_space < space_left) { 134 // Enough space to expand in bulk: 135 bool expand = _virtual_space.expand_by(want_space); 136 assert(expand, "Should be able to expand"); 137 } else if (size < space_left) { 138 // No space to expand in bulk, and this allocation is still possible, 139 // take all the space left: 140 bool expand = _virtual_space.expand_by(space_left); 141 assert(expand, "Should be able to expand"); 142 } else { 143 // No space left: 144 return NULL; 145 } 146 147 _space->set_end((HeapWord *) _virtual_space.high()); 148 res = _space->par_allocate(size); 149 } 150 151 size_t used = _space->used(); 152 if (used - _last_counter_update >= _step_counter_update) { 153 _last_counter_update = used; 154 _monitoring_support->update_counters(); 155 } 156 157 if (used - _last_heap_print >= _step_heap_print) { 158 log_info(gc)("Heap: " SIZE_FORMAT "M reserved, " SIZE_FORMAT "M committed, " SIZE_FORMAT "M used", 159 max_capacity() / M, capacity() / M, used / M); 160 _last_heap_print = used; 161 } 162 163 return res; 164 } 165 166 HeapWord* EpsilonHeap::allocate_new_tlab(size_t size) { 167 return allocate_work(size); 168 } 169 170 HeapWord* EpsilonHeap::mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded) { 171 *gc_overhead_limit_was_exceeded = false; 172 return allocate_work(size); 173 } 174 175 void EpsilonHeap::collect(GCCause::Cause cause) { 176 log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); 177 _monitoring_support->update_counters(); 178 } 179 180 void EpsilonHeap::do_full_collection(bool clear_all_soft_refs) { 181 log_info(gc)("Full GC request for \"%s\" is ignored", GCCause::to_string(gc_cause())); 182 _monitoring_support->update_counters(); 183 } 184 185 void EpsilonHeap::safe_object_iterate(ObjectClosure *cl) { 186 _space->safe_object_iterate(cl); 187 } 188 189 void EpsilonHeap::print_on(outputStream *st) const { 190 st->print_cr("Epsilon Heap"); 191 192 // Cast away constness: 193 ((VirtualSpace)_virtual_space).print_on(st); 194 195 st->print_cr("Allocation space:"); 196 _space->print_on(st); 197 } 198 199 void EpsilonHeap::print_tracing_info() const { 200 Log(gc) log; 201 size_t allocated_kb = used() / K; 202 log.info("Total allocated: " SIZE_FORMAT " KB", 203 allocated_kb); 204 log.info("Average allocation rate: " SIZE_FORMAT " KB/sec", 205 (size_t)(allocated_kb * NANOSECS_PER_SEC / os::elapsed_counter())); 206 }