Skip to content

Commit 1b2c300

Browse files
committed
report allocations
1 parent 4a95680 commit 1b2c300

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
117117
import com.oracle.truffle.api.frame.Frame;
118118
import com.oracle.truffle.api.frame.FrameDescriptor;
119+
import com.oracle.truffle.api.instrumentation.AllocationReporter;
119120
import com.oracle.truffle.api.interop.TruffleObject;
120121
import com.oracle.truffle.api.nodes.Node;
121122
import com.oracle.truffle.api.nodes.NodeCost;
@@ -125,6 +126,7 @@
125126

126127
public final class PythonObjectFactory extends Node {
127128
@CompilationFinal private ContextReference<PythonContext> contextRef;
129+
@CompilationFinal private AllocationReporter allocationReporter;
128130

129131
private PythonObjectFactory() {
130132
}
@@ -139,6 +141,10 @@ private PythonClass lookupClass(PythonBuiltinClassType type) {
139141

140142
@SuppressWarnings("static-method")
141143
public final <T> T trace(T allocatedObject) {
144+
if (reportAllocations()) {
145+
allocationReporter.onEnter(null, 0, AllocationReporter.SIZE_UNKNOWN);
146+
allocationReporter.onReturnValue(allocatedObject, 0, AllocationReporter.SIZE_UNKNOWN);
147+
}
142148
return allocatedObject;
143149
}
144150

@@ -148,11 +154,20 @@ public NodeCost getCost() {
148154
}
149155

150156
public PythonCore getCore() {
157+
return getContext().getCore();
158+
}
159+
160+
private PythonContext getContext() {
151161
if (contextRef == null) {
152162
CompilerDirectives.transferToInterpreterAndInvalidate();
153163
contextRef = PythonLanguage.getContextRef();
164+
allocationReporter = contextRef.get().getEnv().lookup(AllocationReporter.class);
154165
}
155-
return contextRef.get().getCore();
166+
return contextRef.get();
167+
}
168+
169+
private boolean reportAllocations() {
170+
return getContext() != null && allocationReporter != null;
156171
}
157172

158173
/*

0 commit comments

Comments
 (0)