1*330d8983SJohannes Doerfert //===-- Utils/ExponentialBackoff.h - Heuristic helper class ------*- C++ -*===// 2*330d8983SJohannes Doerfert // 3*330d8983SJohannes Doerfert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*330d8983SJohannes Doerfert // See https://llvm.org/LICENSE.txt for license information. 5*330d8983SJohannes Doerfert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*330d8983SJohannes Doerfert // 7*330d8983SJohannes Doerfert //===----------------------------------------------------------------------===// 8*330d8983SJohannes Doerfert // 9*330d8983SJohannes Doerfert // Implement exponential backoff counting. 10*330d8983SJohannes Doerfert // Linearly increments until given maximum, exponentially decrements based on 11*330d8983SJohannes Doerfert // given backoff factor. 12*330d8983SJohannes Doerfert // 13*330d8983SJohannes Doerfert //===----------------------------------------------------------------------===// 14*330d8983SJohannes Doerfert 15*330d8983SJohannes Doerfert #ifndef OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H 16*330d8983SJohannes Doerfert #define OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H 17*330d8983SJohannes Doerfert 18*330d8983SJohannes Doerfert #include <cassert> 19*330d8983SJohannes Doerfert #include <cmath> 20*330d8983SJohannes Doerfert #include <cstdint> 21*330d8983SJohannes Doerfert 22*330d8983SJohannes Doerfert namespace utils { 23*330d8983SJohannes Doerfert 24*330d8983SJohannes Doerfert class ExponentialBackoff { 25*330d8983SJohannes Doerfert int64_t Count = 0; 26*330d8983SJohannes Doerfert const int64_t MaxCount = 0; 27*330d8983SJohannes Doerfert const int64_t CountThreshold = 0; 28*330d8983SJohannes Doerfert const double BackoffFactor = 0; 29*330d8983SJohannes Doerfert 30*330d8983SJohannes Doerfert public: ExponentialBackoff(int64_t MaxCount,int64_t CountThreshold,double BackoffFactor)31*330d8983SJohannes Doerfert ExponentialBackoff(int64_t MaxCount, int64_t CountThreshold, 32*330d8983SJohannes Doerfert double BackoffFactor) 33*330d8983SJohannes Doerfert : MaxCount(MaxCount), CountThreshold(CountThreshold), 34*330d8983SJohannes Doerfert BackoffFactor(BackoffFactor) { 35*330d8983SJohannes Doerfert assert(MaxCount >= 0 && 36*330d8983SJohannes Doerfert "ExponentialBackoff: maximum count value should be non-negative"); 37*330d8983SJohannes Doerfert assert(CountThreshold >= 0 && 38*330d8983SJohannes Doerfert "ExponentialBackoff: count threshold value should be non-negative"); 39*330d8983SJohannes Doerfert assert(BackoffFactor >= 0 && BackoffFactor < 1 && 40*330d8983SJohannes Doerfert "ExponentialBackoff: backoff factor should be in [0, 1) interval"); 41*330d8983SJohannes Doerfert } 42*330d8983SJohannes Doerfert increment()43*330d8983SJohannes Doerfert void increment() { Count = std::min(Count + 1, MaxCount); } 44*330d8983SJohannes Doerfert decrement()45*330d8983SJohannes Doerfert void decrement() { Count *= BackoffFactor; } 46*330d8983SJohannes Doerfert isAboveThreshold()47*330d8983SJohannes Doerfert bool isAboveThreshold() const { return Count > CountThreshold; } 48*330d8983SJohannes Doerfert }; 49*330d8983SJohannes Doerfert 50*330d8983SJohannes Doerfert } // namespace utils 51*330d8983SJohannes Doerfert 52*330d8983SJohannes Doerfert #endif // OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H 53