-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d11ca3
chore: upgrade JNA to 4.5.2
keturn d06441b
feature: limit max data segment size with setrlimit
keturn 4fcf34d
Merge remote-tracking branch 'origin/develop' into feature/memory-lim…
keturn a113145
feat(facade): add `--max-data-size` command line option
keturn 1a8089f
feat(facade): add `--oom-score` command line option
keturn 185a64e
chore(facade): capitalize for consistency
keturn 8d647f7
Merge branch 'develop' into feature/memory-limit-linux
DarkWeird b9dbc2e
Merge branch 'develop' into feature/memory-limit-linux
jdrueckert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
facades/PC/src/main/java/org/terasology/engine/DataSizeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
facades/PC/src/test/java/org/terasology/engine/DataSizeConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!