欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java获得CPU使用率,内存使用率

程序员文章站 2022-04-13 09:29:16
...

 

 

linux下Cpu获取方式之一:

  1、从/proc文件系统获取相关的性能参数
    cpu使用率:    /proc/stat
    内存使用情况:      /proc/meminfo
    网络负载信息:    /proc/net/dev

相应的计算方法:(摘自:什么是proc文件系统,见参考资料)
    (1)   处理器使用率    
    (2)   内存使用率    
    (3)   流入流出数据包    
    (4)   整体网络负载    
     这些数据分别要从/proc/stat、/proc/net/dev、/proc/meminfo三个文件中提取。如里有问题或对要提取的数据不太清楚,可以使用man   proc来查看proc文件系统的联机手册。   
    (1)   处理器使用率    
    这里要从/proc/stat中提取四个数据:用户模式(user)、低优先级的用户模式(nice)、内核模式(system)以及空闲的处理器时间(idle)。它们均位于/proc/stat文件的第一行。CPU的利用率使用如下公式来计算。   
    CPU利用率   =   100   *(user   +   nice   +   system)/(user   +   nice   +   system   +   idle)   
   (2)   内存使用率    
    这里需要从/proc/meminfo文件中提取两个数据,当前内存的使用量(cmem)以及内存总量(amem)。    
    内存使用百分比   =   100   *   (cmem   /   umem)    
    (3)网络利用率    
    为了得到网络利用率的相关数据,需要从/proc/net/dev文件中获得两个数据:从本机输出的数据包数,流入本机的数据包数。它们都位于这个文件的第四行。   
    性能收集程序开始记录下这两个数据的初始值,以后每次获得这个值后均减去这个初始值即为从集群启动开始从本节点通过的数据包。    
    利用上述数据计算出网络的平均负载,方法如下:    
    平均网络负载   =   (输出的数据包+流入的数据包)   /   2
2. 通过/proc文件系统调整相关的内核配置
    允许ip转发   /proc/sys/net/ipv4/ip_forward
    禁止ping    /proc/sys/net/ipv4/icmp_echo_ignore_all
    
    可以在命令行下直接往上述两个“文件”里头写入"1"来实现相关配置,如果写入"0"将取消相关配置。不过在系统重启以后,这些配置将恢复默认设置,所以,如果想让这些修改生效,可以把下面的配置直接写入/etc/profile文件,或者其他随系统启动而执行的程序文件中。
  
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all

 

 

这里计算cpu使用率是有问题的,需要使用上一状态的值来计算

正确的计算方法是,等上一个时间:

1、记录某个时刻cpu的使用情况
2、等上一个时间段
3、再记录此刻的cpu使用情况
4、计算总的时间片
把第一次的所有cpu使用情况求和,得到j1
把第二次的所有cpu使用情况求和,得到j2
j2-j1得到这个时间段的所有时间片
即total=j2-j1=第二次的所有列的和-第一次的所有列的和
5、计算idle时间
idle对应第五列的数据,用第二次的减去第一次的即可
idle=第二次的第五列-第一次的第五列
6、计算cpu使用率
rate=(total-idle)/total

 

 

在Linux/Unix下,CPU利用率分为用户态 ,系统态 和空闲态 ,分别表示CPU处于用户态执行的时间,系统内核执行的时间,和空闲系统进程执行的时间。平时所说的CPU利用率是指:CPU执行非系统空闲进程的时间 / CPU总的执行时间 。

 

 

实例源码

 

 

case class ResMonitorInfo(
                           @transient var totalMemory: Long,
                           @transient var freeMemory: Long,
                           @transient var maxMemory: Long,
                           @transient var osName: String,
                           @transient var totalMemorySize: Long,
                           @transient var freePhysicalMemorySize: Long,
                           @transient var usedMemory: Long,
                           @transient var totalThread: Int,
                           var memUsageRatio: Double,
                           var cpuUsageRatio: Double
                         ) extends ResourseInfo {

  override def toString: String = ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE)
}

