changeset 21140:7d2f6dd603b0

Make DebugScope lazier and generally reduce overhead of Debug being enabled
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Tue, 28 Apr 2015 09:44:39 -0700
parents a17dc2584e81
children 2b1228d97525
files graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/GraalDebugConfig.java graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchPattern.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/MetricImpl.java
diffstat 5 files changed, 99 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/GraalDebugConfig.java	Tue Apr 28 09:44:23 2015 -0700
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/GraalDebugConfig.java	Tue Apr 28 09:44:39 2015 -0700
@@ -172,7 +172,12 @@
     }
 
     private int getLevel(DebugFilter filter) {
-        int level = checkDebugFilter(Debug.currentScope(), filter);
+        int level;
+        if (filter == null) {
+            level = 0;
+        } else {
+            level = filter.matchLevel(Debug.currentScope());
+        }
         if (level > 0 && !checkMethodFilter()) {
             level = 0;
         }
@@ -183,14 +188,6 @@
         return filter != null && checkMethodFilter();
     }
 
-    private static int checkDebugFilter(String currentScope, DebugFilter filter) {
-        if (filter == null) {
-            return 0;
-        } else {
-            return filter.matchLevel(currentScope);
-        }
-    }
-
     /**
      * Extracts a {@link JavaMethod} from an opaque debug context.
      *
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchPattern.java	Tue Apr 28 09:44:23 2015 -0700
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/match/MatchPattern.java	Tue Apr 28 09:44:39 2015 -0700
@@ -76,32 +76,32 @@
 
         static Result wrongClass(Node node, MatchPattern matcher) {
             MatchResult_WRONG_CLASS.increment();
-            return Debug.isEnabled() ? new Result(MatchResultCode.WRONG_CLASS, node, matcher) : CACHED_WRONG_CLASS;
+            return Debug.isLogEnabled() ? new Result(MatchResultCode.WRONG_CLASS, node, matcher) : CACHED_WRONG_CLASS;
         }
 
         static Result namedValueMismatch(Node node, MatchPattern matcher) {
             MatchResult_NAMED_VALUE_MISMATCH.increment();
-            return Debug.isEnabled() ? new Result(MatchResultCode.NAMED_VALUE_MISMATCH, node, matcher) : CACHED_NAMED_VALUE_MISMATCH;
+            return Debug.isLogEnabled() ? new Result(MatchResultCode.NAMED_VALUE_MISMATCH, node, matcher) : CACHED_NAMED_VALUE_MISMATCH;
         }
 
         static Result tooManyUsers(Node node, MatchPattern matcher) {
             MatchResult_TOO_MANY_USERS.increment();
-            return Debug.isEnabled() ? new Result(MatchResultCode.TOO_MANY_USERS, node, matcher) : CACHED_TOO_MANY_USERS;
+            return Debug.isLogEnabled() ? new Result(MatchResultCode.TOO_MANY_USERS, node, matcher) : CACHED_TOO_MANY_USERS;
         }
 
         static Result notInBlock(Node node, MatchPattern matcher) {
             MatchResult_NOT_IN_BLOCK.increment();
-            return Debug.isEnabled() ? new Result(MatchResultCode.NOT_IN_BLOCK, node, matcher) : CACHED_NOT_IN_BLOCK;
+            return Debug.isLogEnabled() ? new Result(MatchResultCode.NOT_IN_BLOCK, node, matcher) : CACHED_NOT_IN_BLOCK;
         }
 
         static Result notSafe(Node node, MatchPattern matcher) {
             MatchResult_NOT_SAFE.increment();
-            return Debug.isEnabled() ? new Result(MatchResultCode.NOT_SAFE, node, matcher) : CACHED_NOT_SAFE;
+            return Debug.isLogEnabled() ? new Result(MatchResultCode.NOT_SAFE, node, matcher) : CACHED_NOT_SAFE;
         }
 
         static Result alreadyUsed(Node node, MatchPattern matcher) {
             MatchResult_ALREADY_USED.increment();
-            return Debug.isEnabled() ? new Result(MatchResultCode.ALREADY_USED, node, matcher) : CACHED_ALREADY_USED;
+            return Debug.isLogEnabled() ? new Result(MatchResultCode.ALREADY_USED, node, matcher) : CACHED_ALREADY_USED;
         }
 
         @Override
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Tue Apr 28 09:44:23 2015 -0700
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Tue Apr 28 09:44:39 2015 -0700
@@ -738,8 +738,8 @@
      *         {@linkplain Indent#close() closed} or null if debugging is disabled
      */
     public static Indent logAndIndent(int logLevel, String msg) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, msg);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, msg);
         }
         return null;
     }
