@@ -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