Skip to content

Commit

Permalink
#39 - ENH: Add jvm.cgroup.cpu.pctThrottle ... as a rate of numThrottl…
Browse files Browse the repository at this point in the history
…e to numPeriod
  • Loading branch information
rbygrave committed Dec 10, 2019
1 parent 1e25493 commit d58b718
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public void collect(MetricStatisticsVisitor collector) {

@Override
public long getValue() {

synchronized (this) {

long nowValue = super.getValue();
long diffValue = nowValue - runningValue;
runningValue = nowValue;
Expand Down
42 changes: 33 additions & 9 deletions src/main/java/io/avaje/metrics/core/JvmCGroupCpuMetricGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ private GaugeLongMetric createCGroupCpuUsage(FileLines cpu) {

private void createCGroupCpuThrottle(FileLines cpuStat) {
CpuStatsSource source = new CpuStatsSource(cpuStat);
metrics.add(incrementing(name("jvm.cgroup.cpu.throttleMicros"), source::getThrottleMicros));
metrics.add(gauge(name("jvm.cgroup.cpu.throttleMicros"), source::getThrottleMicros));
metrics.add(gauge(name("jvm.cgroup.cpu.numPeriod"), source::getNumPeriod));
metrics.add(gauge(name("jvm.cgroup.cpu.numThrottle"), source::getNumThrottle));
metrics.add(gauge(name("jvm.cgroup.cpu.pctThrottle"), source::getPctThrottle));
}

private GaugeLongMetric incrementing(MetricName name, GaugeLong gauge) {
Expand Down Expand Up @@ -133,6 +134,14 @@ static class CpuStatsSource {

private final FileLines source;

private long prevNumPeriod;
private long prevNumThrottle;
private long prevThrottleMicros;

private long currNumPeriod;
private long currNumThrottle;
private long currThrottleMicros;

private long numPeriod;
private long numThrottle;
private long throttleMicros;
Expand All @@ -142,27 +151,42 @@ static class CpuStatsSource {
}

void load() {
for (String line : source.readLines()) {
if (line.startsWith("nr_p")) {
numPeriod = Long.parseLong(line.substring(11));
} else if (line.startsWith("nr_t")) {
// convert from nanos to micros
numThrottle = Long.parseLong(line.substring(13));
} else {
throttleMicros = Long.parseLong(line.substring(15)) / 1000;
synchronized (this) {
for (String line : source.readLines()) {
if (line.startsWith("nr_p")) {
currNumPeriod = Long.parseLong(line.substring(11));
} else if (line.startsWith("nr_t")) {
// convert from nanos to micros
currNumThrottle = Long.parseLong(line.substring(13));
} else {
currThrottleMicros = Long.parseLong(line.substring(15)) / 1000;
}
}
numPeriod = currNumPeriod - prevNumPeriod;
numThrottle = currNumThrottle - prevNumThrottle;
throttleMicros = currThrottleMicros - prevThrottleMicros;
prevNumPeriod = currNumPeriod;
prevNumThrottle = currNumThrottle;
prevThrottleMicros = currThrottleMicros;
}
}

long getThrottleMicros() {
load();
return throttleMicros;
}

long getNumThrottle() {
return numThrottle;
}

long getNumPeriod() {
return numPeriod;
}

long getPctThrottle() {
return (numPeriod <= 0) ? 0 : numThrottle * 100 / numPeriod;
}
}

static class FixedGauge implements GaugeLong {
Expand Down

0 comments on commit d58b718

Please sign in to comment.