changeset 5769:8635406a0c33

factored counter functionality out of CheckcastSnippets in SnippetCounter for use by other snippets
author Doug Simon <doug.simon@oracle.com>
date Thu, 05 Jul 2012 10:31:30 +0200
parents 66ec0bc36a37
children 25e37b01b92a
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/VMToCompilerImpl.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CheckCastSnippets.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/SnippetCounter.java
diffstat 3 files changed, 163 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/VMToCompilerImpl.java	Wed Jul 04 22:01:03 2012 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/VMToCompilerImpl.java	Thu Jul 05 10:31:30 2012 +0200
@@ -288,7 +288,7 @@
         CompilationStatistics.clear("final");
         MethodEntryCounters.printCounters(graalRuntime);
         HotSpotXirGenerator.printCounters(TTY.out().out());
-        CheckCastSnippets.printCounters(TTY.out().out());
+        SnippetCounter.printGroups(TTY.out().out());
     }
 
     private void flattenChildren(DebugValueMap map, DebugValueMap globalMap) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CheckCastSnippets.java	Wed Jul 04 22:01:03 2012 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CheckCastSnippets.java	Thu Jul 05 10:31:30 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 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
@@ -22,15 +22,9 @@
  */
 package com.oracle.graal.hotspot.snippets;
 import static com.oracle.graal.hotspot.snippets.ArrayCopySnippets.*;
-import static com.oracle.graal.hotspot.snippets.CheckCastSnippets.Counter.*;
 import static com.oracle.graal.snippets.Snippet.Multiple.*;
 import static com.oracle.graal.snippets.SnippetTemplate.Arguments.*;
 
-import java.io.*;
-import java.util.*;
-
-import sun.misc.*;
-
 import com.oracle.graal.api.code.*;
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.compiler.*;
@@ -224,54 +218,6 @@
         return false;
     }
 
-    /**
-     * Counters for the various code paths through a checkcast.
-     */
-    public enum Counter {
-        hintsHit("hit a hint type"),
-        exactHit("exact type test succeeded"),
-        exactMiss("exact type test failed"),
-        isNull("object tested was null"),
-        cacheHit("secondary type cache hit"),
-        secondariesHit("secondaries scan succeeded"),
-        secondariesMiss("secondaries scan failed"),
-        displayHit("primary type test succeeded"),
-        displayMiss("primary type test failed"),
-        T_equals_S("object type was equal to secondary type");
-
-        final String description;
-        final int index;
-        long count;
-
-        private Counter(String desc) {
-            this.description = desc;
-            this.index = ordinal();
-        }
-
-        @Fold
-        static int countOffset() {
-            try {
-                return (int) Unsafe.getUnsafe().objectFieldOffset(Counter.class.getDeclaredField("count"));
-            } catch (Exception e) {
-                throw new GraalInternalError(e);
-            }
-        }
-
-        /**
-         * Increments this counter if counters are enabled. The body of this method has been carefully crafted
-         * such that it contains no safepoints and no calls, neither of which are permissible in a snippet.
-         * Also, increments are not guaranteed to be atomic which is acceptable for a counter.
-         */
-        void inc() {
-            if (ENABLED) {
-                DirectObjectStoreNode.storeLong(this, countOffset(), 0, count + 1);
-            }
-        }
-
-        static final Counter[] VALUES = values();
-        static final boolean ENABLED = GraalOptions.CheckcastCounters;
-    }
-
     @Fold
     private static int superCheckOffsetOffset() {
         return HotSpotGraalRuntime.getInstance().getConfig().superCheckOffsetOffset;
@@ -292,41 +238,6 @@
         return HotSpotGraalRuntime.getInstance().getConfig().hubOffset;
     }
 
-    public static void printCounter(PrintStream out, Counter c, long total) {
-        double percent = total == 0D ? 0D : ((double) (c.count * 100)) / total;
-        out.println(String.format("%16s: %5.2f%%%10d  // %s", c.name(), percent, c.count, c.description));
-    }
-
-    public static void printCounters(PrintStream out) {
-        if (!Counter.ENABLED) {
-            return;
-        }
-        Counter[] counters = Counter.values();
-        Arrays.sort(counters, new Comparator<Counter>() {
-            @Override
-            public int compare(Counter o1, Counter o2) {
-                if (o1.count > o2.count) {
-                    return -1;
-                } else if (o2.count > o1.count) {
-                    return 1;
-                }
-                return 0;
-            }
-
-        });
-
-        long total = 0;
-        for (Counter c : counters) {
-            total += c.count;
-        }
-
-        out.println();
-        out.println("** Checkcast counters **");
-        for (Counter c : counters) {
-            printCounter(out, c, total);
-        }
-    }
-
     public static class Templates {
 
         private final Cache cache;
@@ -393,4 +304,16 @@
             return hintHubs;
         }
     }
