add current process memory

This commit is contained in:
huoyo 2022-11-09 21:47:10 +08:00
parent 9949b9d203
commit a8c5a7071c
4 changed files with 93 additions and 13 deletions

View File

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

View File

@ -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<String, String> 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<String,String> getProcessInfo() {
Map<String,String> 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;
}
}

View File

@ -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 @@
<ul class="uk-list-bullet" style="text-align: left;margin-top: -13px">
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;text-transform: unset">All</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalAmount" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;text-transform: unset">Used</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalUsed" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;text-transform: unset">Free</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalFree" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;text-transform: unset">ThisUsed</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="thisUsed" >0</span></li>
</ul>
</div>
</div>

View File

@ -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 @@
<span style="font-size: 12px;color: #3b3f4f;justify-content: space-around;">物理内存:</span><span style="font-size: 15px;color: #020718;font-weight: bold" id="physicalUsedRate" >0</span>
<hr style="margin-top: 7px">
<ul class="uk-list-bullet" style="text-align: left;margin-top: -13px">
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;">总内存:</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalAmount" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;">已使用:</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalUsed" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;">未使用:</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalFree" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;">物理内存:</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalAmount" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;">已使用内存</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="physicalUsed" >0</span></li>
<li style="margin-top: -2px;"><span style="font-size: 8px;color: #3b3f4f;">此程序占用:</span><span style="font-size: 12px;color: #020718;font-weight: bold" id="thisUsed" >0</span></li>
</ul>
</div>
</div>