From 2c1f9bdf5cfe5dfe4024bd83e096af15344d4004 Mon Sep 17 00:00:00 2001 From: teddy Date: Thu, 19 Mar 2026 15:46:23 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=9C=EC=8A=A4=ED=85=9C=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9C=A8=20=EB=AA=A8=EB=8B=88=ED=84=B0=EB=A7=81=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/service/GpuDmonReader.java | 4 +- .../common/service/SystemMonitorService.java | 72 ++++++++----------- 2 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/kamco/cd/training/common/service/GpuDmonReader.java b/src/main/java/com/kamco/cd/training/common/service/GpuDmonReader.java index 00ea055..8e6542d 100644 --- a/src/main/java/com/kamco/cd/training/common/service/GpuDmonReader.java +++ b/src/main/java/com/kamco/cd/training/common/service/GpuDmonReader.java @@ -98,7 +98,7 @@ public class GpuDmonReader { while ((line = br.readLine()) != null) { // 디버깅 로그 - log.info("RAW: [{}]", line); + // log.info("RAW: [{}]", line); // 헤더 제거 if (line.startsWith("#")) continue; @@ -121,8 +121,6 @@ public class GpuDmonReader { } gpuUtilMap.put(index, util); - // GPU 값 들어오는지 확인 디버깅 - log.info("GPU UPDATE → index={}, util={}", index, util); } } diff --git a/src/main/java/com/kamco/cd/training/common/service/SystemMonitorService.java b/src/main/java/com/kamco/cd/training/common/service/SystemMonitorService.java index 443c05e..fc5a01f 100644 --- a/src/main/java/com/kamco/cd/training/common/service/SystemMonitorService.java +++ b/src/main/java/com/kamco/cd/training/common/service/SystemMonitorService.java @@ -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 entry : gpuMap.entrySet()) { + for (Map.Entry> entry : gpuHistory.entrySet()) { int index = entry.getKey(); + Deque q = entry.getValue(); - Deque 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)); }