|
|
|
|
@@ -91,53 +91,44 @@ public class SystemMonitorService {
|
|
|
|
|
// =========================
|
|
|
|
|
private double readCpu() throws Exception {
|
|
|
|
|
|
|
|
|
|
// linux 환경일때만 실행
|
|
|
|
|
if (!isLinux()) {
|
|
|
|
|
return 0; // 또는 -1
|
|
|
|
|
}
|
|
|
|
|
if (!isLinux()) return 0;
|
|
|
|
|
|
|
|
|
|
BufferedReader br = new BufferedReader(new FileReader("/proc/stat"));
|
|
|
|
|
String line = br.readLine(); // "cpu ..." 라인
|
|
|
|
|
br.close();
|
|
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader("/proc/stat"))) {
|
|
|
|
|
|
|
|
|
|
String[] p = line.split("\\s+");
|
|
|
|
|
String line = br.readLine();
|
|
|
|
|
String[] p = line.split("\\s+");
|
|
|
|
|
|
|
|
|
|
long user = Long.parseLong(p[1]);
|
|
|
|
|
long nice = Long.parseLong(p[2]);
|
|
|
|
|
long system = Long.parseLong(p[3]);
|
|
|
|
|
long idle = Long.parseLong(p[4]);
|
|
|
|
|
long iowait = Long.parseLong(p[5]);
|
|
|
|
|
long irq = Long.parseLong(p[6]);
|
|
|
|
|
long softirq = Long.parseLong(p[7]);
|
|
|
|
|
long user = Long.parseLong(p[1]);
|
|
|
|
|
long nice = Long.parseLong(p[2]);
|
|
|
|
|
long system = Long.parseLong(p[3]);
|
|
|
|
|
long idle = Long.parseLong(p[4]);
|
|
|
|
|
long iowait = Long.parseLong(p[5]);
|
|
|
|
|
long irq = Long.parseLong(p[6]);
|
|
|
|
|
long softirq = Long.parseLong(p[7]);
|
|
|
|
|
|
|
|
|
|
// 전체 시간 (누적)
|
|
|
|
|
long total = user + nice + system + idle + iowait + irq + softirq;
|
|
|
|
|
long total = user + nice + system + idle + iowait + irq + softirq;
|
|
|
|
|
long idleAll = idle + iowait;
|
|
|
|
|
|
|
|
|
|
// idle 시간
|
|
|
|
|
long idleAll = idle + iowait;
|
|
|
|
|
if (prevTotal == 0) {
|
|
|
|
|
prevTotal = total;
|
|
|
|
|
prevIdle = idleAll;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long totalDiff = total - prevTotal;
|
|
|
|
|
long idleDiff = idleAll - prevIdle;
|
|
|
|
|
|
|
|
|
|
// 최초 호출 (이전값 없음)
|
|
|
|
|
if (prevTotal == 0) {
|
|
|
|
|
prevTotal = total;
|
|
|
|
|
prevIdle = idleAll;
|
|
|
|
|
|
|
|
|
|
if (totalDiff == 0) return 0;
|
|
|
|
|
|
|
|
|
|
return (1.0 - (double) idleDiff / totalDiff) * 100;
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.warn("CPU read fail", e);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 이전 대비 변화량
|
|
|
|
|
long totalDiff = total - prevTotal;
|
|
|
|
|
long idleDiff = idleAll - prevIdle;
|
|
|
|
|
|
|
|
|
|
log.info("CPU raw total={}, idle={}", total, idleAll);
|
|
|
|
|
|
|
|
|
|
// 상태 업데이트
|
|
|
|
|
prevTotal = total;
|
|
|
|
|
prevIdle = idleAll;
|
|
|
|
|
|
|
|
|
|
log.info("CPU prev total={}, prev idle={}", prevTotal, prevIdle);
|
|
|
|
|
log.info("CPU diff totalDiff={}, idleDiff={}", totalDiff, idleDiff);
|
|
|
|
|
|
|
|
|
|
// CPU 사용률 계산
|
|
|
|
|
return (1.0 - (double) idleDiff / totalDiff) * 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isLinux() {
|
|
|
|
|
@@ -200,13 +191,12 @@ public class SystemMonitorService {
|
|
|
|
|
// =====================
|
|
|
|
|
// GPU 평균 (30초)
|
|
|
|
|
// =====================
|
|
|
|
|
for (Map.Entry<Integer, Integer> entry : gpuMap.entrySet()) {
|
|
|
|
|
for (Map.Entry<Integer, Deque<Integer>> entry : gpuHistory.entrySet()) {
|
|
|
|
|
|
|
|
|
|
int index = entry.getKey();
|
|
|
|
|
Deque<Integer> q = entry.getValue();
|
|
|
|
|
|
|
|
|
|
Deque<Integer> q = gpuHistory.get(index);
|
|
|
|
|
|
|
|
|
|
int avg = (int) (q == null ? 0 : q.stream().mapToInt(i -> i).average().orElse(0));
|
|
|
|
|
int avg = (int) q.stream().mapToInt(i -> i).average().orElse(0);
|
|
|
|
|
|
|
|
|
|
dto.gpus.add(new MonitorDto.Gpu(index, avg));
|
|
|
|
|
}
|
|
|
|
|
|