Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

timblechmann
Copy link

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.

Copy link

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 @ followed by their GitHub username.

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 the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-llvm-support

Author: Tim Blechmann (timblechmann)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/148797.diff

1 Files Affected:

  • (modified) llvm/lib/Support/Windows/Threading.inc (+25)
diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc
index 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

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.
Copy link
Member

@aganea aganea left a 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) {
}

SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {

// SetThreadInformation is only available on Windows 8 and later. so we load
// it at run-time
Copy link
Member

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,
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants