diff --git a/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java b/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java index 10be4f4..3ae13aa 100644 --- a/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java +++ b/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java @@ -5,6 +5,33 @@ public class PhysicalMemoryInfo { private Long freeValue; private Long usedValue; private Double usedRate; + private Long thisUsedValue; + private Double thisUsedRate; + private Long thisUsedPeak; + + public Long getThisUsedPeak() { + return thisUsedPeak; + } + + public void setThisUsedPeak(Long thisUsedPeak) { + this.thisUsedPeak = thisUsedPeak; + } + + public Long getThisUsedValue() { + return thisUsedValue; + } + + public void setThisUsedValue(Long thisUsedValue) { + this.thisUsedValue = thisUsedValue; + } + + public Double getThisUsedRate() { + return thisUsedRate; + } + + public void setThisUsedRate(Double thisUsedRate) { + this.thisUsedRate = thisUsedRate; + } public Long getInitValue() { return initValue; diff --git a/src/main/java/cn/langpy/kotime/service/SysUsageService.java b/src/main/java/cn/langpy/kotime/service/SysUsageService.java index 4fcd9e9..7248bb2 100644 --- a/src/main/java/cn/langpy/kotime/service/SysUsageService.java +++ b/src/main/java/cn/langpy/kotime/service/SysUsageService.java @@ -3,16 +3,25 @@ package cn.langpy.kotime.service; import cn.langpy.kotime.model.CpuInfo; import cn.langpy.kotime.model.HeapMemoryInfo; import cn.langpy.kotime.model.PhysicalMemoryInfo; +import cn.langpy.kotime.util.Context; import com.sun.management.OperatingSystemMXBean; import oshi.SystemInfo; import oshi.hardware.CentralProcessor; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryUsage; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; public class SysUsageService { + private static Logger log = Logger.getLogger(SysUsageService.class.toString()); public static SysUsageService newInstance() { return new SysUsageService(); @@ -27,7 +36,7 @@ public class SysUsageService { } catch (InterruptedException e) { e.printStackTrace(); } - long[] ticks = processor.getSystemCpuLoadTicks(); + long[] ticks = processor.getSystemCpuLoadTicks(); long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; @@ -84,8 +93,52 @@ public class SysUsageService { physicalMemoryInfo.setFreeValue(osmxb.getFreePhysicalMemorySize()); physicalMemoryInfo.setUsedValue(physicalMemoryInfo.getInitValue() - physicalMemoryInfo.getFreeValue()); physicalMemoryInfo.setUsedRate(physicalMemoryInfo.getUsedValue() * 1.0 / physicalMemoryInfo.getInitValue()); + if (isLinux()) { + Map processInfo = getProcessInfo(); + if (processInfo.containsKey("VmSize")) { + String VmRSSStr = processInfo.get("VmRSS"); + String VmSizeStr = VmRSSStr.split(" ")[0].trim(); + long VmRSS = Long.valueOf(VmSizeStr); + physicalMemoryInfo.setThisUsedValue(VmRSS*1024); + double rate = physicalMemoryInfo.getThisUsedValue()*1.0 / physicalMemoryInfo.getInitValue(); + physicalMemoryInfo.setThisUsedRate(rate); + } + } return physicalMemoryInfo; } + public boolean isLinux() { + return System.getProperty("os.name").toLowerCase().contains("linux"); + } + + public Map getProcessInfo() { + Map processMetrics = new HashMap(); + Runtime runtime = Runtime.getRuntime(); + Process process = null; + try { + process = runtime.exec("cat /proc/" + Context.getPid() + "/status"); + } catch (IOException e) { + log.severe("Can not execute '"+"cat /proc/" + Context.getPid() + "/status"+"'"); + return processMetrics; + } + try (InputStream inputStream = process.getInputStream(); + InputStreamReader inputStreamReader = new InputStreamReader(inputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader) + ) { + String line =""; + while ((line = bufferedReader.readLine()) != null){ + String[] split = line.split(":"); + if (split.length==2) { + String key = split[0].trim(); + String value = split[1].trim(); + processMetrics.put(key,value); + } + } + } catch (Exception e) { + log.severe("Can not read the result of '"+"cat /proc/" + Context.getPid() + "/status"+"'"); + } + return processMetrics; + } + } diff --git a/src/main/resources/kotime-en.html b/src/main/resources/kotime-en.html index ff339e6..5f29f56 100644 --- a/src/main/resources/kotime-en.html +++ b/src/main/resources/kotime-en.html @@ -309,7 +309,7 @@ function loadPhysicalMemoryInfo() { $.get('contextPath/koTime/getPhysicalMemoryInfo?token='+globalToken, function (data) { let initValue = data['initValue']/1024/1024; - let freeValue = data['freeValue']/1024/1024; + let thisUsedValue = data['thisUsedValue']/1024/1024; let usedValue = data['usedValue']/1024/1024; let usedRate = data['usedRate']*100; var physicalUsedRateDom = document.querySelector("#physicalUsedRate"); @@ -318,10 +318,10 @@ }else { physicalUsedRateDom.style.color='#29da93'; }; - document.querySelector("#physicalAmount").innerHTML = `${initValue.toFixed()}M`; - document.querySelector("#physicalFree").innerHTML = `${freeValue.toFixed()}M`; - document.querySelector("#physicalUsed").innerHTML = `${usedValue.toFixed()}M`; physicalUsedRateDom.innerHTML = `${usedRate.toFixed(2)}%`; + document.querySelector("#physicalAmount").innerHTML = `${initValue.toFixed()}M`; + document.querySelector("#physicalUsed").innerHTML = `${usedValue.toFixed()}M`; + document.querySelector("#thisUsed").innerHTML = `${thisUsedValue.toFixed()}M`; }); } @@ -600,7 +600,7 @@
  • All:0
  • Used:0
  • -
  • Free:0
  • +
  • ThisUsed:0
diff --git a/src/main/resources/kotime.html b/src/main/resources/kotime.html index d6952cc..ee28f47 100644 --- a/src/main/resources/kotime.html +++ b/src/main/resources/kotime.html @@ -465,7 +465,7 @@ function loadPhysicalMemoryInfo() { $.get('contextPath/koTime/getPhysicalMemoryInfo?token='+globalToken, function (data) { let initValue = data['initValue']/1024/1024; - let freeValue = data['freeValue']/1024/1024; + let thisUsedValue = data['thisUsedValue']/1024/1024; let usedValue = data['usedValue']/1024/1024; let usedRate = data['usedRate']*100; var physicalUsedRateDom = document.querySelector("#physicalUsedRate"); @@ -474,10 +474,10 @@ }else { physicalUsedRateDom.style.color='#29da93'; }; - document.querySelector("#physicalAmount").innerHTML = `${initValue.toFixed()}M`; - document.querySelector("#physicalFree").innerHTML = `${freeValue.toFixed()}M`; - document.querySelector("#physicalUsed").innerHTML = `${usedValue.toFixed()}M`; physicalUsedRateDom.innerHTML = `${usedRate.toFixed(2)}%`; + document.querySelector("#physicalAmount").innerHTML = `${initValue.toFixed()}M`; + document.querySelector("#physicalUsed").innerHTML = `${usedValue.toFixed()}M`; + document.querySelector("#thisUsed").innerHTML = `${thisUsedValue.toFixed()}M`; }); } $(document).ready(function () { @@ -590,9 +590,9 @@ 物理内存:0
    -
  • 总内存:0
  • -
  • 已使用:0
  • -
  • 未使用:0
  • +
  • 总物理内存:0
  • +
  • 已使用内存:0
  • +
  • 此程序占用:0