forked from dropwizard/metrics
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
metrics-sigar/src/main/java/com/yammer/metrics/sigar/UlimitMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.yammer.metrics.sigar; | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
import com.yammer.metrics.Metrics; | ||
import com.yammer.metrics.core.Gauge; | ||
import com.yammer.metrics.core.MetricsRegistry; | ||
|
||
import org.hyperic.sigar.ResourceLimit; | ||
import org.hyperic.sigar.Sigar; | ||
import org.hyperic.sigar.SigarException; | ||
|
||
public class UlimitMetrics extends AbstractSigarMetric { | ||
private final long infinity; | ||
|
||
protected UlimitMetrics(Sigar sigar) { | ||
super(sigar); | ||
infinity = ResourceLimit.INFINITY(); | ||
} | ||
|
||
public static final class Ulimit { | ||
private final long coreFileSize, dataSegSize, | ||
fileSize, pipeSize, | ||
memSize, openFiles, | ||
stackSize, cpuTime, | ||
processes, virtMem; | ||
|
||
private Ulimit(// | ||
long coreFileSize, long dataSegSize, // | ||
long fileSize, long pipeSize, // | ||
long memSize, long openFiles, // | ||
long stackSize, long cpuTime, // | ||
long processes, long virtMem) { | ||
this.coreFileSize = coreFileSize; | ||
this.dataSegSize = dataSegSize; | ||
this.fileSize = fileSize; | ||
this.pipeSize = pipeSize; | ||
this.memSize = memSize; | ||
this.openFiles = openFiles; | ||
this.stackSize = stackSize; | ||
this.cpuTime = cpuTime; | ||
this.processes = processes; | ||
this.virtMem = virtMem; | ||
} | ||
|
||
public static Ulimit fromSigarBean(ResourceLimit lim, long infinity) { | ||
return new Ulimit( // | ||
replaceInfinity(lim.getCoreCur(), infinity), // | ||
replaceInfinity(lim.getDataCur(), infinity), // | ||
replaceInfinity(lim.getFileSizeCur(), infinity), // | ||
replaceInfinity(lim.getPipeSizeCur(), infinity), // | ||
replaceInfinity(lim.getMemoryCur(), infinity), // | ||
replaceInfinity(lim.getOpenFilesCur(), infinity), // | ||
replaceInfinity(lim.getStackCur(), infinity), // | ||
replaceInfinity(lim.getCpuCur(), infinity), // | ||
replaceInfinity(lim.getProcessesCur(), infinity), // | ||
replaceInfinity(lim.getVirtualMemoryCur(), infinity)); | ||
} | ||
|
||
public static Ulimit undef() { | ||
return new Ulimit(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L); | ||
} | ||
|
||
public long coreFileSize() { return coreFileSize; } | ||
public long dataSegSize() { return dataSegSize; } | ||
public long fileSize() { return fileSize; } | ||
public long pipeSize() { return pipeSize; } | ||
public long memSize() { return memSize; } | ||
public long openFiles() { return openFiles; } | ||
public long stackSize() { return stackSize; } | ||
public long cpuTime() { return cpuTime; } | ||
public long processes() { return processes; } | ||
public long virtMemSize() { return virtMem; } | ||
|
||
private static long replaceInfinity(long value, long infinity) { | ||
if (value == infinity) { | ||
return -1L; | ||
} else { | ||
return value; | ||
} | ||
} | ||
} | ||
|
||
public Ulimit ulimit() { | ||
try { | ||
return Ulimit.fromSigarBean(sigar.getResourceLimit(), infinity); | ||
} catch (SigarException e) { | ||
return Ulimit.undef(); | ||
} | ||
} | ||
|
||
public void registerGauges(MetricsRegistry registry) { | ||
registry.newGauge(getClass(), "ulimit-open-files", new Gauge<Long>() { | ||
public Long value() { | ||
return ulimit().openFiles(); | ||
} | ||
}); | ||
registry.newGauge(getClass(), "ulimit-stack-size", new Gauge<Long>() { | ||
public Long value() { | ||
return ulimit().stackSize(); | ||
} | ||
}); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
metrics-sigar/src/test/java/com/yammer/metrics/sigar/tests/UlimitMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.yammer.metrics.sigar.tests; | ||
|
||
import com.yammer.metrics.sigar.UlimitMetrics; | ||
import com.yammer.metrics.sigar.UlimitMetrics.Ulimit; | ||
import com.yammer.metrics.sigar.SigarMetrics; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.Matchers.closeTo; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.lessThan; | ||
|
||
import static org.junit.Assert.assertThat; | ||
|
||
public class UlimitMetricsTest { | ||
private final UlimitMetrics um = SigarMetrics.getInstance().ulimit(); | ||
|
||
@Test | ||
public void openFilesLimitIsGreaterThanZero() throws Exception { | ||
assertThat(um.ulimit().openFiles(), is(greaterThan(0L))); | ||
} | ||
} |