Class AllocationReporter
java.lang.Object
com.oracle.truffle.api.instrumentation.AllocationReporter
Reporter of guest language value allocations. Language implementation ought to use this class to
report all allocations and re-allocations of guest language values. An instance of this class can
be obtained from
Env.lookup(AllocationReporter.class). If
used from compiled code paths, then the allocation reporter must be stored in a compilation final
or final field.
Usage example:
@Override
protected ContextObject createContext(Env env) {
AllocationReporter reporter = env.lookup(AllocationReporter.class);
return new ContextObject(reporter);
}
Object allocateNew() {
AllocationReporter reporter = ContextObject.get(null).getReporter();
// Test if the reporter is active, we should compute the size estimate
if (reporter.isActive()) {
long size = findSizeEstimate();
reporter.onEnter(null, 0, size);
}
// Do the allocation itself
Object newObject = new MyTruffleObject();
// Test if the reporter is active,
// we should compute the allocated object size
if (reporter.isActive()) {
long size = findSize(newObject);
reporter.onReturnValue(newObject, 0, size);
}
return newObject;
}
Object allocateComplex() {
AllocationReporter reporter = ContextObject.get(null).getReporter();
// If the allocated size is a constant, onEnter() and onReturnValue()
// can be called without a fast-path performance penalty when not active
reporter.onEnter(null, 0, 16);
// Do the allocation itself
Object newObject = createComplexObject();
// Report the allocation
reporter.onReturnValue(newObject, 0, 16);
return newObject;
}
- Since:
- 0.27
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longConstant specifying an unknown size. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddActiveListener(Consumer<Boolean> listener) Add a listener that is notified whenactivevalue of this reporter changes.booleanisActive()Test if the reporter instance is actually doing some reporting when notify methods are called.voidReport an intent to allocate a new guest language value, or re-allocate an existing one.voidonReturnValue(Object value, long oldSize, long newSize) Report an allocation of a new one or re-allocation of an existing guest language value.voidremoveActiveListener(Consumer<Boolean> listener) Remove a listener that is notified whenactivevalue of this reporter changes.
-
Field Details
-
SIZE_UNKNOWN
public static final long SIZE_UNKNOWNConstant specifying an unknown size. Use it when it's not possible to estimate size of the memory being allocated.- Since:
- 0.27
- See Also:
-
-
Method Details
-
addActiveListener
Add a listener that is notified whenactivevalue of this reporter changes. The listeneracceptmethod is called with the new value ofisActive().- Since:
- 19.0
-
removeActiveListener
-
isActive
public boolean isActive()Test if the reporter instance is actually doing some reporting when notify methods are called. MethodsonEnter(java.lang.Object, long, long)andonReturnValue(java.lang.Object, long, long)have no effect when this method returns false. A listener can beaddedto listen on changes of this value.- Returns:
truewhen there are someAllocationListeners attached,falseotherwise.- Since:
- 0.27
-
onEnter
Report an intent to allocate a new guest language value, or re-allocate an existing one. This method delegates to all registered listenersAllocationListener.onEnter(com.oracle.truffle.api.instrumentation.AllocationEvent). Only primitive types, String andTruffleObjectare accepted value types. The change in memory consumption caused by the allocation is going to benewSizeEstimate - oldSizewhen both old size and new size are known. The change can be either positive or negative.A call to this method needs to be followed by a call to
onReturnValue(java.lang.Object, long, long)with the actual allocated value, or with the same (re-allocated) value. Nested allocations are supported, several calls toonEnterprior every sub-value allocation can be followed by the appropriate number ofonReturnValuecalls after the sub-values are allocated, in the opposite order.- Parameters:
valueToReallocate-nullin case of a new allocation, or the value that is to be re-allocated.oldSize-0in case of a new allocation, or the size in bytes of value to be re-allocated. Can beSIZE_UNKNOWNwhen the value size is not known.newSizeEstimate- an estimate of the allocation size of the value which is to be created or re-allocated, in bytes. Can beSIZE_UNKNOWNwhen the allocation size is not known.- Since:
- 0.27
-
onReturnValue
Report an allocation of a new one or re-allocation of an existing guest language value. This method notifies all registered listenersAllocationListener.onReturnValue(com.oracle.truffle.api.instrumentation.AllocationEvent). Only primitive types, String andTruffleObjectare accepted value types. The change in memory consumption caused by the allocation isnewSize - oldSizewhen both old size and new size are known. The change can be either positive or negative.A call to
onEnter(java.lang.Object, long, long)must precede this call. In case of re-allocation, the value object passed toonEnter(java.lang.Object, long, long)must be the same instance as the value passed to this method.- Parameters:
value- the value that was newly allocated, or the re-allocated value. Must not benull.oldSize- size in bytes of an old value, if any. Must be0for newly allocated values. In case of re-allocation it's the size of the original value before re-allocation. Can beSIZE_UNKNOWNwhen not known.newSize- the size of the allocated value in bytes. In case of re-allocation, it's the size of the object after re-allocation. ThenewSizemay be less thanoldSizewhen the object size shrinks. Can beSIZE_UNKNOWNwhen not known.- Since:
- 0.27
-