object ResMonitorInfo extends Logging {
  val crawlerUrlRegex = "Linux version (\\d.\\d).[\\w\\W]*".r

  def getLinuxVersion(input: String): String = {
    input match {
      case crawlerUrlRegex(vesrion) => vesrion
      case _ =>
        logError("Invalid linux version info!")
        ""
    }
  }
}

 

 

 

package com.soledede.cloud.resource;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.StringTokenizer;

import com.soledede.cloud.util.JavaLogging;
import com.sun.management.OperatingSystemMXBean;
import org.slf4j.Logger;

/**
 * Created by soledede.weng on 2016/6/2.
 * <p>
 * ResourseTool
 * cpu used , memeory used info
 */
public class ResourseTool {

    private static Logger log = null;

    private static long lastIdleCpuTime = 0;
    private static long lastTotalCpuTime = 0;

    private static String osName = System.getProperty("os.name");

    private static int cpuCores = Runtime.getRuntime().availableProcessors();


    private static final int CPUTIME = 30;


    private static final int FAULTLENGTH = 10;

    private static String linuxVersion = null;


    static {
        log = JavaLogging.log();
        Process process = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader brStat = null;
        try {
            process = Runtime.getRuntime().exec("cat /proc/version");
            is = process.getInputStream();
            isr = new InputStreamReader(is);
            brStat = new BufferedReader(isr);
            linuxVersion = ResMonitorInfo.getLinuxVersion(brStat.readLine());
        } catch (IOException e) {
            linuxVersion = "";
        } finally {
            freeResource(is, isr, brStat);
        }

    }


    public static ResMonitorInfo getResMonitorInfo() {


        int kb = 1024;

        // Available memory
        long totalMemory = Runtime.getRuntime().totalMemory() / kb;
        // Free memory
        long freeMemory = Runtime.getRuntime().freeMemory() / kb;
        // Maximal available memory
        long maxMemory = Runtime.getRuntime().maxMemory() / kb;

        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        // Operating system (OS)
        String osName = System.getProperty("os.name");
        // Total physical memory
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
        // Free physical memory
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
        // Usage physical memory
        long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
                .getFreePhysicalMemorySize())
                / kb;
        double memRatio = (double) usedMemory / (double) totalMemorySize;

        // Total thread active counts
        ThreadGroup parentThread;
        int totalThread = 0;
        double cpuRatio = 0;
        try {
            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent())
                ;
            totalThread = parentThread.activeCount();


