⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 systemresrc.java

📁 实现linux性能分析 如:cpu利用率
💻 JAVA
字号:
import java.io.*;import java.util.*;public class SystemResrc {	public static void main (String[] args) throws  FileNotFoundException ,IOException , InterruptedException{		RandomAccessFile filecpu=new RandomAccessFile("/proc/stat","r");		RandomAccessFile filemem=new RandomAccessFile("/proc/meminfo","r");		RandomAccessFile filenet=new RandomAccessFile("/proc/net/dev","r");		int i,j;		float cputm,memrt,netrcv,nettra;//		String cpu;				while(true){			filecpu.seek(0);			filenet.seek(0);			while(filenet.readLine().charAt(4)!='l');						Scanner cpustat1=new Scanner(filecpu.readLine());			Scanner netstat1=new Scanner(filenet.readLine());			filecpu.seek(0);			filenet.seek(0);			Thread.sleep(1000);			Scanner cpustat2=new Scanner(filecpu.readLine());			while(filenet.readLine().charAt(4)!='l');			Scanner netstat2=new Scanner(filenet.readLine());			cpustat1.next();cpustat2.next();			i=3;j=0;cputm=0;memrt=0;			netstat1.next();netstat2.next();			netrcv=netstat2.nextLong()-netstat1.nextLong();			while(j++<7){netstat1.nextLong();netstat2.nextLong();}			nettra=netstat2.nextLong()-netstat1.nextLong();			System.out.println(""+netrcv+"&"+nettra);//net			while(i-->0)				cputm+=(cpustat2.nextLong()-cpustat1.nextLong());			cputm/=(cputm+cpustat2.nextLong()-cpustat1.nextLong());			cputm*=100;			System.out.println(""+cputm+"%");//cpu						Scanner mem1=new Scanner(filemem.readLine());			Scanner mem2=new Scanner(filemem.readLine());			Scanner mem3=new Scanner(filemem.readLine());			Scanner mem4=new Scanner(filemem.readLine());			filemem.seek(0);			mem1.next();			mem2.next();			mem3.next();			mem4.next();			memrt+=mem2.nextLong()+mem3.nextLong()+mem4.nextLong();			memrt/=mem1.nextLong();			memrt=1-memrt;			System.out.println(""+memrt);//mem							}			}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -