public
Description: Full mirror of JRuby Subversion repository
Homepage: http://jruby.codehaus.org/
Clone URL: git://github.com/vvs/jruby.git
Add a ParserStats MBean to gather metrics on time spent parsing.


git-svn-id: https://svn.codehaus.org/jruby/trunk/jruby@7608 
961051c9-f516-0410-bf72-c9f7e237a7b7
headius (author)
Sun Aug 31 03:14:21 -0700 2008
commit  8ddd9b8feae1f499156e2aa70312174060354c32
tree    58d79c9c477d03f50a827c63256cbc74983a4b53
parent  acfa0b5f5fbfbd608c4aefe65d4a895d89730b36
...
96
97
98
 
99
100
101
...
205
206
207
 
208
209
 
210
211
212
...
566
567
568
 
 
 
 
569
570
571
...
1899
1900
1901
 
1902
1903
1904
1905
 
1906
1907
1908
...
1915
1916
1917
 
1918
1919
1920
1921
 
1922
1923
1924
...
1934
1935
1936
 
1937
1938
1939
1940
1941
 
1942
1943
1944
...
2340
2341
2342
 
2343
2344
2345
...
2976
2977
2978
 
 
 
2979
2980
2981
...
96
97
98
99
100
101
102
...
206
207
208
209
210
211
212
213
214
215
...
569
570
571
572
573
574
575
576
577
578
...
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
...
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
...
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
...
2353
2354
2355
2356
2357
2358
2359
...
2990
2991
2992
2993
2994
2995
2996
2997
2998
0
@@ -96,6 +96,7 @@ import org.jruby.javasupport.JavaSupport;
0
 import org.jruby.management.BeanManager;
0
 import org.jruby.management.ClassCache;
0
 import org.jruby.management.Config;
0
+import org.jruby.management.ParserStats;
0
 import org.jruby.parser.Parser;
0
 import org.jruby.parser.ParserConfiguration;
0
 import org.jruby.runtime.Binding;
0
@@ -205,8 +206,10 @@ public final class Ruby {
0
         this.kcode              = config.getKCode();
0
         this.beanManager        = new BeanManager(this, config.isManagementEnabled());
0
         this.jitCompiler        = new JITCompiler(this);
0
+        this.parserStats        = new ParserStats(this);
0
         
0
         this.beanManager.register(new Config(this));
0
+        this.beanManager.register(parserStats);
0
         this.beanManager.register(new ClassCache(this));
0
         
0
         this.cacheMap = new CacheMap(this);
0
@@ -566,6 +569,10 @@ public final class Ruby {
0
             return (IRubyObject) rj.getValue();
0
         }
0
     }
0
+
0
+    public Parser getParser() {
0
+        return parser;
0
+    }
0
     
0
     public BeanManager getBeanManager() {
0
         return beanManager;
0
@@ -1899,10 +1906,12 @@ public final class Ruby {
0
     }
0
     
0
     public Node parseFile(InputStream in, String file, DynamicScope scope) {
0
+        if (parserStats != null) parserStats.addLoadParse();
0
         return parser.parse(file, in, scope, new ParserConfiguration(0, false, false, true));
0
     }
0
 
0
     public Node parseInline(InputStream in, String file, DynamicScope scope) {
0
+        if (parserStats != null) parserStats.addEvalParse();
0
         return parser.parse(file, in, scope, new ParserConfiguration(0, false, true));
0
     }
0
 
0
@@ -1915,10 +1924,12 @@ public final class Ruby {
0
             bytes = content.getBytes();
0
         }
0
         
0
+        if (parserStats != null) parserStats.addEvalParse();
0
         return parser.parse(file, new ByteArrayInputStream(bytes), scope, 
0
                 new ParserConfiguration(lineNumber, false));
0
     }
0
 
