Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
9a4aa3281a
Commit
1337ec8ddf
@ -28,6 +28,7 @@ import java.util.concurrent.locks.LockSupport;
|
||||
public class RamUsage {
|
||||
|
||||
private File meminfo = new File("/proc/meminfo");
|
||||
private File stat = new File("/proc/stat");
|
||||
|
||||
private double usageSelf = 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);
|
||||
|
||||
usage = _getUsage();
|
||||
// load = _getLoad();
|
||||
load = _getLoad();
|
||||
Thread.yield();
|
||||
LockSupport.parkNanos(1000000000L);
|
||||
}
|
||||
@ -80,21 +81,40 @@ public class RamUsage {
|
||||
return load;
|
||||
}
|
||||
|
||||
private long lastCpuSecond = -1;
|
||||
private long lastCpuForth = -1;
|
||||
private long lastCpuFifth = -1;
|
||||
|
||||
private static double _getLoad() {
|
||||
try {
|
||||
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();
|
||||
process.waitFor();
|
||||
return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine());
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(stat)))) {
|
||||
String[] strings = bufferedReader.readLine().split(" ");
|
||||
|
||||
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) {
|
||||
return 1D;
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return 1D;
|
||||
}
|
||||
}
|
||||
|
||||
private static long getNumber(String s) {
|
||||
return Long.parseLong(s.split(" ")[1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren