|
| 1 | +// Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 5 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 6 | +// please see LICENSE-APACHE2. |
| 7 | +// |
| 8 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// either express or implied. See the LICENSE file for specific language governing |
| 10 | +// rights and limitations of this software. |
| 11 | +// |
| 12 | +// If you have any questions regarding licensing, please contact us at |
| 13 | + |
| 14 | +package com.rabbitmq.stream.perf; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.PrintWriter; |
| 18 | +import java.io.StringWriter; |
| 19 | +import java.lang.management.LockInfo; |
| 20 | +import java.lang.management.ManagementFactory; |
| 21 | +import java.lang.management.MonitorInfo; |
| 22 | +import java.lang.management.RuntimeMXBean; |
| 23 | +import java.lang.management.ThreadInfo; |
| 24 | +import java.time.LocalDateTime; |
| 25 | +import java.time.format.DateTimeFormatter; |
| 26 | +import java.util.List; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
| 29 | +import javax.servlet.http.HttpServletRequest; |
| 30 | +import javax.servlet.http.HttpServletResponse; |
| 31 | +import org.eclipse.jetty.server.Request; |
| 32 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 33 | +import picocli.CommandLine.Option; |
| 34 | + |
| 35 | +class DebugEndpointMonitoring implements Monitoring { |
| 36 | + |
| 37 | + @Option( |
| 38 | + names = {"--monitoring"}, |
| 39 | + description = "Enable HTTP endpoint for monitoring and debugging", |
| 40 | + defaultValue = "false") |
| 41 | + private boolean monitoring; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void configure(MonitoringContext context) { |
| 45 | + if (monitoring) { |
| 46 | + PlainTextThreadDumpFormatter formatter = new PlainTextThreadDumpFormatter(); |
| 47 | + context.addHttpEndpoint( |
| 48 | + "threaddump", |
| 49 | + new AbstractHandler() { |
| 50 | + @Override |
| 51 | + public void handle( |
| 52 | + String target, |
| 53 | + Request baseRequest, |
| 54 | + HttpServletRequest request, |
| 55 | + HttpServletResponse response) |
| 56 | + throws IOException { |
| 57 | + |
| 58 | + ThreadInfo[] threadInfos = |
| 59 | + ManagementFactory.getThreadMXBean().dumpAllThreads(true, true); |
| 60 | + String content = formatter.format(threadInfos); |
| 61 | + |
| 62 | + response.setStatus(HttpServletResponse.SC_OK); |
| 63 | + response.setContentLength(content.length()); |
| 64 | + response.setContentType("text/plain"); |
| 65 | + |
| 66 | + response.getWriter().print(content); |
| 67 | + |
| 68 | + baseRequest.setHandled(true); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // from Spring Boot's PlainTextThreadDumpFormatter |
| 75 | + private static class PlainTextThreadDumpFormatter { |
| 76 | + |
| 77 | + String format(ThreadInfo[] threads) { |
| 78 | + StringWriter dump = new StringWriter(); |
| 79 | + PrintWriter writer = new PrintWriter(dump); |
| 80 | + writePreamble(writer); |
| 81 | + for (ThreadInfo info : threads) { |
| 82 | + writeThread(writer, info); |
| 83 | + } |
| 84 | + return dump.toString(); |
| 85 | + } |
| 86 | + |
| 87 | + private void writePreamble(PrintWriter writer) { |
| 88 | + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 89 | + writer.println(dateFormat.format(LocalDateTime.now())); |
| 90 | + RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); |
| 91 | + writer.printf( |
| 92 | + "Full thread dump %s (%s %s):%n", |
| 93 | + runtime.getVmName(), runtime.getVmVersion(), System.getProperty("java.vm.info")); |
| 94 | + writer.println(); |
| 95 | + } |
| 96 | + |
| 97 | + private void writeThread(PrintWriter writer, ThreadInfo info) { |
| 98 | + writer.printf("\"%s\" - Thread t@%d%n", info.getThreadName(), info.getThreadId()); |
| 99 | + writer.printf(" %s: %s%n", Thread.State.class.getCanonicalName(), info.getThreadState()); |
| 100 | + writeStackTrace(writer, info, info.getLockedMonitors()); |
| 101 | + writer.println(); |
| 102 | + writeLockedOwnableSynchronizers(writer, info); |
| 103 | + writer.println(); |
| 104 | + } |
| 105 | + |
| 106 | + private void writeStackTrace( |
| 107 | + PrintWriter writer, ThreadInfo info, MonitorInfo[] lockedMonitors) { |
| 108 | + int depth = 0; |
| 109 | + for (StackTraceElement element : info.getStackTrace()) { |
| 110 | + writeStackTraceElement( |
| 111 | + writer, element, info, lockedMonitorsForDepth(lockedMonitors, depth), depth == 0); |
| 112 | + depth++; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private List<MonitorInfo> lockedMonitorsForDepth(MonitorInfo[] lockedMonitors, int depth) { |
| 117 | + return Stream.of(lockedMonitors) |
| 118 | + .filter((lockedMonitor) -> lockedMonitor.getLockedStackDepth() == depth) |
| 119 | + .collect(Collectors.toList()); |
| 120 | + } |
| 121 | + |
| 122 | + private void writeStackTraceElement( |
| 123 | + PrintWriter writer, |
| 124 | + StackTraceElement element, |
| 125 | + ThreadInfo info, |
| 126 | + List<MonitorInfo> lockedMonitors, |
| 127 | + boolean firstElement) { |
| 128 | + writer.printf("\tat %s%n", element.toString()); |
| 129 | + LockInfo lockInfo = info.getLockInfo(); |
| 130 | + if (firstElement && lockInfo != null) { |
| 131 | + if (element.getClassName().equals(Object.class.getName()) |
| 132 | + && element.getMethodName().equals("wait")) { |
| 133 | + writer.printf("\t- waiting on %s%n", format(lockInfo)); |
| 134 | + } else { |
| 135 | + String lockOwner = info.getLockOwnerName(); |
| 136 | + if (lockOwner != null) { |
| 137 | + writer.printf( |
| 138 | + "\t- waiting to lock %s owned by \"%s\" t@%d%n", |
| 139 | + format(lockInfo), lockOwner, info.getLockOwnerId()); |
| 140 | + } else { |
| 141 | + writer.printf("\t- parking to wait for %s%n", format(lockInfo)); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + writeMonitors(writer, lockedMonitors); |
| 146 | + } |
| 147 | + |
| 148 | + private String format(LockInfo lockInfo) { |
| 149 | + return String.format("<%x> (a %s)", lockInfo.getIdentityHashCode(), lockInfo.getClassName()); |
| 150 | + } |
| 151 | + |
| 152 | + private void writeMonitors(PrintWriter writer, List<MonitorInfo> lockedMonitorsAtCurrentDepth) { |
| 153 | + for (MonitorInfo lockedMonitor : lockedMonitorsAtCurrentDepth) { |
| 154 | + writer.printf("\t- locked %s%n", format(lockedMonitor)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void writeLockedOwnableSynchronizers(PrintWriter writer, ThreadInfo info) { |
| 159 | + writer.println(" Locked ownable synchronizers:"); |
| 160 | + LockInfo[] lockedSynchronizers = info.getLockedSynchronizers(); |
| 161 | + if (lockedSynchronizers == null || lockedSynchronizers.length == 0) { |
| 162 | + writer.println("\t- None"); |
| 163 | + } else { |
| 164 | + for (LockInfo lockedSynchronizer : lockedSynchronizers) { |
| 165 | + writer.printf("\t- Locked %s%n", format(lockedSynchronizer)); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments