- Notifications
You must be signed in to change notification settings - Fork14.5k
Windows: use EcoQoS for ThreadPriority::Background#148797
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by theLLVM GitHub User Guide. You can also ask questions in a comment on this PR, on theLLVM Discord or on theforums. |
llvmbot commentedJul 15, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@llvm/pr-subscribers-platform-windows @llvm/pr-subscribers-llvm-support Author: Tim Blechmann (timblechmann) ChangesThe SetThreadInformation API allows threads to be scheduled on the most efficient cores on the most efficient frequency. Full diff:https://github.com/llvm/llvm-project/pull/148797.diff 1 Files Affected:
diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.incindex d862dbd7f71c9..b52c6dff4e5f4 100644--- a/llvm/lib/Support/Windows/Threading.inc+++ b/llvm/lib/Support/Windows/Threading.inc@@ -107,6 +107,31 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) { } SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {++ typedef BOOL(WINAPI * SetThreadInformation_t)(+ HANDLE hThread, THREAD_INFORMATION_CLASS ThreadInformationClass,+ _In_reads_bytes_(ThreadInformationSize) PVOID ThreadInformation,+ ULONG ThreadInformationSize);+ static const auto pfnSetThreadInformation =+ (SetThreadInformation_t)GetProcAddress(+ GetModuleHandle(TEXT("kernel32.dll")), "SetThreadInformation");++ if (pfnSetThreadInformation) {+ if (Priority == ThreadPriority::Background) {+ // Use EcoQoS for ThreadPriority::Background available (running on most+ // efficent cores at the most efficient cpu frequency):+ // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadinformation+ // https://learn.microsoft.com/en-us/windows/win32/procthread/quality-of-service+ THREAD_POWER_THROTTLING_STATE state;+ memset(&state, 0, sizeof(state));+ state.Version = THREAD_POWER_THROTTLING_CURRENT_VERSION;+ state.ControlMask = THREAD_POWER_THROTTLING_EXECUTION_SPEED;+ state.StateMask = THREAD_POWER_THROTTLING_EXECUTION_SPEED;+ pfnSetThreadInformation(GetCurrentThread(), ThreadPowerThrottling, &state,+ sizeof(state));+ }+ }+ // https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-setthreadpriority // Begin background processing mode. The system lowers the resource scheduling // priorities of the thread so that it can perform background work without |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
The SetThreadInformation API allows threads to be scheduled on the mostefficient cores on the most efficient frequency.Using this API for ThreadPriority::Background should make clangd-basedIDEs a little less CPU hungry.
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.
Thanks for the changes. Just some more minor nits:
@@ -107,6 +107,39 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) { | |||
} | |||
SetThreadPriorityResultllvm::set_thread_priority(ThreadPriority Priority) { | |||
// SetThreadInformation is only available on Windows 8 and later. so we load | |||
// it at run-time |
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.
Can you please mention explicitely that we want to keep compatibility with Windows 7?
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadinformation | ||
// https://learn.microsoft.com/en-us/windows/win32/procthread/quality-of-service | ||
if (Priority == ThreadPriority::Background) { | ||
setThreadInformation(THREAD_POWER_THROTTLING_EXECUTION_SPEED, |
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.
For the current requirements, we don't need to be that verbose here, just one value would suffice here (and use it for both control mask and state mask above).
The SetThreadInformation API allows threads to be scheduled on the most efficient cores on the most efficient frequency.
Using this API for ThreadPriority::Background should make clangd-based IDEs a little less CPU hungry.