|
26 | 26 | #include "thread.h"
|
27 | 27 | #include "vmStructs.h"
|
28 | 28 | #include <jni.h>
|
| 29 | +#include <limits.h> |
29 | 30 | #include <math.h>
|
30 | 31 | #include <string.h>
|
31 | 32 |
|
@@ -187,15 +188,24 @@ Error ObjectSampler::updateConfiguration(u64 events, double time_coefficient) {
|
187 | 188 | // can change abruptly (low impact of the predicted allocation rate)
|
188 | 189 | CONFIG_UPDATE_CHECK_PERIOD_SECS, 15);
|
189 | 190 |
|
190 |
| - float signal = pid_controller.compute(events, time_coefficient); |
191 |
| - int required_interval = _interval - static_cast<int>(signal); |
192 |
| - required_interval = |
193 |
| - required_interval >= _configured_interval |
194 |
| - ? required_interval |
195 |
| - : _configured_interval; // do not dip below the manually configured |
196 |
| - // sampling interval |
197 |
| - if (required_interval != _interval) { |
198 |
| - _interval = required_interval; |
| 191 | + double signal = pid_controller.compute(events, time_coefficient); |
| 192 | + int64_t signal_adjustment = static_cast<int64_t>(signal); |
| 193 | + // use ints to avoid any wrap around |
| 194 | + int64_t new_interval = static_cast<int64_t>(_interval) - signal_adjustment; |
| 195 | + |
| 196 | + // Clamp to never go below configured min |
| 197 | + if (new_interval < static_cast<int64_t>(_configured_interval)) { |
| 198 | + new_interval = static_cast<int64_t>(_configured_interval); |
| 199 | + } |
| 200 | + |
| 201 | + // We actually need to consider the max interval from JVMTI api (max int32) |
| 202 | + if (new_interval > INT32_MAX) { |
| 203 | + new_interval = INT32_MAX; |
| 204 | + } |
| 205 | + |
| 206 | + if (new_interval != _interval) { |
| 207 | + // clamp the sampling interval to the max positive int value to avoid overflow |
| 208 | + _interval = new_interval; |
199 | 209 | VM::jvmti()->SetHeapSamplingInterval(_interval);
|
200 | 210 | }
|
201 | 211 |
|
|
0 commit comments