0
+    @Deprecated
0
     public Node parse(String content, String file, DynamicScope scope, int lineNumber, 
0
             boolean extraPositionInformation) {
0
         byte[] bytes;
0
@@ -1934,11 +1945,13 @@ public final class Ruby {
0
     }
0
     
0
     public Node parseEval(ByteList content, String file, DynamicScope scope, int lineNumber) {
0
+        if (parserStats != null) parserStats.addEvalParse();
0
         return parser.parse(file, content, scope, new ParserConfiguration(lineNumber, false));
0
     }
0
 
0
     public Node parse(ByteList content, String file, DynamicScope scope, int lineNumber, 
0
             boolean extraPositionInformation) {
0
+        if (parserStats != null) parserStats.addJRubyModuleParse();
0
         return parser.parse(file, content, scope, 
0
                 new ParserConfiguration(lineNumber, extraPositionInformation, false));
0
     }
0
@@ -2340,6 +2353,7 @@ public final class Ruby {
0
 
0
         getBeanManager().unregisterCompiler();
0
         getBeanManager().unregisterConfig();
0
+        getBeanManager().unregisterParserStats();
0
         getBeanManager().unregisterClassCache();
0
         getBeanManager().unregisterMethodCache();
0
 
0
@@ -2976,6 +2990,9 @@ public final class Ruby {
0
     
0
     // Management/monitoring
0
     private BeanManager beanManager;
0
+
0
+    // Parser stats
0
+    private ParserStats parserStats;
0
     
0
     // Compilation
0
     private final JITCompiler jitCompiler;
...
33
34
35
 
 
 
 
36
37
38
...
47
48
49
 
 
 
50
51
52
...
33
34
35
36
37
38
39
40
41
42
...
51
52
53
54
55
56
57
58
59
0
@@ -33,6 +33,10 @@ public class BeanManager {
0
         if (managementEnabled) register(base + "service=Config", config);
0
     }
0
     
0
+    public void register(ParserStatsMBean parserStats) {
0
+        if (managementEnabled) register(base + "service=ParserStats", parserStats);
0
+    }
0
+    
0
     public void register(MethodCacheMBean methodCache) {
0
         if (managementEnabled) register(base + "service=MethodCache", methodCache);
0
     }
0
@@ -47,6 +51,9 @@ public class BeanManager {
0
     public void unregisterConfig() {
0
         if (managementEnabled) unregister(base + "service=Config");
0
     }
0
+    public void unregisterParserStats() {
0
+        if (managementEnabled) unregister(base + "service=ParserStats");
0
+    }
0
     public void unregisterClassCache() {
0
         if (managementEnabled) unregister(base + "service=ClassCache");
0
     }
...
54
55
56
 
 
57
58
59
60
 
 
 
 
 
 
 
 
61
62
63
64
 
65
 
 
 
66
67
68
69
70
71
72
 
 
73
74
75
...
82
83
84
 
85
86
87
...
106
107
108
 
 
 
109
110
111
112
113
114
115
...
137
138
139
140
 
 
 
 
 
 
141
142
...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
74
75
76
77
78
79
 
 
 
80
81
82
83
84
85
86
87
...
94
95
96
97
98
99
100
...
119
120
121
122
123
124
125
 
 
 
126
127
128
...
150
151
152
 
153
154
155
156
157
158
159
160
0
@@ -54,22 +54,34 @@ import org.jruby.util.ByteList;
0
  */
0
 public class Parser {
0
     private final Ruby runtime;
0
+    private volatile long totalTime;
0
+    private volatile int totalBytes;
0
 
0
     public Parser(Ruby runtime) {
0
         this.runtime = runtime;
0
     }
0
+
0
+    public long getTotalTime() {
0
+        return totalTime;
0
+    }
0
+
0
+    public int getTotalBytes() {
0
+        return totalBytes;
0
+    }
0
     
0
     public Node parseRewriter(String file, InputStream content, 
0
             ParserConfiguration configuration) throws SyntaxException {
0
-
0
+        long startTime = System.nanoTime();
0
         DefaultRubyParser parser = RubyParserPool.getInstance().borrowParser();
0
+        parser.setWarnings(new NullWarnings());
0
+        LexerSource lexerSource = LexerSource.getSource(file, content, null, configuration);
0
+        
0
         try {
0
-            parser.setWarnings(new NullWarnings());
0
-            LexerSource lexerSource = LexerSource.getSource(file, content, null, configuration);
0
-            
0
             return parser.parse(configuration, lexerSource).getAST();
0
         } finally {
0
             RubyParserPool.getInstance().returnParser(parser);
0
+            totalTime += System.nanoTime() - startTime;
0
+            totalBytes += lexerSource.getOffset();
0
         }
0
     }
0
     
0
@@ -82,6 +94,7 @@ public class Parser {
0
     @SuppressWarnings("unchecked")
0
     public Node parse(String file, InputStream content, DynamicScope blockScope,
0
             ParserConfiguration configuration) {
0
+        long startTime = System.nanoTime();
0
         IRubyObject scriptLines = runtime.getObject().fastGetConstantAt("SCRIPT_LINES__");
0
         RubyArray list = null;
0
         
0
@@ -106,10 +119,10 @@ public class Parser {
0
         
0
         DefaultRubyParser parser = null;
0
         RubyParserResult result = null;
0
+        parser = RubyParserPool.getInstance().borrowParser();
0
+        parser.setWarnings(runtime.getWarnings());
0
+        LexerSource lexerSource = LexerSource.getSource(file, content, list, configuration);
0
         try {
0
-            parser = RubyParserPool.getInstance().borrowParser();
0
-            parser.setWarnings(runtime.getWarnings());
0
-            LexerSource lexerSource = LexerSource.getSource(file, content, list, configuration);
0
             result = parser.parse(configuration, lexerSource);
0
             if (result.getEndOffset() >= 0) {
0
                 IRubyObject verbose = runtime.getVerbose();
0
@@ -137,6 +150,11 @@ public class Parser {
0
             result.getScope().growIfNeeded();
0
         }
0
 
0
-        return result.getAST();
0
+        Node ast = result.getAST();
0
+        
0
+        totalTime += System.nanoTime() - startTime;
0
+        totalBytes += lexerSource.getOffset();
0
+
0
+        return ast;
0
     }
0
 }

Comments