@@ -757,8 +757,8 @@
      *         {@linkplain Indent#close() closed} or null if debugging is disabled
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg);
         }
         return null;
     }
@@ -776,8 +776,8 @@
      *         {@linkplain Indent#close() closed} or null if debugging is disabled
      */
     public static Indent logAndIndent(int logLevel, String format, int arg) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg);
         }
         return null;
     }
@@ -790,8 +790,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, int arg1, Object arg2) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2);
         }
         return null;
     }
@@ -804,8 +804,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, int arg2) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2);
         }
         return null;
     }
@@ -818,8 +818,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, int arg1, int arg2) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2);
         }
         return null;
     }
@@ -832,8 +832,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2);
         }
         return null;
     }
@@ -846,8 +846,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2, arg3);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3);
         }
         return null;
     }
@@ -860,8 +860,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, int arg1, int arg2, int arg3) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2, arg3);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3);
         }
         return null;
     }
@@ -874,8 +874,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, int arg2, int arg3) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2, arg3);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3);
         }
         return null;
     }
@@ -888,8 +888,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2, arg3, arg4);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3, arg4);
         }
         return null;
     }
@@ -902,8 +902,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2, arg3, arg4, arg5);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3, arg4, arg5);
         }
         return null;
     }
@@ -916,8 +916,8 @@
      * @see #logAndIndent(int, String, Object)
      */
     public static Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) {
-        if (ENABLED) {
-            return logvAndIndent(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6);
+        if (ENABLED && Debug.isLogEnabled(logLevel)) {
+            return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6);
         }
         return null;
     }
@@ -933,13 +933,21 @@
      */
     public static Indent logvAndIndent(int logLevel, String format, Object... args) {
         if (ENABLED) {
-            DebugScope scope = DebugScope.getInstance();
-            scope.log(logLevel, format, args);
-            return scope.pushIndentLogger();
+            if (Debug.isLogEnabled(logLevel)) {
+                return logvAndIndentInternal(logLevel, format, args);
+            }
+            return null;
         }
         throw new InternalError("Use of Debug.logvAndIndent() must be guarded by a test of Debug.isEnabled()");
     }
 
