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