forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][ci] Add way to collect threaddump and heap histogram from t…
…est execution
- Loading branch information
Showing
8 changed files
with
352 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
buildtools/src/main/java/org/apache/pulsar/tests/HeapDumpUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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.pulsar.tests; | ||
|
||
import com.sun.management.HotSpotDiagnosticMXBean; | ||
import java.io.File; | ||
import java.lang.management.ManagementFactory; | ||
import javax.management.MBeanServer; | ||
|
||
public class HeapDumpUtil { | ||
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; | ||
|
||
// Utility method to get the HotSpotDiagnosticMXBean | ||
private static HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean() { | ||
try { | ||
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | ||
return ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Dump the heap of the JVM. | ||
* | ||
* @param file the system-dependent filename | ||
* @param liveObjects if true dump only live objects i.e. objects that are reachable from others | ||
*/ | ||
public static void dumpHeap(File file, boolean liveObjects) { | ||
try { | ||
HotSpotDiagnosticMXBean hotspotMBean = getHotSpotDiagnosticMXBean(); | ||
hotspotMBean.dumpHeap(file.getAbsolutePath(), liveObjects); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error generating heap dump", e); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
buildtools/src/main/java/org/apache/pulsar/tests/HeapHistogramUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.pulsar.tests; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import javax.management.JMException; | ||
import javax.management.ObjectName; | ||
|
||
public class HeapHistogramUtil { | ||
public static String buildHeapHistogram() { | ||
StringBuilder dump = new StringBuilder(); | ||
dump.append(String.format("Timestamp: %s", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()))); | ||
dump.append("\n\n"); | ||
try { | ||
dump.append(callDiagnosticCommand("gcHeapInfo")); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
dump.append("\n"); | ||
try { | ||
dump.append(callDiagnosticCommand("gcClassHistogram")); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
return dump.toString(); | ||
} | ||
|
||
/** | ||
* Calls a diagnostic commands. | ||
* The available operations are similar to what the jcmd commandline tool has, | ||
* however the naming of the operations are different. The "help" operation can be used | ||
* to find out the available operations. For example, the jcmd command "Thread.print" maps | ||
* to "threadPrint" operation name. | ||
*/ | ||
static String callDiagnosticCommand(String operationName, String... args) | ||
throws JMException { | ||
return (String) ManagementFactory.getPlatformMBeanServer() | ||
.invoke(new ObjectName("com.sun.management:type=DiagnosticCommand"), | ||
operationName, new Object[] {args}, new String[] {String[].class.getName()}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.