Updated Branches: refs/heads/karaf-2.3.x d2e5edf3b -> f5f091e2c
[KARAF-2449] Ad memory information and heapdump in the created diagnostic dump Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/f5f091e2 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/f5f091e2 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/f5f091e2 Branch: refs/heads/karaf-2.3.x Commit: f5f091e2c97366ee5d193c1d544fb38e98aa188d Parents: d2e5edf Author: Jean-Baptiste Onofré <[email protected]> Authored: Mon Feb 10 21:46:24 2014 +0100 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Mon Feb 10 21:46:24 2014 +0100 ---------------------------------------------------------------------- diagnostic/common/pom.xml | 1 + .../diagnostic/common/HeapDumpProvider.java | 57 ++++++++++++++++++++ .../diagnostic/common/MemoryDumpProvider.java | 57 ++++++++++++++++++++ .../OSGI-INF/blueprint/diagnostic-services.xml | 6 +++ 4 files changed, 121 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/f5f091e2/diagnostic/common/pom.xml ---------------------------------------------------------------------- diff --git a/diagnostic/common/pom.xml b/diagnostic/common/pom.xml index f9d79e2..01962b8 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/f5f091e2/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..8628daf --- /dev/null +++ b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/HeapDumpProvider.java @@ -0,0 +1,57 @@ +/* + * 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.*; +import java.lang.management.ManagementFactory; + +/** + * Create a heap dump. + */ +public class HeapDumpProvider implements DumpProvider { + + public void createDump(DumpDestination destination) throws Exception { + 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"); + FileInputStream in = new FileInputStream(heapDumpFile); + OutputStream 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 + } + } + +} http://git-wip-us.apache.org/repos/asf/karaf/blob/f5f091e2/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..d0d2e32 --- /dev/null +++ b/diagnostic/common/src/main/java/org/apache/karaf/diagnostic/common/MemoryDumpProvider.java @@ -0,0 +1,57 @@ +/* + * 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.lang.management.ManagementFactory; + +import java.io.OutputStreamWriter; +import java.lang.management.MemoryMXBean; + +/** + * Provider which dump the heap to file heap.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/f5f091e2/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..e6a87bf 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="memory" class="org.apache.karaf.diagnostic.common.MemoryDumpProvider" /> + <service ref="memory" auto-export="interfaces" /> + + <bean id="heapdump" class="org.apache.karaf.diagnostic.common.HeapDumpProvider" /> + <service ref="heapdump" auto-export="interfaces" /> + <bean id="environment" class="org.apache.karaf.diagnostic.common.EnvironmentDumpProvider" > <argument ref="blueprintBundleContext" /> </bean>