+
+    private static final SnippetCounter.Group counters = GraalOptions.CheckcastCounters ? new SnippetCounter.Group("Checkcast") : null;
+    private static final SnippetCounter hintsHit = new SnippetCounter(counters, "hintsHit", "hit a hint type");
+    private static final SnippetCounter exactHit = new SnippetCounter(counters, "exactHit", "exact type test succeeded");
+    private static final SnippetCounter exactMiss = new SnippetCounter(counters, "exactMiss", "exact type test failed");
+    private static final SnippetCounter isNull = new SnippetCounter(counters, "isNull", "object tested was null");
+    private static final SnippetCounter cacheHit = new SnippetCounter(counters, "cacheHit", "secondary type cache hit");
+    private static final SnippetCounter secondariesHit = new SnippetCounter(counters, "secondariesHit", "secondaries scan succeeded");
+    private static final SnippetCounter secondariesMiss = new SnippetCounter(counters, "secondariesMiss", "secondaries scan failed");
+    private static final SnippetCounter displayHit = new SnippetCounter(counters, "displayHit", "primary type test succeeded");
+    private static final SnippetCounter displayMiss = new SnippetCounter(counters, "displayMiss", "primary type test failed");
+    private static final SnippetCounter T_equals_S = new SnippetCounter(counters, "T_equals_S", "object type was equal to secondary type");
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/SnippetCounter.java	Thu Jul 05 10:31:30 2012 +0200
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2012, 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.
+ *
+ * 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.graal.hotspot.snippets;
+
+import java.io.*;
+import java.util.*;
+
+import sun.misc.*;
+
+import com.oracle.graal.graph.*;
+import com.oracle.graal.snippets.Snippet.Fold;
+
+/**
+ * A counter that can be safely {@linkplain #inc() incremented} from within a
+ * snippet for gathering snippet specific metrics.
+ */
+public class SnippetCounter implements Comparable<SnippetCounter> {
+
+    /**
+     * A group of related counters.
+     */
+    public static class Group {
+        final String name;
+        final List<SnippetCounter> counters;
+
+        public Group(String name) {
+            this.name = name;
+            this.counters = new ArrayList<>();
+        }
+
+        @Override
+        public synchronized String toString() {
+            Collections.sort(counters);
+
+            long total = 0;
+            for (SnippetCounter c : counters) {
+                total += c.value;
+            }
+
+            StringBuilder buf = new StringBuilder(String.format("Counters: %s%n", name));
+
+            for (SnippetCounter c : counters) {
+                double percent = total == 0D ? 0D : ((double) (c.value * 100)) / total;
+                buf.append(String.format("%16s: %5.2f%%%10d  // %s%n", c.name, percent, c.value, c.description));
+            }
+            return buf.toString();
+        }
+    }
+
+    /**
+     * Sorts counters in descending order of their {@linkplain #value() values}.
+     */
+    @Override
+    public int compareTo(SnippetCounter o) {
+        if (value > o.value) {
+            return -1;
+        } else if (o.value < value) {
+            return 1;
+        }
+        return 0;
+    }
+
+    private static final List<Group> groups = new ArrayList<>();
+
+    private final Group group;
+    private final int index;
+    private final String name;
+    private final String description;
+    private long value;
+
+    @Fold
+    private static int countOffset() {
+        try {
+            return (int) Unsafe.getUnsafe().objectFieldOffset(SnippetCounter.class.getDeclaredField("value"));
+        } catch (Exception e) {
+            throw new GraalInternalError(e);
+        }
+    }
+
+    /**
+     * Creates a counter.
+     *
+     * @param group the group to which the counter belongs. If this is null, the newly created counter is disabled and
+     *            {@linkplain #inc() incrementing} is a no-op.
+     * @param name the name of the counter
+     * @param description a brief comment describing the metric represented by the counter
+     */
+    public SnippetCounter(Group group, String name, String description) {
+        this.group = group;
+        this.name = name;
+        this.description = description;
+        if (group != null) {
+            List<SnippetCounter> counters = group.counters;
+            this.index = counters.size();
+            counters.add(this);
+            if (index == 0) {
+                groups.add(group);
+            }
+        } else {
+            this.index = -1;
+        }
+    }
+
+    /**
+     * Increments the value of this counter. This method can be safely used in a snippet
+     * if it is invoked on a compile-time constant {@link SnippetCounter} object.
+     */
+    public void inc() {
+        if (group != null) {
+            DirectObjectStoreNode.storeLong(this, countOffset(), 0, value + 1);
+        }
+    }
+
+    /**
+     * Gets the value of this counter.
+     */
+    public long value() {
+        return value;
+    }
+
+    /**
+     * Prints all the counter groups to a given stream.
+     */
+    public static void printGroups(PrintStream out) {
+        for (Group group : groups) {
+            out.println(group);
+        }
+    }
+}