From 2e691411b96ec7d994f56f1c3bc921ea7a86989c Mon Sep 17 00:00:00 2001 From: huoyo <1729913829@qq.com> Date: Wed, 19 Oct 2022 03:30:47 +0800 Subject: [PATCH] add system usage --- pom.xml | 4 +- .../kotime/controller/KoTimeController.java | 27 +++++ .../java/cn/langpy/kotime/model/CpuInfo.java | 40 ++++++++ .../langpy/kotime/model/HeapMemoryInfo.java | 40 ++++++++ .../kotime/model/PhysicalMemoryInfo.java | 40 ++++++++ .../langpy/kotime/service/EmailHandler.java | 4 +- .../kotime/service/KoInvokedHandler.java | 2 +- .../kotime/service/SysUsageService.java | 90 +++++++++++++++++ src/main/resources/kotime-en.html | 97 ++++++++++++++++++ src/main/resources/kotime.html | 99 +++++++++++++++++++ 10 files changed, 437 insertions(+), 6 deletions(-) create mode 100644 src/main/java/cn/langpy/kotime/model/CpuInfo.java create mode 100644 src/main/java/cn/langpy/kotime/model/HeapMemoryInfo.java create mode 100644 src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java create mode 100644 src/main/java/cn/langpy/kotime/service/SysUsageService.java diff --git a/pom.xml b/pom.xml index 767b0cf..b1e443f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.langpy ko-time - 2.3.2 + 2.3.3 KoTime A springboot tool for tracking the paths of the methods,which can help you find method's performances easily. @@ -32,7 +32,7 @@ 2.6.2 2.7.3 2.0.9 - 4.4.2 + 6.2.2 1.12.13 diff --git a/src/main/java/cn/langpy/kotime/controller/KoTimeController.java b/src/main/java/cn/langpy/kotime/controller/KoTimeController.java index aa5dbcd..68805a1 100644 --- a/src/main/java/cn/langpy/kotime/controller/KoTimeController.java +++ b/src/main/java/cn/langpy/kotime/controller/KoTimeController.java @@ -6,6 +6,7 @@ import cn.langpy.kotime.constant.KoConstant; import cn.langpy.kotime.model.*; import cn.langpy.kotime.service.ClassService; import cn.langpy.kotime.service.GraphService; +import cn.langpy.kotime.service.SysUsageService; import cn.langpy.kotime.util.Context; import cn.langpy.kotime.util.InvalidAuthInfoException; import cn.langpy.kotime.util.KoUtil; @@ -307,4 +308,30 @@ public class KoTimeController { } return null; } + + @GetMapping("/getCpuInfo") + @ResponseBody + @Auth + public CpuInfo getCpuInfo() { + SysUsageService usageService = SysUsageService.newInstance(); + CpuInfo cpuInfo = usageService.getCpuInfo(); + return cpuInfo; + } + + @GetMapping("/getHeapMemoryInfo") + @ResponseBody + @Auth + public HeapMemoryInfo getHeapMemoryInfo() { + SysUsageService usageService = SysUsageService.newInstance(); + HeapMemoryInfo heapMemoryInfo = usageService.getHeapMemoryInfo(); + return heapMemoryInfo; + } + @GetMapping("/getPhysicalMemoryInfo") + @ResponseBody + @Auth + public PhysicalMemoryInfo getPhysicalMemoryInfo() { + SysUsageService usageService = SysUsageService.newInstance(); + PhysicalMemoryInfo physicalMemoryInfo = usageService.getPhysicalMemoryInfo(); + return physicalMemoryInfo; + } } diff --git a/src/main/java/cn/langpy/kotime/model/CpuInfo.java b/src/main/java/cn/langpy/kotime/model/CpuInfo.java new file mode 100644 index 0000000..40e24f0 --- /dev/null +++ b/src/main/java/cn/langpy/kotime/model/CpuInfo.java @@ -0,0 +1,40 @@ +package cn.langpy.kotime.model; + +public class CpuInfo { + private Double systemLoad; + private Double userRate; + private Double sysRate; + private Integer logicalNum; + + public Double getSystemLoad() { + return systemLoad; + } + + public void setSystemLoad(Double systemLoad) { + this.systemLoad = systemLoad; + } + + public Double getUserRate() { + return userRate; + } + + public void setUserRate(Double userRate) { + this.userRate = userRate; + } + + public Double getSysRate() { + return sysRate; + } + + public void setSysRate(Double sysRate) { + this.sysRate = sysRate; + } + + public Integer getLogicalNum() { + return logicalNum; + } + + public void setLogicalNum(Integer logicalNum) { + this.logicalNum = logicalNum; + } +} diff --git a/src/main/java/cn/langpy/kotime/model/HeapMemoryInfo.java b/src/main/java/cn/langpy/kotime/model/HeapMemoryInfo.java new file mode 100644 index 0000000..19afafe --- /dev/null +++ b/src/main/java/cn/langpy/kotime/model/HeapMemoryInfo.java @@ -0,0 +1,40 @@ +package cn.langpy.kotime.model; + +public class HeapMemoryInfo { + private Long initValue; + private Long maxValue; + private Long usedValue; + private Double usedRate; + + public Long getInitValue() { + return initValue; + } + + public void setInitValue(Long initValue) { + this.initValue = initValue; + } + + public Long getMaxValue() { + return maxValue; + } + + public void setMaxValue(Long maxValue) { + this.maxValue = maxValue; + } + + public Long getUsedValue() { + return usedValue; + } + + public void setUsedValue(Long usedValue) { + this.usedValue = usedValue; + } + + public Double getUsedRate() { + return usedRate; + } + + public void setUsedRate(Double usedRate) { + this.usedRate = usedRate; + } +} diff --git a/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java b/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java new file mode 100644 index 0000000..10be4f4 --- /dev/null +++ b/src/main/java/cn/langpy/kotime/model/PhysicalMemoryInfo.java @@ -0,0 +1,40 @@ +package cn.langpy.kotime.model; + +public class PhysicalMemoryInfo { + private Long initValue; + private Long freeValue; + private Long usedValue; + private Double usedRate; + + public Long getInitValue() { + return initValue; + } + + public void setInitValue(Long initValue) { + this.initValue = initValue; + } + + public Long getFreeValue() { + return freeValue; + } + + public void setFreeValue(Long freeValue) { + this.freeValue = freeValue; + } + + public Long getUsedValue() { + return usedValue; + } + + public void setUsedValue(Long usedValue) { + this.usedValue = usedValue; + } + + public Double getUsedRate() { + return usedRate; + } + + public void setUsedRate(Double usedRate) { + this.usedRate = usedRate; + } +} diff --git a/src/main/java/cn/langpy/kotime/service/EmailHandler.java b/src/main/java/cn/langpy/kotime/service/EmailHandler.java index d1172d2..df2ddda 100644 --- a/src/main/java/cn/langpy/kotime/service/EmailHandler.java +++ b/src/main/java/cn/langpy/kotime/service/EmailHandler.java @@ -25,9 +25,7 @@ public class EmailHandler implements InvokedHandler { if (current == null || current.getValue() < Context.getConfig().getThreshold()) { return; } - if (mailScope.equals("All")) { - emailSendService.sendNoticeAsync(current); - }else if (current.getMethodType().name().equals(mailScope)) { + if (mailScope.equals("All") || current.getMethodType().name().equals(mailScope)) { emailSendService.sendNoticeAsync(current); } } diff --git a/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java b/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java index 57c07d2..0998ec9 100644 --- a/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java +++ b/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java @@ -44,7 +44,7 @@ public final class KoInvokedHandler implements InvokedHandler { private MethodNode filter(MethodNode currentNode) { if (BloomFilter.exists(currentNode.getId())) { - //allow controllers's routes to be updated + //allow controller's routes to be updated if (!StringUtils.isEmpty(currentNode.getRouteName())) { return currentNode; } diff --git a/src/main/java/cn/langpy/kotime/service/SysUsageService.java b/src/main/java/cn/langpy/kotime/service/SysUsageService.java new file mode 100644 index 0000000..355e9d6 --- /dev/null +++ b/src/main/java/cn/langpy/kotime/service/SysUsageService.java @@ -0,0 +1,90 @@ +package cn.langpy.kotime.service; + +import cn.langpy.kotime.model.CpuInfo; +import cn.langpy.kotime.model.HeapMemoryInfo; +import cn.langpy.kotime.model.PhysicalMemoryInfo; +import com.sun.management.OperatingSystemMXBean; +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; +import java.util.concurrent.TimeUnit; + +public class SysUsageService { + + public static SysUsageService newInstance() { + return new SysUsageService(); + } + + public CpuInfo getCpuInfo() { + SystemInfo systemInfo = new SystemInfo(); + CentralProcessor processor = systemInfo.getHardware().getProcessor(); + long[] prevTicks = processor.getSystemCpuLoadTicks(); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + long[] ticks = processor.getSystemCpuLoadTicks(); + long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] + - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; + + long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] + - prevTicks[CentralProcessor.TickType.IRQ.getIndex()]; + + long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] + - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]; + + long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] + - prevTicks[CentralProcessor.TickType.STEAL.getIndex()]; + + long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] + - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()]; + + long user = ticks[CentralProcessor.TickType.USER.getIndex()] + - prevTicks[CentralProcessor.TickType.USER.getIndex()]; + + long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] + - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()]; + + long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] + - prevTicks[CentralProcessor.TickType.IDLE.getIndex()]; + + long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal; + CpuInfo cpuInfo = new CpuInfo(); + cpuInfo.setLogicalNum(processor.getLogicalProcessorCount()); + cpuInfo.setUserRate(user * 1.0 / totalCpu); + cpuInfo.setSysRate(cSys * 1.0 / totalCpu); + cpuInfo.setSystemLoad(processor.getSystemCpuLoad(1000)); + return cpuInfo; + } + + public HeapMemoryInfo getHeapMemoryInfo() { + MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); + MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); + long initTotalMemorySize = memoryUsage.getInit(); + long maxMemorySize = memoryUsage.getMax(); + long usedMemorySize = memoryUsage.getUsed(); + HeapMemoryInfo heapMemoryInfo = new HeapMemoryInfo(); + heapMemoryInfo.setInitValue(initTotalMemorySize); + heapMemoryInfo.setMaxValue(maxMemorySize); + heapMemoryInfo.setUsedValue(usedMemorySize); + heapMemoryInfo.setUsedRate(usedMemorySize * 1.0 / maxMemorySize); + return heapMemoryInfo; + } + + public PhysicalMemoryInfo getPhysicalMemoryInfo() { + OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + PhysicalMemoryInfo physicalMemoryInfo = new PhysicalMemoryInfo(); + physicalMemoryInfo.setInitValue(osmxb.getTotalPhysicalMemorySize()); + physicalMemoryInfo.setUsedValue(osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()); + physicalMemoryInfo.setFreeValue(osmxb.getFreePhysicalMemorySize()); + physicalMemoryInfo.setUsedValue(physicalMemoryInfo.getInitValue() - physicalMemoryInfo.getFreeValue()); + physicalMemoryInfo.setUsedRate(physicalMemoryInfo.getUsedValue() * 1.0 / physicalMemoryInfo.getInitValue()); + return physicalMemoryInfo; + } + +} + diff --git a/src/main/resources/kotime-en.html b/src/main/resources/kotime-en.html index 7bd62be..1616acb 100644 --- a/src/main/resources/kotime-en.html +++ b/src/main/resources/kotime-en.html @@ -156,6 +156,9 @@ }); $.ajaxSettings.async = true; loadLatestVersion(); + loadCpuInfo(); + loadHeapMemoryInfo(); + loadPhysicalMemoryInfo(); }); let methodParamMap = new Map(); @@ -402,6 +405,62 @@ complete: function () {} }) } + function loadCpuInfo() { + $.get('contextPath/koTime/getCpuInfo?token='+globalToken, function (data) { + let systemLoad = data['systemLoad']*100; + let userRate = data['userRate']*100; + let sysRate = data['sysRate']*100; + let logicalNum = data['logicalNum']; + var systemLoadDom = document.querySelector("#systemLoad"); + if (systemLoad>50) { + systemLoadDom.style.color='#cc0c0c'; + }else { + systemLoadDom.style.color='#29da93'; + }; + systemLoadDom.innerHTML = `${systemLoad.toFixed(2)}%`; + document.querySelector("#cpuSysRate").innerHTML = `${sysRate.toFixed(2)}%`; + document.querySelector("#cpuUserRate").innerHTML = `${userRate.toFixed(2)}%`; + document.querySelector("#cpuLogicalAmount").innerHTML = `${logicalNum}`; + + + }); + } + function loadHeapMemoryInfo() { + $.get('contextPath/koTime/getHeapMemoryInfo?token='+globalToken, function (data) { + let initValue = data['initValue']/1024/1024; + let maxValue = data['maxValue']/1024/1024; + let usedValue = data['usedValue']/1024/1024; + let usedRate = data['usedRate']*100; + var heapUsedRateDom = document.querySelector("#heapUsedRate"); + if (usedRate>50) { + heapUsedRateDom.style.color='#cc0c0c'; + }else { + heapUsedRateDom.style.color='#29da93'; + }; + document.querySelector("#heapInit").innerHTML = `${initValue.toFixed()}M`; + document.querySelector("#heapMax").innerHTML = `${maxValue.toFixed()}M`; + document.querySelector("#heapUsed").innerHTML = `${usedValue.toFixed()}M`; + heapUsedRateDom.innerHTML = `${usedRate.toFixed(2)}%`; + }); + } + function loadPhysicalMemoryInfo() { + $.get('contextPath/koTime/getPhysicalMemoryInfo?token='+globalToken, function (data) { + let initValue = data['initValue']/1024/1024; + let freeValue = data['freeValue']/1024/1024; + let usedValue = data['usedValue']/1024/1024; + let usedRate = data['usedRate']*100; + var physicalUsedRateDom = document.querySelector("#physicalUsedRate"); + if (usedRate>50) { + physicalUsedRateDom.style.color='#cc0c0c'; + }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)}%`; + }); + } @@ -479,6 +538,44 @@ 0 + +
+
+
+ CPU Usage:0 +
+
    +
  • User Usage:0
  • +
  • System Usage:0
  • +
  • Logical Core:0
  • +
