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

Backend self sampling #8309

Merged
merged 13 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/language-server/protocol-language-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ transport formats, please look [here](./protocol-architecture).
- [`library/preinstall`](#librarypreinstall)
- [Runtime Operations](#runtime-operations)
- [`runtime/getComponentGroups`](#runtimegetcomponentgroups)
- [Profiling Operations](#profiling-operations)
- [`profiling/start`](#profilingstart)
- [`profiling/stop`](#profilingstop)
- [Errors](#errors-75)
- [`Error`](#error)
- [`AccessDeniedError`](#accessdeniederror)
Expand Down Expand Up @@ -5091,6 +5094,63 @@ interface RuntimeGetComponentGroupsResult {

None

## Profiling Operations

### `profiling/start`

Sent from the client to the server to initiate gathering the profiling data.
This command should be followed by the [`profiling/stop`](#profilingstop)
request to store the gathered data. After the profiling is started, subsequent
`profiling/start` commands will do nothing.

- **Type:** Request
- **Direction:** Client -> Server
- **Connection:** Protocol
- **Visibility:** Public

#### Parameters

```typescript
interface ProfilingStartParameters {}
```

#### Result

```typescript
interface ProfilingStartResult {}
```

#### Errors

None

### `profiling/stop`

Sent from the client to the server to finish gathering the profiling data. The
collected data is stored in the `ENSO_DATA_DIRECTORY/profiling` directory. After
the profiling is stopped, subsequent `profiling/stop` commands will do nothing.

- **Type:** Request
- **Direction:** Client -> Server
- **Connection:** Protocol
- **Visibility:** Public

#### Parameters

```typescript
interface ProfilingStopParameters {}
```

#### Result

```typescript
interface ProfilingStopResult {}
```

#### Errors

None

## Errors

The language server component also has its own set of errors. This section is
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.enso.languageserver.runtime.events;

import java.io.IOException;
import java.io.PrintStream;
import java.time.Clock;
import java.time.Instant;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.XMLFormatter;

import org.enso.languageserver.runtime.RuntimeConnector;
import org.enso.polyglot.runtime.Runtime;
import org.enso.polyglot.runtime.Runtime$Api$Request;
import org.enso.polyglot.runtime.Runtime$Api$Response;
import org.enso.profiling.events.EventsMonitor;
import scala.Option;

/**
* Gather messages between the language server and the runtime and write them to the provided file
* in XML format.
*/
public final class RuntimeEventsMonitor implements EventsMonitor {

private final PrintStream out;
private final Clock clock;

private static final XMLFormatter EVENT_FORMAT = new XMLFormatter();
private static final String XML_TAG = "<?xml version='1.0'?>";
private static final String RECORDS_TAG_OPEN = "<records>";
private static final String RECORDS_TAG_CLOSE = "</records>";
private static final String MESSAGE_SEPARATOR = ",";
private static final String MESSAGE_EMPTY_REQUEST_ID = "";

/**
* Create an instance of {@link RuntimeEventsMonitor}.
*
* @param out the output stream.
* @param clock the system clock.
*/
public RuntimeEventsMonitor(PrintStream out, Clock clock) {
this.out = out;
this.clock = clock;

out.println(XML_TAG);
out.println(RECORDS_TAG_OPEN);
}

/**
* Create an instance of {@link RuntimeEventsMonitor}.
*
* @param out the output stream.
*/
public RuntimeEventsMonitor(PrintStream out) {
this(out, Clock.systemUTC());
}

/**
* Direction of the message.
*/
private enum Direction {
REQUEST,
RESPONSE
}

@Override
public void registerEvent(Object event) {
if (event instanceof Runtime.ApiEnvelope envelope) {
registerApiEnvelope(envelope);
} else if (event instanceof RuntimeConnector.MessageFromRuntime messageFromRuntime) {
registerApiEnvelope(messageFromRuntime.message());
}
}

@Override
public void close() throws IOException {
out.println(RECORDS_TAG_CLOSE);
out.close();
}

private void registerApiEnvelope(Runtime.ApiEnvelope event) {
if (event instanceof Runtime$Api$Request request) {
String entry =
buildEntry(Direction.REQUEST, request.requestId(), request.payload().getClass());
out.print(entry);
} else if (event instanceof Runtime$Api$Response response) {
String entry =
buildEntry(Direction.RESPONSE, response.correlationId(), response.payload().getClass());
out.print(entry);
}
}

private String buildEntry(Direction direction, Option<UUID> requestId, Class<?> payload) {
String requestIdEntry = requestId.fold(() -> MESSAGE_EMPTY_REQUEST_ID, UUID::toString);
String payloadEntry = payload.getSimpleName();
Instant timeEntry = clock.instant();

String message =
new StringBuilder()
.append(direction)
.append(MESSAGE_SEPARATOR)
.append(requestIdEntry)
.append(MESSAGE_SEPARATOR)
.append(payloadEntry)
.toString();

LogRecord record = new LogRecord(Level.INFO, message);
record.setInstant(timeEntry);

return EVENT_FORMAT.format(record);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import org.enso.languageserver.runtime.RuntimeKiller.{
RuntimeShutdownResult,
ShutDownRuntime
}
import org.enso.profiling.{FileSampler, MethodsSampler, NoopSampler}
import org.enso.profiling.sampler.{
MethodsSampler,
NoopSampler,
OutputStreamSampler
}
import org.slf4j.event.Level

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContextExecutor, Future}

Expand Down Expand Up @@ -105,12 +110,14 @@ class LanguageServerComponent(config: LanguageServerConfig, logLevel: Level)
private def startSampling(config: LanguageServerConfig): MethodsSampler = {
val sampler = config.profilingConfig.profilingPath match {
case Some(path) =>
new FileSampler(path.toFile)
OutputStreamSampler.ofFile(path.toFile)
case None =>
NoopSampler()
new NoopSampler()
}
sampler.start()
config.profilingConfig.profilingTime.foreach(sampler.stop(_))
config.profilingConfig.profilingTime.foreach(timeout =>
sampler.scheduleStop(timeout.length, timeout.unit, ec)
)

sampler
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import org.enso.languageserver.libraries._
import org.enso.languageserver.monitoring.{
HealthCheckEndpoint,
IdlenessEndpoint,
IdlenessMonitor,
NoopEventsMonitor
IdlenessMonitor
}
import org.enso.languageserver.profiling.ProfilingManager
import org.enso.languageserver.protocol.binary.{
BinaryConnectionControllerFactory,
InboundMessageDecoder
Expand All @@ -35,6 +35,7 @@ import org.enso.languageserver.protocol.json.{
}
import org.enso.languageserver.requesthandler.monitoring.PingHandler
import org.enso.languageserver.runtime._
import org.enso.languageserver.runtime.events.RuntimeEventsMonitor
import org.enso.languageserver.search.SuggestionsHandler
import org.enso.languageserver.session.SessionRouter
import org.enso.languageserver.text.BufferRegistry
Expand All @@ -45,10 +46,11 @@ import org.enso.librarymanager.local.DefaultLocalLibraryProvider
import org.enso.librarymanager.published.PublishedLibraryCache
import org.enso.lockmanager.server.LockManagerService
import org.enso.logger.Converter
import org.enso.logger.masking.{MaskedPath, Masking}
import org.enso.logger.masking.Masking
import org.enso.logger.JulHandler
import org.enso.logger.akka.AkkaConverter
import org.enso.polyglot.{HostAccessFactory, RuntimeOptions, RuntimeServerInfo}
import org.enso.profiling.events.NoopEventsMonitor
import org.enso.searcher.sql.{SqlDatabase, SqlSuggestionsRepo}
import org.enso.text.{ContentBasedVersioning, Sha3_224VersionCalculator}
import org.graalvm.polyglot.Engine
Expand All @@ -57,11 +59,12 @@ import org.graalvm.polyglot.io.MessageEndpoint
import org.slf4j.event.Level
import org.slf4j.LoggerFactory

import java.io.File
import java.io.{File, PrintStream}
import java.net.URI
import java.nio.charset.StandardCharsets
import java.time.Clock

import scala.concurrent.duration._
import scala.util.{Failure, Success}

/** A main module containing all components of the server.
*
Expand Down Expand Up @@ -170,19 +173,10 @@ class MainModule(serverConfig: LanguageServerConfig, logLevel: Level) {
val runtimeEventsMonitor =
languageServerConfig.profiling.runtimeEventsLogPath match {
case Some(path) =>
ApiEventsMonitor(path) match {
case Success(monitor) =>
monitor
case Failure(exception) =>
log.error(
"Failed to create runtime events monitor for [{}].",
MaskedPath(path),
exception
)
new NoopEventsMonitor
}
val out = new PrintStream(path.toFile, StandardCharsets.UTF_8)
new RuntimeEventsMonitor(out)
case None =>
new NoopEventsMonitor
new NoopEventsMonitor()
}
log.trace(
"Started runtime events monitor [{}].",
Expand Down Expand Up @@ -373,6 +367,12 @@ class MainModule(serverConfig: LanguageServerConfig, logLevel: Level) {
"project-settings-manager"
)

val profilingManager =
system.actorOf(
ProfilingManager.props(runtimeConnector, distributionManager),
"profiling-manager"
)

val libraryLocations =
LibraryLocations.resolve(
distributionManager,
Expand Down Expand Up @@ -448,6 +448,7 @@ class MainModule(serverConfig: LanguageServerConfig, logLevel: Level) {
runtimeConnector = runtimeConnector,
idlenessMonitor = idlenessMonitor,
projectSettingsManager = projectSettingsManager,
profilingManager = profilingManager,
libraryConfig = libraryConfig,
config = languageServerConfig
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.enso.languageserver.profiling

import org.enso.jsonrpc.{HasParams, HasResult, Method, Unused}

object ProfilingApi {

case object ProfilingStart extends Method("profiling/start") {

implicit val hasParams: HasParams.Aux[this.type, Unused.type] =
new HasParams[this.type] {
type Params = Unused.type
}
implicit val hasResult: HasResult.Aux[this.type, Unused.type] =
new HasResult[this.type] {
type Result = Unused.type
}
}

case object ProfilingStop extends Method("profiling/stop") {

implicit val hasParams: HasParams.Aux[this.type, Unused.type] =
new HasParams[this.type] {
type Params = Unused.type
}
implicit val hasResult: HasResult.Aux[this.type, Unused.type] =
new HasResult[this.type] {
type Result = Unused.type
}
}

}
Loading
Loading