You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2012/04/19 23:12:32 UTC

svn commit: r1328117 - in /geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main: java/org/apache/geronimo/shell/debug/ resources/OSGI-INF/blueprint/

Author: gawor
Date: Thu Apr 19 21:12:31 2012
New Revision: 1328117

URL: http://svn.apache.org/viewvc?rev=1328117&view=rev
Log:
GERONIMO-6331: Simple shell commands for generating thread dump and finding deadlocked threads

Added:
    geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/
    geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/FindDeadlockCommand.java
    geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/ThreadDumpCommand.java
    geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/Utils.java
    geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/resources/OSGI-INF/blueprint/shell-debug.xml

Added: geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/FindDeadlockCommand.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/FindDeadlockCommand.java?rev=1328117&view=auto
==============================================================================
--- geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/FindDeadlockCommand.java (added)
+++ geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/FindDeadlockCommand.java Thu Apr 19 21:12:31 2012
@@ -0,0 +1,72 @@
+/**
+ *  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.geronimo.shell.debug;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
+
+@Command(scope = "debug", name = "find-deadlock", description = "Find deadlocked threads")
+public class FindDeadlockCommand extends OsgiCommandSupport {
+
+    @Option(name = "-f", aliases = { "--file" }, description = "Write the thread information to the specified file")
+    String file = null;
+    
+    protected Object doExecute() throws Exception {
+
+        ThreadMXBean mxbean = ManagementFactory.getThreadMXBean(); 
+        long[] threadIds = mxbean.findDeadlockedThreads();
+        if (threadIds == null || threadIds.length == 0) {
+            System.out.println("No deadlocked threads detected.");
+        } else {
+            ThreadInfo[] threads = mxbean.getThreadInfo(threadIds, true, true);
+            
+            File dumpFile = null;
+            PrintWriter writer = null;
+            if (file != null) {
+                dumpFile = new File(file);
+                writer = new PrintWriter(dumpFile, "UTF-8");
+            } else {
+                writer = new PrintWriter(System.out);
+            }
+            
+            writer.println("Deadlocked threads found:");
+            writer.println();
+            
+            for (ThreadInfo thread : threads) {
+                Utils.writeThreadInfo(thread, writer);
+            }
+            
+            if (file != null) {
+                System.out.println("Thread deadlock information written to " + dumpFile);
+                writer.close();
+            } else {
+                writer.flush();
+            }
+        }
+          
+        return null;
+    }
+       
+}

Added: geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/ThreadDumpCommand.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/ThreadDumpCommand.java?rev=1328117&view=auto
==============================================================================
--- geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/ThreadDumpCommand.java (added)
+++ geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/ThreadDumpCommand.java Thu Apr 19 21:12:31 2012
@@ -0,0 +1,74 @@
+/**
+ *  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.geronimo.shell.debug;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
+
+@Command(scope = "debug", name = "thread-dump", description = "Dump thread information")
+public class ThreadDumpCommand extends OsgiCommandSupport {
+
+    private static final String FORMAT = "yyyyMMdd.HHmmss.SSS";
+    private static final SimpleDateFormat df = new SimpleDateFormat(FORMAT);
+    
+    @Option(name = "-d", aliases = { "--dir" }, description = "Write thread dump to a file in the specified directory")
+    String dir = null;
+    
+    protected Object doExecute() throws Exception {
+
+        ThreadMXBean mxbean = ManagementFactory.getThreadMXBean(); 
+        ThreadInfo[] threads = mxbean.dumpAllThreads(true, true);
+        
+        String timestamp = df.format(new Date());
+        
+        File dumpFile = null;
+        PrintWriter writer = null;
+        if (dir != null) {
+            dumpFile = new File(dir, "threaddump." + timestamp + ".txt");
+            writer = new PrintWriter(dumpFile, "UTF-8");
+        } else {
+            writer = new PrintWriter(System.out);
+        }
+        
+        writer.println("Thread Dump Timestamp: " + timestamp);
+        writer.println();
+        
+        for (ThreadInfo thread : threads) {
+            Utils.writeThreadInfo(thread, writer);
+        }
+        
+        if (dir != null) {
+            System.out.println("Thread dump written to " + dumpFile);
+            writer.close();
+        } else {
+            writer.flush();
+        }
+        
+        return null;
+    }
+       
+}

Added: geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/Utils.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/Utils.java?rev=1328117&view=auto
==============================================================================
--- geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/Utils.java (added)
+++ geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/debug/Utils.java Thu Apr 19 21:12:31 2012
@@ -0,0 +1,74 @@
+/**
+ *  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.geronimo.shell.debug;
+
+import java.io.PrintWriter;
+import java.lang.management.MonitorInfo;
+import java.lang.management.ThreadInfo;
+
+public class Utils {
+
+    public static void writeThreadInfo(ThreadInfo info, PrintWriter w) {
+        w.print("\"" + info.getThreadName() + "\" " + info.getThreadId() + " " + info.getThreadState());
+        if (info.isSuspended()) {
+            w.print(" (suspended)");
+        }
+        w.println();
+
+        MonitorInfo[] lockedMonitors = info.getLockedMonitors();
+        StackTraceElement[] stackTrace = info.getStackTrace();
+        for (int i = 0; i < stackTrace.length; i++) {
+            StackTraceElement ste = stackTrace[i];
+            w.println("\tat " + ste.toString());
+
+            if (i == 0 && info.getLockInfo() != null) {
+                Thread.State ts = info.getThreadState();
+                switch (ts) {
+                    case BLOCKED:
+                        w.print("\t - blocked on " + info.getLockInfo());
+                        writeLockOwnerInfo(info, w);
+                        break;
+                    case WAITING:
+                        w.print("\t - waiting on " + info.getLockInfo());
+                        writeLockOwnerInfo(info, w);
+                        break;
+                    case TIMED_WAITING:
+                        w.println("\t - waiting on " + info.getLockInfo());
+                        writeLockOwnerInfo(info, w);
+                        break;
+                    default:
+                }
+            }
+
+            for (MonitorInfo mi : lockedMonitors) {
+                if (mi.getLockedStackDepth() == i) {
+                    w.println("\t - locked " + mi);
+                }
+            }
+        }
+        
+        w.println();
+    }
+    
+    private static void writeLockOwnerInfo(ThreadInfo info, PrintWriter writer) {
+        if (info.getLockOwnerName() != null) {
+            writer.print(" owned by \"" + info.getLockOwnerName() + "\" id=" + info.getLockOwnerId());
+        }
+        writer.println();
+    }
+}

Added: geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/resources/OSGI-INF/blueprint/shell-debug.xml
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/resources/OSGI-INF/blueprint/shell-debug.xml?rev=1328117&view=auto
==============================================================================
--- geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/resources/OSGI-INF/blueprint/shell-debug.xml (added)
+++ geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main/resources/OSGI-INF/blueprint/shell-debug.xml Thu Apr 19 21:12:31 2012
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           default-activation="lazy">
+
+    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">
+        <command name="debug/thread-dump">
+            <action class="org.apache.geronimo.shell.debug.ThreadDumpCommand"></action>
+        </command>
+        <command name="debug/find-deadlock">
+            <action class="org.apache.geronimo.shell.debug.FindDeadlockCommand"></action>
+        </command>
+    </command-bundle>
+
+</blueprint>



Re: svn commit: r1328117 - in /geronimo/server/branches/3.0-beta/framework/modules/geronimo-shell-base/src/main: java/org/apache/geronimo/shell/debug/ resources/OSGI-INF/blueprint/

Posted by Kevan Miller <ke...@gmail.com>.
On Apr 19, 2012, at 5:12 PM, gawor@apache.org wrote:

> Author: gawor
> Date: Thu Apr 19 21:12:31 2012
> New Revision: 1328117
> 
> URL: http://svn.apache.org/viewvc?rev=1328117&view=rev
> Log:
> GERONIMO-6331: Simple shell commands for generating thread dump and finding deadlocked threads

Nice!

--kevan