2.Sigar获取CPU信息

CPU的基本信息有:vendor、model、mhz、cacheSize;

CPU的统计信息有:user进程占用的百分比、sys进程占用的百分比、nice进程占用的百分比、idle进程占用的百分比、wait进程占用的百分比。

Java代码:

使用的Jar包:sigar.jar、xstream-1.3.1.jar

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package test1;  
  2.   
  3. import org.hyperic.sigar.Cpu;  
  4. import org.hyperic.sigar.CpuInfo;  
  5. import org.hyperic.sigar.CpuPerc;  
  6. import org.hyperic.sigar.Sigar;  
  7. import org.hyperic.sigar.SigarException;  
  8.   
  9. import com.thoughtworks.xstream.XStream;  
  10.   
  11. /** 
  12.  * CPU的基本信息、使用百分比、使用时间 
  13.  */  
  14. public class CpuInfoData {  
  15.     private CpuInfo info;  
  16.     private CpuPerc perc;  
  17.     private Cpu timer;  
  18.   
  19.     public void populate(Sigar sigar) throws SigarException {  
  20.         info = sigar.getCpuInfoList()[0];  
  21.         perc = sigar.getCpuPerc();  
  22.         timer = sigar.getCpu();  
  23.     }  
  24.   
  25.     public static CpuInfoData gather(Sigar sigar) throws SigarException {  
  26.         CpuInfoData data = new CpuInfoData();  
  27.         data.populate(sigar);  
  28.         return data;  
  29.     }  
  30.   
  31.     public static void main(String[] args) throws Exception {  
  32.         Sigar sigar = new Sigar();  
  33.         CpuInfoData cpuInfoData = CpuInfoData.gather(sigar);  
  34.         XStream xstream = new XStream();  
  35.         xstream.alias("CpuInfoData", CpuInfoData.class);  
  36.         System.out.println(xstream.toXML(cpuInfoData));  
  37.     }  
  38.   
  39.     public CpuInfo getInfo() {  
  40.         return info;  
  41.     }  
  42.   
  43.     public void setInfo(CpuInfo info) {  
  44.         this.info = info;  
  45.     }  
  46.   
  47.     public CpuPerc getPerc() {  
  48.         return perc;  
  49.     }  
  50.   
  51.     public void setPerc(CpuPerc perc) {  
  52.         this.perc = perc;  
  53.     }  
  54.   
  55.     public Cpu getTimer() {  
  56.         return timer;  
  57.     }  
  58.   
  59.     public void setTimer(Cpu timer) {  
  60.         this.timer = timer;  
  61.     }  
  62.   
  63. }  

输出:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <CpuInfoData>  
  2.   <info>  
  3.     <vendor>Intel</vendor>  
  4.     <model>Pentium(R) CPU G850 @ 2.90GHz</model>  
  5.     <mhz>2892</mhz>  
  6.     <cacheSize>-1</cacheSize>  
  7.     <totalCores>2</totalCores>  
  8.     <totalSockets>2</totalSockets>  
  9.     <coresPerSocket>1</coresPerSocket>  
  10.   </info>  
  11.   <perc>  
  12.     <user>0.11022044088176353</user>  
  13.     <sys>0.09318637274549098</sys>  
  14.     <nice>0.0</nice>  
  15.     <idle>0.7965931863727455</idle>  
  16.     <wait>0.0</wait>  
  17.     <irq>0.0</irq>  
  18.     <softIrq>0.0</softIrq>  
  19.     <stolen>0.0</stolen>  
  20.     <combined>0.2034068136272545</combined>  
  21.   </perc>  
  22.   <timer>  
  23.     <user>6815574</user>  
  24.     <sys>4927446</sys>  
  25.     <nice>0</nice>  
  26.     <idle>38219136</idle>  
  27.     <wait>0</wait>  
  28.     <irq>110073</irq>  
  29.     <softIrq>0</softIrq>  
  30.     <stolen>0</stolen>  
  31.     <total>74943593</total>  
  32.   </timer>  
  33. </CpuInfoData>  

