package com.hotpot.boos.interaction.ftpUtil.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.log4j.Logger;
/**
* 採集CPU使用率
*/
public class CpuUsage {
private static Logger log = Logger.getLogger(CpuUsage.class);
private static CpuUsage INSTANCE = new CpuUsage();
private CpuUsage(){
}
public static CpuUsage getInstance(){
return INSTANCE;
}
/**
* Purpose:採集CPU使用率
* @param args
* @return float,CPU使用率,小於1
*/
public float get() {
log.info("開始收集cpu使用率");
float cpuUsage = 0;
Process pro1,pro2;
Runtime r = Runtime.getRuntime();
try {
String command = "cat /proc/stat";
//第一次採集CPU時間
long startTime = System.currentTimeMillis();
pro1 = r.exec(command);
BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
String line = null;
long idleCpuTime1 = 0, totalCpuTime1 = 0; //分別爲系統啓動後空閒的CPU時間和總的CPU時間
while((line=in1.readLine()) != null){
if(line.startsWith("cpu")){
line = line.trim();
log.info(line);
String[] temp = line.split("\\s+");
idleCpuTime1 = Long.parseLong(temp[4]);
for(String s : temp){
if(!s.equals("cpu")){
totalCpuTime1 += Long.parseLong(s);
}
}
log.info("IdleCpuTime: " + idleCpuTime1 + ", " + "TotalCpuTime" + totalCpuTime1);
break;
}
}
in1.close();
pro1.destroy();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("CpuUsage休眠時發生InterruptedException. " + e.getMessage());
log.error(sw.toString());
}
//第二次採集CPU時間
long endTime = System.currentTimeMillis();
pro2 = r.exec(command);
BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
long idleCpuTime2 = 0, totalCpuTime2 = 0; //分別爲系統啓動後空閒的CPU時間和總的CPU時間
while((line=in2.readLine()) != null){
if(line.startsWith("cpu")){
line = line.trim();
log.info(line);
String[] temp = line.split("\\s+");
idleCpuTime2 = Long.parseLong(temp[4]);
for(String s : temp){
if(!s.equals("cpu")){
totalCpuTime2 += Long.parseLong(s);
}
}
log.info("IdleCpuTime: " + idleCpuTime2 + ", " + "TotalCpuTime" + totalCpuTime2);
break;
}
}
if(idleCpuTime1 != 0 && totalCpuTime1 !=0 && idleCpuTime2 != 0 && totalCpuTime2 !=0){
cpuUsage = 1 - (float)(idleCpuTime2 - idleCpuTime1)/(float)(totalCpuTime2 - totalCpuTime1);
log.info("本節點CPU使用率爲: " + cpuUsage);
}
in2.close();
pro2.destroy();
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("CpuUsage發生InstantiationException. " + e.getMessage());
log.error(sw.toString());
}
return cpuUsage;
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
while(true){
System.out.println(CpuUsage.getInstance().get());
Thread.sleep(5000);
}
}
}