棣栧厛緙栧啓涓涓狹onitorInfoBean綾伙紝鐢ㄦ潵瑁呰澆鐩戞帶鐨勪竴浜涗俊鎭紝鍖呮嫭鐗╃悊鍐呭瓨銆佸墿浣欑殑鐗╃悊鍐呭瓨銆佸凡浣跨敤鐨勭墿鐞嗗唴瀛樸佸唴瀛樹嬌鐢ㄧ巼絳夊瓧孌碉紝璇ョ被鐨勪唬鐮佸涓嬶細
package com.amigo.performance;
/**
* 鐩戣淇℃伅鐨凧avaBean綾?
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0
* Creation date: 2008-4-25 - 涓婂崍10:37:00
*/
public class MonitorInfoBean {
/** 鍙嬌鐢ㄥ唴瀛? */
private long totalMemory;
/** 鍓╀綑鍐呭瓨. */
private long freeMemory;
/** 鏈澶у彲浣跨敤鍐呭瓨. */
private long maxMemory;
/** 鎿嶄綔緋葷粺. */
private String osName;
/** 鎬葷殑鐗╃悊鍐呭瓨. */
private long totalMemorySize;
/** 鍓╀綑鐨勭墿鐞嗗唴瀛? */
private long freePhysicalMemorySize;
/** 宸蹭嬌鐢ㄧ殑鐗╃悊鍐呭瓨. */
private long usedMemory;
/** 綰跨▼鎬繪暟. */
private int totalThread;
/** cpu浣跨敤鐜? */
private double cpuRatio;
public long getFreeMemory() {
return freeMemory;
}
public void setFreeMemory(long freeMemory) {
this.freeMemory = freeMemory;
}
public long getFreePhysicalMemorySize() {
return freePhysicalMemorySize;
}
public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
this.freePhysicalMemorySize = freePhysicalMemorySize;
}
public long getMaxMemory() {
return maxMemory;
}
public void setMaxMemory(long maxMemory) {
this.maxMemory = maxMemory;
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
public long getTotalMemory() {
return totalMemory;
}
public void setTotalMemory(long totalMemory) {
this.totalMemory = totalMemory;
}
public long getTotalMemorySize() {
return totalMemorySize;
}
public void setTotalMemorySize(long totalMemorySize) {
this.totalMemorySize = totalMemorySize;
}
public int getTotalThread() {
return totalThread;
}
public void setTotalThread(int totalThread) {
this.totalThread = totalThread;
}
public long getUsedMemory() {
return usedMemory;
}
public void setUsedMemory(long usedMemory) {
this.usedMemory = usedMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}
鎺ョ潃緙栧啓涓涓幏寰楀綋鍓嶇殑鐩戞帶淇℃伅鐨勬帴鍙o紝璇ョ被鐨勪唬鐮佸涓嬫墍紺猴細
package com.amigo.performance;
/**
* 鑾峰彇緋葷粺淇℃伅鐨勪笟鍔¢昏緫綾繪帴鍙?
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0
* Creation date: 2008-3-11 - 涓婂崍10:06:06
*/
public interface IMonitorService {
/**
* 鑾峰緱褰撳墠鐨勭洃鎺у璞?
* @return 榪斿洖鏋勯犲ソ鐨勭洃鎺у璞?br /> * @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2008-4-25 - 涓婂崍10:45:08
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}
璇ョ被鐨勫疄鐜扮被MonitorServiceImpl濡備笅鎵紺猴細
package com.amigo.performance;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
/**
* 鑾峰彇緋葷粺淇℃伅鐨勪笟鍔¢昏緫瀹炵幇綾?
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0 Creation date: 2008-3-11 - 涓婂崍10:06:06
*/
public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
/**
* 鑾峰緱褰撳墠鐨勭洃鎺у璞?
* @return 榪斿洖鏋勯犲ソ鐨勭洃鎺у璞?br /> * @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2008-4-25 - 涓婂崍10:45:08
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
// 鍙嬌鐢ㄥ唴瀛?/span>
long totalMemory = Runtime.getRuntime().totalMemory() / kb;
// 鍓╀綑鍐呭瓨
long freeMemory = Runtime.getRuntime().freeMemory() / kb;
// 鏈澶у彲浣跨敤鍐呭瓨
long maxMemory = Runtime.getRuntime().maxMemory() / kb;
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 鎿嶄綔緋葷粺
String osName = System.getProperty("os.name");
// 鎬葷殑鐗╃悊鍐呭瓨
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
// 鍓╀綑鐨勭墿鐞嗗唴瀛?/span>
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
// 宸蹭嬌鐢ㄧ殑鐗╃悊鍐呭瓨
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
/ kb;
// 鑾峰緱綰跨▼鎬繪暟
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
int totalThread = parentThread.activeCount();
double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
}
// 鏋勯犺繑鍥炲璞?/span>
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
infoBean.setMaxMemory(maxMemory);
infoBean.setOsName(osName);
infoBean.setTotalMemory(totalMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setTotalThread(totalThread);
infoBean.setUsedMemory(usedMemory);
infoBean.setCpuRatio(cpuRatio);
return infoBean;
}
/**
* 鑾峰緱CPU浣跨敤鐜?
* @return 榪斿洖cpu浣跨敤鐜?br /> * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2008-4-25 - 涓嬪崍06:05:11
*/
private double getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 鍙栬繘紼嬩俊鎭?/span>
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
} else {
return 0.0;
}
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
}
}
/**
* 璇誨彇CPU淇℃伅.
* @param proc
* @return
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2008-4-25 - 涓嬪崍06:10:14
*/
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 瀛楁鍑虹幇欏哄簭錛欳aption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = Bytes.substring(line, capidx, cmdidx - 1)
.trim();
String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
// log.info("line="+line);
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
idletime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
continue;
}
kneltime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
usertime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 嫻嬭瘯鏂規硶.
* @param args
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2008-4-30 - 涓嬪崍04:47:29
*/
public static void main(String[] args) throws Exception {
IMonitorService service = new MonitorServiceImpl();
MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
System.out.println("cpu鍗犳湁鐜?" + monitorInfo.getCpuRatio());
System.out.println("鍙嬌鐢ㄥ唴瀛?" + monitorInfo.getTotalMemory());
System.out.println("鍓╀綑鍐呭瓨=" + monitorInfo.getFreeMemory());
System.out.println("鏈澶у彲浣跨敤鍐呭瓨=" + monitorInfo.getMaxMemory());
System.out.println("鎿嶄綔緋葷粺=" + monitorInfo.getOsName());
System.out.println("鎬葷殑鐗╃悊鍐呭瓨=" + monitorInfo.getTotalMemorySize() + "kb");
System.out.println("鍓╀綑鐨勭墿鐞嗗唴瀛?" + monitorInfo.getFreeMemory() + "kb");
System.out.println("宸蹭嬌鐢ㄧ殑鐗╃悊鍐呭瓨=" + monitorInfo.getUsedMemory() + "kb");
System.out.println("綰跨▼鎬繪暟=" + monitorInfo.getTotalThread() + "kb");
}
}
璇ュ疄鐜扮被涓渶瑕佺敤鍒頒竴涓嚜宸辯紪鍐檅yte鐨勫伐鍏風被錛岃綾葷殑浠g爜濡備笅鎵紺猴細
package com.amigo.performance;
/**
* byte鎿嶄綔綾?
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0
* Creation date: 2008-4-30 - 涓嬪崍04:57:23
*/
public class Bytes {
/**
* 鐢變簬String.subString瀵規眽瀛楀鐞嗗瓨鍦ㄩ棶棰橈紙鎶婁竴涓眽瀛楄涓轟竴涓瓧鑺?錛屽洜姝ゅ湪
* 鍖呭惈姹夊瓧鐨勫瓧絎︿覆鏃跺瓨鍦ㄩ殣鎮o紝鐜拌皟鏁村涓嬶細
* @param src 瑕佹埅鍙栫殑瀛楃涓?br /> * @param start_idx 寮濮嬪潗鏍囷紙鍖呮嫭璇ュ潗鏍?
* @param end_idx 鎴鍧愭爣錛堝寘鎷鍧愭爣錛?br /> * @return
*/
public static String substring(String src, int start_idx, int end_idx){
byte[] b = src.getBytes();
String tgt = "";
for(int i=start_idx; i<=end_idx; i++){
tgt +=(char)b[i];
}
return tgt;
}
}
榪愯涓婱onitorBeanImpl綾伙紝璇昏呭皢浼氱湅鍒板綋鍓嶇殑鍐呭瓨銆乧pu鍒╃敤鐜囩瓑淇℃伅銆?/span>