53 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// Implement exponential backoff counting.10// Linearly increments until given maximum, exponentially decrements based on11// given backoff factor.12//13//===----------------------------------------------------------------------===//14 15#ifndef OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H16#define OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H17 18#include <cassert>19#include <cmath>20#include <cstdint>21 22namespace utils {23 24class ExponentialBackoff {25 int64_t Count = 0;26 const int64_t MaxCount = 0;27 const int64_t CountThreshold = 0;28 const double BackoffFactor = 0;29 30public: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 43 void increment() { Count = std::min(Count + 1, MaxCount); }44 45 void decrement() { Count *= BackoffFactor; }46 47 bool isAboveThreshold() const { return Count > CountThreshold; }48};49 50} // namespace utils51 52#endif // OMPTARGET_UTILS_EXPONENTIAL_BACKOFF_H53