SteamWar/BauSystem2.0
Archiviert
12
0

Update load usage calculation
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-06-17 18:18:45 +02:00
Ursprung 9a4aa3281a
Commit 1337ec8ddf

Datei anzeigen

@ -28,6 +28,7 @@ import java.util.concurrent.locks.LockSupport;
public class RamUsage { public class RamUsage {
private File meminfo = new File("/proc/meminfo"); private File meminfo = new File("/proc/meminfo");
private File stat = new File("/proc/stat");
private double usageSelf = 0D; private double usageSelf = 0D;
private double usage = 0D; private double usage = 0D;
@ -48,7 +49,7 @@ public class RamUsage {
// System.out.println("Self: " + usageSelf + "/" + usageSelfByMax + /* " " + maxMemory + " " + totalMemory + " " + freeMemory + " " + usedMemory + */ " Ram: " + usage + " CPU: " + load); // System.out.println("Self: " + usageSelf + "/" + usageSelfByMax + /* " " + maxMemory + " " + totalMemory + " " + freeMemory + " " + usedMemory + */ " Ram: " + usage + " CPU: " + load);
usage = _getUsage(); usage = _getUsage();
// load = _getLoad(); load = _getLoad();
Thread.yield(); Thread.yield();
LockSupport.parkNanos(1000000000L); LockSupport.parkNanos(1000000000L);
} }
@ -80,21 +81,40 @@ public class RamUsage {
return load; return load;
} }
private long lastCpuSecond = -1;
private long lastCpuForth = -1;
private long lastCpuFifth = -1;
private static double _getLoad() { private static double _getLoad() {
try { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(stat)))) {
Process process = new ProcessBuilder("bash", "-c", "cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); String[] strings = bufferedReader.readLine().split(" ");
process.waitFor();
return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); long cpuSecond = Long.parseLong(strings[2]);
long cpuForth = Long.parseLong(strings[4]);
long cpuFifth = Long.parseLong(strings[5]);
if (lastCpuSecond == -1) {
lastCpuSecond = cpuSecond;
lastCpuForth = cpuForth;
lastCpuFifth = cpuFifth;
return 0D;
}
long cpuSecondDiff = cpuSecond - lastCpuSecond;
long cpuForthDiff = cpuForth - lastCpuForth;
long cpuSixthDiff = cpuFifth - lastCpuFifth;
lastCpuSecond = cpuSecond;
lastCpuForth = cpuForth;
lastCpuFifth = cpuFifth;
return (cpuSecondDiff + cpuForthDiff) / (double) (cpuSecondDiff + cpuForthDiff + cpuSixthDiff);
} catch (IOException e) { } catch (IOException e) {
return 1D; return 1D;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return 1D;
} }
} }
private static long getNumber(String s) { private static long getNumber(String s) {
return Long.parseLong(s.split(" ")[1]); return Long.parseLong(s.split(" ")[1]);
} }
} }