|
| 1 | +package dev.suresh.routes |
| 2 | + |
| 3 | +import com.sun.management.HotSpotDiagnosticMXBean |
| 4 | +import dev.suresh.jvmRuntimeInfo |
| 5 | +import dev.suresh.plugins.debug |
| 6 | +import dev.suresh.runOnVirtualThread |
| 7 | +import io.ktor.http.* |
| 8 | +import io.ktor.server.application.* |
| 9 | +import io.ktor.server.response.* |
| 10 | +import io.ktor.server.routing.* |
| 11 | +import java.io.PrintStream |
| 12 | +import java.lang.management.ManagementFactory |
| 13 | +import jdk.jfr.Configuration |
| 14 | +import jdk.jfr.consumer.RecordingStream |
| 15 | +import kotlin.io.path.deleteIfExists |
| 16 | +import kotlin.io.path.name |
| 17 | +import kotlin.io.path.pathString |
| 18 | +import kotlin.time.Duration.Companion.milliseconds |
| 19 | +import kotlin.time.Duration.Companion.minutes |
| 20 | +import kotlin.time.toJavaDuration |
| 21 | +import kotlinx.coroutines.sync.Mutex |
| 22 | +import kotlinx.coroutines.sync.withLock |
| 23 | +import one.converter.Arguments |
| 24 | +import one.converter.FlameGraph |
| 25 | +import one.converter.jfr2flame |
| 26 | +import one.jfr.JfrReader |
| 27 | +import one.profiler.AsyncProfiler |
| 28 | +import one.profiler.AsyncProfilerLoader |
| 29 | +import one.profiler.Events |
| 30 | + |
| 31 | +private val DEBUG = ScopedValue.newInstance<Boolean>() |
| 32 | + |
| 33 | +val mutex = Mutex() |
| 34 | + |
| 35 | +val profiler: AsyncProfiler? by lazy { |
| 36 | + val ap = AsyncProfilerLoader.loadOrNull() |
| 37 | + ap.start(Events.CPU, 1000) |
| 38 | + ap |
| 39 | +} |
| 40 | + |
| 41 | +fun Route.mgmtRoutes() { |
| 42 | + |
| 43 | + get("/info") { |
| 44 | + call.respond(ScopedValue.where(DEBUG, call.debug).get { jvmRuntimeInfo(DEBUG.get()) }) |
| 45 | + } |
| 46 | + |
| 47 | + get("/profile") { |
| 48 | + // Run the blocking operation on virtual thread and make sure |
| 49 | + // only one profile operation is running at a time. |
| 50 | + when { |
| 51 | + mutex.isLocked -> call.respondText("Profile operation is already running") |
| 52 | + else -> |
| 53 | + mutex.withLock { |
| 54 | + runOnVirtualThread { |
| 55 | + val jfrPath = kotlin.io.path.createTempFile("profile", ".jfr") |
| 56 | + RecordingStream(Configuration.getConfiguration("profile")).use { |
| 57 | + it.setMaxSize(100 * 1024 * 1024) |
| 58 | + it.setMaxAge(2.minutes.toJavaDuration()) |
| 59 | + it.enable("jdk.CPULoad").withPeriod(100.milliseconds.toJavaDuration()) |
| 60 | + it.enable("jdk.JavaMonitorEnter").withStackTrace() |
| 61 | + it.startAsync() |
| 62 | + Thread.sleep(5_000) |
| 63 | + it.dump(jfrPath) |
| 64 | + println("JFR file written to ${jfrPath.toAbsolutePath()}") |
| 65 | + } |
| 66 | + |
| 67 | + when (call.request.queryParameters.contains("download")) { |
| 68 | + true -> { |
| 69 | + call.response.header( |
| 70 | + HttpHeaders.ContentDisposition, |
| 71 | + ContentDisposition.Attachment.withParameter( |
| 72 | + ContentDisposition.Parameters.FileName, jfrPath.fileName.name) |
| 73 | + .toString()) |
| 74 | + call.respondFile(jfrPath.toFile()) |
| 75 | + } |
| 76 | + else -> { |
| 77 | + val jfr2flame = jfr2flame(JfrReader(jfrPath.pathString), Arguments()) |
| 78 | + val flameGraph = FlameGraph() |
| 79 | + jfr2flame.convert(flameGraph) |
| 80 | + |
| 81 | + call.respondOutputStream(contentType = ContentType.Text.Html) { |
| 82 | + flameGraph.dump(PrintStream(this)) |
| 83 | + } |
| 84 | + jfrPath.deleteIfExists() |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + get("/heapdump") { |
| 93 | + val server = ManagementFactory.getPlatformMBeanServer() |
| 94 | + val hotspot = |
| 95 | + ManagementFactory.newPlatformMXBeanProxy( |
| 96 | + server, |
| 97 | + "com.sun.management:type=HotSpotDiagnostic", |
| 98 | + HotSpotDiagnosticMXBean::class.java) |
| 99 | + |
| 100 | + val heapDumpPath = kotlin.io.path.createTempFile("heapdump", ".hprof") |
| 101 | + heapDumpPath.deleteIfExists() |
| 102 | + hotspot.dumpHeap(heapDumpPath.pathString, true) |
| 103 | + call.response.header( |
| 104 | + HttpHeaders.ContentDisposition, |
| 105 | + ContentDisposition.Attachment.withParameter( |
| 106 | + ContentDisposition.Parameters.FileName, heapDumpPath.fileName.name) |
| 107 | + .toString()) |
| 108 | + call.respondFile(heapDumpPath.toFile()) |
| 109 | + heapDumpPath.deleteIfExists() |
| 110 | + } |
| 111 | +} |
0 commit comments