Updated Branches: refs/heads/master d59f009d6 -> 16fb646dc
[KARAF-2449] Include memory information 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/16fb646d Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/16fb646d Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/16fb646d Branch: refs/heads/master Commit: 16fb646dc0f7ff29b8886fdf7af88cae56d34098 Parents: d59f009 Author: Jean-Baptiste Onofré <[email protected]> Authored: Tue Feb 11 02:41:47 2014 +0100 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Tue Feb 11 02:41:47 2014 +0100 ---------------------------------------------------------------------- diagnostic/core/pom.xml | 1 + .../core/internal/HeapDumpProvider.java | 71 ++++++++++++++++++++ .../core/internal/MemoryDumpProvider.java | 54 +++++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 6 ++ 4 files changed, 132 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/16fb646d/diagnostic/core/pom.xml ---------------------------------------------------------------------- diff --git a/diagnostic/core/pom.xml b/diagnostic/core/pom.xml index 431293a..ecf844b 100644 --- a/diagnostic/core/pom.xml +++ b/diagnostic/core/pom.xml @@ -83,6 +83,7 @@ ${project.artifactId}.common </Export-Package> <Import-Package> + com.sun.management*;resolution:=optional, * </Import-Package> <Private-Package> http://git-wip-us.apache.org/repos/asf/karaf/blob/16fb646d/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/HeapDumpProvider.java ---------------------------------------------------------------------- diff --git a/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/HeapDumpProvider.java b/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/HeapDumpProvider.java new file mode 100644 index 0000000..c4f7c70 --- /dev/null +++ b/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/HeapDumpProvider.java @@ -0,0 +1,71 @@ +/* + * 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.core.internal; + +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 { + + @Override + 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); + } + in.close(); + out.flush(); + out.close(); + // 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/16fb646d/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/MemoryDumpProvider.java ---------------------------------------------------------------------- diff --git a/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/MemoryDumpProvider.java b/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/MemoryDumpProvider.java new file mode 100644 index 0000000..ff23f9d --- /dev/null +++ b/diagnostic/core/src/main/java/org/apache/karaf/diagnostic/core/internal/MemoryDumpProvider.java @@ -0,0 +1,54 @@ +/* + * 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.core.internal; + +import org.apache.karaf.diagnostic.core.common.TextDumpProvider; + +import java.io.OutputStreamWriter; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; + +/** + * Provider which dump the memory information in the memory.txt file. + */ +public class MemoryDumpProvider extends TextDumpProvider { + + 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/16fb646d/diagnostic/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/diagnostic/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/diagnostic/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 4e9d9e1..46a3cf4 100644 --- a/diagnostic/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/diagnostic/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -44,6 +44,12 @@ <bean id="threads" class="org.apache.karaf.diagnostic.core.internal.ThreadDumpProvider" /> <service ref="threads" auto-export="interfaces" /> + <bean id="memory" class="org.apache.karaf.diagnostic.core.internal.MemoryDumpProvider" /> + <service ref="memory" auto-export="interfaces" /> + + <bean id="heap" class="org.apache.karaf.diagnostic.core.internal.HeapDumpProvider" /> + <service ref="heap" auto-export="interfaces" /> + <reference-list id="providers" availability="optional" interface="org.apache.karaf.diagnostic.core.DumpProvider" />
