Skip to content

Commit

Permalink
Add function 'boinc_wobble_sleep'
Browse files Browse the repository at this point in the history
  • Loading branch information
computezrmle authored Mar 24, 2024
1 parent b2af307 commit 8f7ed63
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#define M_LN2 0.693147180559945309417
#endif

#ifndef BOINC_WOBBLE_SLEEP_MIN_SECONDS
#define BOINC_WOBBLE_SLEEP_MIN_SECONDS 0.01
#endif

#include "boinc_stdio.h"

#ifndef _WIN32
Expand Down Expand Up @@ -130,6 +134,67 @@ void boinc_sleep(double seconds) {
#endif
}

void boinc_wobble_sleep(double min_sec, double max_sec, double wobble) {

Check warning on line 137 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L137

Added line #L137 was not covered by tests
// usage examples
//
// from another function call (e.g. in a loop):
// boinc_wobble_sleep(1.7, 2.3, 0.1);
// results in a sleep with 1 value per call (by random) out of this possible values:
// 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3
//
// from another function call (e.g. in a loop):
// boinc_wobble_sleep(3.0, 5.0, 2.0);
// results in a sleep with 1 value per call (by random) out of this possible values:
// 3.0, 5.0
//
// from another function call (e.g. in a loop):
// boinc_wobble_sleep(1.0, 1.0, 0.3);
// results in a sleep with the same value per call:
// 1.0
// same as to call boinc_sleep(1.0) in this case
//

double diff_sec = 0.0;

Check warning on line 157 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L157

Added line #L157 was not covered by tests

// plausibility checks
//
min_sec = (min_sec < 0.0) ? 0.0 : min_sec;
max_sec = (max_sec < min_sec) ? min_sec : max_sec;

Check warning on line 162 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L161-L162

Added lines #L161 - L162 were not covered by tests

// don't wobble if min_sec and max_sec are too close together
//
diff_sec = max_sec - min_sec;

Check warning on line 166 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L166

Added line #L166 was not covered by tests
if (diff_sec < BOINC_WOBBLE_SLEEP_MIN_SECONDS) {
// don't sleep at all if also min_sec == 0
//
if (min_sec > 0.0) {
boinc_sleep(min_sec);

Check warning on line 171 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L171

Added line #L171 was not covered by tests
}
} else {
int wobble_n = 0;
int rand_index = 0;

Check warning on line 175 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L174-L175

Added lines #L174 - L175 were not covered by tests

// adjustments
//
wobble = (wobble < BOINC_WOBBLE_SLEEP_MIN_SECONDS) ? BOINC_WOBBLE_SLEEP_MIN_SECONDS : wobble;
wobble = (wobble > diff_sec) ? diff_sec : wobble;
wobble_n = (int)(diff_sec / wobble);
wobble = diff_sec / (double)wobble_n;

Check warning on line 182 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L179-L182

Added lines #L179 - L182 were not covered by tests

// choose a random index between 0 and wobble_n
//
std::random_device rand_device;
std::default_random_engine rand_engine(rand_device());
std::uniform_int_distribution<int> uniform_dist(0, wobble_n);
rand_index = uniform_dist(rand_engine);

Check warning on line 189 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L186-L189

Added lines #L186 - L189 were not covered by tests

// finally call boinc_sleep with the wobbled value
//
min_sec = min_sec + (double)rand_index * (double)wobble;
boinc_sleep(min_sec);

Check warning on line 194 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L193-L194

Added lines #L193 - L194 were not covered by tests
}
}

void push_unique(string s, vector<string>& v) {
for (unsigned int i=0; i<v.size();i++) {
if (s == v[i]) return;
Expand Down
2 changes: 2 additions & 0 deletions lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef BOINC_UTIL_H
#define BOINC_UTIL_H

#include <random>
#include <stdlib.h>
#include <string>
#include <vector>
Expand All @@ -29,6 +30,7 @@
extern double dtime();
extern double dday();
extern void boinc_sleep(double);
extern void boinc_wobble_sleep(double, double, double);
extern void push_unique(std::string, std::vector<std::string>&);

// NOTE: use #include <functional> to get max,min
Expand Down

0 comments on commit 8f7ed63

Please sign in to comment.