-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On-deman backend heap and thread dump (#8320)
close #8249 Changelog: - add: `profiling/snapshot` request that takes a heap dump of the language server and puts it in the `ENSO_DATA_DIRECTORY/profiling` direcotry
- Loading branch information
Showing
15 changed files
with
284 additions
and
5 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
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
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
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
70 changes: 70 additions & 0 deletions
70
...ain/scala/org/enso/languageserver/requesthandler/profiling/ProfilingSnapshotHandler.scala
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,70 @@ | ||
package org.enso.languageserver.requesthandler.profiling | ||
|
||
import akka.actor.{Actor, ActorRef, Cancellable, Props} | ||
import com.typesafe.scalalogging.LazyLogging | ||
import org.enso.jsonrpc._ | ||
import org.enso.languageserver.profiling.{ProfilingApi, ProfilingProtocol} | ||
import org.enso.languageserver.requesthandler.RequestTimeout | ||
import org.enso.languageserver.util.UnhandledLogging | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
/** A request handler for `profiling/snapshot` commands. | ||
* | ||
* @param timeout a request timeout | ||
* @param profilingManager a reference to the profiling manager | ||
*/ | ||
class ProfilingSnapshotHandler( | ||
timeout: FiniteDuration, | ||
profilingManager: ActorRef | ||
) extends Actor | ||
with LazyLogging | ||
with UnhandledLogging { | ||
|
||
import context.dispatcher | ||
|
||
override def receive: Receive = requestStage | ||
|
||
private def requestStage: Receive = { | ||
case Request(ProfilingApi.ProfilingSnapshot, id, _) => | ||
profilingManager ! ProfilingProtocol.ProfilingSnapshotRequest | ||
val cancellable = | ||
context.system.scheduler.scheduleOnce(timeout, self, RequestTimeout) | ||
context.become( | ||
responseStage( | ||
id, | ||
sender(), | ||
cancellable | ||
) | ||
) | ||
} | ||
|
||
private def responseStage( | ||
id: Id, | ||
replyTo: ActorRef, | ||
cancellable: Cancellable | ||
): Receive = { | ||
case RequestTimeout => | ||
logger.error("Request [{}] timed out.", id) | ||
replyTo ! ResponseError(Some(id), Errors.RequestTimeout) | ||
context.stop(self) | ||
|
||
case ProfilingProtocol.ProfilingSnapshotResponse => | ||
replyTo ! ResponseResult(ProfilingApi.ProfilingSnapshot, id, Unused) | ||
cancellable.cancel() | ||
context.stop(self) | ||
} | ||
|
||
} | ||
|
||
object ProfilingSnapshotHandler { | ||
|
||
/** Creates configuration object used to create a [[ProfilingSnapshotHandler]]. | ||
* | ||
* @param timeout request timeout | ||
* @param profilingManager reference to the profiling manager | ||
*/ | ||
def props(timeout: FiniteDuration, profilingManager: ActorRef): Props = | ||
Props(new ProfilingSnapshotHandler(timeout, profilingManager)) | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...guage-server/src/test/scala/org/enso/languageserver/profiling/TestProfilingSnapshot.scala
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,16 @@ | ||
package org.enso.languageserver.profiling | ||
|
||
import org.enso.profiling.snapshot.ProfilingSnapshot | ||
|
||
import java.nio.file.{Files, Path} | ||
|
||
/** Generating a memory dump can take significant time. This test profiling snapshot | ||
* just creates an empty file in the specified location. | ||
*/ | ||
final class TestProfilingSnapshot extends ProfilingSnapshot { | ||
|
||
/** @inheritdoc */ | ||
override def generateSnapshot(output: Path): Unit = { | ||
Files.write(output, Array.emptyByteArray) | ||
} | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
lib/scala/profiling-utils/src/main/java/org/enso/profiling/snapshot/HeapDumpGenerator.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,38 @@ | ||
package org.enso.profiling.snapshot; | ||
|
||
import com.sun.management.HotSpotDiagnosticMXBean; | ||
import java.io.IOException; | ||
import java.lang.management.ManagementFactory; | ||
import java.nio.file.Path; | ||
import javax.management.MBeanServer; | ||
|
||
final class HeapDumpGenerator { | ||
|
||
private static final String HotSpotBeanName = "com.sun.management:type=HotSpotDiagnostic"; | ||
private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean; | ||
|
||
private HeapDumpGenerator() {} | ||
|
||
/** | ||
* Store the heap dump in the output file in the same format as the hprof heap dump. | ||
* | ||
* @param output the output file. | ||
* @param dumpOnlyLiveObjects if {@code true} dump only <i>live</i> objects i.e. objects that are | ||
* reachable from others. | ||
*/ | ||
public static void generateHeapDump(Path output, boolean dumpOnlyLiveObjects) throws IOException { | ||
if (hotSpotDiagnosticMXBean == null) { | ||
synchronized (HeapDumpGenerator.class) { | ||
hotSpotDiagnosticMXBean = getHotSpotDiagnosticMXBean(); | ||
} | ||
} | ||
|
||
hotSpotDiagnosticMXBean.dumpHeap(output.toString(), dumpOnlyLiveObjects); | ||
} | ||
|
||
private static HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean() throws IOException { | ||
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); | ||
return ManagementFactory.newPlatformMXBeanProxy( | ||
mBeanServer, HotSpotBeanName, HotSpotDiagnosticMXBean.class); | ||
} | ||
} |
Oops, something went wrong.