會出現硬碟、CPU 等使用率資訊,us指的是使用者耗用的CPU使用率, sy是作業系統本身耗
用CPU使用率,id則為idle的意思,表示閒置的CPU使用率,例如本例 100- 6 -4 = 90 。
中途施工時發現一個問題,也就是mac的disk數量會變動,第1個disk0為硬碟,但disk1有可
能是安裝程式產生出來的一個disk,或是隨身碟。
這個程式會利用到iostat 的一個參數 -w ,它可以在每次執行iostat 後,間隔指定的時間,再
輸出一次資料,直到永遠(除非你去取消它)。
這程式中用到了
iostat -w 1 ,表示每隔一秒會輸出一次資訊。
接下來是要用來繪圖用的一個JComponent 程式碼,透過
overwrite paintComponent 的方式來繪製元件的內容。
package javamaccpu_useage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
/**
*
* @author flowercatswets
*/
public class DrawPanel extends JComponent
{
int useage[]=new int[100];
int shift=30;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clipRect(0, 0, 120, 400);
g.setColor(Color.black);
//黑底
g.fillRect(shift, 0, shift+100, 100);
//畫白橫格線
g.setColor(Color.white);
for (int i=20;i<=100;i=i+20)
{
g.drawLine(shift, i, 100+shift, i);
}
//畫白垂直格線
for (int i=20;i<=100;i=i+20)
{
g.drawLine(shift+i, 0, shift+i, 100);
}
//畫上數值20 40 60 80 100
g.setColor(Color.black);
for (int i=100;i>=20;i=i-20)
{
g.drawString(String.valueOf(100-i), 0, i);
}
//畫上CPU使用率
g.setColor(Color.GREEN);
for (int i=0;i
然候是主程式
package javamaccpu_useage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JFrame;
/**
*
* @author flowercatswets
*/
public class JavaMacCPU_Useage {
public static void main(String[] args) {
//Init Class
JavaMacCPU_Useage cpu=new JavaMacCPU_Useage();
//Init JFrame
JFrame JF=new JFrame("Mac CPU");
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame.setDefaultLookAndFeelDecorated(true);
JF.setBounds(100, 100, 150, 150);
//JF.pack();
JF.setVisible(true);
//InitDrawPanel
DrawPanel dp=new DrawPanel();
//Add JPanel to JFrame
JF.getContentPane().add(dp);
//Set DrawPanel Size
dp.setBounds(0,0, 120, 120);
//Excute
cpu.executeShell(dp,JF);
}
public void executeShell(DrawPanel dp,JFrame JF)
{
//Init Process
Process p;
//Init CPU Useage array
int[] useage=new int[100];;
//command
String command="iostat -w 1";
try
{
//run command
p= Runtime.getRuntime().exec(command);
//get Process input
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
//Init
String line="";
int totaluse;
int offset = 0;
//get inpurt data
while((line=reader.readLine())!=null)
{
try
{
// disk0 disk1 cpu load average
// or
// KB/t tps MB/s KB/t tps MB/s us sy id 1m 5m 15m
if (line.contains("cpu") || line.contains("us"))
{
//disk0 disk1 cpu load average
if (line.contains("cpu"))
{
offset=findCPU_offset(line);
}
continue;
}
//get cpu data offset success
if (offset>0)
totaluse=getCPU(line,offset);
//get cpu data offset fail
else
totaluse=0;
//new data in the last of array
useage[(useage.length-1)]=totaluse;
//set data to draw
dp.setData(useage);
//offset data to left postion
for (int i=1;i<99;i++)
useage[i]=useage[i+1];
//repant
JF.repaint();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
catch(Exception e)
{
}
}
//取得CPU的資訊
private int getCPU(String line,int offset)
{
int count=0;
double us=0;
double sys=0;
String tmp[]=line.split(" ");
for (String tmp1 : tmp) {
if (tmp1.trim().length() > 0) {
count++;
if (count==(offset*3)+1) {
us = Double.parseDouble(tmp1);
} else if (count==(offset*3)+2) {
sys = Double.parseDouble(tmp1);
break;
}
}
}
return (int)(us+sys);
}
//判斷CPU 的資訊在第幾個位置
private int findCPU_offset(String line)
{
int count=0;
int offset=0;
String tmp[]=line.split(" ");
for (String tmp1 : tmp) {
if (tmp1.trim().length() > 0) {
count++;
if (tmp1.equals("cpu")) {
offset=count-1;
}
}
}
return offset;
}
}
接下來是執行結果截圖


沒有留言:
張貼留言