Skip to content

Commit d58b718

Browse files
committed
#39 - ENH: Add jvm.cgroup.cpu.pctThrottle ... as a rate of numThrottle to numPeriod
1 parent 1e25493 commit d58b718

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/main/java/io/avaje/metrics/core/DefaultGaugeLongMetric.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public void collect(MetricStatisticsVisitor collector) {
106106

107107
@Override
108108
public long getValue() {
109-
110109
synchronized (this) {
111-
112110
long nowValue = super.getValue();
113111
long diffValue = nowValue - runningValue;
114112
runningValue = nowValue;

src/main/java/io/avaje/metrics/core/JvmCGroupCpuMetricGroup.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ private GaugeLongMetric createCGroupCpuUsage(FileLines cpu) {
9898

9999
private void createCGroupCpuThrottle(FileLines cpuStat) {
100100
CpuStatsSource source = new CpuStatsSource(cpuStat);
101-
metrics.add(incrementing(name("jvm.cgroup.cpu.throttleMicros"), source::getThrottleMicros));
101+
metrics.add(gauge(name("jvm.cgroup.cpu.throttleMicros"), source::getThrottleMicros));
102102
metrics.add(gauge(name("jvm.cgroup.cpu.numPeriod"), source::getNumPeriod));
103103
metrics.add(gauge(name("jvm.cgroup.cpu.numThrottle"), source::getNumThrottle));
104+
metrics.add(gauge(name("jvm.cgroup.cpu.pctThrottle"), source::getPctThrottle));
104105
}
105106

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

134135
private final FileLines source;
135136

137+
private long prevNumPeriod;
138+
private long prevNumThrottle;
139+
private long prevThrottleMicros;
140+
141+
private long currNumPeriod;
142+
private long currNumThrottle;
143+
private long currThrottleMicros;
144+
136145
private long numPeriod;
137146
private long numThrottle;
138147
private long throttleMicros;
@@ -142,27 +151,42 @@ static class CpuStatsSource {
142151
}
143152

144153
void load() {
145-
for (String line : source.readLines()) {
146-
if (line.startsWith("nr_p")) {
147-
numPeriod = Long.parseLong(line.substring(11));
148-
} else if (line.startsWith("nr_t")) {
149-
// convert from nanos to micros
150-
numThrottle = Long.parseLong(line.substring(13));
151-
} else {
152-
throttleMicros = Long.parseLong(line.substring(15)) / 1000;
154+
synchronized (this) {
155+
for (String line : source.readLines()) {
156+
if (line.startsWith("nr_p")) {
157+
currNumPeriod = Long.parseLong(line.substring(11));
158+
} else if (line.startsWith("nr_t")) {
159+
// convert from nanos to micros
160+
currNumThrottle = Long.parseLong(line.substring(13));
161+
} else {
162+
currThrottleMicros = Long.parseLong(line.substring(15)) / 1000;
163+
}
153164
}
165+
numPeriod = currNumPeriod - prevNumPeriod;
166+
numThrottle = currNumThrottle - prevNumThrottle;
167+
throttleMicros = currThrottleMicros - prevThrottleMicros;
168+
prevNumPeriod = currNumPeriod;
169+
prevNumThrottle = currNumThrottle;
170+
prevThrottleMicros = currThrottleMicros;
154171
}
155172
}
173+
156174
long getThrottleMicros() {
157175
load();
158176
return throttleMicros;
159177
}
178+
160179
long getNumThrottle() {
161180
return numThrottle;
162181
}
182+
163183
long getNumPeriod() {
164184
return numPeriod;
165185
}
186+
187+
long getPctThrottle() {
188+
return (numPeriod <= 0) ? 0 : numThrottle * 100 / numPeriod;
189+
}
166190
}
167191

168192
static class FixedGauge implements GaugeLong {

0 commit comments

Comments
 (0)