Merge pull request '시스템 사용율 모니터링 기능 테스트' (#160) from feat/training_260303 into develop

Reviewed-on: #160
This commit was merged in pull request #160.
This commit is contained in:
2026-03-19 15:46:40 +09:00
2 changed files with 32 additions and 44 deletions

View File

@@ -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);
}
}

View File

@@ -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));
}