Skip to content

Commit

Permalink
wasm gc: support setting min/max size of linear memory, available for…
Browse files Browse the repository at this point in the history
… direct buffers, in Maven and Gradle
  • Loading branch information
konsoletyper committed Feb 5, 2025
1 parent aefc9df commit b3af74d
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public class TeaVMTool {
private Set<File> generatedFiles = new HashSet<>();
private int minHeapSize = 4 * (1 << 20);
private int maxHeapSize = 128 * (1 << 20);
private int minDirectBuffersSize = 2 * (1 << 20);
private int maxDirectBuffersSize = 32 * (1 << 20);
private ReferenceCache referenceCache;
private boolean heapDump;
private boolean shortFileNames;
Expand Down Expand Up @@ -268,6 +270,14 @@ public void setMaxHeapSize(int maxHeapSize) {
this.maxHeapSize = maxHeapSize;
}

public void setMinDirectBuffersSize(int minDirectBuffersSize) {
this.minDirectBuffersSize = minDirectBuffersSize;
}

public void setMaxDirectBuffersSize(int maxDirectBuffersSize) {
this.maxDirectBuffersSize = maxDirectBuffersSize;
}

public ClassLoader getClassLoader() {
return classLoader;
}
Expand Down Expand Up @@ -406,6 +416,8 @@ private WasmGCTarget prepareWebAssemblyGCTarget() {
target.setDebugInfo(debugInformationGenerated);
target.setDebugInfoLevel(debugInformationGenerated ? WasmDebugInfoLevel.FULL : wasmDebugInfoLevel);
target.setDebugInfoLocation(wasmDebugInfoLocation);
target.setBufferHeapMinSize(minDirectBuffersSize);
target.setBufferHeapMaxSize(maxDirectBuffersSize);
if (sourceMapsFileGenerated) {
wasmSourceMapWriter = new SourceMapBuilder();
target.setSourceMapBuilder(wasmSourceMapWriter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public interface BuildStrategy {

void setMaxHeapSize(int maxHeapSize);

void setMinDirectBuffersSize(int minDirectBuffersSize);

void setMaxDirectBuffersSize(int maxDirectBuffersSize);

void setHeapDump(boolean heapDump);

void setShortFileNames(boolean shortFileNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class InProcessBuildStrategy implements BuildStrategy {
private WasmDebugInfoLocation wasmDebugInfoLocation;
private int minHeapSize = 4 * 1024 * 1024;
private int maxHeapSize = 128 * 1024 * 1024;
private int minDirectBuffersSize = 2 * 1024 * 1024;
private int maxDirectBuffersSize = 32 * 1024 * 1024;
private final List<SourceFileProvider> sourceFileProviders = new ArrayList<>();
private boolean heapDump;
private TeaVMProgressListener progressListener;
Expand Down Expand Up @@ -243,6 +245,16 @@ public void setMaxHeapSize(int maxHeapSize) {
this.maxHeapSize = maxHeapSize;
}

@Override
public void setMinDirectBuffersSize(int minDirectBuffersSize) {
this.minDirectBuffersSize = minDirectBuffersSize;
}

@Override
public void setMaxDirectBuffersSize(int maxDirectBuffersSize) {
this.maxDirectBuffersSize = maxDirectBuffersSize;
}

@Override
public void setHeapDump(boolean heapDump) {
this.heapDump = heapDump;
Expand Down Expand Up @@ -291,6 +303,8 @@ public BuildResult build() throws BuildException {
tool.setWasmDebugInfoLocation(wasmDebugInfoLocation);
tool.setMinHeapSize(minHeapSize);
tool.setMaxHeapSize(maxHeapSize);
tool.setMinDirectBuffersSize(minDirectBuffersSize);
tool.setMaxDirectBuffersSize(maxDirectBuffersSize);
tool.setHeapDump(heapDump);
tool.setShortFileNames(shortFileNames);
tool.setAssertionsRemoved(assertionsRemoved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ public void setMaxHeapSize(int maxHeapSize) {
request.maxHeapSize = maxHeapSize;
}

@Override
public void setMinDirectBuffersSize(int minDirectBuffersSize) {
request.minDirectBuffersSize = minDirectBuffersSize;
}

@Override
public void setMaxDirectBuffersSize(int maxDirectBuffersSize) {
request.maxDirectBuffersSize = maxDirectBuffersSize;
}

@Override
public void setHeapDump(boolean heapDump) {
request.heapDump = heapDump;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public RemoteBuildResponse build(RemoteBuildRequest request, RemoteBuildCallback
tool.setWasmDebugInfoLevel(request.wasmDebugInfoLevel);
tool.setMinHeapSize(request.minHeapSize);
tool.setMaxHeapSize(request.maxHeapSize);
tool.setMinDirectBuffersSize(request.minDirectBuffersSize);
tool.setMaxDirectBuffersSize(request.maxDirectBuffersSize);
tool.setHeapDump(request.heapDump);
tool.setShortFileNames(request.shortFileNames);
tool.setAssertionsRemoved(request.assertionsRemoved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class RemoteBuildRequest implements Serializable {
public WasmDebugInfoLevel wasmDebugInfoLevel;
public int minHeapSize;
public int maxHeapSize;
public int minDirectBuffersSize;
public int maxDirectBuffersSize;
public boolean heapDump;
public boolean shortFileNames;
public boolean assertionsRemoved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ private void setupWasmGCDefaults() {
.orElse(SourceFilePolicy.LINK_LOCAL_FILES));
wasmGC.getModularRuntime().convention(property("wasm-gc.modularRuntime")
.map(Boolean::parseBoolean).orElse(false));
wasmGC.getMinDirectBuffersSize().convention(property("wasm-gc.minDirectBuffersSize")
.map(Integer::parseInt)
.orElse(2));
wasmGC.getMaxDirectBuffersSize().convention(property("wasm-gc.maxDirectBuffersSize")
.map(Integer::parseInt)
.orElse(32));
}

private void setupWasiDefaults() {
Expand Down
2 changes: 2 additions & 0 deletions tools/gradle/src/main/java/org/teavm/gradle/TeaVMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ private void registerWasmGCTask(Project project, Configuration configuration) {
task.getStrict().convention(wasmGC.getStrict());
task.getSourceMap().convention(wasmGC.getSourceMap());
task.getSourceFilePolicy().convention(wasmGC.getSourceFilePolicy());
task.getMinDirectBuffersSize().convention(wasmGC.getMinDirectBuffersSize());
task.getMaxDirectBuffersSize().convention(wasmGC.getMaxDirectBuffersSize());
setupSources(task.getSourceFiles(), project);
buildTask.dependsOn(task);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public interface TeaVMWasmGCConfiguration extends TeaVMCommonConfiguration, TeaV
Property<SourceFilePolicy> getSourceFilePolicy();

Property<Boolean> getModularRuntime();

Property<Integer> getMinDirectBuffersSize();

Property<Integer> getMaxDirectBuffersSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public GenerateWasmGCTask() {
getDebugInfoLocation().convention(WasmDebugInfoLocation.EXTERNAL);
getSourceMap().convention(false);
getSourceFilePolicy().convention(SourceFilePolicy.LINK_LOCAL_FILES);
getMinDirectBuffersSize().convention(2);
getMaxDirectBuffersSize().convention(32);
}

@Input
Expand All @@ -58,12 +60,20 @@ public GenerateWasmGCTask() {
@Optional
public abstract Property<SourceFilePolicy> getSourceFilePolicy();

@Input
public abstract Property<Integer> getMinDirectBuffersSize();

@Input
public abstract Property<Integer> getMaxDirectBuffersSize();

@Override
protected void setupBuilder(BuildStrategy builder) {
builder.setStrict(getStrict().get());
builder.setObfuscated(getObfuscated().get());
builder.setDebugInformationGenerated(getDebugInformation().get());
builder.setSourceMapsFileGenerated(getSourceMap().get());
builder.setMinDirectBuffersSize(getMinDirectBuffersSize().get() * 1024 * 1024);
builder.setMaxDirectBuffersSize(getMaxDirectBuffersSize().get() * 1024 * 1024);
switch (getDebugInfoLevel().get()) {
case FULL:
builder.setWasmDebugInfoLevel(org.teavm.backend.wasm.WasmDebugInfoLevel.FULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ public class TeaVMCompileMojo extends AbstractMojo {
@Parameter(property = "teavm.maxHeapSize", defaultValue = "128")
private int maxHeapSize;

@Parameter(property = "teavm.minDirectBuffersSize", defaultValue = "2")
private int minDirectBuffersSize;

@Parameter(property = "teavm.maxDirectBuffersSize", defaultValue = "32")
private int maxDirectBuffersSize;

@Parameter(property = "teavm.outOfProcess", defaultValue = "false")
private boolean outOfProcess;

Expand Down Expand Up @@ -200,6 +206,8 @@ private void setupBuilder(BuildStrategy builder) throws MojoExecutionException {
: TeaVMSourceFilePolicy.DO_NOTHING);
builder.setMinHeapSize(minHeapSize * 1024 * 1024);
builder.setMaxHeapSize(maxHeapSize * 1024 * 1024);
builder.setMinDirectBuffersSize(minDirectBuffersSize * 1024 * 1024);
builder.setMaxDirectBuffersSize(maxDirectBuffersSize * 1024 * 1024);
builder.setShortFileNames(shortFileNames);
builder.setAssertionsRemoved(assertionsRemoved);
} catch (RuntimeException e) {
Expand Down

0 comments on commit b3af74d

Please sign in to comment.