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
Modifier and TypeFieldDescriptionstatic final long
Constant specifying an unknown size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addActiveListener
(Consumer<Boolean> listener) Add a listener that is notified whenactive
value of this reporter changes.boolean
isActive()
Test if the reporter instance is actually doing some reporting when notify methods are called.void
Report an intent to allocate a new guest language value, or re-allocate an existing one.void
onReturnValue
(Object value, long oldSize, long newSize) Report an allocation of a new one or re-allocation of an existing guest language value.void
removeActiveListener
(Consumer<Boolean> listener) Remove a listener that is notified whenactive
value 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 whenactive
value of this reporter changes. The listeneraccept
method is called with the new value ofisActive()
.- Since:
- 19.0
-
removeActiveListener
Remove a listener that is notified whenactive
value of this reporter changes.- Since:
- 19.0
-
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 beadded
to listen on changes of this value.- Returns:
true
when there are someAllocationListener
s attached,false
otherwise.- 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 andTruffleObject
are accepted value types. The change in memory consumption caused by the allocation is going to benewSizeEstimate - oldSize
when 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 toonEnter
prior every sub-value allocation can be followed by the appropriate number ofonReturnValue
calls after the sub-values are allocated, in the opposite order.- Parameters:
valueToReallocate
-null
in case of a new allocation, or the value that is to be re-allocated.oldSize
-0
in case of a new allocation, or the size in bytes of value to be re-allocated. Can beSIZE_UNKNOWN
when 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_UNKNOWN
when 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 andTruffleObject
are accepted value types. The change in memory consumption caused by the allocation isnewSize - oldSize
when 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 be0
for newly allocated values. In case of re-allocation it's the size of the original value before re-allocation. Can beSIZE_UNKNOWN
when 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. ThenewSize
may be less thanoldSize
when the object size shrinks. Can beSIZE_UNKNOWN
when not known.- Since:
- 0.27
-