Skip to content

Commit

Permalink
Move log about jemalloc from startup script to Java
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Aug 18, 2022
1 parent 1678d60 commit 8f212e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
13 changes: 13 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
import org.hyperledger.besu.util.number.Fraction;
import org.hyperledger.besu.util.number.Percentage;
import org.hyperledger.besu.util.number.PositiveNumber;
import org.hyperledger.besu.util.platform.PlatformDetector;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -1391,6 +1392,7 @@ public void parse(
handleUnstableOptions();
preparePlugins();
parse(resultHandler, exceptionHandler, args);
detectJemalloc();
}

@Override
Expand Down Expand Up @@ -1492,6 +1494,17 @@ private void registerConverters() {
commandLine.registerConverter(MetricCategory.class, metricCategoryConverter);
}

private void detectJemalloc() {
// jemalloc is only supported on Linux at the moment
if (PlatformDetector.getOSType().equals("linux")) {
Optional.ofNullable(environment.get("BESU_USING_JEMALLOC"))
.ifPresentOrElse(
present -> logger.info("Using jemalloc"),
() ->
logger.info("jemalloc not found, you could improve memory usage installing it"));
}
}

private void handleStableOptions() {
commandLine.addMixin("Ethstats", ethstatsOptions);
commandLine.addMixin("Private key file", nodePrivateKeyFileOption);
Expand Down
3 changes: 2 additions & 1 deletion besu/src/main/scripts/unixStartScript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,16 @@ APP_ARGS=`save "\$@"`
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- \$DEFAULT_JVM_OPTS \$JAVA_OPTS \$${optsEnvironmentVar} <% if ( appNameSystemProperty ) { %>"\"-D${appNameSystemProperty}=\$APP_BASE_NAME\"" <% } %>-classpath "\"\$CLASSPATH\"" <% if ( mainClassName.startsWith('--module ') ) { %>--module-path "\"\$MODULE_PATH\"" <% } %>${mainClassName} "\$APP_ARGS"

unset BESU_USING_JEMALLOC
if [ "\$darwin" = "false" -a "\$msys" = "false" ]; then
# check if jemalloc is available
TEST_JEMALLOC=\$(LD_PRELOAD=libjemalloc.so sh -c true 2>&1)

# if jemalloc is available the output is empty, otherwise the output has an error line
if [ -z "\$TEST_JEMALLOC" ]; then
export LD_PRELOAD=libjemalloc.so
export BESU_USING_JEMALLOC=true
else
echo "INFO: jemalloc not found, you could improve memory usage installing it."
# jemalloc not available, as fallback limit malloc to 2 arenas
export MALLOC_ARENA_MAX=2
fi
Expand Down
17 changes: 17 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.hyperledger.besu.cli.config.NetworkName.CLASSIC;
import static org.hyperledger.besu.cli.config.NetworkName.DEV;
Expand Down Expand Up @@ -94,6 +95,7 @@
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest;
import org.hyperledger.besu.util.number.Fraction;
import org.hyperledger.besu.util.number.Percentage;
import org.hyperledger.besu.util.platform.PlatformDetector;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -5315,4 +5317,19 @@ public void pkiBlockCreationFullConfig() throws Exception {
assertThat(pkiKeyStoreConfig.getTrustStorePassword()).isEqualTo("foo");
assertThat(pkiKeyStoreConfig.getCrlFilePath()).hasValue(Path.of("/tmp/crl"));
}

@Test
public void logsUsingJemallocWhenEnvVarPresent() {
assumeThat(PlatformDetector.getOSType(), is("linux"));
setEnvironmentVariable("BESU_USING_JEMALLOC", "true");
parseCommand();
verify(mockLogger).info("Using jemalloc");
}

@Test
public void logsSuggestInstallingJemallocWhenEnvVarNotPresent() {
assumeThat(PlatformDetector.getOSType(), is("linux"));
parseCommand();
verify(mockLogger).info("jemalloc not found, you could improve memory usage installing it");
}
}

0 comments on commit 8f212e1

Please sign in to comment.