fix: NEZ-2142 nz-talon 修改进程cpu利用率计算
This commit is contained in:
@@ -22,7 +22,7 @@ public class OSHIController extends BaseController{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/oshi/process")
|
@GetMapping("/oshi/process")
|
||||||
public R getProcessInfo(HttpServletRequest request){
|
public R getProcessInfo(HttpServletRequest request) throws InterruptedException {
|
||||||
|
|
||||||
List<Map> result = OSHIUtils.getProcessInfo();
|
List<Map> result = OSHIUtils.getProcessInfo();
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import java.net.UnknownHostException;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
public class OSHIUtils {
|
public class OSHIUtils {
|
||||||
@@ -134,13 +136,16 @@ public class OSHIUtils {
|
|||||||
* 进程信息
|
* 进程信息
|
||||||
* @return result
|
* @return result
|
||||||
*/
|
*/
|
||||||
public static List<Map> getProcessInfo(){
|
public static List<Map> getProcessInfo() throws InterruptedException {
|
||||||
|
|
||||||
List<Map> result = Tool.ListUtil.list(false);
|
List<Map> result = Tool.ListUtil.list(false);
|
||||||
List<OSProcess> procs = operatingSystem.getProcesses();
|
List<OSProcess> oldProcess = operatingSystem.getProcesses();
|
||||||
|
Thread.sleep(500);
|
||||||
|
List<OSProcess> currentProcess = operatingSystem.getProcesses();
|
||||||
|
Map<Integer, Object> cpuUsage = getCpuUsage(currentProcess, oldProcess);
|
||||||
GlobalMemory memory = hal.getMemory();
|
GlobalMemory memory = hal.getMemory();
|
||||||
|
|
||||||
for (OSProcess proc : procs) {
|
for (OSProcess proc : currentProcess) {
|
||||||
HashMap<String, Object> procsData = Tool.MapUtil.newHashMap();
|
HashMap<String, Object> procsData = Tool.MapUtil.newHashMap();
|
||||||
procsData.put("name",proc.getName());
|
procsData.put("name",proc.getName());
|
||||||
procsData.put("commandLine",proc.getCommandLine());
|
procsData.put("commandLine",proc.getCommandLine());
|
||||||
@@ -148,12 +153,7 @@ public class OSHIUtils {
|
|||||||
procsData.put("parentProcessID",proc.getParentProcessID());
|
procsData.put("parentProcessID",proc.getParentProcessID());
|
||||||
procsData.put("startTime",proc.getStartTime());
|
procsData.put("startTime",proc.getStartTime());
|
||||||
procsData.put("priority",proc.getPriority());
|
procsData.put("priority",proc.getPriority());
|
||||||
/**
|
procsData.put("cpuUsage",(double)cpuUsage.get(proc.getProcessID()) * 100d);
|
||||||
* For per-Process CPU ticks, there is no "idle" counter available, so the calculation ends up being (active time / up time)
|
|
||||||
* This interpretation matches the value displayed in ps or top on Unix-based operating systems
|
|
||||||
* If you want per-Process CPU load to match the Windows Task Manager display, you should divide OSHI's calculation by the number of logical processors
|
|
||||||
*/
|
|
||||||
procsData.put("cpuUsage",Tool.NumberUtil.div(proc.getKernelTime() + proc.getUserTime(), proc.getUpTime()) * 100d);
|
|
||||||
procsData.put("memUsage",Tool.NumberUtil.div(proc.getResidentSetSize(), memory.getTotal()) * 100d);
|
procsData.put("memUsage",Tool.NumberUtil.div(proc.getResidentSetSize(), memory.getTotal()) * 100d);
|
||||||
procsData.put("state",proc.getState().toString());
|
procsData.put("state",proc.getState().toString());
|
||||||
procsData.put("openFiles",proc.getOpenFiles());
|
procsData.put("openFiles",proc.getOpenFiles());
|
||||||
@@ -169,6 +169,23 @@ public class OSHIUtils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取进程cpu利用率
|
||||||
|
* @param currentProc
|
||||||
|
* @param oldProcess
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static Map<Integer,Object> getCpuUsage(List<OSProcess> currentProc, List<OSProcess> oldProcess) {
|
||||||
|
|
||||||
|
Map<Integer, OSProcess> procs = oldProcess.stream().collect(Collectors.toMap(OSProcess::getProcessID, Function.identity()));
|
||||||
|
HashMap<Integer, Object> map = Tool.MapUtil.newHashMap();
|
||||||
|
for (OSProcess proc : currentProc) {
|
||||||
|
OSProcess old = procs.get(proc.getProcessID());
|
||||||
|
map.put(proc.getProcessID(),proc.getProcessCpuLoadBetweenTicks(old));
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络连接信息
|
* 网络连接信息
|
||||||
* @return result
|
* @return result
|
||||||
|
|||||||
Reference in New Issue
Block a user