Skip to content

Commit

Permalink
Separating instrument related files into runtime-instrument-common pr…
Browse files Browse the repository at this point in the history
…oject (#6992)
  • Loading branch information
JaroslavTulach authored Jun 8, 2023
1 parent 876ecfc commit e6652b8
Show file tree
Hide file tree
Showing 84 changed files with 153 additions and 102 deletions.
28 changes: 28 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ lazy val enso = (project in file("."))
launcher,
downloader,
`runtime-language-epb`,
`runtime-instrument-common`,
`runtime-instrument-id-execution`,
`runtime-instrument-repl-debugger`,
`runtime-instrument-runtime-server`,
Expand Down Expand Up @@ -1413,13 +1414,37 @@ lazy val runtime = (project in file("engine/runtime"))
.dependsOn(`syntax-rust-definition`)
.dependsOn(testkit % Test)

lazy val `runtime-instrument-common` =
(project in file("engine/runtime-instrument-common"))
.configs(Benchmark)
.settings(
frgaalJavaCompilerSetting,
inConfig(Compile)(truffleRunOptionsSettings),
inConfig(Benchmark)(Defaults.testSettings),
instrumentationSettings,
Test / javaOptions ++= Seq(
"-Dgraalvm.locatorDisabled=true",
s"--upgrade-module-path=${file("engine/runtime/build-cache/truffle-api.jar").absolutePath}"
),
bench := (Benchmark / test).tag(Exclusive).value,
Benchmark / parallelExecution := false,
Test / fork := true,
Test / envVars ++= distributionEnvironmentOverrides ++ Map(
"ENSO_TEST_DISABLE_IR_CACHE" -> "false"
)
)
.dependsOn(
runtime % "compile->compile;test->test;runtime->runtime;bench->bench"
)

lazy val `runtime-instrument-id-execution` =
(project in file("engine/runtime-instrument-id-execution"))
.settings(
inConfig(Compile)(truffleRunOptionsSettings),
instrumentationSettings
)
.dependsOn(runtime)
.dependsOn(`runtime-instrument-common`)

lazy val `runtime-instrument-repl-debugger` =
(project in file("engine/runtime-instrument-repl-debugger"))
Expand All @@ -1428,6 +1453,7 @@ lazy val `runtime-instrument-repl-debugger` =
instrumentationSettings
)
.dependsOn(runtime)
.dependsOn(`runtime-instrument-common`)

lazy val `runtime-instrument-runtime-server` =
(project in file("engine/runtime-instrument-runtime-server"))
Expand All @@ -1436,6 +1462,7 @@ lazy val `runtime-instrument-runtime-server` =
instrumentationSettings
)
.dependsOn(runtime)
.dependsOn(`runtime-instrument-common`)

