Skip to content

Commit b751eec

Browse files
authored
Properly handle the adaptive sampling interval overflow (#213)
1 parent 4fd5124 commit b751eec

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

ddprof-lib/src/main/cpp/objectSampler.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "thread.h"
2727
#include "vmStructs.h"
2828
#include <jni.h>
29+
#include <limits.h>
2930
#include <math.h>
3031
#include <string.h>
3132

@@ -187,15 +188,24 @@ Error ObjectSampler::updateConfiguration(u64 events, double time_coefficient) {
187188
// can change abruptly (low impact of the predicted allocation rate)
188189
CONFIG_UPDATE_CHECK_PERIOD_SECS, 15);
189190

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;
199209
VM::jvmti()->SetHeapSamplingInterval(_interval);
200210
}
201211

0 commit comments

Comments
 (0)