码迷,mamicode.com
首页 > Windows程序 > 详细

借助Sigar API获取CPU相关信息

时间:2015-02-27 15:17:00      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

        Sigar(全称System Information Gatherer And Reporter,即系统信息收集报表器),它提供了一个开源的跨平台的收集计算机硬件和操作系统信息的API(该API底层接口用C语言编写),本文将演示如何借助Sigar API获取CPU相关信息:

package com.ghj.packageoftest;

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;

/**
 * 借助Sigar API获取CPU相关信息
 * 
 * @author GaoHuanjie 
 */
public class CpuTool {

	public static void main(String[] args) throws SigarException {
		Sigar sigar = new Sigar();

		CpuInfo info = sigar.getCpuInfoList()[0];
		System.out.println("CpuInfo mhz:" + info.getMhz());
		System.out.println("CpuInfo vendor:" + info.getVendor());
		System.out.println("CpuInfo Model:" + info.getModel());
		System.out.println("CpuInfo cacheSize:" + info.getCacheSize());
		System.out.println("CpuInfo totalCores:" + info.getTotalCores());
		System.out.println("CpuInfo totalSockets:" + info.getTotalSockets());
		System.out.println("CpuInfo coresPerSocket:" + info.getCoresPerSocket());

		Cpu cpu = sigar.getCpu();
		System.err.println("CPU user:" + cpu.getUser());
		System.err.println("CPU sys:" + cpu.getSys());
		System.err.println("CPU nice:" + cpu.getNice());
		System.err.println("CPU idle:" + cpu.getIdle());
		System.err.println("CPU wait:" + cpu.getWait());
		System.err.println("CPU irq:" + cpu.getIrq());
		System.err.println("CPU total:" + cpu.getTotal());
		System.err.println("CPU stolen:" + cpu.getStolen());
		System.err.println("CPU softIrq:" + cpu.getSoftIrq());

		CpuPerc cpuPerc = sigar.getCpuPerc();
		System.out.println("CpuPerc user:" + cpuPerc.getUser());
		System.out.println("CpuPerc sys:" + cpuPerc.getSys());
		System.out.println("CpuPerc nice:" + cpuPerc.getNice());
		System.out.println("CpuPerc idle:" + cpuPerc.getIdle());
		System.out.println("CpuPerc wait:" + cpuPerc.getWait());
		System.out.println("CpuPerc stolen:" + cpuPerc.getStolen());
		System.out.println("CpuPerc softIrq:" + cpuPerc.getSoftIrq());
		System.out.println("CpuPerc combined:" + cpuPerc.getCombined());
	}
}
        【0分下载实例代码

借助Sigar API获取CPU相关信息

标签:

原文地址:http://blog.csdn.net/gaohuanjie/article/details/43967093

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!