* @throws InterruptedException
*/
public static float getCpuInfo() throws IOException, InterruptedException
{
File file = new File("/proc/stat");
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
StringTokenizer token = new StringTokenizer(br.readLine());
token.nextToken();
int user1 = Integer.parseInt(token.nextToken());
int nice1 = Integer.parseInt(token.nextToken());
int sys1 = Integer.parseInt(token.nextToken());
int idle1 = Integer.parseInt(token.nextToken());
Thread.sleep(1000);
br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
token = new StringTokenizer(br.readLine());
token.nextToken();
int user2 = Integer.parseInt(token.nextToken());
int nice2 = Integer.parseInt(token.nextToken());
int sys2 = Integer.parseInt(token.nextToken());
int idle2 = Integer.parseInt(token.nextToken());
return (float)((user2 + sys2 + nice2) - (user1 + sys1 + nice1)) / (float)((user2 + nice2 + sys2 + idle2) - (user1 + nice1 + sys1 + idle1));
}
}
/**
* 测试类
*
* <p>@author javer QQ:84831612</p>
* @date 2005
*/
public class JaverTest
{
public static void main(String args) throws Exception
{
int memInfo = LinuxSystemTool.getMemInfo();
System.out.println("MemTotal:" + memInfo[0]);
System.out.println("MemFree:" + memInfo);
System.out.println("SwapTotal:" + memInfo);
System.out.println("SwapFree:" + memInfo);
System.out.println("CPU利用率:" + LinuxSystemTool.getCpuInfo());
}
}