You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2017/07/03 11:20:21 UTC

svn commit: r1800633 - in /jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak: run/ThreadDumpCommand.java threadDump/ThreadDumpThreadNames.java

Author: thomasm
Date: Mon Jul  3 11:20:21 2017
New Revision: 1800633

URL: http://svn.apache.org/viewvc?rev=1800633&view=rev
Log:
OAK-4521 Oak-run: add profiling and analysis tools

Added:
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/threadDump/ThreadDumpThreadNames.java
Modified:
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/ThreadDumpCommand.java

Modified: jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/ThreadDumpCommand.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/ThreadDumpCommand.java?rev=1800633&r1=1800632&r2=1800633&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/ThreadDumpCommand.java (original)
+++ jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/ThreadDumpCommand.java Mon Jul  3 11:20:21 2017
@@ -43,6 +43,7 @@ import joptsimple.OptionSpec;
 
 import org.apache.jackrabbit.oak.commons.Profiler;
 import org.apache.jackrabbit.oak.run.commons.Command;
+import org.apache.jackrabbit.oak.threadDump.ThreadDumpThreadNames;
 import org.apache.jackrabbit.oak.threadDump.ThreadDumpCleaner;
 import org.apache.jackrabbit.oak.threadDump.ThreadDumpConverter;
 
@@ -53,17 +54,19 @@ public class ThreadDumpCommand implement
     @Override
     public void execute(String... args) throws Exception {
         OptionParser parser = new OptionParser();
-        OptionSpec<Void> convertSpec = parser.accepts("convert", 
+        OptionSpec<Void> convertSpec = parser.accepts("convert",
                 "convert the thread dumps to the standard format");
-        OptionSpec<Void> filterSpec = parser.accepts("filter", 
+        OptionSpec<Void> filterSpec = parser.accepts("filter",
                 "filter the thread dumps, only keep working threads");
-        OptionSpec<Void> profileSpec = parser.accepts("profile", 
+        OptionSpec<Void> threadNamesSpec = parser.accepts("threadNames",
+                "create a summary of thread names");
+        OptionSpec<Void> profileSpec = parser.accepts("profile",
                 "profile the thread dumps");
-        OptionSpec<Void> profileClassesSpec = parser.accepts("profileClasses", 
+        OptionSpec<Void> profileClassesSpec = parser.accepts("profileClasses",
                 "profile classes");
-        OptionSpec<Void> profileMethodsSpec = parser.accepts("profileMethods", 
+        OptionSpec<Void> profileMethodsSpec = parser.accepts("profileMethods",
                 "profile methods");
-        OptionSpec<Void> profilePackagesSpec = parser.accepts("profilePackages", 
+        OptionSpec<Void> profilePackagesSpec = parser.accepts("profilePackages",
                 "profile packages");
         OptionSpec<?> helpSpec = parser.acceptsAll(
                 asList("h", "?", "help"), "show help").forHelp();
@@ -79,6 +82,7 @@ public class ThreadDumpCommand implement
         }
         boolean convert = options.has(convertSpec);
         boolean filter = options.has(filterSpec);
+        boolean threadNames = options.has(threadNamesSpec);
         boolean profile = options.has(profileSpec);
         boolean profileClasses = options.has(profileClassesSpec);
         boolean profileMethods = options.has(profileMethodsSpec);
@@ -92,12 +96,20 @@ public class ThreadDumpCommand implement
             if (convert) {
                 file = ThreadDumpConverter.process(file);
                 System.out.println("Converted to " + file.getAbsolutePath());
-                
+
+            }
+            if (threadNames) {
+                File f = ThreadDumpThreadNames.process(file);
+                System.out.println("Thread names written to " + f.getAbsolutePath());
             }
             if (filter) {
                 file = ThreadDumpCleaner.process(file);
                 System.out.println("Filtered into " + file.getAbsolutePath());
             }
+            if (threadNames) {
+                File f = ThreadDumpThreadNames.process(file);
+                System.out.println("Thread names written to " + f.getAbsolutePath());
+            }
             if (profile) {
                 ArrayList<String> list = new ArrayList<String>();
                 if (profileClasses) {
@@ -105,16 +117,16 @@ public class ThreadDumpCommand implement
                 }
                 if (profileMethods) {
                     list.add("-methods");
-                }                
+                }
                 if (profilePackages) {
                     list.add("-packages");
-                }                
+                }
                 list.add(file.getAbsolutePath());
                 Profiler.main(list.toArray(new String[0]));
             }
         }
     }
-    
+
     private static File combineAndExpandFiles(File file) throws IOException {
         if (!file.exists() || !file.isDirectory()) {
             if (!file.getName().endsWith(".gz")) {
@@ -132,7 +144,7 @@ public class ThreadDumpCommand implement
         }
         return target;
     }
-    
+
     private static int processFileOrDirectory(File file, PrintWriter writer)
             throws IOException {
         if (file.isFile()) {
@@ -147,7 +159,7 @@ public class ThreadDumpCommand implement
         }
         return count;
     }
-    
+
     private static int processFile(File file, PrintWriter writer) throws IOException {
         Reader reader = null;
         int count = 0;
@@ -163,7 +175,7 @@ public class ThreadDumpCommand implement
                 System.out.println("Extracting " + file.getAbsolutePath());
                 InputStream fileStream = new FileInputStream(file);
                 InputStream gzipStream = new GZIPInputStream(fileStream);
-                reader = new InputStreamReader(gzipStream);                
+                reader = new InputStreamReader(gzipStream);
             } else {
                 System.out.println("Reading " + file.getAbsolutePath());
                 reader = new FileReader(file);
@@ -183,7 +195,7 @@ public class ThreadDumpCommand implement
                 }
                 if (s == null) {
                     break;
-                }          
+                }
                 if (s.startsWith("Full thread dump") || s.startsWith("Full Java thread dump")) {
                     fullThreadDumps++;
                 }
@@ -199,6 +211,6 @@ public class ThreadDumpCommand implement
             }
         }
         return count;
