2023年12月16日发(作者:)

public void setTotalMemory(long totalMemory) { emory = totalMemory; } public long getTotalMemorySize() { return totalMemorySize; } public void setTotalMemorySize(long totalMemorySize) { emorySize = totalMemorySize; } public int getTotalThread() { return totalThread; } public void setTotalThread(int totalThread) { hread = totalThread; } public long getUsedMemory() { return usedMemory; } public void setUsedMemory(long usedMemory) { mory = usedMemory; } public double getCpuRatio() { return cpuRatio; } public void setCpuRatio(double cpuRatio) { io = cpuRatio; }}2.1.4import treamReader;import mberReader;//import mentFactory;//import ingSystemMXBean;import .*;import mentFactory;import Tokenizer;public class MonitorServiceImpl implements IMonitorService { private static final int CPUTIME = 30; private static final int PERCENT = 100; private static final int FAULTLENGTH = 10; private static final File versionFile = new File("/proc/version"); private static String linuxVersion = null; public MonitorInfoBean getMonitorInfoBean() throws Exception { int kb = 1024; long totalMemory = time().totalMemory() / kb; long freeMemory = time().freeMemory() / kb; long maxMemory = time().maxMemory() / kb; // OperatingSystemMXBean osmxb = (OperatingSystemMXBean) // ManagementFactory // .getOperatingSystemMXBean(); // String osName = perty(""); // long totalMemorySize = alPhysicalMemorySize() / kb; // long freePhysicalMemorySize = ePhysicalMemorySize() / kb; // long usedMemory = (alPhysicalMemorySize() - osmxb // .getFreePhysicalMemorySize()) / kb; ThreadGroup parentThread; for (parentThread = tThread().getThreadGroup(); ent() != null; parentThread = ent()); int totalThread = Count(); double cpuRatio = 0; // if (rCase().startsWith("windows")) { // cpuRatio = RatioForWindows(); // } else { // cpuRatio = RateForLinux(); // } MonitorInfoBean infoBean = new MonitorInfoBean(); eMemory(freeMemory);

// ePhysicalMemorySize(freePhysicalMemorySize); Memory(maxMemory); // ame(osName); alMemory(totalMemory); // alMemorySize(totalMemorySize); alThread(totalThread); // dMemory(usedMemory); Ratio(cpuRatio); return infoBean; } private static double getCpuRateForLinux() { InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTokenizer tokenStat = null; try { n("Get usage rate of CUP , linux version: " + linuxVersion); Process process = time().exec("top -b -n 1"); is = utStream(); isr = new InputStreamReader(is); brStat = new BufferedReader(isr); if (("2.4")) { ne(); ne(); ne(); ne(); tokenStat = new StringTokenizer(ne()); ken(); ken(); String user = ken(); ken(); String system = ken(); ken(); String nice = ken(); n(user + " , " + system + " , " + nice); user = ing(0, f("%")); system = ing(0, f("%")); nice = ing(0, f("%")); float userUsage = new Float(user).floatValue(); float systemUsage = new Float(system).floatValue(); float niceUsage = new Float(nice).floatValue(); return (userUsage + systemUsage + niceUsage) / 100; } else { ne(); ne(); tokenStat = new StringTokenizer(ne()); ken(); ken(); ken(); ken(); ken(); ken(); ken(); String cpuUsage = ken(); n("CPU idle : " + cpuUsage); Float usage = new Float(ing(0, f("%"))); return (1 - alue() / 100); } } catch (IOException ioe) { n(sage()); freeResource(is, isr, brStat); return 1; } finally { freeResource(is, isr, brStat); } } private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader br) { try { if (is != null) (); if (isr != null) (); if (br != null) (); } catch (IOException ioe) {

n(sage()); } } private double getCpuRatioForWindows() { try { String procCmd = ("windir") + " process get Caption,CommandLine," + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; long[] c0 = readCpu(time().exec(procCmd)); (CPUTIME); long[] c1 = readCpu(time().exec(procCmd)); if (c0 != null && c1 != null) { long idletime = c1[0] - c0[0]; long busytime = c1[1] - c0[1]; return f( PERCENT * (busytime) / (busytime + idletime)).doubleValue(); } else { return 0.0; } } catch (Exception ex) { tackTrace(); return 0.0; } } private long[] readCpu(final Process proc) { long[] retn = new long[2]; try { putStream().close(); InputStreamReader ir = new InputStreamReader(utStream()); LineNumberReader input = new LineNumberReader(ir); String line = ne(); if (line == null || () < FAULTLENGTH) { return null; } int capidx = f("Caption"); int cmdidx = f("CommandLine"); int rocidx = f("ReadOperationCount"); int umtidx = f("UserModeTime"); int kmtidx = f("KernelModeTime"); int wocidx = f("WriteOperationCount"); long idletime = 0; long kneltime = 0; long usertime = 0; while ((line = ne()) != null) { if (() < wocidx) { continue; } String caption = ing(line, capidx, cmdidx - 1) .trim(); String cmd = ing(line, cmdidx, kmtidx - 1).trim(); if (f("") >= 0) { continue; } // ("line="+line); if (("System Idle Process") || ("System")) { idletime += f( ing(line, kmtidx, rocidx - 1).trim()).longValue(); idletime += f( ing(line, umtidx, wocidx - 1).trim()).longValue(); continue; } kneltime += f( ing(line, kmtidx, rocidx - 1).trim()).longValue(); usertime += f( ing(line, umtidx, wocidx - 1).trim()).longValue(); } retn[0] = idletime; retn[1] = kneltime + usertime; return retn; } catch (Exception ex) { tackTrace(); } finally { try { utStream().close(); } catch (Exception e) { tackTrace();