Skip to content

Commit

Permalink
Make memory detection with oshi more safe to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Jan 5, 2025
1 parent 7b27e84 commit d988926
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ dependencies {
}
modelApi libs.gson
modelImplementation libs.oshi.core
modelImplementation libs.slf4j.simple
modelCompileOnlyApi cLibs.jspecify
modelCompileOnly cLibs.bundles.compileonly
modelAnnotationProcessor cLibs.bundles.annotationprocessor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package dev.lukebemish.taskgraphrunner.model.conversion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;

class SystemSpecsFinder {
private static final Logger LOGGER = LoggerFactory.getLogger(SystemSpecsFinder.class);

static long recommendedThreads() {
return Runtime.getRuntime().availableProcessors() / 2;
}

static long recommendedMemory() {
if (TOTAL_MEMORY == -1) {
return GIGABYTE * 4;
}
return Math.min(TOTAL_MEMORY / 5, GIGABYTE * 4);
}

Expand All @@ -16,8 +23,14 @@ static long recommendedMemory() {
private static final long GIGABYTE = 1024 * 1024 * 1024;

static {
var systemInfo = new SystemInfo();
var hardware = systemInfo.getHardware();
TOTAL_MEMORY = hardware.getMemory().getTotal();
long totalMemory = -1;
try {
var systemInfo = new SystemInfo();
var hardware = systemInfo.getHardware();
totalMemory = hardware.getMemory().getTotal();
} catch (Throwable e) {
LOGGER.warn("Failed to get system specs; memory allocated to tasks may not be optimal", e);
}
TOTAL_MEMORY = totalMemory;
}
}
1 change: 1 addition & 0 deletions src/model/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requires static org.jspecify;
requires dev.lukebemish.forkedtaskexecutor;
requires com.github.oshi;
requires org.slf4j;

opens dev.lukebemish.taskgraphrunner.model.conversion to com.google.gson;
opens dev.lukebemish.taskgraphrunner.model to com.google.gson;
Expand Down

0 comments on commit d988926

Please sign in to comment.