+
+
+
+
+ Heap Memory:0 +
+
    +
  • Init:0
  • +
  • Max:0
  • +
  • Used:0
  • +
+
+
+
+
+ Physical Memory:0 +
+
    +
  • All:0
  • +
  • Used:0
  • +
  • Free:0
  • +
+
+
+

Please call apis before visiting this page!

diff --git a/src/main/resources/kotime.html b/src/main/resources/kotime.html index 673f694..38b7e17 100644 --- a/src/main/resources/kotime.html +++ b/src/main/resources/kotime.html @@ -155,6 +155,9 @@ }); $.ajaxSettings.async = true; loadLatestVersion(); + loadCpuInfo(); + loadHeapMemoryInfo(); + loadPhysicalMemoryInfo(); }); let methodParamMap = new Map(); @@ -400,6 +403,63 @@ complete: function () {} }) } + function loadCpuInfo() { + $.get('contextPath/koTime/getCpuInfo?token='+globalToken, function (data) { + let systemLoad = data['systemLoad']*100; + let userRate = data['userRate']*100; + let sysRate = data['sysRate']*100; + let logicalNum = data['logicalNum']; + var systemLoadDom = document.querySelector("#systemLoad"); + if (systemLoad>50) { + systemLoadDom.style.color='#cc0c0c'; + }else { + systemLoadDom.style.color='#29da93'; + }; + systemLoadDom.innerHTML = `${systemLoad.toFixed(2)}%`; + document.querySelector("#cpuSysRate").innerHTML = `${sysRate.toFixed(2)}%`; + document.querySelector("#cpuUserRate").innerHTML = `${userRate.toFixed(2)}%`; + document.querySelector("#cpuLogicalAmount").innerHTML = `${logicalNum}个`; + + + }); + } + function loadHeapMemoryInfo() { + $.get('contextPath/koTime/getHeapMemoryInfo?token='+globalToken, function (data) { + let initValue = data['initValue']/1024/1024; + let maxValue = data['maxValue']/1024/1024; + let usedValue = data['usedValue']/1024/1024; + let usedRate = data['usedRate']*100; + var heapUsedRateDom = document.querySelector("#heapUsedRate"); + if (usedRate>50) { + heapUsedRateDom.style.color='#cc0c0c'; + }else { + heapUsedRateDom.style.color='#29da93'; + }; + document.querySelector("#heapInit").innerHTML = `${initValue.toFixed()}M`; + document.querySelector("#heapMax").innerHTML = `${maxValue.toFixed()}M`; + document.querySelector("#heapUsed").innerHTML = `${usedValue.toFixed()}M`; + heapUsedRateDom.innerHTML = `${usedRate.toFixed(2)}%`; + }); + } + function loadPhysicalMemoryInfo() { + $.get('contextPath/koTime/getPhysicalMemoryInfo?token='+globalToken, function (data) { + let initValue = data['initValue']/1024/1024; + let freeValue = data['freeValue']/1024/1024; + let usedValue = data['usedValue']/1024/1024; + let usedRate = data['usedRate']*100; + var physicalUsedRateDom = document.querySelector("#physicalUsedRate"); + if (usedRate>50) { + physicalUsedRateDom.style.color='#cc0c0c'; + }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)}%`; + }); + } + @@ -473,6 +533,45 @@ 0
+ +
+
+
+ CPU负载:0 +
+
    +
  • 用户使用率:0
  • +
  • 系统使用率:0
  • +
  • 逻辑核心数:0
  • +
+
+
+
+
+ 堆内存:0 +
+
    +
  • 初始值:0
  • +
  • 最大值:0
  • +
  • 已使用:0
  • +
+
+
+
+
+ 物理内存:0 +
+
    +
  • 总内存:0
  • +
  • 已使用:0
  • +
  • 未使用:0
  • +
+
+
+
+

接口根据调用情况统计,未调用的接口无法被统计到,请先调用接口