All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.common.caching.statistics.customcounters.systemcounters.VMCpuUsage Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alachisoft.ncache.common.caching.statistics.customcounters.systemcounters;

/**
 * @author Muneeb Shahid
 */
public class VMCpuUsage extends SystemCounter {
    long nanoBefore;
    long cpuBefore;
    long cpuAfter;
    long nanoAfter;

    public VMCpuUsage(String name, String instance) {
        super(name, instance);
        if (getSunOSMXBean() != null && getJavaOSMXBean() != null) {
            nanoBefore = System.nanoTime();
            cpuBefore = getSunOSMXBean().getProcessCpuTime();
        }
    }

    @Override
    protected void calculate(double percent) {
        if (getSunOSMXBean() != null && getJavaOSMXBean() != null) {
// Using Sigar:
            //percent = getSigar().getProcCpu(getSigar().getPid()).getPercent() * 100;

            // Using JMX
            // Since we are trying to use sigar as less as possible hence aleranate algo using jmx
            cpuAfter = getSunOSMXBean().getProcessCpuTime();
            nanoAfter = System.nanoTime();
            if (nanoAfter > nanoBefore) {
                percent = ((cpuAfter - cpuBefore) * 100D) / (nanoAfter - nanoBefore);
            } else {
                percent = 0;
            }
            setValue(percent / getJavaOSMXBean().getAvailableProcessors());

            cpuBefore = getSunOSMXBean().getProcessCpuTime();
            nanoBefore = System.nanoTime();
        }
        return;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy