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

📄 processus.java

📁 这是一个在linux环境下
💻 JAVA
字号:
package fr.umlv.projet.procfs;import java.io.File;import java.io.FileNotFoundException;import java.io.UnsupportedEncodingException;import java.util.Scanner;/** * This class is used to read and save the real time state of processus. * @author MA Xiao Jun & HUANG Wei * @see fr.umlv.projet.procfs.CpuTime */public class Processus {		private int pid;	private String name;	private String state;	private String ppid;	private String tgid;	private String sleepAVG;	private String tpid;	private String tracerPid;	private String uid;	private String gid;	private String user;	private String fdsize;	private String vmSize;	private String vmLck;	private String vmRss;	private String vmData;	private String vmStk;	private String vmExe;	private String vmLib;	private String vmPTE;	private String threads;	private String cmdLine;	private String meminfo;	private String cpuinfo;		private float oldctime = 0.0f;	private float oldstime = 0.0f;	private float newctime = 0.0f;	private float newstime = 0.0f;	private long oldtime = 0;	private long newtime = 0;		public static native String getUserName(int pid);		static{		System.loadLibrary("testjni");	}	//	public Processus getInstance(int pid){//	if(instance == null)//	instance = new Processus(pid);//	else instance = //	return instance;//	}//		/**	 * to change type from float to String and limite the number of decimal fraction.	 * @param value the number which is going to be changed size.	 * @param size the number of decimal fraction.	 */		public static String changeFloatSize(float value,int size){		String s = new String(value+"");		int index = s.indexOf('.');		float num = 0.5f;		if(s.length()>index+size+1){			if(s.charAt(index+size+1)>'4'){				for(int i=0;i<size;i++)					num *= 0.1f;				value += num;			}			s = new String(value+"");			String s1 = s.substring(0,index+size+1);			return s1;}		else return new String(value+"");	}		/**	 * to save the data of information of processus. 	 * @param sc read from the 	 * 	 */	public synchronized void enregistreData(Scanner sc){		String s = sc.next();		if(s.contains("Name"))			this.name = sc.next();		if(s.contains("State"))			this.state = sc.next();		if(s.contains("PPid"))			this.ppid = sc.next();		if(s.contains("Uid")){			sc.next();			this.uid = sc.next();			int i = Integer.parseInt(this.uid);			this.user = getUserName(i);		}		if(s.contains("Gid"))			this.gid = sc.next();		if(s.contains("TracerPid"))			this.tracerPid = sc.next();		if(s.contains("SleepAVG"))			this.sleepAVG = sc.next();		if(s.contains("Tgid"))			this.tgid =sc.next();		if(s.contains("FDSize"))			this.fdsize = sc.next();		if(s.contains("VmSize"))			this.vmSize = sc.next(); 		if(s.contains("VmLck"))			this.vmLck = sc.next();		if(s.contains("VmStk"))			this.vmStk = sc.next();		if(s.contains("VmRSS"))			this.vmRss = sc.next();		if(s.contains("VmData"))			this.vmData = sc.next();		if(s.contains("VmExe"))			this.vmExe = sc.next();		if(s.contains("VmLib"))			this.vmLib = sc.next();		if(s.contains("VmPTE"))			this.vmPTE = sc.next();		if(s.contains("Threads"))			this.threads = sc.next();	}		/**	 * to make the data corresponding.	 */	public void traiterData(){		float Rss = Float.parseFloat(vmRss);		float memTotal = Float.parseFloat(Meminfo.getMemTotal());		float mem = (Rss/memTotal)*100.0f;		this.meminfo = changeFloatSize(mem,2);	}		/**	 * to calculate the data about cpu.	 * @param oldctime client processus time before modification	 * @param oldstime service processus time before modification	 * @param oldtime system processus time before modification	 */	public void calculCpu(float oldctime,float oldstime,long oldtime){		float et = (newtime - oldtime)/1000.0f;		float scale = et/10.0f;		float cpu = ((newctime+newstime)-(oldctime+oldstime))*scale;		//System.out.println("cpu "+cpu+"   et "+et);		cpuinfo = changeFloatSize(cpu,2);;				this.oldctime = newctime;		this.oldstime = newstime;		this.oldtime = newtime;	}		public Processus(int pid,float oldctime,float oldstime,long oldtime) {		newtime = System.currentTimeMillis();		this.pid = pid;		String path1 = "/proc/"+pid+"/status";		String path2 = "/proc/"+pid+"/cmdline";		String path3 = "/proc/"+pid+"/stat";		try {			Scanner sc1 = new Scanner(new File(path1));			while(sc1.hasNext()){				enregistreData(sc1);			}			Scanner sc2 = new Scanner(new File(path2));			while(sc2.hasNext()){				try {					this.cmdLine = new String(sc2.next().getBytes("UTF-8"),"US-ASCII");				} catch (UnsupportedEncodingException e) {					e.printStackTrace();				}			}			Scanner sc3 = new Scanner(new File(path3));			for(int i = 0;i<13;i++){				sc3.next();			}			newctime = Float.parseFloat(sc3.next());			newstime = Float.parseFloat(sc3.next());		} catch (FileNotFoundException e) {			//e.printStackTrace();		}		traiterData();		calculCpu(oldctime,oldstime,oldtime);	}		/**	 * return id of the processus.	 * @return pid.	 */	public int getPid(){		return pid;	}		/**	 * return name of processus.	 * @return name of the processus.	 */	public String getName(){		return name;	}		/**	 * return state of processus.	 * @return the state of processus.	 */	public String getState(){		return state;	}		/**	 * Return id of processus parents.	 * @return ppid.	 */	public String getPpid(){		return ppid;	}		/**	 * to output the information of processus by means of String. 	 */	@Override	public String toString() {		StringBuilder sb = new StringBuilder();		sb.append(pid);		sb.append(name);		sb.append(state);		sb.append(ppid);		return sb.toString();	}			public static void main(String[] args){		//Processus proc = new Processus(4252);	}		/**	 * Return fdsize of processus.	 * @return fdsize.	 */	public String getFdsize() {		return fdsize;	}	/**	 * Return group id of processus.	 * @return gid.	 */	public String getGid() {		return gid;	}		/**	 * Return sleepAVG of processus.	 * @return sleepAVG.	 */	public String getSleepAVG() {		return sleepAVG;	}		/**	 * Return thread group id of processus.	 * @return tgid.	 */	public String getTgid() {		return tgid;	}		/**	 * Return information of thread.	 * @return threads	 */	public String getThreads() {		return threads;	}	/**	 * Return tpid of processus.	 * @return tpid.	 */	public String getTpid() {		return tpid;	}	/**	 * Return information of tracerPid.	 * @return tracerPid.	 */	public String getTracerPid() {		return tracerPid;	}	/**	 * Return Uid of processus.	 * @return Uid.	 */	public String getUid() {		return uid;	}		/**	 * Return user of processus.	 * @return user.	 */	public String getUser() {		return user;	}	/**	 * Return vmData of processus.	 * @return vmData.	 */	public String getVmData() {		return vmData;	}	/**	 * Return vmExe of processus.	 * @return vmExe.	 */	public String getVmExe() {		return vmExe;	}	/**	 * Return vmLck of processus.	 * @return vmLck.	 */	public String getVmLck() {		return vmLck;	}	/**	 * Return vmLib of processus.	 * @return vmLib.	 */	public String getVmLib() {		return vmLib;	}	/**	 * Return vmPTE of processus.	 * @return vmPTE.	 */	public String getVmPTE() {		return vmPTE;	}	/**	 * Return vmRss of processus.	 * @return vmRss.	 */	public String getVmRss() {		return vmRss;	}	/**	 * Return vmSize of processus.	 * @return vmSize.	 */	public String getVmSize() {		return vmSize;	}	/**	 * Return vmStk of processus.	 * @return vmStk.	 */	public String getVmStk() {		return vmStk;	}	/**	 * Return cmdLine of processus.	 * @return cmdLine.	 */	public String getCmdLine() {		return cmdLine;	}	/**	 * Return cpuinfo of processus.	 * @return cpuinfo.	 */	public String getCpuinfo() {		return cpuinfo;	}	/**	 * Return meminfo of processus.	 * @return meminfo.	 */	public String getMeminfo() {		return meminfo;	}	/**	 * Return oldctime of processus.	 * @return oldctime.	 */	public float getOldctime() {		return oldctime;	}	/**	 * Change the oldctime.	 * @param oldctime the new value of oldctime.	 */	public void setOldctime(float oldctime) {		this.oldctime = oldctime;	}	/**	 * Return the oldtstime of processus.	 * @return oldstime.	 */	public float getOldstime() {		return oldstime;	}	/**	 * Change the oldstime.	 * @param oldstime the new value of oldstime.	 */	public void setOldstime(float oldstime) {		this.oldstime = oldstime;	}	/**	 * Return the oldtime of processus.	 * @return oldtime.	 */	public long getOldtime() {		return oldtime;	}	/**	 * Change the oldtime.	 * @param oldtime the new value of oldtime.	 */	public void setOldtime(long oldtime) {		this.oldtime = oldtime;	}	}

⌨️ 快捷键说明

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