Engine
public final class Engine implements AutoCloseable- Inheritance
ObjectEngine
- Implements
An execution engine for Graal guest languages that allows to inspect the the installed guest languages, instruments and their available options.
By default every context creates its own engine instance implicitly when instantiated. Multiple contexts can use an explicit engine when using a context builder. If contexts share the same engine instance then they share instruments and their configuration.
It can be useful to create an engine instance without a context to only access meta-data for installed languages, instruments and their available options.
Methods20
Map<String, Language> getLanguages()
Map<String, Instrument> getInstruments()
getInstruments
since 19.0public Map< String, Instrument> getInstruments()Gets all installed instruments of this engine. An instrument alters and/or monitors the execution of guest language source code. Common examples for instruments are debuggers, profilers, or monitoring tools. Instruments are enabled via options passed to the engine when the engine or context is constructed.
Returns
Map<String, Instrument> - No description.
OptionDescriptors getOptions()
getOptions
since 19.0public OptionDescriptors getOptions()Returns all options available for the engine. The engine offers options with the following groups:
engine: options to configure the behavior of this engine.
compiler: options to configure the behavior of the compiler.
The language and instrument specific options need to be retrieved using Instrument.getOptions() or Language.getOptions().
Returns
OptionDescriptors- No description.
String getVersion()
getVersion
since 19.0public String getVersion()Gets the version string of the engine in an unspecified format.
Returns
String- No description.
void close(boolean)
close
since 19.0public void close(boolean cancelIfExecuting)Closes this engine and frees up allocated native resources. If there are still open context instances that were created using this engine and they are currently not being executed then they will be closed automatically. If an attempt to close an engine was successful then consecutive calls to close have no effect. If a context is cancelled then the currently executing thread will throw a PolyglotException. The exception indicates that it was cancelled.
Parameters
cancelIfExecutingboolean- if
truethen currently executing contexts will be cancelled, else anIllegalStateExceptionis thrown.
void close()
close
since 19.0public void close()Closes this engine and frees up allocated native resources. If there are still open context instances that were created using this engine and they are currently not being executed then they will be closed automatically. If an attempt to close the engine was successful then consecutive calls to close have no effect.
Throws
IllegalStateExceptionunchecked- if there currently executing open context instances.
Overrides close().
See also
boolean storeCache(Path)
storeCache
since 25.0public boolean storeCache(Path targetFile) throws UnsupportedOperationExceptionStores the auxiliary engine cache to the targetFile without cancellation.
Parameters
targetFilePath- No description.
Returns
boolean- No description.
Throws
UnsupportedOperationException- if this engine or the host virtual machine does not support storing the cache.
boolean storeCache(Path, WordPointer)
storeCache
since 25.0public boolean storeCache(
Path targetFile,
WordPointer cancelledWord
) throws CancellationException, UnsupportedOperationExceptionStores the auxiliary engine cache to the targetFile. If it already exists, the file will be overwritten. The option engine.CacheStoreEnabled must be set to true to use this feature. Stored caches may be loaded by specifying the path using the engine.CacheLoad option.
Note that this feature is experimental and only supported on native-image hosts built with auxiliary engine caching enabled.
Basic Usage:
// Store the engine cache into a file
Path store = Files.createTempFile("cache", "engine");
try (Engine e = Engine.newBuilder().allowExperimentalOptions(true).option("engine.CacheStoreEnabled", "true").build()) {
try (Context c = Context.newBuilder().engine(e).build()) {
// Evaluate sources, run application
}
e.storeCache(store);
}
// Load the engine cache from a file
try (Engine e = Engine.newBuilder().allowExperimentalOptions(true).option("engine.CacheLoad", store.toAbsolutePath().toString()).build()) {
try (Context c = Context.newBuilder().engine(e).build()) {
// The context should be able to use
// the existing code cache.
}
}See the documentation on auxiliary engine caching for further details.
Parameters
targetFilePath- the file to which the cache is stored
cancelledWordWordPointer- a native pointer; if set to a non-zero value, the operation is cancelled. Allows cancellation of the cache store operation through a
cancelledcontrol word. The memoryaddresspointing to the control word is polled periodically during storage without guaranteed frequency and may be delayed by safepoints such as garbage collection. A control word value of zero must be maintained for the duration of the operation. If a non-zero value is detected, the operation will be cancelled. A non-null provided pointer must remain accessible during the entire operation. Providing an invalid or inaccessible pointer may result in a VM crash.
Returns
booleantrueif the file was written; otherwise,false
Throws
CancellationException- if the storeCache operation was cancelled via the
cancelledpointer UnsupportedOperationException- if this engine or host virtual machine does not support cache storage
ByteBuffer persistCache(Engine.CancellationCallback)
persistCache
since 25.1public ByteBuffer persistCache(Engine.CancellationCallback callback)Persists the auxiliary engine cache into an in-memory buffer. The option engine.CacheStoreEnabled must be set to true to use this feature. The returned buffer contains the cache image bytes and can be written to a file by the caller.
Note that this feature is experimental and only supported on native-image hosts built with auxiliary engine caching enabled.
If callback is non-null, it is polled periodically to request cancellation. Cancellation support during the low-level auxiliary image persistence phase is only available on hosts that support it; otherwise callback cancellation is limited to the compilation-preparation phase. Implementations that cannot support the callback semantics may throw UnsupportedOperationException.
Parameters
callbackEngine.CancellationCallback- callback used to request cancellation, or
nullfor no cancellation
Returns
ByteBuffer- a buffer containing the persisted cache image, or
nullif no image was produced, for example because the configured maximum image size was exceeded
Throws
CancellationExceptionunchecked- if the persist operation was cancelled via the callback
UnsupportedOperationExceptionunchecked- if this engine or host virtual machine does not support in-memory cache persistence
String getImplementationName()
getImplementationName
since 19.0public String getImplementationName()Gets a human-readable name of the polyglot implementation (for example, "Default Truffle Engine" or "Graal Truffle Engine"). The returned value may change without notice. The value is never null.
Returns
String- No description.
String toString()Described in toString().
static Engine create()
create
staticsince 19.0public static Engine create()Creates a new engine instance with default configuration. This method is a shortcut for newBuilder().build().
Returns
Engine- No description.
static Engine create(String...)
create
staticsince 21.3Creates a new engine instance with default configuration with a set of permitted languages. This method is a shortcut for newBuilder(permittedLanguages).build().
Parameters
permittedLanguagesString...- No description.
Returns
Engine- No description.
static Engine.Builder newBuilder()
newBuilder
staticsince 21.3public static Engine.Builder newBuilder()Creates a new engine builder that allows to configure an engine instance. This method is equivalent to calling newBuilder(String...) with an empty set of permitted languages.
Returns
Engine.Builder- No description.
static Engine.Builder newBuilder(String...)
newBuilder
staticsince 21.3public static Engine.Builder newBuilder(String... permittedLanguages)Creates a new engine builder that allows to configure an engine instance.
Parameters
permittedLanguagesString...- names of languages permitted in the engine. If no languages are provided, then all installed languages will be permitted. All contexts created with this engine will inherit the set of permitted languages.
Returns
Engine.Builder- a builder that can create a context
static Engine.Builder newBuilder(Engine.Preinitialized)
newBuilder
staticsince 25.5public static Engine.Builder newBuilder(Engine.Preinitialized state)Creates a new engine builder that consumes the given prepared state when Builder.build() is called. The builder inherits the permitted languages and sandbox policy used to prepare the state. A prepared state can create only one engine. Calling build() consumes the state even if building the engine fails.
Runtime options must be compatible with the prepared state. The options engine.SpecializationStatistics and engine.AssertProbes, and the options of retained instruments, must match the preparation configuration. For retained instruments, this includes which options were explicitly set. The experimental-option policy uses the consuming builder's configuration.
Parameters
stateEngine.Preinitialized- the prepared state to consume
Returns
Engine.Builder- a builder that can create an engine
Throws
NullPointerExceptionunchecked- if
stateisnull
static Path findHome()
findHome
staticsince 19.0public static Path findHome()Finds the GraalVM home folder. This is equivalent to HomeFinder.getHomeFolder() which should be preferred.
Returns
Path- the path to a folder containing the GraalVM or
nullif it cannot be found
Set<Source> getCachedSources()
getCachedSources
since 20.3Returns the sources previously cached by this engine. Only sources may be returned that allow caching (default on). The source cache of the engine is using weak references to refer to the source objects. Calling this method will result in a strong reference to all cached sources of this engine until the returned set is no longer referenced. This method is useful to find out which sources very already evaluated by this engine. This method only returns sources that were evaluated using Context.eval(Source). Sources evaluated by the guest application will not be returned. The return set is never null and not modifiable.
static boolean copyResources(Path, String...)
copyResources
staticsince 23.1public static boolean copyResources(Path targetFolder, String... components) throws IOExceptionUnpacks the language or instrument internal resources specified by the components into the targetFolder directory.
During execution, the internal resource cache location can be overridden by the following system properties:
polyglot.engine.resourcePath: Sets the cache location to the given path. The expected folder structure is the structure generated by this method.polyglot.engine.resourcePath.<component>: Retains the default cache folder but overrides caches for the specified component with the given path. The expected folder structure istargetFolder/<component>.polyglot.engine.resourcePath.<component>.<resource-id>: Retains the default cache folder but overrides caches for the specified resource within the component with the given path. The expected folder structure istargetFolder/<component>/<resource-id>.
Parameters
Returns
booleantrueif at least one of thecomponentshas associated resources that were unpacked intotargetFolder
Throws
IllegalArgumentExceptionunchecked- if the
componentslist contains an id of a language or instrument that is not installed IOException- in case of an IO error
static boolean supportsCompilation()
supportsCompilation
staticsince 25.1public static boolean supportsCompilation()Returns whether the current runtime supports optimizing guest language execution.
When this method returns false, guest code runs on the fallback runtime unless a polyglot isolate is used. Embedders can use this method to decide whether to enable isolate execution, for example with Context.newBuilder().spawnIsolate(!Engine.supportsCompilation()).
Returns
booleantrueif guest language execution can be optimized at run time;falseif only the fallback runtime is available
Nested types
public final class .BuilderNo description.
public interface .CancellationCallbackA callback that is invoked repeatedly while persisting the auxiliary engine cache.
public static final class .PreinitializedPrepared engine state that can be consumed by one
engine build.