-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client: improve implementation of CPU throttling #5233
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1241,54 +1241,66 @@ void ACTIVE_TASK::set_task_state(int val, const char* where) { | |
} | ||
|
||
#ifndef SIM | ||
#// CPU throttling is done by starting/stopping running jobs | ||
// with 1-sec resolution; we can't use finer resolution because the API | ||
// polls for start/stop messages every second. | ||
// | ||
// This is done in a separate thread so that it works smoothly | ||
// even if the main thread is doing something time-consuming. | ||
// | ||
// The throttling factor can change at any time; | ||
// we need to respond to these changes reasonably quickly. | ||
// We use the following algorithm: | ||
// Maintain a "level" X | ||
// every second, add usage limit (0..100) to X. | ||
// if it's over 100, don't throttle and subtract 100 | ||
// if it's less than 100, throttle | ||
|
||
#ifdef _WIN32 | ||
DWORD WINAPI throttler(LPVOID) { | ||
#else | ||
void* throttler(void*) { | ||
#endif | ||
|
||
// Initialize diagnostics framework for this thread | ||
// | ||
static double x = 100; | ||
diagnostics_thread_init(); | ||
|
||
while (1) { | ||
client_thread_mutex.lock(); | ||
double limit = gstate.current_cpu_usage_limit(); | ||
if (gstate.tasks_suspended || limit >= 99.99) { | ||
client_thread_mutex.unlock(); | ||
// ::Sleep((int)(1000*10)); // for Win debugging | ||
boinc_sleep(10); | ||
|
||
// if limit is 100, make sure we're not throttled | ||
// | ||
if (limit >= 100) { | ||
if (gstate.tasks_throttled) { | ||
gstate.active_tasks.unsuspend_all(SUSPEND_REASON_CPU_THROTTLE); | ||
gstate.tasks_throttled = false; | ||
} | ||
boinc_sleep(1); | ||
continue; | ||
} | ||
double on, off, on_frac = limit / 100; | ||
#if 0 | ||
// sub-second CPU throttling | ||
// DOESN'T WORK BECAUSE OF 1-SEC API POLL | ||
#define THROTTLE_PERIOD 1. | ||
on = THROTTLE_PERIOD * on_frac; | ||
off = THROTTLE_PERIOD - on; | ||
#else | ||
// throttling w/ at least 1 sec between suspend/resume | ||
if (on_frac > .5) { | ||
off = 1; | ||
on = on_frac/(1.-on_frac); | ||
} else { | ||
on = 1; | ||
off = (1.-on_frac)/on_frac; | ||
|
||
// if tasks are suspended for some other reason, | ||
// we don't need to do anything | ||
// | ||
if (gstate.tasks_suspended) { | ||
boinc_sleep(1); | ||
continue; | ||
} | ||
#endif | ||
|
||
gstate.tasks_throttled = true; | ||
gstate.active_tasks.suspend_all(SUSPEND_REASON_CPU_THROTTLE); | ||
client_thread_mutex.unlock(); | ||
boinc_sleep(off); | ||
client_thread_mutex.lock(); | ||
if (!gstate.tasks_suspended) { | ||
gstate.active_tasks.unsuspend_all(SUSPEND_REASON_CPU_THROTTLE); | ||
x += limit; | ||
if (x >= 100) { | ||
if (gstate.tasks_throttled) { | ||
gstate.active_tasks.unsuspend_all(SUSPEND_REASON_CPU_THROTTLE); | ||
gstate.tasks_throttled = false; | ||
} | ||
x -= 100; | ||
} else { | ||
if (!gstate.tasks_throttled) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there still a problem that changing prefs triggers a reschedule, which will resume any suspended task without resetting |
||
gstate.active_tasks.suspend_all(SUSPEND_REASON_CPU_THROTTLE); | ||
gstate.tasks_throttled = true; | ||
} | ||
} | ||
gstate.tasks_throttled = false; | ||
client_thread_mutex.unlock(); | ||
boinc_sleep(on); | ||
boinc_sleep(1); | ||
} | ||
return 0; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need the mutex locking?