SIGAR (full Name System Information Gatherer and Reporter, which is the Systems information Collection Report), provides an open source, cross-platform API for collecting computer hardware and operating system information (the API's underlying interface is written in C). This article shows you how to get CPU-related information with the SIGAR API:
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;/** * with SIGAR API gets CPU-related information * * @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 min Download Instance code】
Get CPU-related information with the SIGAR API