com.codahale.metrics.jvm.FileDescriptorRatioGauge Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-jvm Show documentation
Show all versions of metrics-jvm Show documentation
A set of classes which allow you to monitor critical aspects of your Java Virtual Machine
using Metrics.
The newest version!
package com.codahale.metrics.jvm;
import com.codahale.metrics.RatioGauge;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
/**
* A gauge for the ratio of used to total file descriptors.
*/
public class FileDescriptorRatioGauge extends RatioGauge {
private static boolean unixOperatingSystemMXBeanExists = false;
private final OperatingSystemMXBean os;
static {
try {
Class.forName("com.sun.management.UnixOperatingSystemMXBean");
unixOperatingSystemMXBeanExists = true;
} catch (ClassNotFoundException e) {
// do nothing
}
}
/**
* Creates a new gauge using the platform OS bean.
*/
public FileDescriptorRatioGauge() {
this(ManagementFactory.getOperatingSystemMXBean());
}
/**
* Creates a new gauge using the given OS bean.
*
* @param os an {@link OperatingSystemMXBean}
*/
public FileDescriptorRatioGauge(OperatingSystemMXBean os) {
this.os = os;
}
@Override
protected Ratio getRatio() {
if (unixOperatingSystemMXBeanExists && os instanceof com.sun.management.UnixOperatingSystemMXBean) {
final com.sun.management.UnixOperatingSystemMXBean unixOs = (com.sun.management.UnixOperatingSystemMXBean) os;
return Ratio.of(unixOs.getOpenFileDescriptorCount(), unixOs.getMaxFileDescriptorCount());
} else {
return Ratio.of(Double.NaN, Double.NaN);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy