Thursday, July 08, 2010

โค้ดจาวา แสดงชื่อ CPU

/*
* CpuInfo.java
*
*/
package pcu.config;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarLoader;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.Shell;
import org.hyperic.sigar.cmd.SigarCommandBase;

public class CpuInfo extends SigarCommandBase {

public boolean displayTimes = true;

public CpuInfo(Shell shell) {
super(shell);
}

public CpuInfo() {
super();
}

public String getUsageShort() {
return "Display cpu information";
}

private void output(CpuPerc cpu) {
println("User Time....." + CpuPerc.format(cpu.getUser()));
println("Sys Time......" + CpuPerc.format(cpu.getSys()));
println("Idle Time....." + CpuPerc.format(cpu.getIdle()));
println("Wait Time....." + CpuPerc.format(cpu.getWait()));
println("Nice Time....." + CpuPerc.format(cpu.getNice()));
println("Combined......" + CpuPerc.format(cpu.getCombined()));
println("Irq Time......" + CpuPerc.format(cpu.getIrq()));
if (SigarLoader.IS_LINUX) {
println("SoftIrq Time.." + CpuPerc.format(cpu.getSoftIrq()));
println("Stolen Time...." + CpuPerc.format(cpu.getStolen()));
}
println("");
}

public void output(String[] args) throws SigarException {
org.hyperic.sigar.CpuInfo[] infos = this.sigar.getCpuInfoList();

CpuPerc[] cpus = this.sigar.getCpuPercList();

org.hyperic.sigar.CpuInfo info = infos[0];
long cacheSize = info.getCacheSize();
println("Vendor........." + info.getVendor());
println("Model.........." + info.getModel());
println("Mhz............" + info.getMhz());
println("Total CPUs....." + info.getTotalCores());
if ((info.getTotalCores() != info.getTotalSockets()) ||
(info.getCoresPerSocket() > info.getTotalCores()))
{
println("Physical CPUs.." + info.getTotalSockets());
println("Cores per CPU.." + info.getCoresPerSocket());
}

if (cacheSize != Sigar.FIELD_NOTIMPL) {
println("Cache size...." + cacheSize);
}
println("");

if (!this.displayTimes) {
return;
}

for (int i=0; i println("CPU " + i + ".........");
output(cpus[i]);
}

println("Totals........");
output(this.sigar.getCpuPerc());
}

public static void main(String[] args) throws Exception {
//Determine System Architechture
String osName = System.getProperty("os.name");
String osArch = System.getProperty("os.arch");
String sigarFile = "";
if(osArch.toLowerCase().contains("x86")) {
if(osName.toLowerCase().contains("linux")) sigarFile = "libsigar-x86-linux.so";
else if(osName.toLowerCase().contains("windows")) sigarFile = "sigar-x86-winnt.dll";
} else if(osArch.toLowerCase().contains("amd64")) {
if(osName.toLowerCase().contains("linux")) sigarFile = "libsigar-amd64-linux.so";
else if(osName.toLowerCase().contains("windows")) sigarFile = "sigar-amd64-winnt.dll";
}
//Load library
String userDir = System.getProperty("user.dir");
System.load( (userDir+"/lib/lib_hyperic-sigar-1.6.3/"+sigarFile) );

//Execute
new CpuInfo().processCommand(args);
}
}


จะแสดงผลลัพธ์

Model..........Core(TM)2 Duo CPU P8700 @ 2.53GHz
Mhz............2527
Total CPUs.....2
Physical CPUs..1
Cores per CPU..2

CPU 0.........
User Time.....30.3%
Sys Time......36.3%
Idle Time.....33.2%
Wait Time.....0.0%
Nice Time.....0.0%
Combined......66.7%
Irq Time......0.0%

CPU 1.........
User Time.....33.2%
Sys Time......36.3%
Idle Time.....30.3%
Wait Time.....0.0%
Nice Time.....0.0%
Combined......69.6%
Irq Time......0.0%

Totals........
User Time.....6.4%
Sys Time......0.0%
Idle Time.....93.6%
Wait Time.....0.0%
Nice Time.....0.0%
Combined......6.4%
Irq Time......0.0%