mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
pr#124
This commit is contained in:
131
hutool-system/src/main/java/cn/hutool/system/oshi/CpuInfo.java
Normal file
131
hutool-system/src/main/java/cn/hutool/system/oshi/CpuInfo.java
Normal file
@@ -0,0 +1,131 @@
|
||||
package cn.hutool.system.oshi;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
* <p>2020-05-21 14:19</p>
|
||||
*
|
||||
* @author Dai Yuanchuan
|
||||
**/
|
||||
public class CpuInfo {
|
||||
|
||||
/**
|
||||
* cpu核心数
|
||||
*/
|
||||
private Integer cpuNum;
|
||||
|
||||
/**
|
||||
* CPU总的使用率
|
||||
*/
|
||||
private double toTal;
|
||||
|
||||
/**
|
||||
* CPU系统使用率
|
||||
*/
|
||||
private double sys;
|
||||
|
||||
/**
|
||||
* CPU用户使用率
|
||||
*/
|
||||
private double used;
|
||||
|
||||
/**
|
||||
* CPU当前等待率
|
||||
*/
|
||||
private double wait;
|
||||
|
||||
/**
|
||||
* CPU当前空闲率
|
||||
*/
|
||||
private double free;
|
||||
|
||||
/**
|
||||
* CPU型号信息
|
||||
*/
|
||||
private String cpuModel;
|
||||
|
||||
public CpuInfo() {
|
||||
}
|
||||
|
||||
public CpuInfo(Integer cpuNum, double toTal, double sys, double used, double wait, double free, String cpuModel) {
|
||||
this.cpuNum = cpuNum;
|
||||
this.toTal = toTal;
|
||||
this.sys = sys;
|
||||
this.used = used;
|
||||
this.wait = wait;
|
||||
this.free = free;
|
||||
this.cpuModel = cpuModel;
|
||||
}
|
||||
|
||||
public Integer getCpuNum() {
|
||||
return cpuNum;
|
||||
}
|
||||
|
||||
public void setCpuNum(Integer cpuNum) {
|
||||
this.cpuNum = cpuNum;
|
||||
}
|
||||
|
||||
public double getToTal() {
|
||||
return toTal;
|
||||
}
|
||||
|
||||
public void setToTal(double toTal) {
|
||||
this.toTal = toTal;
|
||||
}
|
||||
|
||||
public double getSys() {
|
||||
return sys;
|
||||
}
|
||||
|
||||
public void setSys(double sys) {
|
||||
this.sys = sys;
|
||||
}
|
||||
|
||||
public double getUsed() {
|
||||
return used;
|
||||
}
|
||||
|
||||
public void setUsed(double used) {
|
||||
this.used = used;
|
||||
}
|
||||
|
||||
public double getWait() {
|
||||
return wait;
|
||||
}
|
||||
|
||||
public void setWait(double wait) {
|
||||
this.wait = wait;
|
||||
}
|
||||
|
||||
public double getFree() {
|
||||
return free;
|
||||
}
|
||||
|
||||
public void setFree(double free) {
|
||||
this.free = free;
|
||||
}
|
||||
|
||||
public String getCpuModel() {
|
||||
return cpuModel;
|
||||
}
|
||||
|
||||
public void setCpuModel(String cpuModel) {
|
||||
this.cpuModel = cpuModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
DecimalFormat format = new DecimalFormat("#.00");
|
||||
return "CpuInfo{" +
|
||||
"cpu核心数=" + cpuNum +
|
||||
", CPU总的使用率=" + toTal +
|
||||
", CPU系统使用率=" + sys +
|
||||
", CPU用户使用率=" + used +
|
||||
", CPU当前等待率=" + wait +
|
||||
", CPU当前空闲率=" + free +
|
||||
", CPU利用率=" + Double.parseDouble(format.format((100 - getFree()))) +
|
||||
", CPU型号信息='" + cpuModel + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -9,6 +9,10 @@ import oshi.hardware.HardwareAbstractionLayer;
|
||||
import oshi.hardware.NetworkIF;
|
||||
import oshi.hardware.Sensors;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
import oshi.util.Util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Oshi库封装的工具类,通过此工具类,可获取系统、硬件相关信息
|
||||
@@ -95,19 +99,77 @@ public class OshiUtil {
|
||||
* 获取磁盘相关信息,可能有多个磁盘(包括可移动磁盘等)
|
||||
*
|
||||
* @return 磁盘相关信息
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static HWDiskStore[] getDiskStores() {
|
||||
public static List<HWDiskStore> getDiskStores() {
|
||||
return hardware.getDiskStores();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取网络相关信息,可能多块网卡
|
||||
* @return 网络相关信息
|
||||
* @since 5.3.5
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static NetworkIF[] getNetworkIFs(){
|
||||
public static List<NetworkIF> getNetworkIFs(){
|
||||
return hardware.getNetworkIFs();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ cpu
|
||||
|
||||
/**
|
||||
* 获取系统CPU 系统使用率、用户使用率、利用率等等 相关信息
|
||||
*
|
||||
* @return 系统 CPU 使用率 等信息
|
||||
*/
|
||||
public static CpuInfo getCpuInfo() {
|
||||
return getCpuInfo(1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统CPU 系统使用率、用户使用率、利用率等等 相关信息
|
||||
*
|
||||
* @param waitingTime 设置等待时间
|
||||
* @return 系统 CPU 使用率 等信息
|
||||
*/
|
||||
public static CpuInfo getCpuInfo(long waitingTime) {
|
||||
return getCpuInfo(OshiUtil.getProcessor(), waitingTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统CPU 系统使用率、用户使用率、利用率等等 相关信息
|
||||
*
|
||||
* @param processor {@link CentralProcessor}
|
||||
* @param waitingTime 设置等待时间
|
||||
* @return 系统 CPU 使用率 等信息
|
||||
*/
|
||||
private static CpuInfo getCpuInfo(CentralProcessor processor, long waitingTime) {
|
||||
CpuInfo cpuInfo = new CpuInfo();
|
||||
// CPU信息
|
||||
long[] prevTicks = processor.getSystemCpuLoadTicks();
|
||||
// 这里必须要设置延迟
|
||||
Util.sleep(waitingTime);
|
||||
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 = Math.max(user + nice + cSys + idle + ioWait + irq + softIrq + steal, 0);
|
||||
final DecimalFormat format = new DecimalFormat("#.00");
|
||||
cpuInfo.setCpuNum(processor.getLogicalProcessorCount());
|
||||
cpuInfo.setToTal(totalCpu);
|
||||
cpuInfo.setSys(Double.parseDouble(format.format(cSys <= 0 ? 0 : (100d * cSys / totalCpu))));
|
||||
cpuInfo.setUsed(Double.parseDouble(format.format(user <= 0 ? 0 : (100d * user / totalCpu))));
|
||||
if (totalCpu == 0) {
|
||||
cpuInfo.setWait(0);
|
||||
} else {
|
||||
cpuInfo.setWait(Double.parseDouble(format.format(100d * ioWait / totalCpu)));
|
||||
}
|
||||
cpuInfo.setFree(Double.parseDouble(format.format(idle <= 0 ? 0 : (100d * idle / totalCpu))));
|
||||
cpuInfo.setCpuModel(processor.toString());
|
||||
return cpuInfo;
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
package cn.hutool.system;
|
||||
|
||||
import cn.hutool.system.oshi.CpuInfo;
|
||||
import cn.hutool.system.oshi.OshiUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.system.oshi.OshiUtil;
|
||||
|
||||
public class OshiTest {
|
||||
|
||||
@Test
|
||||
@@ -12,4 +12,10 @@ public class OshiTest {
|
||||
long total = OshiUtil.getMemory().getTotal();
|
||||
Assert.assertTrue(total > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCupInfo() {
|
||||
CpuInfo cpuInfo = OshiUtil.getCpuInfo();
|
||||
Assert.assertNotNull(cpuInfo);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user