Skip to content

Commit bad2935

Browse files
committed
Properly handle the adaptive sampling interval overflow
1 parent 718678a commit bad2935

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 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

@@ -188,14 +189,16 @@ Error ObjectSampler::updateConfiguration(u64 events, double time_coefficient) {
188189
CONFIG_UPDATE_CHECK_PERIOD_SECS, 15);
189190

190191
float signal = pid_controller.compute(events, time_coefficient);
191-
int required_interval = _interval - static_cast<int>(signal);
192+
// we are in unsigned land here - if the signal is larger thant the current sampling interval, clamp the diff to _configured_interval
193+
u64 required_interval = _interval >= signal ? static_cast<u64>(_interval) - static_cast<int>(signal) : _configured_interval;
192194
required_interval =
193195
required_interval >= _configured_interval
194196
? required_interval
195197
: _configured_interval; // do not dip below the manually configured
196198
// sampling interval
197199
if (required_interval != _interval) {
198-
_interval = required_interval;
200+
// clamp the sampling interval to the max positive int value to avoid overflow
201+
_interval = static_cast<int>(std::min(required_interval, static_cast<u64>(INT_MAX)));
199202
VM::jvmti()->SetHeapSamplingInterval(_interval);
200203
}
201204

0 commit comments

Comments
 (0)