author | iveresov |
Wed, 20 Jun 2018 17:30:43 -0700 | |
changeset 50689 | 070b5313b2fe |
parent 49873 | 26ebfe8ce852 |
child 50858 | 2d3e99a72541 |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
2 |
* Copyright (c) 2012, 2015, 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 |
package org.graalvm.compiler.hotspot.stubs; |
|
24 |
||
25 |
import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG; |
|
26 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.HEAP_END_LOCATION; |
|
27 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.HEAP_TOP_LOCATION; |
|
28 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.PROTOTYPE_MARK_WORD_LOCATION; |
|
29 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.TLAB_FAST_REFILL_WASTE_LOCATION; |
|
30 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.TLAB_NOF_REFILLS_LOCATION; |
|
31 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.TLAB_REFILL_WASTE_LIMIT_LOCATION; |
|
32 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.TLAB_SIZE_LOCATION; |
|
33 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.TLAB_SLOW_ALLOCATIONS_LOCATION; |
|
34 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.TLAB_THREAD_ALLOCATED_BYTES_LOCATION; |
|
35 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.arrayBaseOffset; |
|
36 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.getAndClearObjectResult; |
|
37 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.initializeTlab; |
|
38 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.isInstanceKlassFullyInitialized; |
|
46344 | 39 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readLayoutHelper; |
43972 | 40 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.log2WordSize; |
41 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.prototypeMarkWordOffset; |
|
42 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readTlabEnd; |
|
43 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readTlabStart; |
|
44 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readTlabTop; |
|
45 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.registerAsWord; |
|
46 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.threadAllocatedBytesOffset; |
|
47 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.threadTlabSizeOffset; |
|
48 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabAlignmentReserveInHeapWords; |
|
49 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabFastRefillWasteOffset; |
|
50 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabIntArrayMarkWord; |
|
51 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabNumberOfRefillsOffset; |
|
52 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabRefillWasteIncrement; |
|
53 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabRefillWasteLimitOffset; |
|
54 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabSlowAllocationsOffset; |
|
55 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.tlabStats; |
|
47798 | 56 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.useCMSIncrementalMode; |
50689
070b5313b2fe
8205105: VM crashes with "assert(Universe::heap()->is_in_reserved(start + words - 1)) failed: not in heap"
iveresov
parents:
49873
diff
changeset
|
57 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.useFastTLABRefill; |
43972 | 58 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.useG1GC; |
59 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.useTLAB; |
|
60 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.wordSize; |
|
61 |
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.writeTlabTop; |
|
62 |
import static org.graalvm.compiler.hotspot.stubs.StubUtil.handlePendingException; |
|
63 |
import static org.graalvm.compiler.hotspot.stubs.StubUtil.newDescriptor; |
|
64 |
import static org.graalvm.compiler.hotspot.stubs.StubUtil.printf; |
|
65 |
import static org.graalvm.compiler.hotspot.stubs.StubUtil.verifyObject; |
|
66 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.FAST_PATH_PROBABILITY; |
|
67 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability; |
|
46459 | 68 |
|
43972 | 69 |
import org.graalvm.compiler.api.replacements.Fold; |
70 |
import org.graalvm.compiler.api.replacements.Snippet; |
|
71 |
import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; |
|
72 |
import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; |
|
73 |
import org.graalvm.compiler.graph.Node.ConstantNodeParameter; |
|
74 |
import org.graalvm.compiler.graph.Node.NodeIntrinsic; |
|
75 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; |
|
76 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders; |
|
77 |
import org.graalvm.compiler.hotspot.nodes.GraalHotSpotVMConfigNode; |
|
78 |
import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode; |
|
79 |
import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; |
|
80 |
import org.graalvm.compiler.hotspot.replacements.NewObjectSnippets; |
|
81 |
import org.graalvm.compiler.hotspot.word.KlassPointer; |
|
82 |
import org.graalvm.compiler.nodes.ConstantNode; |
|
46344 | 83 |
import org.graalvm.compiler.options.OptionValues; |
43972 | 84 |
import org.graalvm.compiler.word.Word; |
49873 | 85 |
import jdk.internal.vm.compiler.word.WordFactory; |
43972 | 86 |
|
87 |
import jdk.vm.ci.code.Register; |
|
88 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; |
|
89 |
import jdk.vm.ci.meta.JavaKind; |
|
90 |
||
91 |
/** |
|
92 |
* Stub implementing the fast path for TLAB refill during instance class allocation. This stub is |
|
93 |
* called from the {@linkplain NewObjectSnippets inline} allocation code when TLAB allocation fails. |
|
94 |
* If this stub fails to refill the TLAB or allocate the object, it calls out to the HotSpot C++ |
|
95 |
* runtime for to complete the allocation. |
|
96 |
*/ |
|
97 |
public class NewInstanceStub extends SnippetStub { |
|
98 |
||
46344 | 99 |
public NewInstanceStub(OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { |
100 |
super("newInstance", options, providers, linkage); |
|
43972 | 101 |
} |
102 |
||
103 |
@Override |
|
104 |
protected Object[] makeConstArgs() { |
|
105 |
HotSpotResolvedObjectType intArrayType = (HotSpotResolvedObjectType) providers.getMetaAccess().lookupJavaType(int[].class); |
|
106 |
int count = method.getSignature().getParameterCount(false); |
|
107 |
Object[] args = new Object[count]; |
|
108 |
assert checkConstArg(1, "intArrayHub"); |
|
109 |
assert checkConstArg(2, "threadRegister"); |
|
46344 | 110 |
assert checkConstArg(3, "options"); |
43972 | 111 |
args[1] = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), intArrayType.klass(), null); |
112 |
args[2] = providers.getRegisters().getThreadRegister(); |
|
46344 | 113 |
args[3] = options; |
43972 | 114 |
return args; |
115 |
} |
|
116 |
||
117 |
private static Word allocate(Word thread, int size) { |
|
118 |
Word top = readTlabTop(thread); |
|
119 |
Word end = readTlabEnd(thread); |
|
120 |
Word newTop = top.add(size); |
|
121 |
/* |
|
122 |
* this check might lead to problems if the TLAB is within 16GB of the address space end |
|
123 |
* (checked in c++ code) |
|
124 |
*/ |
|
125 |
if (probability(FAST_PATH_PROBABILITY, newTop.belowOrEqual(end))) { |
|
126 |
writeTlabTop(thread, newTop); |
|
127 |
return top; |
|
128 |
} |
|
46459 | 129 |
return WordFactory.zero(); |
43972 | 130 |
} |
131 |
||
132 |
@Fold |
|
46344 | 133 |
static boolean logging(OptionValues options) { |
134 |
return StubOptions.TraceNewInstanceStub.getValue(options); |
|
43972 | 135 |
} |
136 |
||
137 |
/** |
|
138 |
* Re-attempts allocation after an initial TLAB allocation failed or was skipped (e.g., due to |
|
139 |
* -XX:-UseTLAB). |
|
140 |
* |
|
141 |
* @param hub the hub of the object to be allocated |
|
142 |
* @param intArrayHub the hub for {@code int[].class} |
|
143 |
*/ |
|
144 |
@Snippet |
|
46344 | 145 |
private static Object newInstance(KlassPointer hub, @ConstantParameter KlassPointer intArrayHub, @ConstantParameter Register threadRegister, @ConstantParameter OptionValues options) { |
43972 | 146 |
/* |
147 |
* The type is known to be an instance so Klass::_layout_helper is the instance size as a |
|
148 |
* raw number |
|
149 |
*/ |
|
150 |
Word thread = registerAsWord(threadRegister); |
|
151 |
boolean inlineContiguousAllocationSupported = GraalHotSpotVMConfigNode.inlineContiguousAllocationSupported(); |
|
50689
070b5313b2fe
8205105: VM crashes with "assert(Universe::heap()->is_in_reserved(start + words - 1)) failed: not in heap"
iveresov
parents:
49873
diff
changeset
|
152 |
if (useFastTLABRefill(INJECTED_VMCONFIG) && !forceSlowPath(options) && inlineContiguousAllocationSupported && !useCMSIncrementalMode(INJECTED_VMCONFIG)) { |
43972 | 153 |
if (isInstanceKlassFullyInitialized(hub)) { |
46344 | 154 |
int sizeInBytes = readLayoutHelper(hub); |
155 |
Word memory = refillAllocate(thread, intArrayHub, sizeInBytes, logging(options)); |
|
43972 | 156 |
if (memory.notEqual(0)) { |
157 |
Word prototypeMarkWord = hub.readWord(prototypeMarkWordOffset(INJECTED_VMCONFIG), PROTOTYPE_MARK_WORD_LOCATION); |
|
158 |
NewObjectSnippets.formatObjectForStub(hub, sizeInBytes, memory, prototypeMarkWord); |
|
159 |
return verifyObject(memory.toObject()); |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
||
46344 | 164 |
if (logging(options)) { |
43972 | 165 |
printf("newInstance: calling new_instance_c\n"); |
166 |
} |
|
167 |
||
168 |
newInstanceC(NEW_INSTANCE_C, thread, hub); |
|
169 |
handlePendingException(thread, true); |
|
170 |
return verifyObject(getAndClearObjectResult(thread)); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Attempts to refill the current thread's TLAB and retries the allocation. |
|
175 |
* |
|
176 |
* @param intArrayHub the hub for {@code int[].class} |
|
177 |
* @param sizeInBytes the size of the allocation |
|
178 |
* @param log specifies if logging is enabled |
|
179 |
* |
|
46459 | 180 |
* @return the newly allocated, uninitialized chunk of memory, or {@link WordFactory#zero()} if |
181 |
* the operation was unsuccessful |
|
43972 | 182 |
*/ |
183 |
static Word refillAllocate(Word thread, KlassPointer intArrayHub, int sizeInBytes, boolean log) { |
|
184 |
// If G1 is enabled, the "eden" allocation space is not the same always |
|
185 |
// and therefore we have to go to slowpath to allocate a new TLAB. |
|
186 |
if (useG1GC(INJECTED_VMCONFIG)) { |
|
46459 | 187 |
return WordFactory.zero(); |
43972 | 188 |
} |
189 |
if (!useTLAB(INJECTED_VMCONFIG)) { |
|
46459 | 190 |
return edenAllocate(WordFactory.unsigned(sizeInBytes), log); |
43972 | 191 |
} |
46459 | 192 |
Word intArrayMarkWord = WordFactory.unsigned(tlabIntArrayMarkWord(INJECTED_VMCONFIG)); |
43972 | 193 |
int alignmentReserveInBytes = tlabAlignmentReserveInHeapWords(INJECTED_VMCONFIG) * wordSize(); |
194 |
||
195 |
Word top = readTlabTop(thread); |
|
196 |
Word end = readTlabEnd(thread); |
|
197 |
||
198 |
// calculate amount of free space |
|
199 |
long tlabFreeSpaceInBytes = end.subtract(top).rawValue(); |
|
200 |
||
201 |
if (log) { |
|
202 |
printf("refillTLAB: thread=%p\n", thread.rawValue()); |
|
203 |
printf("refillTLAB: top=%p\n", top.rawValue()); |
|
204 |
printf("refillTLAB: end=%p\n", end.rawValue()); |
|
205 |
printf("refillTLAB: tlabFreeSpaceInBytes=%ld\n", tlabFreeSpaceInBytes); |
|
206 |
} |
|
207 |
||
208 |
long tlabFreeSpaceInWords = tlabFreeSpaceInBytes >>> log2WordSize(); |
|
209 |
||
210 |
// Retain TLAB and allocate object in shared space if |
|
211 |
// the amount free in the TLAB is too large to discard. |
|
212 |
Word refillWasteLimit = thread.readWord(tlabRefillWasteLimitOffset(INJECTED_VMCONFIG), TLAB_REFILL_WASTE_LIMIT_LOCATION); |
|
213 |
if (tlabFreeSpaceInWords <= refillWasteLimit.rawValue()) { |
|
214 |
if (tlabStats(INJECTED_VMCONFIG)) { |
|
215 |
// increment number of refills |
|
216 |
thread.writeInt(tlabNumberOfRefillsOffset(INJECTED_VMCONFIG), thread.readInt(tlabNumberOfRefillsOffset(INJECTED_VMCONFIG), TLAB_NOF_REFILLS_LOCATION) + 1, TLAB_NOF_REFILLS_LOCATION); |
|
217 |
if (log) { |
|
218 |
printf("thread: %p -- number_of_refills %d\n", thread.rawValue(), thread.readInt(tlabNumberOfRefillsOffset(INJECTED_VMCONFIG), TLAB_NOF_REFILLS_LOCATION)); |
|
219 |
} |
|
220 |
// accumulate wastage |
|
221 |
int wastage = thread.readInt(tlabFastRefillWasteOffset(INJECTED_VMCONFIG), TLAB_FAST_REFILL_WASTE_LOCATION) + (int) tlabFreeSpaceInWords; |
|
222 |
if (log) { |
|
223 |
printf("thread: %p -- accumulated wastage %d\n", thread.rawValue(), wastage); |
|
224 |
} |
|
225 |
thread.writeInt(tlabFastRefillWasteOffset(INJECTED_VMCONFIG), wastage, TLAB_FAST_REFILL_WASTE_LOCATION); |
|
226 |
} |
|
227 |
||
228 |
// if TLAB is currently allocated (top or end != null) then |
|
229 |
// fill [top, end + alignment_reserve) with array object |
|
230 |
if (top.notEqual(0)) { |
|
231 |
int headerSize = arrayBaseOffset(JavaKind.Int); |
|
232 |
// just like the HotSpot assembler stubs, assumes that tlabFreeSpaceInInts fits in |
|
233 |
// an int |
|
234 |
int tlabFreeSpaceInInts = (int) tlabFreeSpaceInBytes >>> 2; |
|
235 |
int length = ((alignmentReserveInBytes - headerSize) >>> 2) + tlabFreeSpaceInInts; |
|
46344 | 236 |
NewObjectSnippets.formatArray(intArrayHub, 0, length, headerSize, top, intArrayMarkWord, false, false, null); |
43972 | 237 |
|
238 |
long allocated = thread.readLong(threadAllocatedBytesOffset(INJECTED_VMCONFIG), TLAB_THREAD_ALLOCATED_BYTES_LOCATION); |
|
239 |
allocated = allocated + top.subtract(readTlabStart(thread)).rawValue(); |
|
240 |
thread.writeLong(threadAllocatedBytesOffset(INJECTED_VMCONFIG), allocated, TLAB_THREAD_ALLOCATED_BYTES_LOCATION); |
|
241 |
} |
|
242 |
||
243 |
// refill the TLAB with an eden allocation |
|
244 |
Word tlabRefillSizeInWords = thread.readWord(threadTlabSizeOffset(INJECTED_VMCONFIG), TLAB_SIZE_LOCATION); |
|
245 |
Word tlabRefillSizeInBytes = tlabRefillSizeInWords.multiply(wordSize()); |
|
246 |
// allocate new TLAB, address returned in top |
|
247 |
top = edenAllocate(tlabRefillSizeInBytes, log); |
|
248 |
if (top.notEqual(0)) { |
|
249 |
end = top.add(tlabRefillSizeInBytes.subtract(alignmentReserveInBytes)); |
|
250 |
initializeTlab(thread, top, end); |
|
251 |
||
252 |
return NewInstanceStub.allocate(thread, sizeInBytes); |
|
253 |
} else { |
|
46459 | 254 |
return WordFactory.zero(); |
43972 | 255 |
} |
256 |
} else { |
|
257 |
// Retain TLAB |
|
258 |
Word newRefillWasteLimit = refillWasteLimit.add(tlabRefillWasteIncrement(INJECTED_VMCONFIG)); |
|
259 |
thread.writeWord(tlabRefillWasteLimitOffset(INJECTED_VMCONFIG), newRefillWasteLimit, TLAB_REFILL_WASTE_LIMIT_LOCATION); |
|
260 |
if (log) { |
|
261 |
printf("refillTLAB: retaining TLAB - newRefillWasteLimit=%p\n", newRefillWasteLimit.rawValue()); |
|
262 |
} |
|
263 |
||
264 |
if (tlabStats(INJECTED_VMCONFIG)) { |
|
265 |
thread.writeInt(tlabSlowAllocationsOffset(INJECTED_VMCONFIG), thread.readInt(tlabSlowAllocationsOffset(INJECTED_VMCONFIG), TLAB_SLOW_ALLOCATIONS_LOCATION) + 1, |
|
266 |
TLAB_SLOW_ALLOCATIONS_LOCATION); |
|
267 |
} |
|
268 |
||
46459 | 269 |
return edenAllocate(WordFactory.unsigned(sizeInBytes), log); |
43972 | 270 |
} |
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Attempts to allocate a chunk of memory from Eden space. |
|
275 |
* |
|
276 |
* @param sizeInBytes the size of the chunk to allocate |
|
277 |
* @param log specifies if logging is enabled |
|
46459 | 278 |
* @return the allocated chunk or {@link WordFactory#zero()} if allocation fails |
43972 | 279 |
*/ |
280 |
public static Word edenAllocate(Word sizeInBytes, boolean log) { |
|
281 |
final long heapTopRawAddress = GraalHotSpotVMConfigNode.heapTopAddress(); |
|
282 |
final long heapEndRawAddress = GraalHotSpotVMConfigNode.heapEndAddress(); |
|
283 |
||
46459 | 284 |
Word heapTopAddress = WordFactory.unsigned(heapTopRawAddress); |
285 |
Word heapEndAddress = WordFactory.unsigned(heapEndRawAddress); |
|
43972 | 286 |
|
287 |
while (true) { |
|
288 |
Word heapTop = heapTopAddress.readWord(0, HEAP_TOP_LOCATION); |
|
289 |
Word newHeapTop = heapTop.add(sizeInBytes); |
|
290 |
if (newHeapTop.belowOrEqual(heapTop)) { |
|
46459 | 291 |
return WordFactory.zero(); |
43972 | 292 |
} |
293 |
||
294 |
Word heapEnd = heapEndAddress.readWord(0, HEAP_END_LOCATION); |
|
295 |
if (newHeapTop.aboveThan(heapEnd)) { |
|
46459 | 296 |
return WordFactory.zero(); |
43972 | 297 |
} |
46344 | 298 |
if (heapTopAddress.logicCompareAndSwapWord(0, heapTop, newHeapTop, HEAP_TOP_LOCATION)) { |
43972 | 299 |
return heapTop; |
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
@Fold |
|
46344 | 305 |
static boolean forceSlowPath(OptionValues options) { |
306 |
return StubOptions.ForceUseOfNewInstanceStub.getValue(options); |
|
43972 | 307 |
} |
308 |
||
309 |
public static final ForeignCallDescriptor NEW_INSTANCE_C = newDescriptor(NewInstanceStub.class, "newInstanceC", void.class, Word.class, KlassPointer.class); |
|
310 |
||
311 |
@NodeIntrinsic(StubForeignCallNode.class) |
|
312 |
public static native void newInstanceC(@ConstantNodeParameter ForeignCallDescriptor newInstanceC, Word thread, KlassPointer hub); |
|
313 |
} |