com.alachisoft.ncache.common.caching.statistics.customcounters.systemcounters.VMCpuUsage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
/*
* 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;
}
}