Use the actual available free memory instead of the max possible

This commit is contained in:
Laurent Clouet
2018-08-10 17:33:50 +02:00
parent 4dc0603587
commit 6ded57fc0a
6 changed files with 65 additions and 1 deletions

View File

@@ -287,7 +287,15 @@ public class Server extends Thread implements HostnameVerifier, X509TrustManager
HttpURLConnection connection = null;
try {
OS os = OS.getOS();
String url = String.format("%s?computemethod=%s&cpu_cores=%s&ram_max=%s&rendertime_max=%s", this.getPage("request-job"), this.user_config.computeMethodToInt(), ((this.user_config.getNbCores() == -1) ? os.getCPU().cores() : this.user_config.getNbCores()), this.user_config.getMaxMemory(), this.user_config.getMaxRenderTime());
int maxMemory = this.user_config.getMaxMemory();
int freeMemory = os.getFreeMemory();
if (maxMemory < 0) {
maxMemory = freeMemory;
}
else if (freeMemory > 0 && maxMemory > 0) {
maxMemory = Math.min(maxMemory, freeMemory);
}
String url = String.format("%s?computemethod=%s&cpu_cores=%s&ram_max=%s&rendertime_max=%s", this.getPage("request-job"), this.user_config.computeMethodToInt(), ((this.user_config.getNbCores() == -1) ? os.getCPU().cores() : this.user_config.getNbCores()), maxMemory, this.user_config.getMaxRenderTime());
if (this.user_config.getComputeMethod() != ComputeType.CPU && this.user_config.getGPUDevice() != null) {
String gpu_model = "";
try {

View File

@@ -133,6 +133,11 @@ public class FreeBSD extends OS {
return 0;
}
@Override
public int getFreeMemory() {
return -1;
}
@Override
public String getCUDALib() {
return "cuda";

View File

@@ -116,6 +116,35 @@ public class Linux extends OS {
return 0;
}
@Override
public int getFreeMemory() {
try {
String filePath = "/proc/meminfo";
Scanner scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("MemFree")) {
String buf[] = line.split(":");
if (buf.length > 0) {
Integer buf2 = new Integer(buf[1].trim().split(" ")[0]);
return (((buf2 / 262144) + 1) * 262144); // 256*1024 = 262144
}
}
}
scanner.close();
}
catch (java.lang.NoClassDefFoundError e) {
System.err.println("OS::Linux::getFreeMemory error " + e + " mostly because Scanner class was introducted by Java 5 and you are running a lower version");
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public String getCUDALib() {
return "cuda";

View File

@@ -140,6 +140,11 @@ public class Mac extends OS {
return -1;
}
@Override
public int getFreeMemory() {
return -1;
}
@Override
public Process exec(List<String> command, Map<String, String> env) throws IOException {
List<String> actual_command = command;

View File

@@ -33,6 +33,8 @@ public abstract class OS {
public abstract int getMemory();
public abstract int getFreeMemory();
public abstract String getRenderBinaryPath();
public String getCUDALib() {

View File

@@ -102,6 +102,21 @@ public class Windows extends OS {
return 0;
}
@Override
public int getFreeMemory() {
try {
MEMORYSTATUSEX _memory = new MEMORYSTATUSEX();
if (Kernel32.INSTANCE.GlobalMemoryStatusEx(_memory)) {
return (int) (_memory.ullAvailPhys.longValue() / 1024); // size in KB
}
}
catch (Exception e) {
e.printStackTrace();
}
return -1;
}
@Override
public String getCUDALib() {
return "nvcuda";