-    }    
+    }
 
 }

Added: jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/threadDump/ThreadDumpThreadNames.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/threadDump/ThreadDumpThreadNames.java?rev=1800633&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/threadDump/ThreadDumpThreadNames.java (added)
+++ jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/threadDump/ThreadDumpThreadNames.java Mon Jul  3 11:20:21 2017
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.threadDump;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.PrintWriter;
+
+/**
+ * A tool that converts a file with date/time and thread dumps into a list of
+ * date/time and just the thread names.
+ */
+public class ThreadDumpThreadNames {
+
+    public static File process(File file) throws IOException {
+        String fileName = file.getName();
+        if (fileName.endsWith(".txt")) {
+            fileName = fileName.substring(0, fileName.length() - ".txt".length());
+        }
+        File target = new File(file.getParentFile(), fileName + ".threadNames.txt");
+        PrintWriter writer = new PrintWriter(new BufferedWriter(
+                new FileWriter(target)));
+        try {
+            processFile(file, writer);
+        } finally {
+            writer.close();
+        }
+        return target;
+    }
+
+    private static void processFile(File file, PrintWriter writer) throws IOException {
+        LineNumberReader r = new LineNumberReader(new BufferedReader(
+                new FileReader(file)));
+        try {
+            process(r, writer);
+        } finally {
+            r.close();
+        }
+    }
+
+    private static void process(LineNumberReader reader, PrintWriter writer) throws IOException {
+        String dateAndTime = "";
+        while (true) {
+            String line = reader.readLine();
+            if (line == null) {
+                break;
+            }
+            if (line.length() == "2017-06-29 17:23:17".length()) {
+                boolean isDateTime = true;
+                for(char c : line.toCharArray()) {
+                    if (!Character.isDigit(c) && " -:".indexOf(c) < 0) {
+                        isDateTime = false;
+                        break;
+                    }
+                }
+                if (isDateTime) {
+                    dateAndTime = line;
+                    continue;
+                }
+            }
+            if (line.startsWith("\"")) {
+                line = line.substring(0, line.lastIndexOf('\"') + 1);
+                writer.write(dateAndTime + " " + line + "\n");
+            }
+        }
+    }
+
+}