You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2014/02/11 18:19:24 UTC

git commit: [KARAF-2449] Include memory informationo in the dump created by the dev:create-dump command

Updated Branches:
  refs/heads/karaf-2.x 39d3c582e -> 05d4078b1


[KARAF-2449] Include memory informationo in the dump created by the dev:create-dump command


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/05d4078b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/05d4078b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/05d4078b

Branch: refs/heads/karaf-2.x
Commit: 05d4078b1f9e5adbce2f1139bb18fc62e080eefa
Parents: 39d3c58
Author: Jean-Baptiste Onofré <jb...@apache.org>
Authored: Tue Feb 11 18:18:42 2014 +0100
Committer: Jean-Baptiste Onofré <jb...@apache.org>
Committed: Tue Feb 11 18:18:42 2014 +0100

----------------------------------------------------------------------
 diagnostic/common/pom.xml                       |  1 +
 .../diagnostic/common/HeapDumpProvider.java     | 67 ++++++++++++++++++++
 .../diagnostic/common/MemoryDumpProvider.java   | 56 ++++++++++++++++
 .../OSGI-INF/blueprint/diagnostic-services.xml  |  6 ++
 4 files changed, 130 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/05d4078b/diagnostic/common/pom.xml
----------------------------------------------------------------------
diff --git a/diagnostic/common/pom.xml b/diagnostic/common/pom.xml
index e3ec03d..435910f 100644
--- a/diagnostic/common/pom.xml
+++ b/diagnostic/common/pom.xml
@@ -85,6 +85,7 @@
                 <configuration>
                     <instructions>
                         <Import-Package>
+                            com.sun.management*;resolution:=optional,
                             *
                         </Import-Package>
                     </instructions>

http://git-wip-us.apache.org/repos/asf/karaf/blob/05d4078b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/HeapDumpProvider.java
----------------------------------------------------------------------
diff --git a/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/HeapDumpProvider.java b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/HeapDumpProvider.java
new file mode 100644
index 0000000..e41627d
--- /dev/null
+++ b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/HeapDumpProvider.java
@@ -0,0 +1,67 @@
+/*
+ * 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.diagnostic.common;
+
+import com.sun.management.HotSpotDiagnosticMXBean;
+import org.apache.karaf.diagnostic.core.DumpDestination;
+import org.apache.karaf.diagnostic.core.DumpProvider;
+
+import javax.management.MBeanServer;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.OutputStream;
+import java.lang.management.ManagementFactory;
+
+/**
+ * Create a heap dump.
+ */
+public class HeapDumpProvider implements DumpProvider {
+
+    public void createDump(DumpDestination destination) throws Exception {
+        FileInputStream in = null;
+        OutputStream out = null;
+        try {
+            MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
+            HotSpotDiagnosticMXBean diagnosticMXBean = ManagementFactory.newPlatformMXBeanProxy(mBeanServer,
+                    "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
+            diagnosticMXBean.dumpHeap("heapdump.txt", false);
+            // copy the dump in the destination
+            File heapDumpFile = new File("heapdump.txt");
+            in = new FileInputStream(heapDumpFile);
+            out = destination.add("heapdump.txt");
+            byte[] buffer = new byte[2048];
+            while ((in.read(buffer) != -1)) {
+                out.write(buffer);
+            }
+            // remove the original dump
+            if (heapDumpFile.exists()) {
+                heapDumpFile.delete();
+            }
+        } catch (Exception e) {
+            // nothing to do
+        } finally {
+            if (in != null) {
+                in.close();
+            }
+            if (out != null) {
+                out.flush();
+                out.close();
+            }
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/05d4078b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/MemoryDumpProvider.java
----------------------------------------------------------------------
diff --git a/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/MemoryDumpProvider.java b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/MemoryDumpProvider.java
new file mode 100644
index 0000000..1727b64
--- /dev/null
+++ b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/MemoryDumpProvider.java
@@ -0,0 +1,56 @@
+/*
+ * 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.diagnostic.common;
+
+import org.apache.karaf.diagnostic.core.common.TextDumpProvider;
+
+import java.io.OutputStreamWriter;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+
+/**
+ * Provider for memory information in memory.txt.
+ */
+public class MemoryDumpProvider extends TextDumpProvider {
+
+    /**
+     * Create a new dump entry which contains information about memory usage.
+     */
+    public MemoryDumpProvider() {
+        super("memory.txt");
+    }
+
+    @Override
+    protected void writeDump(OutputStreamWriter outputStream) throws Exception {
+        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
+
+        outputStream.write("Number of objects waiting finalization: " + memoryMXBean.getObjectPendingFinalizationCount() + "\n\n");
+
+        outputStream.write("Heap:\n");
+        outputStream.write("\tInit:      " + memoryMXBean.getHeapMemoryUsage().getInit() + "\n");
+        outputStream.write("\tUser:      " + memoryMXBean.getHeapMemoryUsage().getUsed() + "\n");
+        outputStream.write("\tCommitted: " + memoryMXBean.getHeapMemoryUsage().getCommitted() + "\n");
+        outputStream.write("\tMax:       " + memoryMXBean.getHeapMemoryUsage().getMax() + "\n");
+
+        outputStream.write("Non-Heap: \n");
+        outputStream.write("\tInit:      " + memoryMXBean.getNonHeapMemoryUsage().getInit() + "\n");
+        outputStream.write("\tUser:      " + memoryMXBean.getNonHeapMemoryUsage().getUsed() + "\n");
+        outputStream.write("\tCommitted: " + memoryMXBean.getNonHeapMemoryUsage().getCommitted() + "\n");
+        outputStream.write("\tMax:       " + memoryMXBean.getNonHeapMemoryUsage().getMax() + "\n");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/05d4078b/diagnostic/common/src/main/resources/OSGI-INF/blueprint/diagnostic-services.xml
----------------------------------------------------------------------
diff --git a/diagnostic/common/src/main/resources/OSGI-INF/blueprint/diagnostic-services.xml b/diagnostic/common/src/main/resources/OSGI-INF/blueprint/diagnostic-services.xml
index ab4626a..c3618c8 100644
--- a/diagnostic/common/src/main/resources/OSGI-INF/blueprint/diagnostic-services.xml
+++ b/diagnostic/common/src/main/resources/OSGI-INF/blueprint/diagnostic-services.xml
@@ -40,6 +40,12 @@
     <bean id="threads" class="org.apache.karaf.diagnostic.common.ThreadDumpProvider" />
     <service ref="threads" auto-export="interfaces" />
 
+    <bean id="heapdump" class="org.apache.karaf.diagnostic.common.HeapDumpProvider" />
+    <service ref="heapdump" auto-export="interfaces" />
+
+    <bean id="memory" class="org.apache.karaf.diagnostic.common.MemoryDumpProvider" />
+    <service ref="memory" auto-export="interfaces" />
+
     <bean id="environment" class="org.apache.karaf.diagnostic.common.EnvironmentDumpProvider" >
         <argument ref="blueprintBundleContext" />
     </bean>