Java代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package test3;  
  2.   
  3. import org.hyperic.sigar.CpuInfo;  
  4. import org.hyperic.sigar.CpuPerc;  
  5. import org.hyperic.sigar.Sigar;  
  6. import org.hyperic.sigar.SigarException;  
  7. import org.hyperic.sigar.SigarLoader;  
  8.   
  9. /** 
  10.  * Display cpu information for each cpu found on the system. 
  11.  */  
  12. public class CpuInfoTest {  
  13.     private Sigar sigar = new Sigar();  
  14.   
  15.     private void output(CpuPerc cpu) {  
  16.         System.out.println("User Time....." + CpuPerc.format(cpu.getUser()));// 用户态使用的CPU百分比  
  17.         System.out.println("Sys Time......" + CpuPerc.format(cpu.getSys()));// 系统态使用的CPU百分比  
  18.         System.out.println("Idle Time....." + CpuPerc.format(cpu.getIdle()));// CPU空闲时间  
  19.         System.out.println("Wait Time....." + CpuPerc.format(cpu.getWait()));// CPU等待时间  
  20.         System.out.println("Nice Time....." + CpuPerc.format(cpu.getNice()));//  
  21.         System.out.println("Combined......" + CpuPerc.format(cpu.getCombined()));// User Time + Sys Time  
  22.         System.out.println("Irq Time......" + CpuPerc.format(cpu.getIrq()));// CPU中断时间  
  23.         if (SigarLoader.IS_LINUX) {  
  24.             System.out.println("SoftIrq Time.." + CpuPerc.format(cpu.getSoftIrq()));// 软中断CPU时间  
  25.             System.out.println("Stolen Time...." + CpuPerc.format(cpu.getStolen()));  
  26.         }  
  27.         System.out.println("");  
  28.     }  
  29.   
  30.     public void output(String[] args) throws SigarException {  
  31.         CpuInfo[] infos = this.sigar.getCpuInfoList();//  
  32.         CpuInfo info = infos[0];  
  33.         System.out.println("Vendor........." + info.getVendor());// 芯片供应商  
  34.         System.out.println("Model.........." + info.getModel());// CPU型号  
  35.         System.out.println("Mhz............" + info.getMhz());// 主频  
  36.         System.out.println("Total CPUs....." + info.getTotalCores());// CPU逻辑个数  
  37.   
  38.         if ((info.getTotalCores() != info.getTotalSockets()) || (info.getCoresPerSocket() > info.getTotalCores())) {  
  39.             System.out.println("Physical CPUs.." + info.getTotalSockets());// CPU物理个数  
  40.             System.out.println("Cores per CPU.." + info.getCoresPerSocket());// 每个CPU核数  
  41.         }  
  42.   
  43.         long cacheSize = info.getCacheSize();  
  44.         if (cacheSize != Sigar.FIELD_NOTIMPL) {  
  45.             System.out.println("Cache size...." + cacheSize);// 缓存  
  46.         }  
  47.         System.out.println("");  
  48.   
  49.         // 每个核使用情况  
  50.         CpuPerc[] cpus = this.sigar.getCpuPercList();  
  51.         for (int i = 0; i < cpus.length; i++) {  
  52.             System.out.println("CPU " + i + ".........");  
  53.             output(cpus[i]);  
  54.         }  
  55.   
  56.         // 整个CPU使用情况  
  57.         System.out.println("Totals........");  
  58.         output(this.sigar.getCpuPerc());  
  59.     }  
  60.   
  61.     public static void main(String[] args) throws Exception {  
  62.         new CpuInfoTest().output(args);  
  63.     }  
  64. }  

输出:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Vendor.........Intel  
  2. Model..........Pentium(R) CPU G850 @ 2.90GHz  
  3. Mhz............2892  
  4. Total CPUs.....2  
  5.   
  6. CPU 0.........  
  7. User Time.....6.2%  
  8. Sys Time......9.2%  
  9. Idle Time.....84.5%  
  10. Wait Time.....0.0%  
  11. Nice Time.....0.0%  
  12. Combined......15.4%  
  13. Irq Time......0.0%  
  14.   
  15. CPU 1.........  
  16. User Time.....3.2%  
  17. Sys Time......6.2%  
  18. Idle Time.....90.6%  
  19. Wait Time.....0.0%  
  20. Nice Time.....0.0%  
  21. Combined......9.4%  
  22. Irq Time......0.0%  
  23.   
  24. Totals........  
  25. User Time.....9.3%  
  26. Sys Time......18.8%  
  27. Idle Time.....71.8%  
  28. Wait Time.....0.0%  
  29. Nice Time.....0.0%  
  30. Combined......28.1%  
  31. Irq Time......0.0%  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值