package com.codahale.metrics.jvm;
import com.codahale.metrics.RatioGauge;
import com.sun.management.UnixOperatingSystemMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
A gauge for the ratio of used to total file descriptors.
/**
* A gauge for the ratio of used to total file descriptors.
*/
public class FileDescriptorRatioGauge extends RatioGauge {
private final OperatingSystemMXBean os;
Creates a new gauge using the platform OS bean.
/**
* Creates a new gauge using the platform OS bean.
*/
public FileDescriptorRatioGauge() {
this(ManagementFactory.getOperatingSystemMXBean());
}
Creates a new gauge using the given OS bean.
Params: - os – an
OperatingSystemMXBean
/**
* 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 (os instanceof UnixOperatingSystemMXBean) {
final UnixOperatingSystemMXBean unixOs = (UnixOperatingSystemMXBean) os;
return Ratio.of(unixOs.getOpenFileDescriptorCount(), unixOs.getMaxFileDescriptorCount());
} else {
return Ratio.of(Double.NaN, Double.NaN);
}
}
}