+    private static Indent logvAndIndentInternal(int logLevel, String format, Object... args) {
+        assert ENABLED && Debug.isLogEnabled(logLevel) : "must have checked Debug.isLogEnabled()";
+        DebugScope scope = DebugScope.getInstance();
+        scope.log(logLevel, format, args);
+        return scope.pushIndentLogger();
+    }
+
     /**
      * This override exists to catch cases when {@link #logAndIndent(String, Object)} is called with
      * one argument bound to a varargs method parameter. It will bind to this method instead of the
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Tue Apr 28 09:44:23 2015 -0700
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Tue Apr 28 09:44:39 2015 -0700
@@ -47,7 +47,7 @@
                 if (parentIndent != null) {
                     parentIndent.printScopeName(str);
                 }
-                str.append(indent).append("[thread:").append(Thread.currentThread().getId()).append("] scope: ").append(qualifiedName).append(System.lineSeparator());
+                str.append(indent).append("[thread:").append(Thread.currentThread().getId()).append("] scope: ").append(getQualifiedName()).append(System.lineSeparator());
                 logScopeName = false;
             }
         }
@@ -92,8 +92,10 @@
 
     private final Object[] context;
 
-    private final DebugValueMap valueMap;
-    private final String qualifiedName;
+    private DebugValueMap valueMap;
+
+    private String qualifiedName;
+    private final String unqualifiedName;
 
     private static final char SCOPE_SEP = '.';
 
@@ -110,9 +112,8 @@
     public static DebugScope getInstance() {
         DebugScope result = instanceTL.get();
         if (result == null) {
-            DebugScope topLevelDebugScope = new DebugScope(Thread.currentThread().getName(), "", null, false);
+            DebugScope topLevelDebugScope = new DebugScope(Thread.currentThread());
             instanceTL.set(topLevelDebugScope);
-            DebugValueMap.registerTopLevel(topLevelDebugScope.getValueMap());
             return topLevelDebugScope;
         } else {
             return result;
@@ -123,17 +124,23 @@
         return configTL.get();
     }
 
-    private DebugScope(String name, String qualifiedName, DebugScope parent, boolean sandbox, Object... context) {
+    static final Object[] EMPTY_CONTEXT = new Object[0];
+
+    private DebugScope(Thread thread) {
+        this(thread.getName(), null, false);
+        computeValueMap(thread.getName());
+        DebugValueMap.registerTopLevel(getValueMap());
+    }
+
+    private DebugScope(String unqualifiedName, DebugScope parent, boolean sandbox, Object... context) {
         this.parent = parent;
         this.sandbox = sandbox;
         this.parentConfig = getConfig();
         this.context = context;
-        this.qualifiedName = qualifiedName;
+        this.unqualifiedName = unqualifiedName;
         if (parent != null) {
-            lastUsedIndent = new IndentImpl(parent.lastUsedIndent);
-            logScopeName = !parent.qualifiedName.equals(qualifiedName);
+            logScopeName = !unqualifiedName.equals("");
         } else {
-            lastUsedIndent = new IndentImpl(null);
             logScopeName = true;
         }
 
@@ -141,7 +148,9 @@
         // set while logging
         this.output = TTY.cachedOut;
         assert context != null;
+    }
 
+    private void computeValueMap(String name) {
         if (parent != null) {
             for (DebugValueMap child : parent.getValueMap().getChildren()) {
                 if (child.getName().equals(name)) {
@@ -240,7 +249,9 @@
     }
 
     public void log(int logLevel, String msg, Object... args) {
-        lastUsedIndent.log(logLevel, msg, args);
+        if (isLogEnabled(logLevel)) {
+            getLastUsedIndent().log(logLevel, msg, args);
+        }
     }
 
     public void dump(int dumpLevel, Object object, String formatString, Object... args) {
@@ -297,7 +308,7 @@
     public DebugScope scope(CharSequence name, DebugConfig sandboxConfig, Object... newContextObjects) {
         DebugScope newScope = null;
         if (sandboxConfig != null) {
-            newScope = new DebugScope(name.toString(), name.toString(), this, true, newContextObjects);
+            newScope = new DebugScope(name.toString(), this, true, newContextObjects);
             configTL.set(sandboxConfig);
         } else {
             newScope = this.createChild(name.toString(), newContextObjects);
@@ -371,6 +382,9 @@
     }
 
     private DebugValueMap getValueMap() {
+        if (valueMap == null) {
+            computeValueMap(unqualifiedName);
+        }
         return valueMap;
     }
 
@@ -383,12 +397,7 @@
     }
 
     private DebugScope createChild(String newName, Object[] newContext) {
-        String newQualifiedName = newName;
-        if (this.qualifiedName.length() > 0) {
-            newQualifiedName = this.qualifiedName + SCOPE_SEP + newName;
-        }
-        DebugScope result = new DebugScope(newName, newQualifiedName, this, false, newContext);
-        return result;
+        return new DebugScope(newName, this, false, newContext);
     }
 
     public Iterable<Object> getCurrentContext() {
@@ -451,11 +460,29 @@
     }
 
     public String getQualifiedName() {
+        if (qualifiedName == null) {
+            if (parent == null) {
+                qualifiedName = unqualifiedName;
+            } else {
+                qualifiedName = parent.getQualifiedName() + SCOPE_SEP + unqualifiedName;
+            }
+        }
         return qualifiedName;
     }
 
     public Indent pushIndentLogger() {
-        lastUsedIndent = lastUsedIndent.indent();
+        lastUsedIndent = getLastUsedIndent().indent();
+        return lastUsedIndent;
+    }
+
+    public IndentImpl getLastUsedIndent() {
+        if (lastUsedIndent == null) {
+            if (parent != null) {
+                lastUsedIndent = new IndentImpl(parent.getLastUsedIndent());
+            } else {
+                lastUsedIndent = new IndentImpl(null);
+            }
+        }
         return lastUsedIndent;
     }
 }
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/MetricImpl.java	Tue Apr 28 09:44:23 2015 -0700
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/MetricImpl.java	Tue Apr 28 09:44:39 2015 -0700
@@ -28,8 +28,10 @@
 
     public MetricImpl(String name, boolean conditional) {
         super(name, conditional);
-        // Allows for zero-count metrics to be shown
-        getCurrentValue();
+        if (isEnabled()) {
+            // Allows for zero-count metrics to be shown
+            getCurrentValue();
+        }
     }
 
     public void increment() {