            if (osName.toLowerCase().startsWith("windows")) {
                cpuRatio = getCpuRatioForWindows();
            } else {
                cpuRatio = getCpuRateForLinux();
            }

        } catch (Exception e) {
            log.error("read cpu or memory info failed!", e.getCause());
        }
        return new ResMonitorInfo(totalMemory, freeMemory, maxMemory,
                osName, totalMemorySize, freePhysicalMemorySize, usedMemory,
                totalThread, memRatio, cpuRatio
        );
    }

    private static double getCpuRateForLinux() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader brStat = null;
        StringTokenizer tokenStat = null;
        try {
            System.out.println("Get usage rate of CUP , linux version: " + linuxVersion);

            Process process = Runtime.getRuntime().exec("top -b -n 1");
            is = process.getInputStream();
            isr = new InputStreamReader(is);
            brStat = new BufferedReader(isr);

            if (linuxVersion.equals("2.4")) {
                brStat.readLine();
                brStat.readLine();
                brStat.readLine();
                brStat.readLine();

                tokenStat = new StringTokenizer(brStat.readLine());
                tokenStat.nextToken();
                tokenStat.nextToken();
                String user = tokenStat.nextToken();
                tokenStat.nextToken();
                String system = tokenStat.nextToken();
                tokenStat.nextToken();
                String nice = tokenStat.nextToken();

                System.out.println(user + " , " + system + " , " + nice);

                user = user.substring(0, user.indexOf("%"));
                system = system.substring(0, system.indexOf("%"));
                nice = nice.substring(0, nice.indexOf("%"));

                float userUsage = Float.parseFloat(user);
                float systemUsage = Float.parseFloat(system);
                float niceUsage = Float.parseFloat(nice);

                return (userUsage + systemUsage + niceUsage) / 100;
            } else {
                brStat.readLine();
                brStat.readLine();

                tokenStat = new StringTokenizer(brStat.readLine());
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                tokenStat.nextToken();
                String cpuIdle = tokenStat.nextToken();


                System.out.println("CPU idle : " + cpuIdle);
                Float idle = new Float(cpuIdle.substring(0, cpuIdle.indexOf("%")));

                return (1 - idle / 100);
            }
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            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)
                is.close();
            if (isr != null)
                isr.close();
            if (br != null)
                br.close();
        } catch (IOException ioe) {
            log.error("close stream failed!", ioe.getMessage());
        }
    }


    /**
     * Get cpu ratio.
     *
     * @return
     */
    private static double getCpuRatioForWindows() {
        try {
            String procCmd = System.getenv("windir")
                    + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
                    + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
            long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
            Thread.sleep(CPUTIME);
            long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
            if (c0 != null && c1 != null) {
                long idletime = c1[0] - c0[0];
                long busytime = c1[1] - c0[1];
                return (double) (busytime) / (double) (busytime + idletime);
            } else {
                return 0.0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0.0;
        }
    }

    /**
     * @return cpu rate
     */
    private static long[] readCpu(final Process proc) {
        long[] retn = new long[2];
        try {
            proc.getOutputStream().close();
            InputStreamReader ir = new InputStreamReader(proc.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line = input.readLine();
            if (line == null || line.length() <= FAULTLENGTH) {
                return null;
            }
            int capidx = line.indexOf("Caption");
            int cmdidx = line.indexOf("CommandLine");
            int rocidx = line.indexOf("ReadOperationCount");
            int umtidx = line.indexOf("UserModeTime");
            int kmtidx = line.indexOf("KernelModeTime");
            int wocidx = line.indexOf("WriteOperationCount");
            long idletime = 0;
            long kneltime = 0;
            long usertime = 0;
            while ((line = input.readLine()) != null) {
                if (line.length() < wocidx) {
                    continue;
                }
                // Order field:Caption,CommandLine,KernelModeTime,ReadOperationCount,
                // ThreadCount,UserModeTime,WriteOperation
                String caption = Bytes.substring(line, capidx, cmdidx - 1)
                        .trim();
                String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
                if (cmd.contains("wmic.exe")) {
                    continue;
                }
                if (caption.equals("System Idle Process") || caption.equals("System")) {
                    String idl = Bytes.substring(line, kmtidx, rocidx - 1).trim();
                    String id2 = Bytes.substring(line, umtidx, wocidx - 1).trim();
                    if (isNumeric(idl))
                        idletime += Long.valueOf(idl);
                    if (isNumeric(id2))
                        idletime += Long.valueOf(id2);
                    continue;
                }

                String knel1 = Bytes.substring(line, kmtidx, rocidx - 1).trim();
                String knel2 = Bytes.substring(line, umtidx, wocidx - 1).trim();
                if (isNumeric(knel1))
                    kneltime += Long.valueOf(knel1);
                if (isNumeric(knel2))
                    usertime += Long.valueOf(knel2);
            }
            retn[0] = idletime;
            retn[1] = kneltime + usertime;
            return retn;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                proc.getInputStream().close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static MemoryInfo getMemInfo() throws IOException,
            InterruptedException {
        MemoryInfo memInfo = new MemoryInfo();
        if (!osName.toLowerCase().startsWith("windows")) {
            long totalMem = 0;
            long freeMem = 0;


            Process pro = null;
            Runtime r = Runtime.getRuntime();

            String command = "cat /proc/meminfo";
            pro = r.exec(command);

            int count = 0;
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    pro.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] memInfos = line.split("\\s+");
                if (memInfos[0].toLowerCase().startsWith("MemTotal".toLowerCase())) {
                    totalMem = Long.valueOf(memInfos[1]).longValue();
                    memInfo.memTotal_$eq(totalMem);
                    count++;
                } else if (memInfos[0].toLowerCase().startsWith(
                        "MemFree".toLowerCase())) {
                    freeMem = Long.valueOf(memInfos[1]).longValue();
                    memInfo.memFree_$eq(freeMem);
                    count++;
                } else if (memInfos[0].toLowerCase().startsWith(
                        "SwapTotal".toLowerCase())) {
                    memInfo.swapFree_$eq(Long.valueOf(memInfos[1]).longValue());
                    count++;
                } else if (memInfos[0].toLowerCase().startsWith(
                        "SwapFree".toLowerCase())) {
                    memInfo.swapFree_$eq(Long.valueOf(memInfos[1]).longValue());
                    count++;
                }
                if (count == 4) {
                    memInfo.memUsage_$eq(1 - (double) freeMem
                            / (double) totalMem);
                    break;
                }

            }
        }
        return memInfo;

    }

    public static CpuInfo getCpuInfo() throws IOException, InterruptedException,
            RuntimeException {
        if (osName.toLowerCase().startsWith("windows")) return new CpuInfo(ResourseTool.cpuCores, 0.0);
        long[] l = getCpuInfoFromLinux();
        if (l == null)
            throw new RuntimeException();
        if (lastIdleCpuTime == 0 || lastTotalCpuTime == 0) {// first fetch
            lastIdleCpuTime = l[0];
            lastTotalCpuTime = l[1];
            Thread.sleep(1000);
            l = getCpuInfoFromLinux();
        }
        /*CpuInfo cpuInfo = new CpuInfo(ResourseTool.cpuCores, (double) 1 - (l[0] - lastIdleCpuTime)
                / (double) (l[1] - lastTotalCpuTime));*/
        double cpuTotal = l[1] - lastTotalCpuTime;
        double cpuIdle = l[0] - lastIdleCpuTime;
        double cpuUsageRate = (cpuTotal - cpuIdle) / cpuTotal;
        CpuInfo cpuInfo = new CpuInfo(ResourseTool.cpuCores, cpuUsageRate);
        lastIdleCpuTime = l[0];
        lastTotalCpuTime = l[1];
        return cpuInfo;
    }


    private static long[] getCpuInfoFromLinux() throws IOException {
        long[] l = new long[2];
        Process pro;
        Runtime r = Runtime.getRuntime();
        String command = "cat /proc/stat";
        pro = r.exec(command);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                pro.getInputStream()));
        String line = null;
        long idleCpuTime = 0, totalCpuTime = 0;
        while ((line = in.readLine()) != null) {
            if (line.toLowerCase().startsWith("cpu")) {
                line = line.trim();
                String[] temp = line.split("\\s+");
                idleCpuTime += Long.valueOf(temp[4]).longValue();
                for (int i = 0; i < temp.length; i++) {
                    if (!temp[i].toLowerCase().startsWith("cpu")) {
                        totalCpuTime += Long.valueOf(temp[i]).longValue();
                    }
                }
            } else if (idleCpuTime != 0L && totalCpuTime != 0L) {
                l[0] = idleCpuTime;
                l[1] = totalCpuTime;
                break;
            }
        }
        in.close();
        pro.destroy();
        return l;
    }

    public static boolean isNumeric(String str) {
        if (str == null || str.equalsIgnoreCase("")) return false;
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        //System.out.println(System.getProperty("os.name"));
        // System.out.println(cpuCores);
        ResMonitorInfo monitorInfo = ResourseTool.getResMonitorInfo();
        System.out.print(monitorInfo.toString());
    }

    public static void testssss() {
        System.out.println("come in....");
    }

    public static String testS() {
        return "what's";
    }
}

 

 

 

 

/**
 * Created by soledede.weng on 2016/6/14.
 */
public class Bytes {
    public static String substring(String src, int start_idx, int end_idx) {
        byte[] b = src.getBytes();
        String tgt = "";
        for (int i = start_idx; i <= end_idx; i++) {
            tgt += (char) b[i];
        }
        return tgt;
    }
}