changeset 19593:6d895b028a44

Truffle/InstrumentationTools: clean up some misleading code and commentary in CoverageTracker
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Wed, 25 Feb 2015 17:53:51 -0800
parents bc3cd000d9c8
children 81a4eeea0264
files graal/com.oracle.truffle.api/src/com/oracle/truffle/api/tools/CoverageTracker.java
diffstat 1 files changed, 21 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/tools/CoverageTracker.java	Wed Feb 25 18:25:51 2015 +0100
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/tools/CoverageTracker.java	Wed Feb 25 17:53:51 2015 -0800
@@ -27,13 +27,11 @@
 import java.io.*;
 import java.util.*;
 import java.util.Map.Entry;
-import java.util.concurrent.atomic.*;
 
 import com.oracle.truffle.api.frame.*;
 import com.oracle.truffle.api.instrument.*;
 import com.oracle.truffle.api.instrument.impl.*;
 import com.oracle.truffle.api.nodes.*;
-import com.oracle.truffle.api.nodes.Node.Child;
 import com.oracle.truffle.api.source.*;
 
 /**
@@ -160,7 +158,7 @@
                 curSource = source;
                 curLineTable = new Long[source.getLineCount()];
             }
-            curLineTable[lineNo - 1] = entry.getValue().count.longValue();
+            curLineTable[lineNo - 1] = entry.getValue().count;
         }
         if (curSource != null) {
             result.put(curSource, curLineTable);
@@ -206,7 +204,7 @@
             while (curLineNo < lineNo) {
                 displayLine(out, null, curSource, curLineNo++);
             }
-            displayLine(out, entry.getValue().count, curSource, curLineNo++);
+            displayLine(out, entry.getValue(), curSource, curLineNo++);
         }
         if (curSource != null) {
             while (curLineNo <= curSource.getLineCount()) {
@@ -215,11 +213,11 @@
         }
     }
 
-    private static void displayLine(PrintStream out, AtomicLong value, Source source, int lineNo) {
-        if (value == null) {
+    private static void displayLine(PrintStream out, CoverageRecord record, Source source, int lineNo) {
+        if (record == null) {
             out.format("%14s", " ");
         } else {
-            out.format("(%12d)", value.longValue());
+            out.format("(%12d)", record.count);
         }
         out.format(" %3d: ", lineNo);
         out.println(source.getCode(lineNo));
@@ -227,33 +225,25 @@
 
     /**
      * A listener for events at each instrumented AST location. This listener counts
-     * "execution calls" to the instrumented node and is <em>stateful</em>. State in listeners must
-     * be considered carefully since ASTs, along with all instrumentation (including event listener
-     * such as this) are routinely cloned by the Truffle runtime. AST cloning is <em>shallow</em>
-     * (for non- {@link Child} nodes), so in this case the actual count <em>is shared</em> among all
-     * the clones; the count is also held in a table indexed by source line.
-     * <p>
-     * In contrast, a primitive field would <em>not</em> be shared among clones and resulting counts
-     * would not be accurate.
+     * "execution calls" to the instrumented node.
      */
-    private final class CoverageEventListener extends DefaultEventListener {
+    private final class CoverageRecord extends DefaultEventListener {
 
-        /**
-         * Shared by all clones of the associated instrument and by the table of counters for the
-         * line.
-         */
-        private final AtomicLong count;
+        private final SourceSection srcSection; // The text of the code being counted
+        private Instrument instrument;  // The attached Instrument, in case need to remove.
+        private long count = 0;
 
-        CoverageEventListener(AtomicLong count) {
-            this.count = count;
+        CoverageRecord(SourceSection srcSection) {
+            this.srcSection = srcSection;
         }
 
         @Override
         public void enter(Node node, VirtualFrame vFrame) {
             if (isEnabled()) {
-                count.getAndIncrement();
+                count++;
             }
         }
+
     }
 
     private static final class LineLocationEntryComparator implements Comparator<Entry<LineLocation, CoverageRecord>> {
@@ -282,35 +272,23 @@
                     if (record != null) {
                         // Another node starts on same line; count only the first (textually)
                         if (srcSection.getCharIndex() > record.srcSection.getCharIndex()) {
-                            // Record already in place, corresponds to code earlier on line
+                            // Existing record, corresponds to code earlier on line
                             return;
                         } else {
-                            // Record already in place, corresponds to later code; replace it
+                            // Existing record, corresponds to code at a later position; replace it
                             record.instrument.dispose();
                         }
                     }
-                    final AtomicLong count = new AtomicLong();
-                    final CoverageEventListener eventListener = new CoverageEventListener(count);
-                    final Instrument instrument = Instrument.create(eventListener, CoverageTracker.class.getSimpleName());
+
+                    final CoverageRecord coverage = new CoverageRecord(srcSection);
+                    final Instrument instrument = Instrument.create(coverage, CoverageTracker.class.getSimpleName());
+                    coverage.instrument = instrument;
                     instruments.add(instrument);
                     probe.attach(instrument);
-                    coverageMap.put(lineLocation, new CoverageRecord(srcSection, instrument, count));
+                    coverageMap.put(lineLocation, coverage);
                 }
             }
         }
     }
 
-    private class CoverageRecord {
-
-        final SourceSection srcSection; // The text of the code being counted
-        final Instrument instrument;  // The attached Instrument, in case need to remove.
-        final AtomicLong count;
-
-        CoverageRecord(SourceSection srcSection, Instrument instrument, AtomicLong count) {
-            this.srcSection = srcSection;
-            this.instrument = instrument;
-            this.count = count;
-        }
-    }
-
 }