GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/backoff.cc
Date: 2025-04-20 02:34:28
Exec Total Coverage
Lines: 37 39 94.9%
Branches: 6 10 60.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Exponential backoff (sleep) with cutoff.
5 */
6
7
8 #include "backoff.h"
9
10 #include <ctime>
11
12 #include "util/logging.h"
13 #include "util/posix.h"
14 #include "util/smalloc.h"
15
16 using namespace std; // NOLINT
17
18 250123 void BackoffThrottle::Init(const unsigned init_delay_ms,
19 const unsigned max_delay_ms,
20 const unsigned reset_after_ms)
21 {
22 250123 init_delay_ms_ = init_delay_ms;
23 250123 max_delay_ms_ = max_delay_ms;
24 250123 reset_after_ms_ = reset_after_ms;
25 250123 prng_.InitLocaltime();
26
27 250121 lock_ =
28 250151 reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
29 250121 int retval = pthread_mutex_init(lock_, NULL);
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250150 times.
250150 assert(retval == 0);
31
32 250150 Reset();
33 250123 }
34
35
36 249803 BackoffThrottle::~BackoffThrottle() {
37 249803 pthread_mutex_destroy(lock_);
38 249808 free(lock_);
39 249808 }
40
41
42 250128 void BackoffThrottle::Reset() {
43 250128 pthread_mutex_lock(lock_);
44 250210 delay_range_ = 0;
45 250210 last_throttle_ = 0;
46 250210 pthread_mutex_unlock(lock_);
47 250165 }
48
49
50 9 void BackoffThrottle::Throttle() {
51 9 time_t now = time(NULL);
52
53 9 pthread_mutex_lock(lock_);
54
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (unsigned(now - last_throttle_) < reset_after_ms_/1000) {
55
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (delay_range_ < max_delay_ms_) {
56
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (delay_range_ == 0)
57 3 delay_range_ = init_delay_ms_;
58 else
59 delay_range_ *= 2;
60 }
61 3 unsigned delay = prng_.Next(delay_range_) + 1;
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (delay > max_delay_ms_)
63 delay = max_delay_ms_;
64
65 3 pthread_mutex_unlock(lock_);
66 3 LogCvmfs(kLogCvmfs, kLogDebug, "backoff throttle %d ms", delay);
67 3 SafeSleepMs(delay);
68 3 pthread_mutex_lock(lock_);
69 }
70 9 last_throttle_ = now;
71 9 pthread_mutex_unlock(lock_);
72 9 }
73