author | shade |
Sun, 20 May 2018 22:03:10 +0200 | |
branch | epsilon-gc-branch |
changeset 56577 | 338a117fb65c |
parent 56510 | 54c1aa06c1d8 |
child 56580 | b6c934c3fda2 |
permissions | -rw-r--r-- |
55767 | 1 |
/* |
56350 | 2 |
* Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved. |
55767 | 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" |
|
55939 | 25 |
#include "gc/epsilon/epsilonHeap.hpp" |
55974 | 26 |
#include "gc/epsilon/epsilonMemoryPool.hpp" |
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
27 |
#include "gc/epsilon/epsilonThreadLocalData.hpp" |
56354 | 28 |
#include "memory/allocation.hpp" |
29 |
#include "memory/allocation.inline.hpp" |
|
30 |
#include "memory/resourceArea.hpp" |
|
55767 | 31 |
|
55939 | 32 |
jint EpsilonHeap::initialize() { |
55767 | 33 |
size_t init_byte_size = _policy->initial_heap_byte_size(); |
34 |
size_t max_byte_size = _policy->max_heap_byte_size(); |
|
35 |
size_t align = _policy->heap_alignment(); |
|
36 |
||
37 |
ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size, align); |
|
38 |
_virtual_space.initialize(heap_rs, init_byte_size); |
|
39 |
||
40 |
MemRegion committed_region((HeapWord*)_virtual_space.low(), (HeapWord*)_virtual_space.high()); |
|
41 |
MemRegion reserved_region((HeapWord*)_virtual_space.low_boundary(), (HeapWord*)_virtual_space.high_boundary()); |
|
42 |
||
43 |
initialize_reserved_region(reserved_region.start(), reserved_region.end()); |
|
44 |
||
45 |
_space = new ContiguousSpace(); |
|
46 |
_space->initialize(committed_region, true, true); |
|
47 |
||
56449 | 48 |
BarrierSet::set_barrier_set(new EpsilonBarrierSet()); |
55767 | 49 |
|
50 |
_max_tlab_size = MIN2(CollectedHeap::max_tlab_size(), EpsilonMaxTLABSize / HeapWordSize); |
|
51 |
||
52 |
_monitoring_support = new EpsilonMonitoringSupport(this); |
|
53 |
_last_counter_update = 0; |
|
55979 | 54 |
_last_heap_print = 0; |
55 |
||
56 |
_step_counter_update = MIN2<size_t>(max_byte_size / 16, EpsilonUpdateCountersStep); |
|
57 |
_step_heap_print = (EpsilonPrintHeapStep == 0) ? SIZE_MAX : (max_byte_size / EpsilonPrintHeapStep); |
|
55767 | 58 |
|
56577 | 59 |
_decay_time_ns = (int64_t) EpsilonTLABDecayTime * 1000 * 1000; |
60 |
||
55767 | 61 |
if (init_byte_size != max_byte_size) { |
62 |
log_info(gc)("Initialized with " SIZE_FORMAT "M heap, resizeable to up to " SIZE_FORMAT "M heap with " SIZE_FORMAT "M steps", |
|
63 |
init_byte_size / M, max_byte_size / M, EpsilonMinHeapExpand / M); |
|
64 |
} else { |
|
65 |
log_info(gc)("Initialized with " SIZE_FORMAT "M non-resizeable heap", init_byte_size / M); |
|
66 |
} |
|
67 |
if (UseTLAB) { |
|
56577 | 68 |
log_info(gc)("Using TLAB allocation; max: " SIZE_FORMAT "K", _max_tlab_size * HeapWordSize / K); |
69 |
if (EpsilonElasticTLAB) { |
|
70 |
log_info(gc)("Elastic TLABs with elasticity = %.2fx", EpsilonTLABElasticity); |
|
71 |
} |
|
72 |
if (EpsilonElasticTLABDecay) { |
|
73 |
log_info(gc)("Elastic TLABs with decay = " INT64_FORMAT " ms", EpsilonTLABDecayTime); |
|
74 |
} |
|
55767 | 75 |
} else { |
76 |
log_info(gc)("Not using TLAB allocation"); |
|
77 |
} |
|
78 |
||
79 |
return JNI_OK; |
|
80 |
} |
|
81 |
||
55974 | 82 |
void EpsilonHeap::post_initialize() { |
83 |
CollectedHeap::post_initialize(); |
|
84 |
} |
|
85 |
||
86 |
void EpsilonHeap::initialize_serviceability() { |
|
55975 | 87 |
_pool = new EpsilonMemoryPool(this); |
88 |
_memory_manager.add_pool(_pool); |
|
55974 | 89 |
} |
90 |
||
91 |
GrowableArray<GCMemoryManager*> EpsilonHeap::memory_managers() { |
|
55975 | 92 |
GrowableArray<GCMemoryManager*> memory_managers(1); |
93 |
memory_managers.append(&_memory_manager); |
|
55974 | 94 |
return memory_managers; |
95 |
} |
|
96 |
||
97 |
GrowableArray<MemoryPool*> EpsilonHeap::memory_pools() { |
|
56492 | 98 |
GrowableArray<MemoryPool*> memory_pools(1); |
55975 | 99 |
memory_pools.append(_pool); |
55974 | 100 |
return memory_pools; |
101 |
} |
|
102 |
||
56352 | 103 |
size_t EpsilonHeap::unsafe_max_tlab_alloc(Thread* thr) const { |
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
104 |
// Return max allocatable TLAB size, and let allocation path figure out |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
105 |
// the actual TLAB allocation size. |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
106 |
return _max_tlab_size; |
55767 | 107 |
} |
108 |
||
55939 | 109 |
EpsilonHeap* EpsilonHeap::heap() { |
55767 | 110 |
CollectedHeap* heap = Universe::heap(); |
55939 | 111 |
assert(heap != NULL, "Uninitialized access to EpsilonHeap::heap()"); |
56492 | 112 |
assert(heap->kind() == CollectedHeap::Epsilon, "Not an Epsilon heap"); |
55939 | 113 |
return (EpsilonHeap*)heap; |
55767 | 114 |
} |
115 |
||
55939 | 116 |
HeapWord* EpsilonHeap::allocate_work(size_t size) { |
55767 | 117 |
HeapWord* res = _space->par_allocate(size); |
118 |
||
119 |
while (res == NULL) { |
|
120 |
// Allocation failed, attempt expansion, and retry: |
|
121 |
MutexLockerEx ml(Heap_lock); |
|
55980
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
122 |
|
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
123 |
size_t space_left = max_capacity() - capacity(); |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
124 |
size_t want_space = MAX2(size, EpsilonMinHeapExpand); |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
125 |
|
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
126 |
if (want_space < space_left) { |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
127 |
// Enough space to expand in bulk: |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
128 |
bool expand = _virtual_space.expand_by(want_space); |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
129 |
assert(expand, "Should be able to expand"); |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
130 |
} else if (size < space_left) { |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
131 |
// No space to expand in bulk, and this allocation is still possible, |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
132 |
// take all the space left: |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
133 |
bool expand = _virtual_space.expand_by(space_left); |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
134 |
assert(expand, "Should be able to expand"); |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
135 |
} else { |
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
136 |
// No space left: |
55767 | 137 |
return NULL; |
138 |
} |
|
55980
67d289ae67f5
Make sure Epsilon is able to use the last bit of expanded heap
shade
parents:
55979
diff
changeset
|
139 |
|
55767 | 140 |
_space->set_end((HeapWord *) _virtual_space.high()); |
141 |
res = _space->par_allocate(size); |
|
142 |
} |
|
143 |
||
144 |
size_t used = _space->used(); |
|
55979 | 145 |
if (used - _last_counter_update >= _step_counter_update) { |
55767 | 146 |
_last_counter_update = used; |
147 |
_monitoring_support->update_counters(); |
|
148 |
} |
|
55979 | 149 |
|
150 |
if (used - _last_heap_print >= _step_heap_print) { |
|
151 |
log_info(gc)("Heap: " SIZE_FORMAT "M reserved, " SIZE_FORMAT "M committed, " SIZE_FORMAT "M used", |
|
152 |
max_capacity() / M, capacity() / M, used / M); |
|
153 |
_last_heap_print = used; |
|
154 |
} |
|
155 |
||
55767 | 156 |
return res; |
157 |
} |
|
158 |
||
56508 | 159 |
HeapWord* EpsilonHeap::allocate_new_tlab(size_t min_size, |
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
160 |
size_t requested_size, |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
161 |
size_t* actual_size) { |
56577 | 162 |
Thread* thread = Thread::current(); |
163 |
||
164 |
// Defaults in case elastic paths are not taken |
|
165 |
bool fits = true; |
|
166 |
size_t size = requested_size; |
|
167 |
size_t ergo_tlab = requested_size; |
|
168 |
int64_t time = 0; |
|
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
169 |
|
56577 | 170 |
if (EpsilonElasticTLAB) { |
171 |
ergo_tlab = EpsilonThreadLocalData::ergo_tlab_size(thread); |
|
172 |
||
173 |
if (EpsilonElasticTLABDecay) { |
|
174 |
int64_t last_alloc = EpsilonThreadLocalData::last_alloc_time(thread); |
|
175 |
time = (int64_t) os::javaTimeNanos(); |
|
176 |
||
177 |
assert(last_alloc <= time, "time should be monotonic"); |
|
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
178 |
|
56577 | 179 |
// If the thread had not allocated recently, retract the ergonomic size. |
180 |
// This conserves memory when the thread had initial burst of allocations, |
|
181 |
// and then started allocating only sporadically. |
|
182 |
if (last_alloc != 0 && (time - last_alloc > _decay_time_ns)) { |
|
183 |
ergo_tlab = 0; |
|
184 |
EpsilonThreadLocalData::set_ergo_tlab_size(thread, 0); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
// If we can fit the allocation under current TLAB size, do so. |
|
189 |
// Otherwise, we want to elastically increase the TLAB size. |
|
190 |
fits = (requested_size <= ergo_tlab); |
|
191 |
if (!fits) { |
|
192 |
size = (size_t) (ergo_tlab * EpsilonTLABElasticity); |
|
193 |
} |
|
194 |
} |
|
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
195 |
|
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
196 |
// Honor boundaries |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
197 |
size = MAX2(min_size, MIN2(_max_tlab_size, size)); |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
198 |
|
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
199 |
if (log_is_enabled(Trace, gc)) { |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
200 |
ResourceMark rm; |
56577 | 201 |
log_trace(gc)("TLAB size for \"%s\" (Requested: " SIZE_FORMAT "K, Min: " SIZE_FORMAT |
202 |
"K, Max: " SIZE_FORMAT "K, Ergo: " SIZE_FORMAT "K) -> " SIZE_FORMAT "K", |
|
203 |
thread->name(), |
|
204 |
requested_size * HeapWordSize / K, |
|
205 |
min_size * HeapWordSize / K, |
|
206 |
_max_tlab_size * HeapWordSize / K, |
|
207 |
ergo_tlab * HeapWordSize / K, |
|
208 |
size * HeapWordSize / K); |
|
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
209 |
} |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
210 |
|
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
211 |
HeapWord* res = allocate_work(size); |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
212 |
if (res != NULL) { |
56577 | 213 |
// Allocation successful |
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
214 |
*actual_size = size; |
56577 | 215 |
if (EpsilonElasticTLABDecay) { |
216 |
EpsilonThreadLocalData::set_last_alloc_time(thread, time); |
|
217 |
} |
|
218 |
if (EpsilonElasticTLAB && !fits) { |
|
219 |
// If we requested expansion, this is our new ergonomic TLAB size |
|
220 |
EpsilonThreadLocalData::set_ergo_tlab_size(thread, size); |
|
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
221 |
} |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
222 |
} else { |
56577 | 223 |
// Allocation failed, reset ergonomics to try and fit smaller TLABs |
224 |
if (EpsilonElasticTLAB) { |
|
225 |
EpsilonThreadLocalData::set_ergo_tlab_size(thread, 0); |
|
226 |
} |
|
56510
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
227 |
} |
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
228 |
|
54c1aa06c1d8
Epsilon should use Elastic TLABs facility to control sizes
shade
parents:
56508
diff
changeset
|
229 |
return res; |
55767 | 230 |
} |
231 |
||
55939 | 232 |
HeapWord* EpsilonHeap::mem_allocate(size_t size, bool *gc_overhead_limit_was_exceeded) { |
55767 | 233 |
*gc_overhead_limit_was_exceeded = false; |
234 |
return allocate_work(size); |
|
235 |
} |
|
236 |
||
55939 | 237 |
void EpsilonHeap::collect(GCCause::Cause cause) { |
56353 | 238 |
log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); |
55767 | 239 |
_monitoring_support->update_counters(); |
240 |
} |
|
241 |
||
55939 | 242 |
void EpsilonHeap::do_full_collection(bool clear_all_soft_refs) { |
56353 | 243 |
log_info(gc)("Full GC request for \"%s\" is ignored", GCCause::to_string(gc_cause())); |
55767 | 244 |
_monitoring_support->update_counters(); |
245 |
} |
|
246 |
||
55939 | 247 |
void EpsilonHeap::safe_object_iterate(ObjectClosure *cl) { |
55767 | 248 |
_space->safe_object_iterate(cl); |
249 |
} |
|
250 |
||
55939 | 251 |
void EpsilonHeap::print_on(outputStream *st) const { |
55767 | 252 |
st->print_cr("Epsilon Heap"); |
253 |
||
254 |
// Cast away constness: |
|
255 |
((VirtualSpace)_virtual_space).print_on(st); |
|
256 |
||
257 |
st->print_cr("Allocation space:"); |
|
258 |
_space->print_on(st); |
|
259 |
} |
|
260 |
||
55939 | 261 |
void EpsilonHeap::print_tracing_info() const { |
55767 | 262 |
Log(gc) log; |
263 |
size_t allocated_kb = used() / K; |
|
264 |
log.info("Total allocated: " SIZE_FORMAT " KB", |
|
265 |
allocated_kb); |
|
266 |
log.info("Average allocation rate: " SIZE_FORMAT " KB/sec", |
|
55976 | 267 |
(size_t)(allocated_kb * NANOSECS_PER_SEC / os::elapsed_counter())); |
55767 | 268 |
} |