lazy val `runtime-with-instruments` =
(project in file("engine/runtime-with-instruments"))
Expand Down Expand Up @@ -1481,6 +1508,7 @@ lazy val `runtime-with-instruments` =
}
)
.dependsOn(runtime % "compile->compile;test->test;runtime->runtime")
.dependsOn(`runtime-instrument-common`)
.dependsOn(`runtime-instrument-id-execution`)
.dependsOn(`runtime-instrument-repl-debugger`)
.dependsOn(`runtime-instrument-runtime-server`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
import com.oracle.truffle.api.instrumentation.EventBinding;
import com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory;
import com.oracle.truffle.api.nodes.RootNode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
import org.enso.interpreter.instrument.execution.Timer;
import org.enso.interpreter.instrument.profiling.ProfilingInfo;
import org.enso.interpreter.node.EnsoRootNode;
import org.enso.interpreter.node.MethodRootNode;
import org.enso.interpreter.node.callable.FunctionCallInstrumentationNode;
import org.enso.interpreter.node.expression.atom.QualifiedAccessorNode;
import org.enso.interpreter.runtime.Module;
import org.enso.interpreter.runtime.callable.argument.ArgumentDefinition;
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.callable.function.FunctionSchema;
import org.enso.interpreter.runtime.data.Type;
import org.enso.logger.masking.MaskedString;
import org.enso.pkg.QualifiedName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.enso.interpreter.instrument.NotificationHandler;
import org.enso.interpreter.instrument.RuntimeCache;
import org.enso.interpreter.instrument.UpdatesSynchronizationState;
import org.enso.interpreter.instrument.execution.Timer;
import org.enso.interpreter.instrument.Timer;
import org.enso.interpreter.node.callable.FunctionCallInstrumentationNode;
import org.enso.interpreter.node.expression.builtin.text.util.TypeToDisplayTextNodeGen;
import org.enso.interpreter.runtime.EnsoContext;
Expand Down Expand Up @@ -118,7 +118,7 @@ public FunctionCallInstrumentationNode.FunctionCall prepareFunctionCall(
}

public void initializeLanguageServerConnection(Endpoint endpoint) {
var notificationHandler = new NotificationHandler.InteractiveMode(endpoint);
var notificationHandler = new InteractiveMode(endpoint);
notificationForwarder.addListener(notificationHandler);

if (connectedLockManager != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ import org.enso.text.editing.{IndexedSource, TextEditor}
import scala.collection.mutable
import scala.util.Using

/** Simple editing change description.
*
* @param ir the current literal
* @param edit the editor change
* @param newIr the new literal
*/
case class SimpleUpdate(
ir: IR.Literal,
edit: TextEdit,
newIr: IR.Literal
)

/** The changeset of a module containing the computed list of invalidated
* expressions.
*
Expand Down Expand Up @@ -111,9 +99,9 @@ final class ChangesetBuilder[A: TextEditor: IndexedSource](

oldIr match {
case node: IR.Literal.Number =>
newIR(pending).map(ir => SimpleUpdate(node, pending.edit, ir))
newIR(pending).map(ir => new SimpleUpdate(node, pending.edit, ir))
case node: IR.Literal.Text =>
newIR(pending).map(ir => SimpleUpdate(node, pending.edit, ir))
newIR(pending).map(ir => new SimpleUpdate(node, pending.edit, ir))
case _ => None
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.enso.interpreter.service

import com.typesafe.scalalogging.Logger
import org.enso.cli.task.{ProgressNotification, TaskProgress}
import org.enso.distribution.ProgressAndLockNotificationForwarder
import org.enso.editions.{LibraryName, LibraryVersion}
import org.enso.pkg.QualifiedName
import org.enso.polyglot.runtime.Runtime.{Api, ApiResponse}
import org.enso.interpreter.instrument.NotificationHandler
import org.enso.interpreter.instrument.ProgressNotificationTranslator
import org.enso.interpreter.instrument.Endpoint

import java.nio.file.Path

/** A [[NotificationHandler]] for interactive mode, which forwards the
* notifications to the Language Server, which then should forward them to
* the IDE.
*/
private class InteractiveMode(endpoint: Endpoint)
extends ProgressAndLockNotificationForwarder
with NotificationHandler {
private val logger = Logger[InteractiveMode]

private def sendMessage(message: ApiResponse): Unit = {
val response = Api.Response(None, message)
endpoint.sendToClient(response)
}

/** @inheritdoc */
override def addedLibrary(
libraryName: LibraryName,
libraryVersion: LibraryVersion,
location: Path
): Unit = {
sendMessage(
Api.LibraryLoaded(
namespace = libraryName.namespace,
name = libraryName.name,
version = libraryVersion.toString,
location = location.toFile
)
)
endpoint.sendToSelf(
Api.Request(
Api.DeserializeLibrarySuggestions(libraryName)
)
)
}

/** @inheritdoc */
override def serializeModule(module: QualifiedName): Unit =
endpoint.sendToSelf(
Api.Request(Api.SerializeModule(module))
)

/** @inheritdoc */
override def trackProgress(message: String, task: TaskProgress[_]): Unit = {
logger.info(message)
super.trackProgress(message, task)
}

override def sendProgressNotification(
notification: ProgressNotification
): Unit = sendMessage(
ProgressNotificationTranslator.translate(
"compiler/downloadingDependencies",
notification
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.enso.interpreter.instrument.execution.Timer;
import org.enso.interpreter.instrument.Timer;
import org.enso.interpreter.instrument.profiling.ExecutionTime;
import org.enso.interpreter.instrument.profiling.ProfilingInfo;
import org.enso.interpreter.node.ExpressionNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Optional;
import org.enso.distribution.locking.LockManager;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.service.ExecutionService;
import org.enso.lockmanager.client.ConnectedLockManager;
import org.enso.polyglot.RuntimeServerInfo;
import org.graalvm.options.OptionDescriptor;
import org.graalvm.options.OptionDescriptors;
Expand Down Expand Up @@ -61,7 +65,23 @@ public void onLanguageContextInitialized(TruffleContext context, LanguageInfo la
Object token = context.enter(null);
ExecutionService service;
try {
service = instrument.env.lookup(language, ExecutionService.class);
var ctx = EnsoContext.get(null);
var idExecutionInstrument =
Optional.ofNullable(
instrument.env.getInstruments().get(IdExecutionService.INSTRUMENT_ID))
.map(
idValueListenerInstrument ->
instrument.env.lookup(
idValueListenerInstrument, IdExecutionService.class));

var timer = instrument.env.lookup(language, Timer.class);
var notificationHandler =
instrument.env.lookup(language, NotificationHandler.Forwarder.class);
var connectedLockManager = instrument.env.lookup(language, LockManager.class) instanceof ConnectedLockManager connected ? connected : null;
service =
new ExecutionService(
ctx, idExecutionInstrument, notificationHandler, connectedLockManager, timer);

} finally {
context.leave(null, token);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.enso.compiler.context;

import org.enso.compiler.core.IR;
import org.enso.text.editing.model.TextEdit;

/**
* Simple editing change description.
*
* @param ir the current literal
* @param edit the editor change
* @param newIr the new literal
*/
public record SimpleUpdate(
IR.Literal ir,
TextEdit edit,
IR.Literal newIr
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.PrintStream;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.enso.compiler.Compiler;
Expand All @@ -28,10 +27,10 @@
import org.enso.distribution.locking.LockManager;
import org.enso.distribution.locking.ThreadSafeFileLockManager;
import org.enso.interpreter.epb.EpbLanguage;
import org.enso.interpreter.instrument.IdExecutionService;
import org.enso.interpreter.instrument.NotificationHandler;
import org.enso.interpreter.instrument.NotificationHandler.Forwarder;
import org.enso.interpreter.instrument.NotificationHandler.TextMode$;
import org.enso.interpreter.instrument.execution.Timer;
import org.enso.interpreter.instrument.Timer;
import org.enso.interpreter.node.EnsoRootNode;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.node.ProgramRootNode;
Expand All @@ -40,7 +39,6 @@
import org.enso.interpreter.runtime.tag.AvoidIdInstrumentationTag;
import org.enso.interpreter.runtime.tag.IdentifiedTag;
import org.enso.interpreter.runtime.tag.Patchable;
import org.enso.interpreter.service.ExecutionService;
import org.enso.interpreter.util.FileDetector;
import org.enso.lockmanager.client.ConnectedLockManager;
import org.enso.logger.masking.MaskingFactory;
Expand Down Expand Up @@ -72,7 +70,8 @@
contextPolicy = TruffleLanguage.ContextPolicy.SHARED,
dependentLanguages = {EpbLanguage.ID},
fileTypeDetectors = FileDetector.class,
services = ExecutionService.class)
services= { Timer.class, NotificationHandler.Forwarder.class, LockManager.class }
)
@ProvidedTags({
DebuggerTags.AlwaysHalt.class,
StandardTags.CallTag.class,
Expand All @@ -86,7 +85,6 @@
Patchable.Tag.class
})
public final class EnsoLanguage extends TruffleLanguage<EnsoContext> {
private Optional<IdExecutionService> idExecutionInstrument = Optional.empty();
private static final LanguageReference<EnsoLanguage> REFERENCE =
LanguageReference.create(EnsoLanguage.class);

Expand All @@ -113,6 +111,7 @@ protected EnsoContext createContext(Env env) {
if (isTextMode) {
notificationHandler.addListener(TextMode$.MODULE$);
}
env.registerService(notificationHandler);

TruffleLogger logger = env.getLogger(EnsoLanguage.class);

Expand All @@ -127,26 +126,22 @@ protected EnsoContext createContext(Env env) {
"Detected interactive mode, will try to connect to a lock manager managed by it.");
connectedLockManager = new ConnectedLockManager();
lockManager = connectedLockManager;
env.registerService(connectedLockManager);
} else {
logger.finest("Detected text mode, using a standalone lock manager.");
lockManager = new ThreadSafeFileLockManager(distributionManager.paths().locks());
env.registerService(lockManager);
}


boolean isExecutionTimerEnabled =
env.getOptions().get(RuntimeOptions.ENABLE_EXECUTION_TIMER_KEY);
Timer timer = isExecutionTimerEnabled ? new Timer.Nanosecond() : new Timer.Disabled();
env.registerService(timer);

EnsoContext context =
new EnsoContext(
this, getLanguageHome(), env, notificationHandler, lockManager, distributionManager);
idExecutionInstrument =
Optional.ofNullable(env.getInstruments().get(IdExecutionService.INSTRUMENT_ID))
.map(
idValueListenerInstrument ->
env.lookup(idValueListenerInstrument, IdExecutionService.class));
env.registerService(
new ExecutionService(
context, idExecutionInstrument, notificationHandler, connectedLockManager, timer));

return context;
}
Expand Down Expand Up @@ -349,11 +344,6 @@ protected Object getScope(EnsoContext context) {
return context.getTopScope();
}

/** @return a reference to the execution instrument */
public Optional<IdExecutionService> getIdExecutionService() {
return idExecutionInstrument;
}

/** Conversions of primitive values */
protected Object getLanguageView(EnsoContext context, Object value) {
if (value instanceof Boolean b ) {
Expand Down
Loading

0 comments on commit e6652b8

Please sign in to comment.