对/proc/net目录的解释参考:
http://www.linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html?page=1
一、CPU
使用proc文件系统,"proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。"
从/proc文件系统获取cpu使用情况: cat /proc/stat
在Linux的内核中,有一个全 局变量:Jiffies。 Jiffies代表时间。它的单位随硬件平台的不同而不同。系统里定义了一个常数HZ,代表每秒种最小时间间隔的数目。这样jiffies的单位就是 1/HZ。Intel平台jiffies的单位是1/100秒,这就是系统所能分辨的最小时间间隔了。每个CPU时间片,Jiffies都要加1。 CPU的利用率就是用执行用户态+系统态的Jiffies除以总的Jifffies来表示。
[root@localhost LoadBalanceAlg]# cat /proc/stat
cpu 71095 55513 76751 2545622893 303185 4160 47722 0
cpu0 3855 1134 4284 159122519 3882 0 717 0
cpu1 4236 770 5837 159113370 11291 6 865 0
cpu2 4934 1142 5048 158991321 130622 362 2939 0
cpu3 2320 14774 5177 159111528 1417 8 1138 0
cpu4 2694 405 3086 159071174 56284 235 2477 0
cpu5 1701 886 2560 159129034 1316 2 849 0
cpu6 2937 450 2863 159068480 59183 228 2198 0
cpu7 916 316 2426 159130057 1682 1 933 0
cpu8 2543 50 3509 159122844 4467 1 2911 0
cpu9 4761 827 6296 159118849 4490 8 1086 0
cpu10 8517 4236 9148 159102063 9791 173 2382 0
cpu11 22001 29737 14602 159065992 2583 6 1382 0
cpu12 3453 150 3075 159113794 5387 1162 9276 0
cpu13 2120 424 3403 159126526 2608 7 1199 0
cpu14 2637 65 2663 159107796 6704 1914 14503 0
cpu15 1462 142 2763 159127539 1470 39 2859 0
intr 1636622296 1591605869 4 0 4 4 0 0 0 1 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 952 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1005479 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32763528 0 0 0 0 0 0 0 1697776 0 0 0 0 0 0 0 1556158 2 0 0 0 0 0 0 1598011 0 0 0 0 0 0 0 1287622 0 0 0 0 0 0 0 1522517 0 0 0 0 0 0 0 2467360 0 0 0 0 0 0 0 1116999 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 431778894
btime 1363058934
processes 279394
procs_running 1
procs_blocked 0
public class Runtime
每个 Java 应用程序都有一个 Runtime
类实例,使应用程序能够与其运行的环境相连接。可以通过getRuntime
方法获取当前运行时。
应用程序不能创建自己的 Runtime 类实例。
public abstract class Process
ProcessBuilder.start()
和Runtime.exec
方法创建一个本机进程,并返回 Process
子类的一个实例,该实例可用来控制进程并获得相关信息。
package site.mwq.resource; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; /** * 采集CPU使用率 */ public class CpuUsage extends ResourceUsage { private static CpuUsage INSTANCE = new CpuUsage(); private CpuUsage(){ } public static CpuUsage getInstance(){ return INSTANCE; } public String[] getCpuData(Runtime r){ Process pro = null; String[] res = null; String command = "cat /proc/stat"; try { pro = r.exec(command); BufferedReader in1 = new BufferedReader(new InputStreamReader(pro.getInputStream())); String line = null; while((line=in1.readLine()) != null){ if(line.startsWith("cpu")){ line = line.trim(); res = line.split("\\s+"); if(res[0].equals("cpu")){ Print.printLine(line); break; } } } in1.close(); pro.destroy(); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); } return res; } /** * Purpose:采集CPU使用率 * @param args * @return float,CPU使用率,小于1 */ @Override public float get() { long idleCpuTime1 = 0, totalCpuTime1 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间 long idleCpuTime2 = 0, totalCpuTime2 = 0; float cpuUsage = 0; //// Runtime runtime = Runtime.getRuntime(); String[] data1 = getCpuData(runtime); //第一次测数据 for(int i=1;i<data1.length;i++){ totalCpuTime1 += Long.parseLong(data1[i]); } idleCpuTime1 = Long.parseLong(data1[4]); try { Thread.sleep(1000); /**等待时间**/ } catch (InterruptedException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); } String[] data2 = getCpuData(runtime); //第二次测数据 for(int i=1;i<data2.length;i++){ totalCpuTime2 += Long.parseLong(data2[i]); } idleCpuTime2 = Long.parseLong(data2[4]); //// if(idleCpuTime1 != 0 && totalCpuTime1 !=0 && idleCpuTime2 != 0 && totalCpuTime2 !=0){ cpuUsage = 1 - (float)(idleCpuTime2 - idleCpuTime1)/(float)(totalCpuTime2 - totalCpuTime1); } 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); } } }
package site.mwq.resource; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; /** * 采集内存使用率 */ public class MemUsage extends ResourceUsage{ private static MemUsage INSTANCE = new MemUsage(); private MemUsage(){} public static MemUsage getInstance(){ return INSTANCE; } /** * Purpose:采集内存使用率 * @param args * @return float,内存使用率,小于1 */ @Override public float get() { float memUsage = 0.0f; Process pro = null; Runtime r = Runtime.getRuntime(); String command = "cat /proc/meminfo"; try { pro = r.exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream())); String line = null; int count = 0; long totalMem = 0, freeMem = 0; while((line=in.readLine()) != null){ String[] memInfo = line.split("\\s+"); if(memInfo[0].startsWith("MemTotal")){ totalMem = Long.parseLong(memInfo[1]); } if(memInfo[0].startsWith("MemFree")){ freeMem = Long.parseLong(memInfo[1]); } memUsage = 1- (float)freeMem/(float)totalMem; if(++count == 2){ break; } } in.close(); pro.destroy(); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); } return memUsage; } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { while(true){ System.out.println(MemUsage.getInstance().get()); Thread.sleep(5000); } } }
package site.mwq.resource; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; /** * 采集网络带宽使用率 */ public class NetUsage extends ResourceUsage { private static NetUsage INSTANCE = new NetUsage(); private final static float TotalBandwidth = 1000; //网口带宽,Mbps,使用命令ethtool eth0查看 private NetUsage(){} public static NetUsage getInstance(){ return INSTANCE; } public String[] getNetData(Runtime r){ Process pro1 = null; String command = "cat /proc/net/dev"; String[] res = null; try { pro1 = r.exec(command); BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream())); String line = null; while((line=in1.readLine()) != null){ line = line.trim(); if(line.startsWith("eth0")){ res = line.split("\\s+"); break; } } in1.close(); pro1.destroy(); } catch (IOException e) { e.printStackTrace(); } return res; } /** * @Purpose:采集网络带宽使用率 * @param args * @return float,网络带宽使用率,小于1 */ @Override public float get() { float netUsage = 0.0f; long inSize1 = 0, outSize1 = 0; long inSize2 = 0 ,outSize2 = 0; /// Runtime r = Runtime.getRuntime(); long startTime = System.currentTimeMillis(); String[] data1 = getNetData(r); //第一次测数据 inSize1 = Long.parseLong(data1[1]); //Receive bytes,单位为Byte outSize1 = Long.parseLong(data1[9]); //Transmit bytes,单位为Byte try { Thread.sleep(1000); /**等待一段时间**/ } catch (InterruptedException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); } long endTime = System.currentTimeMillis(); String[] data2 = getNetData(r); //第二次测数据 inSize2 = Long.parseLong(data2[1]); outSize2 = Long.parseLong(data2[9]); if(inSize1 != 0 && outSize1 !=0 && inSize2 != 0 && outSize2 !=0){ float interval = (float)(endTime - startTime); //单位为毫秒 float curRate = (float)(inSize2 - inSize1 + outSize2 - outSize1)*8/(1000*interval); //网口传输速度, 单位为bps bit每秒 netUsage = curRate/TotalBandwidth; } return netUsage; } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { while(true){ System.out.println(NetUsage.getInstance().get()); Thread.sleep(5000); } } }
package site.mwq.resource; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; public class Print { public static File file = null; public static PrintWriter pw = null; static{ String path = System.getProperty("user.dir"); file = new File(path+"/log.txt"); try { pw = new PrintWriter(new FileOutputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void printLine(String str){ pw.println(str); pw.flush(); } public static void main(String[] args) { printLine("test"); } }