Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jvm] Params to include sync info in thread dump. #1304

Merged
merged 1 commit into from Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions metrics-jvm/src/main/java/com/codahale/metrics/jvm/ThreadDump.java
Expand Up @@ -23,12 +23,29 @@ public ThreadDump(ThreadMXBean threadMXBean) {
}

/**
* Dumps all of the threads' current information to an output stream.
* Dumps all of the threads' current information, including synchronization, to an output stream.
*
* @param out an output stream
*/
public void dump(OutputStream out) {
final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(true, true);
dump(true, true, out);
}

/**
* Dumps all of the threads' current information, optionally including synchronization, to an output stream.
*
* Having control over including synchronization info allows using this method (and its wrappers, i.e.
* ThreadDumpServlet) in environments where getting object monitor and/or ownable synchronizer usage is not
* supported. It can also speed things up.
*
* See {@link ThreadMXBean#dumpAllThreads(boolean, boolean)}
*
* @param lockedMonitors dump all locked monitors if true
* @param lockedSynchronizers dump all locked ownable synchronizers if true
* @param out an output stream
*/
public void dump(boolean lockedMonitors, boolean lockedSynchronizers, OutputStream out) {
final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers);
final PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, UTF_8));

for (int ti = threads.length - 1; ti >= 0; ti--) {
Expand Down
Expand Up @@ -34,6 +34,9 @@ public void init() throws ServletException {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
final boolean includeMonitors = getParam(req.getParameter("monitors"), true);
final boolean includeSynchronizers = getParam(req.getParameter("synchronizers"), true);

resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(CONTENT_TYPE);
resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
Expand All @@ -42,7 +45,11 @@ protected void doGet(HttpServletRequest req,
return;
}
try (OutputStream output = resp.getOutputStream()) {
threadDump.dump(output);
threadDump.dump(includeMonitors, includeSynchronizers, output);
}
}

private static Boolean getParam(String initParam, boolean defaultValue) {
return initParam == null ? defaultValue : Boolean.parseBoolean(initParam);
}
}