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

feat: set limits for maximum memory use. #4147

Merged
merged 8 commits into from
Aug 19, 2021
11 changes: 11 additions & 0 deletions facades/PC/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ logger.info("PC VERSION: {}", version)
group = "org.terasology.facades"

dependencies {
implementation(group = "net.java.dev.jna", name = "jna-platform", version = "5.6.0")
implementation(group = "info.picocli", name = "picocli", version = "4.5.2")
annotationProcessor("info.picocli:picocli-codegen:4.5.2")

Expand All @@ -68,6 +69,16 @@ dependencies {

// TODO: Consider whether we can move the CR dependency back here from the engine, where it is referenced from the main menu
implementation(group = "org.terasology.crashreporter", name = "cr-terasology", version = "4.1.0")

implementation(platform("org.junit:junit-bom:5.7.1")) {
// junit-bom will set version numbers for the other org.junit dependencies.
}
implementation("org.junit.jupiter:junit-jupiter-api")
implementation("org.junit.jupiter:junit-jupiter-params")
Comment on lines +73 to +77
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME: these are supposed to be test dependencies!

testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")

testImplementation("com.google.truth:truth:1.1.2")
testImplementation("com.google.truth.extensions:truth-java8-extension:1.1.2")
}

tasks.named<JavaCompile>("compileJava") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine;

import picocli.CommandLine;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataSizeConverter implements CommandLine.ITypeConverter<Long> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME: docstring

protected final static Pattern pattern = Pattern.compile(
"(?<n>\\d+([.,]\\d*)?)" +
"\\s*" +
"(?<suffix>\\p{Alpha})?b?",
Pattern.CASE_INSENSITIVE
);

@SuppressWarnings("unused") // used by Unit.valueOf
enum Unit {
B(1),
K(1 << 10),
M(1 << 20),
G(1 << 30),
T(1L << 40);

private final BigDecimal scale;

Unit(long scale) {
this.scale = BigDecimal.valueOf(scale);
}

BigDecimal multiply(BigDecimal n) {
return n.multiply(scale);
}
}

@Override
public Long convert(String value) {
if (value == null) {
throw new CommandLine.TypeConversionException("null input");
}
Matcher match = pattern.matcher(value);
if (!match.matches()) {
throw new CommandLine.TypeConversionException("JANK");
}
BigDecimal n = new BigDecimal(match.group("n"));
if (match.group("suffix") != null) {
Unit unit = Unit.valueOf(match.group("suffix").toUpperCase(Locale.ROOT));
n = unit.multiply(n);
}
return n.toBigInteger().longValueExact();
}
}
50 changes: 48 additions & 2 deletions facades/PC/src/main/java/org/terasology/engine/Terasology.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine;

import com.sun.jna.Platform;
import com.sun.jna.platform.unix.LibC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.crashreporter.CrashReporter;
Expand All @@ -10,7 +12,6 @@
import org.terasology.engine.core.LoggingContext;
import org.terasology.engine.core.PathManager;
import org.terasology.engine.core.StandardGameStatus;
import org.terasology.engine.core.TerasologyConstants;
import org.terasology.engine.core.TerasologyEngine;
import org.terasology.engine.core.TerasologyEngineBuilder;
import org.terasology.engine.core.modes.StateLoading;
Expand Down Expand Up @@ -43,8 +44,10 @@

import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -72,12 +75,25 @@ public final class Terasology implements Callable<Integer> {
@CommandLine.Spec CommandLine.Model.CommandSpec spec;

@SuppressWarnings("unused")
@Option(names = {"--help", "-help", "/help", "-h", "/h", "-?", "/?"}, usageHelp = true, description = "show help")
@Option(names = {"--help", "-help", "/help", "-h", "/h", "-?", "/?"}, usageHelp = true, description = "Show help")
private boolean helpRequested;

@Option(names = "--headless", description = "Start headless (no graphics)")
private boolean isHeadless;

@Option(names = "--max-data-size",
description = "Set maximum process data size [Linux only]",
paramLabel = "<size>",
converter = DataSizeConverter.class
)
Long maxDataSize;

@Option(names = "--oom-score",
description = "Adjust out-of-memory score [Linux only]",
paramLabel = "<score>"
)
Integer outOfMemoryScore;

@Option(names = "--crash-report", defaultValue = "true", negatable = true, description = "Enable crash reporting")
private boolean crashReportEnabled;

Expand Down Expand Up @@ -208,6 +224,13 @@ private static void setupLogging() {
}

private void handleLaunchArguments() throws IOException {
if (outOfMemoryScore!= null) {
adjustOutOfMemoryScore(outOfMemoryScore);
}
if (maxDataSize != null) {
setMemoryLimit(maxDataSize);
}

if (homeDir != null) {
logger.info("homeDir is {}", homeDir);
PathManager.getInstance().useOverrideHomePath(homeDir);
Expand Down Expand Up @@ -301,4 +324,27 @@ private static int getLastNumber(String str) {
return Integer.parseInt(str.substring(positionOfLastDigit));
}

private static void setMemoryLimit(long bytes) {
// Memory-limiting techniques are highly platform-specific.
if (Platform.isLinux()) {
final LibC.Rlimit dataLimit = new LibC.Rlimit();
dataLimit.rlim_cur = bytes;
dataLimit.rlim_max = bytes;
// Under Linux ≥ 4.7, we can limit the maximum size of the process's data segment, which includes its
// heap. Note we cannot directly limit its resident set size, see setrlimit(3).
LibC.INSTANCE.setrlimit(LibC.RLIMIT_DATA, dataLimit);
} else {
logger.warn("--max-data-size is not supported on platform {}", Platform.RESOURCE_PREFIX);
}
}

private static void adjustOutOfMemoryScore(int adjustment) {
Path procFile = Paths.get("/proc", "self", "oom_score_adj");
try {
Files.write(procFile, String.valueOf(adjustment).getBytes(),
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
logger.error("Failed to adjust out-of-memory score.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DataSizeConverterTest {

@ParameterizedTest
@CsvSource({
"1, 1",
"16, 16b",
"126, 0.1234k",
"512, 0.5 k",
"11264, 11k",
"11264, 11Kb",
"44040192, 42 m",
"268435456, 256M",
"2684354560, 2.5g",
"8589934592, 8G",
})
void testValidInputs(long expected, String input) {
assertEquals(expected, new DataSizeConverter().convert(input));
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"b", "mb", " mb", " ", "🆗", "-5m", "null", "NaN"})
void testBadInputs(String input) {
DataSizeConverter converter = new DataSizeConverter();
assertThrows(CommandLine.TypeConversionException.class, () -> converter.convert(input));
}
}