You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2012/08/17 10:39:54 UTC
svn commit: r1374173 - in /karaf/trunk/shell/commands: ./
src/main/java/org/apache/karaf/shell/commands/impl/
src/main/resources/OSGI-INF/blueprint/
src/test/java/org/apache/karaf/shell/commands/impl/
Author: cschneider
Date: Fri Aug 17 08:39:53 2012
New Revision: 1374173
URL: http://svn.apache.org/viewvc?rev=1374173&view=rev
Log:
KARAF-1742 Add threads command
Added:
karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/ThreadsAction.java
karaf/trunk/shell/commands/src/test/java/org/apache/karaf/shell/commands/impl/ThreadsTest.java
Modified:
karaf/trunk/shell/commands/pom.xml
karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
Modified: karaf/trunk/shell/commands/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/pom.xml?rev=1374173&r1=1374172&r2=1374173&view=diff
==============================================================================
--- karaf/trunk/shell/commands/pom.xml (original)
+++ karaf/trunk/shell/commands/pom.xml Fri Aug 17 08:39:53 2012
@@ -43,6 +43,10 @@
<artifactId>org.apache.karaf.shell.console</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.karaf.shell</groupId>
+ <artifactId>org.apache.karaf.shell.table</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.apache.karaf</groupId>
<artifactId>org.apache.karaf.util</artifactId>
<scope>provided</scope>
Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/ThreadsAction.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/ThreadsAction.java?rev=1374173&view=auto
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/ThreadsAction.java (added)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/ThreadsAction.java Fri Aug 17 08:39:53 2012
@@ -0,0 +1,75 @@
+/*
+ * 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.karaf.shell.commands.impl;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.console.AbstractAction;
+import org.apache.karaf.shell.table.ShellTable;
+
+/**
+ * Command for showing the full tree of bundles that have been used to resolve
+ * a given bundle.
+ */
+@Command(scope = "shell", name = "threads", description = "Prints the current threads (optionally with stacktraces)")
+public class ThreadsAction extends AbstractAction {
+
+ @Argument(name = "id", description="Show details for thread with this Id", required = false, multiValued = false)
+ Long id;
+
+ protected Object doExecute() throws Exception {
+ ThreadMXBean threadsBean = ManagementFactory.getThreadMXBean();
+
+ if (id != null) {
+ printThread(threadsBean, id);
+ } else {
+ printThreadList(threadsBean);
+ }
+ return null;
+ }
+
+ void printThread(ThreadMXBean threadsBean, Long id) {
+ threadsBean.setThreadCpuTimeEnabled(true);
+ ThreadInfo ti = threadsBean.getThreadInfo(id, Integer.MAX_VALUE);
+ System.out.println("Thread " + ti.getThreadId() + " " + ti.getThreadName() + " " + ti.getThreadState());
+ System.out.println("Stacktrace:");
+ StackTraceElement[] st = ti.getStackTrace();
+ for (StackTraceElement ste : st) {
+ System.out.println(ste.getClassName() + "." + ste.getMethodName() + " line: " + ste.getLineNumber());
+ }
+ }
+
+ void printThreadList(ThreadMXBean threadsBean) {
+ ThreadInfo[] threadInfoAr = threadsBean.dumpAllThreads(false, false);
+ ShellTable table = new ShellTable();
+ table.column("Id");
+ table.column("Name");
+ table.column("State");
+ table.column("CPU time");
+ table.column("User time");
+ for (ThreadInfo ti : threadInfoAr) {
+ long id = ti.getThreadId();
+ table.addRow().addContent(id, ti.getThreadName(), ti.getThreadState(), threadsBean.getThreadCpuTime(id) / 1000000, threadsBean.getThreadUserTime(id) / 1000000);
+ }
+ table.print(System.out);
+ }
+
+}
Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1374173&r1=1374172&r2=1374173&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Fri Aug 17 08:39:53 2012
@@ -86,6 +86,9 @@
<action class="org.apache.karaf.shell.commands.impl.TailAction"/>
</command>
<command>
+ <action class="org.apache.karaf.shell.commands.impl.ThreadsAction"/>
+ </command>
+ <command>
<action class="org.apache.karaf.shell.commands.impl.WatchAction">
<property name="commandProcessor" ref="commandProcessor"/>
</action>
Added: karaf/trunk/shell/commands/src/test/java/org/apache/karaf/shell/commands/impl/ThreadsTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/test/java/org/apache/karaf/shell/commands/impl/ThreadsTest.java?rev=1374173&view=auto
==============================================================================
--- karaf/trunk/shell/commands/src/test/java/org/apache/karaf/shell/commands/impl/ThreadsTest.java (added)
+++ karaf/trunk/shell/commands/src/test/java/org/apache/karaf/shell/commands/impl/ThreadsTest.java Fri Aug 17 08:39:53 2012
@@ -0,0 +1,23 @@
+package org.apache.karaf.shell.commands.impl;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadMXBean;
+
+import org.junit.Test;
+
+/**
+ * These are not real tests as they do no assertions. They simply help to see how the layout will look like.
+ */
+public class ThreadsTest {
+ @Test
+ public void testThreadlist() throws Exception {
+ ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+ new ThreadsAction().printThreadList(threadMXBean);
+ }
+
+ @Test
+ public void testThreadInfo() throws Exception {
+ ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+ new ThreadsAction().printThread(threadMXBean, 1L);
+ }
+}