现在的位置: 首页 > 综合 > 正文

借助Sigar API获取CPU相关信息

2019年11月13日 ⁄ 综合 ⁄ 共 1842字 ⁄ 字号 评论关闭

        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分下载实例代码

抱歉!评论已关闭.