CPU的基本信息有:vendor、model、mhz、cacheSize;
CPU的统计信息有:user进程占用的百分比、sys进程占用的百分比、nice进程占用的百分比、idle进程占用的百分比、wait进程占用的百分比。
Java代码:
使用的Jar包:sigar.jar、xstream-1.3.1.jar
package test1;
import org.hyperic.sigar.Cpu;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import com.thoughtworks.xstream.XStream;
/**
* CPU的基本信息、使用百分比、使用时间
*/
public class CpuInfoData {
private CpuInfo info;
private CpuPerc perc;
private Cpu timer;
public void populate(Sigar sigar) throws SigarException {
info = sigar.getCpuInfoList()[0];
perc = sigar.getCpuPerc();
timer = sigar.getCpu();
}
public static CpuInfoData gather(Sigar sigar) throws SigarException {
CpuInfoData data = new CpuInfoData();
data.populate(sigar);
return data;
}
public static void main(String[] args) throws Exception {
Sigar sigar = new Sigar();
CpuInfoData cpuInfoData = CpuInfoData.gather(sigar);
XStream xstream = new XStream();
xstream.alias("CpuInfoData", CpuInfoData.class);
System.out.println(xstream.toXML(cpuInfoData));
}
public CpuInfo getInfo() {
return info;
}
public void setInfo(CpuInfo info) {
this.info = info;
}
public CpuPerc getPerc() {
return perc;
}
public void setPerc(CpuPerc perc) {
this.perc = perc;
}
public Cpu getTimer() {
return timer;
}
public void setTimer(Cpu timer) {
this.timer = timer;
}
}
输出:
<CpuInfoData>
<info>
<vendor>Intel</vendor>
<model>Pentium(R) CPU G850 @ 2.90GHz</model>
<mhz>2892</mhz>
<cacheSize>-1</cacheSize>
<totalCores>2</totalCores>
<totalSockets>2</totalSockets>
<coresPerSocket>1</coresPerSocket>
</info>
<perc>
<user>0.11022044088176353</user>
<sys>0.09318637274549098</sys>
<nice>0.0</nice>
<idle>0.7965931863727455</idle>
<wait>0.0</wait>
<irq>0.0</irq>
<softIrq>0.0</softIrq>
<stolen>0.0</stolen>
<combined>0.2034068136272545</combined>
</perc>
<timer>
<user>6815574</user>
<sys>4927446</sys>
<nice>0</nice>
<idle>38219136</idle>
<wait>0</wait>
<irq>110073</irq>
<softIrq>0</softIrq>
<stolen>0</stolen>
<total>74943593</total>
</timer>
</CpuInfoData>
Java代码:
package test3;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarLoader;
/**
* Display cpu information for each cpu found on the system.
*/
public class CpuInfoTest {
private Sigar sigar = new Sigar();
private void output(CpuPerc cpu) {
System.out.println("User Time....." + CpuPerc.format(cpu.getUser()));// 用户态使用的CPU百分比
System.out.println("Sys Time......" + CpuPerc.format(cpu.getSys()));// 系统态使用的CPU百分比
System.out.println("Idle Time....." + CpuPerc.format(cpu.getIdle()));// CPU空闲时间
System.out.println("Wait Time....." + CpuPerc.format(cpu.getWait()));// CPU等待时间
System.out.println("Nice Time....." + CpuPerc.format(cpu.getNice()));//
System.out.println("Combined......" + CpuPerc.format(cpu.getCombined()));// User Time + Sys Time
System.out.println("Irq Time......" + CpuPerc.format(cpu.getIrq()));// CPU中断时间
if (SigarLoader.IS_LINUX) {
System.out.println("SoftIrq Time.." + CpuPerc.format(cpu.getSoftIrq()));// 软中断CPU时间
System.out.println("Stolen Time...." + CpuPerc.format(cpu.getStolen()));
}
System.out.println("");
}
public void output(String[] args) throws SigarException {
CpuInfo[] infos = this.sigar.getCpuInfoList();//
CpuInfo info = infos[0];
System.out.println("Vendor........." + info.getVendor());// 芯片供应商
System.out.println("Model.........." + info.getModel());// CPU型号
System.out.println("Mhz............" + info.getMhz());// 主频
System.out.println("Total CPUs....." + info.getTotalCores());// CPU逻辑个数
if ((info.getTotalCores() != info.getTotalSockets()) || (info.getCoresPerSocket() > info.getTotalCores())) {
System.out.println("Physical CPUs.." + info.getTotalSockets());// CPU物理个数
System.out.println("Cores per CPU.." + info.getCoresPerSocket());// 每个CPU核数
}
long cacheSize = info.getCacheSize();
if (cacheSize != Sigar.FIELD_NOTIMPL) {
System.out.println("Cache size...." + cacheSize);// 缓存
}
System.out.println("");
// 每个核使用情况
CpuPerc[] cpus = this.sigar.getCpuPercList();
for (int i = 0; i < cpus.length; i++) {
System.out.println("CPU " + i + ".........");
output(cpus[i]);
}
// 整个CPU使用情况
System.out.println("Totals........");
output(this.sigar.getCpuPerc());
}
public static void main(String[] args) throws Exception {
new CpuInfoTest().output(args);
}
}
输出:
Vendor.........Intel
Model..........Pentium(R) CPU G850 @ 2.90GHz
Mhz............2892
Total CPUs.....2
CPU 0.........
User Time.....6.2%
Sys Time......9.2%
Idle Time.....84.5%
Wait Time.....0.0%
Nice Time.....0.0%
Combined......15.4%
Irq Time......0.0%
CPU 1.........
User Time.....3.2%
Sys Time......6.2%
Idle Time.....90.6%
Wait Time.....0.0%
Nice Time.....0.0%
Combined......9.4%
Irq Time......0.0%
Totals........
User Time.....9.3%
Sys Time......18.8%
Idle Time.....71.8%
Wait Time.....0.0%
Nice Time.....0.0%
Combined......28.1%
Irq Time......0.0%