fix getUsed bug

This commit is contained in:
Looly 2022-01-26 10:31:07 +08:00
parent 48e0e99bb6
commit bd89ec9335
3 changed files with 33 additions and 12 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.7.21 (2022-01-25)
# 5.7.21 (2022-01-26)
### 🐣新特性
* 【extra 】 增加jetbrick模板支持
@ -13,6 +13,7 @@
### 🐞Bug修复
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUGissue#2112@Github
* 【extra 】 修复EmojiUtil.toHtmlHex()方法pr#519@Gitee
* 【system 】 修复CpuInfo.getUsed()方法issue#2116@Github
-------------------------------------------------------------------------------------------------------------
# 5.7.20 (2022-01-20)

View File

@ -1,5 +1,6 @@
package cn.hutool.system.oshi;
import cn.hutool.core.util.NumberUtil;
import oshi.hardware.CentralProcessor;
import java.text.DecimalFormat;
@ -31,7 +32,7 @@ public class CpuInfo {
/**
* CPU用户使用率
*/
private double used;
private double user;
/**
* CPU当前等待率
@ -75,16 +76,16 @@ public class CpuInfo {
* @param cpuNum CPU核心数
* @param toTal CPU总的使用率
* @param sys CPU系统使用率
* @param used CPU用户使用率
* @param user CPU用户使用率
* @param wait CPU当前等待率
* @param free CPU当前空闲率
* @param cpuModel CPU型号信息
*/
public CpuInfo(Integer cpuNum, double toTal, double sys, double used, double wait, double free, String cpuModel) {
public CpuInfo(Integer cpuNum, double toTal, double sys, double user, double wait, double free, String cpuModel) {
this.cpuNum = cpuNum;
this.toTal = toTal;
this.sys = sys;
this.used = used;
this.user = user;
this.wait = wait;
this.free = free;
this.cpuModel = cpuModel;
@ -114,12 +115,12 @@ public class CpuInfo {
this.sys = sys;
}
public double getUsed() {
return used;
public double getUser() {
return user;
}
public void setUsed(double used) {
this.used = used;
public void setUser(double user) {
this.user = user;
}
public double getWait() {
@ -154,16 +155,25 @@ public class CpuInfo {
this.ticks = ticks;
}
/**
* 获取用户+系统的总的CPU使用率
*
* @return 总CPU使用率
*/
public double getUsed() {
return NumberUtil.sub(100, this.free);
}
@Override
public String toString() {
return "CpuInfo{" +
"CPU核心数=" + cpuNum +
", CPU总的使用率=" + toTal +
", CPU系统使用率=" + sys +
", CPU用户使用率=" + used +
", CPU用户使用率=" + user +
", CPU当前等待率=" + wait +
", CPU当前空闲率=" + free +
", CPU利用率=" + LOAD_FORMAT.format(100 - free) +
", CPU利用率=" + getUsed() +
", CPU型号信息='" + cpuModel + '\'' +
'}';
}
@ -185,7 +195,7 @@ public class CpuInfo {
final long totalCpu = ticks.totalCpu();
this.toTal = totalCpu;
this.sys = formatDouble(ticks.cSys, totalCpu);
this.used = formatDouble(ticks.user, totalCpu);
this.user = formatDouble(ticks.user, totalCpu);
this.wait = formatDouble(ticks.ioWait, totalCpu);
this.free = formatDouble(ticks.idle, totalCpu);
}

View File

@ -1,8 +1,10 @@
package cn.hutool.system;
import cn.hutool.core.lang.Console;
import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import oshi.software.os.OSProcess;
@ -28,4 +30,12 @@ public class OshiTest {
final OSProcess currentProcess = OshiUtil.getCurrentProcess();
Assert.assertEquals("java", currentProcess.getName());
}
@Test
@Ignore
public void getUsedTest(){
while (true){
Console.log(OshiUtil.getCpuInfo().getUsed());
}
}
}