# HG changeset patch # User Michael Van De Vanter # Date 1442794799 25200 # Node ID 59e022cee52919ce71d8748585ceaf59d7b1dd81 # Parent 3f2052afcb6de38762ca12696352478d965b8a40 Truffle/Instrumentation: rename InstrumentationTool (abstract parent for CoverageTracker, LineToProbesMap, etc.) to Instrumenter.Tool, eliminating the need for an Accessor instance. diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java Sun Sep 20 17:19:59 2015 -0700 @@ -31,7 +31,6 @@ import java.util.HashMap; import java.util.Map; -import com.oracle.truffle.api.instrument.InstrumentationTool; import com.oracle.truffle.api.instrument.Instrumenter; import com.oracle.truffle.api.instrument.Probe; import com.oracle.truffle.api.instrument.ProbeListener; @@ -44,7 +43,7 @@ * An {@link InstrumentationTool} that builds a map of every {@link Probe} attached to some AST, * indexed by {@link Source} and line number. */ -final class LineToProbesMap extends InstrumentationTool { +final class LineToProbesMap extends Instrumenter.Tool { private static final boolean TRACE = false; private static final PrintStream OUT = System.out; diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java Sun Sep 20 17:19:59 2015 -0700 @@ -61,7 +61,6 @@ private static Accessor NODES; private static Accessor INTEROP; private static Accessor INSTRUMENT; - private static Accessor TOOL; private static Accessor DEBUG; private static final ThreadLocal CURRENT_VM = new ThreadLocal<>(); @@ -178,11 +177,6 @@ throw new IllegalStateException(); } DEBUG = this; - } else if (this.getClass().getSimpleName().endsWith("Tool")) { - if (TOOL != null) { - throw new IllegalStateException(); - } - TOOL = this; } else { if (SPI != null) { throw new IllegalStateException(); diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/InstrumentationTool.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/InstrumentationTool.java Sun Sep 20 16:48:50 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,209 +0,0 @@ -/* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.truffle.api.instrument; - -import com.oracle.truffle.api.impl.Accessor; - -/** - * {@linkplain Instrument Instrumentation}-based tools that gather data during Guest Language - * program execution. - *

- * Tools share a common life cycle: - *

- *

- * Tool-specific methods that access data collected by the tool should: - *

