Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/NodeExecCounter.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.tools/src/com/oracle/truffle/tools/NodeExecCounter.java@3b8bbf51d320 |
children | dc83cc1f94f2 3aad794eec0e |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 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. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.tools; | |
26 | |
27 import java.io.*; | |
28 import java.util.*; | |
29 import java.util.concurrent.atomic.*; | |
30 | |
31 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | |
32 import com.oracle.truffle.api.frame.*; | |
33 import com.oracle.truffle.api.instrument.*; | |
34 import com.oracle.truffle.api.instrument.impl.*; | |
35 import com.oracle.truffle.api.nodes.*; | |
36 import com.oracle.truffle.api.nodes.Node.Child; | |
37 | |
38 /** | |
39 * An {@link InstrumentationTool} that counts interpreter <em>execution calls</em> to AST nodes, | |
40 * tabulated by the type of called nodes; counting can be enabled <em>all</em> nodes or restricted | |
41 * to nodes with a specified {@linkplain SyntaxTag tag} that is presumed to be applied external to | |
42 * the tool. | |
43 * <p> | |
44 * <b>Tool Life Cycle</b> | |
45 * <p> | |
46 * See {@link InstrumentationTool} for the life cycle common to all such tools. | |
47 * </p> | |
48 * <b>Execution Counts</b> | |
49 * <p> | |
50 * <ul> | |
51 * <li>"Execution call" on a node is is defined as invocation of a node method that is instrumented | |
52 * to produce the event {@link StandardInstrumentListener#enter(Probe, Node, VirtualFrame)};</li> | |
53 * <li>Execution calls are tabulated only at <em>instrumented</em> nodes, i.e. those for which | |
54 * {@linkplain Node#isInstrumentable() isInstrumentable() == true};</li> | |
55 * <li>Execution calls are tabulated only at nodes present in the AST when originally created; | |
56 * dynamically added nodes will not be instrumented.</li> | |
57 * </ul> | |
58 * </p> | |
59 * <b>Failure Log</b> | |
60 * <p> | |
61 * For the benefit of language implementors, the tool maintains a log describing failed attempts to | |
62 * probe AST nodes. Most failures occur when the type of the wrapper created by | |
63 * {@link Node#createWrapperNode()} is not assignable to the relevant {@link Child} field in the | |
64 * node's parent. | |
65 * </p> | |
66 * <p> | |
67 * {@linkplain #reset() Resetting} the counts has no effect on the failure log. | |
68 * </p> | |
69 * <b>Results</b> | |
70 * <p> | |
71 * A modification-safe copy of the {@linkplain #getCounts() counts} can be retrieved at any time, | |
72 * without effect on the state of the tool. | |
73 * </p> | |
74 * <p> | |
75 * A "default" {@linkplain #print(PrintStream) print()} method can summarizes the current counts at | |
76 * any time in a simple textual format, without effect on the state of the tool. | |
77 * </p> | |
78 * | |
79 * @see Instrument | |
80 * @see SyntaxTag | |
81 * @see ProbeFailure | |
82 */ | |
83 public final class NodeExecCounter extends InstrumentationTool { | |
84 | |
85 /** | |
86 * Execution count for AST nodes of a particular type. | |
87 */ | |
88 public interface NodeExecutionCount { | |
89 Class<?> nodeClass(); | |
90 | |
91 long executionCount(); | |
92 } | |
93 | |
94 /** | |
95 * Listener for events at instrumented nodes. Counts are maintained in a shared table, so the | |
96 * listener is stateless and can be shared by every {@link Instrument}. | |
97 */ | |
98 private final StandardInstrumentListener instrumentListener = new DefaultStandardInstrumentListener() { | |
99 @Override | |
100 public void enter(Probe probe, Node node, VirtualFrame vFrame) { | |
101 if (isEnabled()) { | |
102 final Class<?> nodeClass = node.getClass(); | |
103 /* | |
104 * Everything up to here is inlined by Truffle compilation. Delegate the next part | |
105 * to a method behind an inlining boundary. | |
106 * | |
107 * Note that it is not permitted to pass a {@link VirtualFrame} across an inlining | |
108 * boundary; they are truly virtual in inlined code. | |
109 */ | |
110 AtomicLong nodeCounter = getCounter(nodeClass); | |
111 nodeCounter.getAndIncrement(); | |
112 } | |
113 } | |
114 | |
115 /** | |
116 * Mark this method as a boundary that will stop Truffle inlining, which should not be | |
117 * allowed to inline the hash table method or any other complex library code. | |
118 */ | |
119 @TruffleBoundary | |
120 private AtomicLong getCounter(Class<?> nodeClass) { | |
121 AtomicLong nodeCounter = counters.get(nodeClass); | |
122 if (nodeCounter == null) { | |
123 nodeCounter = new AtomicLong(); | |
124 counters.put(nodeClass, nodeCounter); | |
125 } | |
126 return nodeCounter; | |
127 } | |
128 }; | |
129 | |
130 /** Counting data. */ | |
131 private final Map<Class<?>, AtomicLong> counters = new HashMap<>(); | |
132 | |
133 /** Failure log. */ | |
134 private final List<ProbeFailure> failures = new ArrayList<>(); | |
135 | |
136 /** For disposal. */ | |
137 private final List<Instrument> instruments = new ArrayList<>(); | |
138 | |
139 /** | |
140 * If non-null, counting is restricted to nodes holding this tag. | |
141 */ | |
142 private final SyntaxTag countingTag; | |
143 | |
144 /** | |
145 * Prober used only when instrumenting every node. | |
146 */ | |
147 private ASTProber astProber; | |
148 | |
149 /** | |
150 * Listener used only when restricting counting to a specific tag. | |
151 */ | |
152 private ProbeListener probeListener; | |
153 | |
154 /** | |
155 * Create a per node-type execution counting tool for all nodes in subsequently created ASTs. | |
156 */ | |
157 public NodeExecCounter() { | |
158 this.countingTag = null; | |
159 } | |
160 | |
161 /** | |
162 * Creates a per-type execution counting for nodes tagged as specified in subsequently created | |
163 * ASTs. | |
164 */ | |
165 public NodeExecCounter(SyntaxTag tag) { | |
166 this.countingTag = tag; | |
167 } | |
168 | |
169 @Override | |
170 protected boolean internalInstall() { | |
171 if (countingTag == null) { | |
172 astProber = new ExecCounterASTProber(); | |
173 Probe.registerASTProber(astProber); | |
174 } else { | |
175 probeListener = new NodeExecCounterProbeListener(); | |
176 Probe.addProbeListener(probeListener); | |
177 } | |
178 return true; | |
179 } | |
180 | |
181 @Override | |
182 protected void internalReset() { | |
183 counters.clear(); | |
184 failures.clear(); | |
185 } | |
186 | |
187 @Override | |
188 protected void internalDispose() { | |
189 if (astProber != null) { | |
190 Probe.unregisterASTProber(astProber); | |
191 } | |
192 if (probeListener != null) { | |
193 Probe.removeProbeListener(probeListener); | |
194 } | |
195 for (Instrument instrument : instruments) { | |
196 instrument.dispose(); | |
197 } | |
198 } | |
199 | |
200 /** | |
201 * Gets a modification-safe summary of the current per-type node execution counts; does not | |
202 * affect the counts. | |
203 */ | |
204 public NodeExecutionCount[] getCounts() { | |
205 final Collection<Map.Entry<Class<?>, AtomicLong>> entrySet = counters.entrySet(); | |
206 final NodeExecutionCount[] result = new NodeExecCountImpl[entrySet.size()]; | |
207 int i = 0; | |
208 for (Map.Entry<Class<?>, AtomicLong> entry : entrySet) { | |
209 result[i++] = new NodeExecCountImpl(entry.getKey(), entry.getValue().longValue()); | |
210 } | |
211 return result; | |
212 } | |
213 | |
214 /** | |
215 * Gets a log containing a report of every failed attempt to instrument a node. | |
216 */ | |
217 public ProbeFailure[] getFailures() { | |
218 return failures.toArray(new ProbeFailure[failures.size()]); | |
219 } | |
220 | |
221 /** | |
222 * A default printer for the current counts, producing lines of the form | |
223 * " <count> : <node type>" in descending order of count. | |
224 */ | |
225 public void print(PrintStream out) { | |
226 print(out, false); | |
227 } | |
228 | |
229 /** | |
230 * A default printer for the current counts, producing lines of the form | |
231 * " <count> : <node type>" in descending order of count. | |
232 * | |
233 * @param out | |
234 * @param verbose whether to describe nodes on which instrumentation failed | |
235 */ | |
236 public void print(PrintStream out, boolean verbose) { | |
237 | |
238 final long missedNodes = failures.size(); | |
239 out.println(); | |
240 if (countingTag == null) { | |
241 out.println("Execution counts by node type:"); | |
242 } else { | |
243 out.println("\"" + countingTag.name() + "\"-tagged execution counts by node type:"); | |
244 } | |
245 final StringBuilder disclaim = new StringBuilder("("); | |
246 if (missedNodes > 0) { | |
247 disclaim.append(Long.toString(missedNodes) + " original AST nodes not instrumented, "); | |
248 } | |
249 disclaim.append("dynamically added nodes not instrumented)"); | |
250 out.println(disclaim.toString()); | |
251 NodeExecutionCount[] execCounts = getCounts(); | |
252 // Sort in descending order | |
253 Arrays.sort(execCounts, new Comparator<NodeExecutionCount>() { | |
254 | |
255 public int compare(NodeExecutionCount o1, NodeExecutionCount o2) { | |
256 return Long.compare(o2.executionCount(), o1.executionCount()); | |
257 } | |
258 | |
259 }); | |
260 for (NodeExecutionCount nodeCount : execCounts) { | |
261 out.format("%12d", nodeCount.executionCount()); | |
262 out.println(" : " + nodeCount.nodeClass().getName()); | |
263 } | |
264 | |
265 if (verbose && missedNodes > 0) { | |
266 out.println("Instrumentation failures for execution counts:"); | |
267 | |
268 for (ProbeFailure failure : failures) { | |
269 out.println("\t" + failure.getMessage()); | |
270 } | |
271 } | |
272 } | |
273 | |
274 /** | |
275 * A prober that attempts to probe and instrument every node. | |
276 */ | |
277 private class ExecCounterASTProber implements ASTProber, NodeVisitor { | |
278 | |
279 public boolean visit(Node node) { | |
280 | |
281 if (node.isInstrumentable()) { | |
282 try { | |
283 final Instrument instrument = Instrument.create(instrumentListener, "NodeExecCounter"); | |
284 instruments.add(instrument); | |
285 node.probe().attach(instrument); | |
286 } catch (ProbeException ex) { | |
287 failures.add(ex.getFailure()); | |
288 } | |
289 } | |
290 return true; | |
291 } | |
292 | |
293 public void probeAST(Node node) { | |
294 node.accept(this); | |
295 } | |
296 } | |
297 | |
298 /** | |
299 * A listener that assumes ASTs have been tagged external to this tool, and which instruments | |
300 * nodes holding a specified tag. | |
301 */ | |
302 private class NodeExecCounterProbeListener extends DefaultProbeListener { | |
303 | |
304 @Override | |
305 public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) { | |
306 if (countingTag == tag) { | |
307 final Instrument instrument = Instrument.create(instrumentListener, NodeExecCounter.class.getSimpleName()); | |
308 instruments.add(instrument); | |
309 probe.attach(instrument); | |
310 } | |
311 } | |
312 } | |
313 | |
314 private static class NodeExecCountImpl implements NodeExecutionCount { | |
315 | |
316 private final Class<?> nodeClass; | |
317 private final long count; | |
318 | |
319 public NodeExecCountImpl(Class<?> nodeClass, long count) { | |
320 this.nodeClass = nodeClass; | |
321 this.count = count; | |
322 } | |
323 | |
324 public Class<?> nodeClass() { | |
325 return nodeClass; | |
326 } | |
327 | |
328 public long executionCount() { | |
329 return count; | |
330 } | |
331 } | |
332 } |