comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Probe.java @ 21131:d6d9631eb057

TruffleInstrumentation: rename Probe.setTagTrap() to Probe.setBeforeTagTrap() and add Probe.setAfterTagTrap()
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 21 Apr 2015 17:02:06 -0700
parents 7d21cdb15e54
children c072fbce5756
comparison
equal deleted inserted replaced
21130:7d21cdb15e54 21131:d6d9631eb057
107 /** 107 /**
108 * All Probes that have been created. 108 * All Probes that have been created.
109 */ 109 */
110 private static final List<WeakReference<Probe>> probes = new ArrayList<>(); 110 private static final List<WeakReference<Probe>> probes = new ArrayList<>();
111 111
112 @CompilationFinal private static SyntaxTagTrap tagTrap = null; 112 /**
113 * A global trap that triggers notification just before executing any Node that is Probed with a
114 * matching tag.
115 */
116 @CompilationFinal private static SyntaxTagTrap beforeTagTrap = null;
117
118 /**
119 * A global trap that triggers notification just after executing any Node that is Probed with a
120 * matching tag.
121 */
122 @CompilationFinal private static SyntaxTagTrap afterTagTrap = null;
113 123
114 private static final class FindSourceVisitor implements NodeVisitor { 124 private static final class FindSourceVisitor implements NodeVisitor {
115 125
116 Source source = null; 126 Source source = null;
117 127
197 } 207 }
198 } 208 }
199 return taggedProbes; 209 return taggedProbes;
200 } 210 }
201 211
202 // TODO (mlvdv) can this be generalized to permit multiple traps without a performance hit? 212 // TODO (mlvdv) generalize to permit multiple "before traps" without a performance hit?
203 /** 213 /**
204 * Sets the current "tag trap"; there can be no more than one set at a time. 214 * Sets the current "<em>before</em> tag trap"; there can be no more than one in effect.
205 * <ul> 215 * <ul>
206 * <li>A non-null trap sets a callback to be triggered whenever execution reaches a 216 * <li>The before-trap triggers a callback just <strong><em>before</em></strong> execution
207 * {@link Probe} (either existing or subsequently created) with the specified tag.</li> 217 * reaches <strong><em>any</em></strong> {@link Probe} (either existing or subsequently created)
208 * <li>Setting the trap to null clears the existing trap.</li> 218 * with the specified {@link SyntaxTag}.</li>
209 * <li>Setting a non-null trap when one is already set will clear the previously set trap.</li> 219 * <li>Setting the before-trap to {@code null} clears an existing before-trap.</li>
220 * <li>Setting a non{@code -null} before-trap when one is already set clears the previously set
221 * before-trap.</li>
210 * </ul> 222 * </ul>
211 * 223 *
212 * @param newTagTrap The {@link SyntaxTagTrap} to set. 224 * @param newBeforeTagTrap The new "before" {@link SyntaxTagTrap} to set.
213 */ 225 */
214 public static void setTagTrap(SyntaxTagTrap newTagTrap) { 226 public static void setBeforeTagTrap(SyntaxTagTrap newBeforeTagTrap) {
215 tagTrap = newTagTrap; 227 beforeTagTrap = newBeforeTagTrap;
216 for (WeakReference<Probe> ref : probes) { 228 for (WeakReference<Probe> ref : probes) {
217 final Probe probe = ref.get(); 229 final Probe probe = ref.get();
218 if (probe != null) { 230 if (probe != null) {
219 probe.notifyTrapSet(); 231 probe.notifyTrapsChanged();
232 }
233 }
234 }
235
236 // TODO (mlvdv) generalize to permit multiple "after traps" without a performance hit?
237 /**
238 * Sets the current "<em>after</em> tag trap"; there can be no more than one in effect.
239 * <ul>
240 * <li>The after-trap triggers a callback just <strong><em>after</em></strong> execution leaves
241 * <strong><em>any</em></strong> {@link Probe} (either existing or subsequently created) with
242 * the specified {@link SyntaxTag}.</li>
243 * <li>Setting the after-trap to {@code null} clears an existing after-trap.</li>
244 * <li>Setting a non{@code -null} after-trap when one is already set clears the previously set
245 * after-trap.</li>
246 * </ul>
247 *
248 * @param newAfterTagTrap The new "after" {@link SyntaxTagTrap} to set.
249 */
250 public static void setAfterTagTrap(SyntaxTagTrap newAfterTagTrap) {
251 afterTagTrap = newAfterTagTrap;
252 for (WeakReference<Probe> ref : probes) {
253 final Probe probe = ref.get();
254 if (probe != null) {
255 probe.notifyTrapsChanged();
220 } 256 }
221 } 257 }
222 } 258 }
223 259
224 private final SourceSection sourceSection; 260 private final SourceSection sourceSection;
235 * there may have been a deopt). Every time a check fails, gets replaced by a new unchanged 271 * there may have been a deopt). Every time a check fails, gets replaced by a new unchanged
236 * assumption. 272 * assumption.
237 */ 273 */
238 @CompilationFinal private Assumption probeStateUnchangedAssumption = probeStateUnchangedCyclic.getAssumption(); 274 @CompilationFinal private Assumption probeStateUnchangedAssumption = probeStateUnchangedCyclic.getAssumption();
239 275
240 // Must invalidate whenever this changes. 276 // Must invalidate whenever changed
241 @CompilationFinal private boolean isTrapActive = false; 277 @CompilationFinal private boolean isBeforeTrapActive = false;
278
279 // Must invalidate whenever changed
280 @CompilationFinal private boolean isAfterTrapActive = false;
242 281
243 /** 282 /**
244 * Intended for use only by {@link ProbeNode}. 283 * Intended for use only by {@link ProbeNode}.
245 */ 284 */
246 Probe(ProbeNode probeNode, SourceSection sourceSection) { 285 Probe(ProbeNode probeNode, SourceSection sourceSection) {
277 if (!tags.contains(tag)) { 316 if (!tags.contains(tag)) {
278 tags.add(tag); 317 tags.add(tag);
279 for (ProbeListener listener : probeListeners) { 318 for (ProbeListener listener : probeListeners) {
280 listener.probeTaggedAs(this, tag, tagValue); 319 listener.probeTaggedAs(this, tag, tagValue);
281 } 320 }
282 if (tagTrap != null && tag == tagTrap.getTag()) { 321
283 this.isTrapActive = true; 322 // Update the status of this Probe with respect to global tag traps
323 boolean tagTrapsChanged = false;
324 if (beforeTagTrap != null && tag == beforeTagTrap.getTag()) {
325 this.isBeforeTrapActive = true;
326 tagTrapsChanged = true;
327 }
328 if (afterTagTrap != null && tag == afterTagTrap.getTag()) {
329 this.isAfterTrapActive = true;
330 tagTrapsChanged = true;
331 }
332 if (tagTrapsChanged) {
284 invalidateProbeUnchanged(); 333 invalidateProbeUnchanged();
285 } 334 }
286 } 335 }
287 } 336 }
288 337
347 void registerProbeNodeClone(ProbeNode probeNode) { 396 void registerProbeNodeClone(ProbeNode probeNode) {
348 probeNodeClones.add(new WeakReference<>(probeNode)); 397 probeNodeClones.add(new WeakReference<>(probeNode));
349 } 398 }
350 399
351 /** 400 /**
352 * Gets the currently active {@linkplain SyntaxTagTrap tagTrap}; {@code null} if not set. 401 * Gets the currently active <strong><em>before</em></strong> {@linkplain SyntaxTagTrap Tag
353 */ 402 * Trap} at this Probe. Non{@code -null} if the global
354 SyntaxTagTrap getTrap() { 403 * {@linkplain Probe#setBeforeTagTrap(SyntaxTagTrap) Before Tag Trap} is set and if this Probe
404 * holds the {@link SyntaxTag} specified in the trap.
405 */
406 SyntaxTagTrap getBeforeTrap() {
355 checkProbeUnchanged(); 407 checkProbeUnchanged();
356 return isTrapActive ? tagTrap : null; 408 return isBeforeTrapActive ? beforeTagTrap : null;
409 }
410
411 /**
412 * Gets the currently active <strong><em>after</em></strong> {@linkplain SyntaxTagTrap Tag Trap}
413 * at this Probe. Non{@code -null} if the global
414 * {@linkplain Probe#setAfterTagTrap(SyntaxTagTrap) After Tag Trap} is set and if this Probe
415 * holds the {@link SyntaxTag} specified in the trap.
416 */
417 SyntaxTagTrap getAfterTrap() {
418 checkProbeUnchanged();
419 return isAfterTrapActive ? afterTagTrap : null;
357 } 420 }
358 421
359 /** 422 /**
360 * To be called wherever in the Probe/Instrument chain there are dependencies on the probe 423 * To be called wherever in the Probe/Instrument chain there are dependencies on the probe
361 * state's @CompilatonFinal fields. 424 * state's @CompilatonFinal fields.
372 435
373 void invalidateProbeUnchanged() { 436 void invalidateProbeUnchanged() {
374 probeStateUnchangedCyclic.invalidate(); 437 probeStateUnchangedCyclic.invalidate();
375 } 438 }
376 439
377 private void notifyTrapSet() { 440 private void notifyTrapsChanged() {
378 this.isTrapActive = tagTrap != null && this.isTaggedAs(tagTrap.getTag()); 441 this.isBeforeTrapActive = beforeTagTrap != null && this.isTaggedAs(beforeTagTrap.getTag());
442 this.isAfterTrapActive = afterTagTrap != null && this.isTaggedAs(afterTagTrap.getTag());
379 invalidateProbeUnchanged(); 443 invalidateProbeUnchanged();
380 } 444 }
381 445
382 private String getTagsDescription() { 446 private String getTagsDescription() {
383 final StringBuilder sb = new StringBuilder(); 447 final StringBuilder sb = new StringBuilder();