Returns usage per CPU in percents from 0 to 100 - Java Native OS

Java examples for Native OS:CPU

Description

Returns usage per CPU in percents from 0 to 100

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.List;

public class Main {
    private static final String CPU_INFO = "proc/cpuinfo";
    private static final int VALUABLE_PROCSTAT_PARAMETERS_COUNT = 3;
    private static final String ID_PROCESSOR = "processor";
    private static Integer cpu_count = null;

    /**/*from  w ww  .j av  a 2  s  .c o  m*/
     * Returns usage per CPU in percents from 0 to 100
     * @param prev last CPU usage values array
     * @param now fresh CPUU usage values array
     * @return usage per CPU in percents from 0 to 100
     */
    public static final int[] getCpuUsage(List<long[]> prev,
            List<long[]> now) {
        int cpuCount = getCpuCount();
        int[] result = new int[cpuCount];
        if (prev != null && now != null && prev.size() == cpuCount
                && now.size() == cpuCount) {
            for (int i = 0; i < cpuCount; i++) {
                long[] prevArr = prev.get(i);
                long[] nowArr = now.get(i);

                if (prevArr == null || nowArr == null) {
                    result[i] = 0;
                } else {
                    long prevTotal = getTotalLoad(prevArr);
                    long nowTotal = getTotalLoad(nowArr);
                    long prevWork = getWorkLoad(prevArr);
                    long nowWork = getWorkLoad(nowArr);
                    double cpuResult = 0.0d;
                    if (nowTotal - prevTotal != 0) {
                        cpuResult = Math
                                .abs(((nowWork - prevWork) * 100.0d)
                                        / (double) (nowTotal - prevTotal));
                    }
                    result[i] = (int) cpuResult;
                }
            }
        }

        return result;
    }

    /**
     * Returns number of CPUs in the system
     * @return number of CPUs in the system
     */
    public static final int getCpuCount() {
        if (cpu_count == null) {
            innerGetCpuCount();
        }
        return cpu_count;
    }

    private static long getTotalLoad(long[] args) {
        long result = 0;
        for (int i = 0; i < args.length; i++) {
            result += args[i];
        }
        return result;
    }

    private static long getWorkLoad(long[] args) {
        long result = 0;
        for (int i = 0; i < VALUABLE_PROCSTAT_PARAMETERS_COUNT; i++) {
            result += args[i];
        }
        return result;
    }

    private static void innerGetCpuCount() {
        cpu_count = 0;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(CPU_INFO));

            String line;
            while ((line = br.readLine()) != null) {
                if (line.toLowerCase().contains(ID_PROCESSOR)) {
                    cpu_count++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related Tutorials