- * Note:
- * Tool installation is currently global to the Truffle Execution environment. When - * language-agnostic management of individual execution environments is added to the platform, - * installation will be (optionally) specific to a single execution environment. - */ -public abstract class InstrumentationTool { - // TODO (mlvdv) still thinking about the most appropriate name for this class of tools - - private enum ToolState { - - /** Not yet installed, inert. */ - UNINSTALLED, - - /** Installed, collecting data. */ - ENABLED, - - /** Installed, not collecting data. */ - DISABLED, - - /** Was installed, but now removed, inactive, and no longer usable. */ - DISPOSED; - } - - private ToolState toolState = ToolState.UNINSTALLED; - - private Instrumenter instrumenter; - - protected InstrumentationTool() { - } - - /** - * Connect the tool to some part of the Truffle runtime, and enable data collection to start. - * Instrumentation will only be added to subsequently created ASTs. - * - * @throws IllegalStateException if the tool has previously been installed. - */ - public final void install(Instrumenter inst) { - checkUninstalled(); - if (inst != null) { - this.instrumenter = inst; - } - if (internalInstall()) { - toolState = ToolState.ENABLED; - } - } - - /** - * @return whether the tool is currently collecting data. - */ - public final boolean isEnabled() { - return toolState == ToolState.ENABLED; - } - - /** - * Switches tool state between enabled (collecting data) and disabled (not - * collecting data, but keeping data already collected). - * - * @throws IllegalStateException if not yet installed or disposed. - */ - public final void setEnabled(boolean isEnabled) { - checkInstalled(); - internalSetEnabled(isEnabled); - toolState = isEnabled ? ToolState.ENABLED : ToolState.DISABLED; - } - - /** - * Clears any data already collected, but otherwise does not change the state of the tool. - * - * @throws IllegalStateException if not yet installed or disposed. - */ - public final void reset() { - checkInstalled(); - internalReset(); - } - - /** - * Makes the tool permanently disabled, removes instrumentation, but keeps data already - * collected. - * - * @throws IllegalStateException if not yet installed or disposed. - */ - public final void dispose() { - checkInstalled(); - internalDispose(); - toolState = ToolState.DISPOSED; - } - - /** - * @return whether the installation succeeded. - */ - protected abstract boolean internalInstall(); - - /** - * No subclass action required. - * - * @param isEnabled - */ - protected void internalSetEnabled(boolean isEnabled) { - } - - protected abstract void internalReset(); - - protected abstract void internalDispose(); - - protected final Instrumenter getInstrumenter() { - if (instrumenter == null) { - instrumenter = ACCESSOR.getInstrumenter(null); - } - return instrumenter; - } - - /** - * Ensure that the tool is currently installed. - * - * @throws IllegalStateException - */ - private void checkInstalled() throws IllegalStateException { - if (toolState == ToolState.UNINSTALLED) { - throw new IllegalStateException("Tool " + getClass().getSimpleName() + " not yet installed"); - } - if (toolState == ToolState.DISPOSED) { - throw new IllegalStateException("Tool " + getClass().getSimpleName() + " has been disposed"); - } - } - - /** - * Ensure that the tool has not yet been installed. - * - * @throws IllegalStateException - */ - private void checkUninstalled() { - if (toolState != ToolState.UNINSTALLED) { - throw new IllegalStateException("Tool " + getClass().getSimpleName() + " has already been installed"); - } - } - - static final class AccessorTool extends Accessor { - - @Override - protected Instrumenter getInstrumenter(Object vm) { - return super.getInstrumenter(null); - } - } - - static final AccessorTool ACCESSOR = new AccessorTool(); - -} diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java Sun Sep 20 17:19:59 2015 -0700 @@ -67,8 +67,181 @@ return visitor.source; } + private enum ToolState { + + /** Not yet installed, inert. */ + UNINSTALLED, + + /** Installed, collecting data. */ + ENABLED, + + /** Installed, not collecting data. */ + DISABLED, + + /** Was installed, but now removed, inactive, and no longer usable. */ + DISPOSED; + } + + /** + * {@linkplain Instrument Instrumentation}-based tools that gather data during Guest Language + * program execution. + *

+ * Tools share a common life cycle: + *

+ *

+ * Tool-specific methods that access data collected by the tool should: + *

+ * Note:
+ * Tool installation is currently global to the Truffle Execution environment. When + * language-agnostic management of individual execution environments is added to the platform, + * installation will be (optionally) specific to a single execution environment. + */ + public abstract static class Tool { + // TODO (mlvdv) still thinking about the most appropriate name for this class of tools + + private ToolState toolState = ToolState.UNINSTALLED; + + private Instrumenter instrumenter; + + protected Tool() { + } + + /** + * Connect the tool to some part of the Truffle runtime, and enable data collection to + * start. Instrumentation will only be added to subsequently created ASTs. + * + * @throws IllegalStateException if the tool has previously been installed. + */ + public final void install(Instrumenter inst) { + checkUninstalled(); + this.instrumenter = inst; + + if (internalInstall()) { + toolState = ToolState.ENABLED; + } + instrumenter.tools.add(this); + } + + /** + * @return whether the tool is currently collecting data. + */ + public final boolean isEnabled() { + return toolState == ToolState.ENABLED; + } + + /** + * Switches tool state between enabled (collecting data) and disabled (not + * collecting data, but keeping data already collected). + * + * @throws IllegalStateException if not yet installed or disposed. + */ + public final void setEnabled(boolean isEnabled) { + checkInstalled(); + internalSetEnabled(isEnabled); + toolState = isEnabled ? ToolState.ENABLED : ToolState.DISABLED; + } + + /** + * Clears any data already collected, but otherwise does not change the state of the tool. + * + * @throws IllegalStateException if not yet installed or disposed. + */ + public final void reset() { + checkInstalled(); + internalReset(); + } + + /** + * Makes the tool permanently disabled, removes instrumentation, but keeps data + * already collected. + * + * @throws IllegalStateException if not yet installed or disposed. + */ + public final void dispose() { + checkInstalled(); + internalDispose(); + toolState = ToolState.DISPOSED; + instrumenter.tools.remove(this); + } + + /** + * @return whether the installation succeeded. + */ + protected abstract boolean internalInstall(); + + /** + * No subclass action required. + * + * @param isEnabled + */ + protected void internalSetEnabled(boolean isEnabled) { + } + + protected abstract void internalReset(); + + protected abstract void internalDispose(); + + protected final Instrumenter getInstrumenter() { + return instrumenter; + } + + /** + * Ensure that the tool is currently installed. + * + * @throws IllegalStateException + */ + private void checkInstalled() throws IllegalStateException { + if (toolState == ToolState.UNINSTALLED) { + throw new IllegalStateException("Tool " + getClass().getSimpleName() + " not yet installed"); + } + if (toolState == ToolState.DISPOSED) { + throw new IllegalStateException("Tool " + getClass().getSimpleName() + " has been disposed"); + } + } + + /** + * Ensure that the tool has not yet been installed. + * + * @throws IllegalStateException + */ + private void checkUninstalled() { + if (toolState != ToolState.UNINSTALLED) { + throw new IllegalStateException("Tool " + getClass().getSimpleName() + " has already been installed"); + } + } + } + private final Object vm; + /** Tools that have been created, but not yet disposed. */ + private final Set tools = Collections.synchronizedSet(new LinkedHashSet()); + private final Set astProbers = Collections.synchronizedSet(new LinkedHashSet()); private final List probeListeners = new ArrayList<>(); @@ -395,6 +568,7 @@ return afterTagTrap; } + // TODO (mlvdv) build this in as a VM event? /** * Enables instrumentation in a newly created AST by applying all registered instances of * {@link ASTProber}. diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.tools.test/src/com/oracle/truffle/tools/test/TruffleToolTest.java --- a/truffle/com.oracle.truffle.tools.test/src/com/oracle/truffle/tools/test/TruffleToolTest.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.tools.test/src/com/oracle/truffle/tools/test/TruffleToolTest.java Sun Sep 20 17:19:59 2015 -0700 @@ -31,7 +31,6 @@ import org.junit.Test; -import com.oracle.truffle.api.instrument.InstrumentationTool; import com.oracle.truffle.api.instrument.Instrumenter; import com.oracle.truffle.api.vm.TruffleVM; @@ -131,7 +130,7 @@ return instrumenter; } - private static final class DummyTruffleTool extends InstrumentationTool { + private static final class DummyTruffleTool extends Instrumenter.Tool { @Override protected boolean internalInstall() { diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java --- a/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java Sun Sep 20 17:19:59 2015 -0700 @@ -34,7 +34,6 @@ import java.util.TreeSet; import com.oracle.truffle.api.instrument.Instrument; -import com.oracle.truffle.api.instrument.InstrumentationTool; import com.oracle.truffle.api.instrument.Instrumenter; import com.oracle.truffle.api.instrument.Probe; import com.oracle.truffle.api.instrument.ProbeListener; @@ -83,7 +82,7 @@ * @see Instrument * @see SyntaxTag */ -public final class CoverageTracker extends InstrumentationTool { +public final class CoverageTracker extends Instrumenter.Tool { /** Counting data. */ private final Map coverageMap = new HashMap<>(); diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/LineToProbesMap.java --- a/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/LineToProbesMap.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/LineToProbesMap.java Sun Sep 20 17:19:59 2015 -0700 @@ -31,7 +31,6 @@ import java.util.HashMap; import java.util.Map; -import com.oracle.truffle.api.instrument.InstrumentationTool; import com.oracle.truffle.api.instrument.Instrumenter; import com.oracle.truffle.api.instrument.Probe; import com.oracle.truffle.api.instrument.ProbeListener; @@ -44,7 +43,7 @@ * An {@link InstrumentationTool} that builds a map of every {@link Probe} attached to some AST, * indexed by {@link Source} and line number. */ -public final class LineToProbesMap extends InstrumentationTool { +public final class LineToProbesMap extends Instrumenter.Tool { private static final boolean TRACE = false; private static final PrintStream OUT = System.out; diff -r 3f2052afcb6d -r 59e022cee529 truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/NodeExecCounter.java --- a/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/NodeExecCounter.java Sun Sep 20 16:48:50 2015 -0700 +++ b/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/NodeExecCounter.java Sun Sep 20 17:19:59 2015 -0700 @@ -38,7 +38,6 @@ import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.instrument.ASTProber; import com.oracle.truffle.api.instrument.Instrument; -import com.oracle.truffle.api.instrument.InstrumentationTool; import com.oracle.truffle.api.instrument.Instrumenter; import com.oracle.truffle.api.instrument.Probe; import com.oracle.truffle.api.instrument.ProbeException; @@ -97,7 +96,7 @@ * @see SyntaxTag * @see ProbeFailure */ -public final class NodeExecCounter extends InstrumentationTool { +public final class NodeExecCounter extends Instrumenter.Tool { /** * Execution count for AST nodes of a particular type.