From b2e7d8a2efed854c8e96ffcb012b162bfd605e00 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Mon, 19 May 2025 18:05:42 +0200 Subject: [PATCH] Properly handle the adaptive sampling interval overflow --- ddprof-lib/src/main/cpp/objectSampler.cpp | 28 +++++++++++++++-------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/ddprof-lib/src/main/cpp/objectSampler.cpp b/ddprof-lib/src/main/cpp/objectSampler.cpp index 854c4fa94..1ea69d90d 100644 --- a/ddprof-lib/src/main/cpp/objectSampler.cpp +++ b/ddprof-lib/src/main/cpp/objectSampler.cpp @@ -26,6 +26,7 @@ #include "thread.h" #include "vmStructs.h" #include +#include #include #include @@ -187,15 +188,24 @@ Error ObjectSampler::updateConfiguration(u64 events, double time_coefficient) { // can change abruptly (low impact of the predicted allocation rate) CONFIG_UPDATE_CHECK_PERIOD_SECS, 15); - float signal = pid_controller.compute(events, time_coefficient); - int required_interval = _interval - static_cast(signal); - required_interval = - required_interval >= _configured_interval - ? required_interval - : _configured_interval; // do not dip below the manually configured - // sampling interval - if (required_interval != _interval) { - _interval = required_interval; + double signal = pid_controller.compute(events, time_coefficient); + int64_t signal_adjustment = static_cast(signal); + // use ints to avoid any wrap around + int64_t new_interval = static_cast(_interval) - signal_adjustment; + + // Clamp to never go below configured min + if (new_interval < static_cast(_configured_interval)) { + new_interval = static_cast(_configured_interval); + } + + // We actually need to consider the max interval from JVMTI api (max int32) + if (new_interval > INT32_MAX) { + new_interval = INT32_MAX; + } + + if (new_interval != _interval) { + // clamp the sampling interval to the max positive int value to avoid overflow + _interval = new_interval; VM::jvmti()->SetHeapSamplingInterval(_interval); }