Skip to content

Commit 7f7e069

Browse files
committed
Windows: use EcoQoS for ThreadPriority::Background
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.
1 parent be200e2 commit 7f7e069

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

llvm/lib/Support/Windows/Threading.inc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
107107
}
108108

109109
SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
110+
111+
typedef BOOL(WINAPI * SetThreadInformation_t)(
112+
HANDLE hThread, THREAD_INFORMATION_CLASS ThreadInformationClass,
113+
_In_reads_bytes_(ThreadInformationSize) PVOID ThreadInformation,
114+
ULONG ThreadInformationSize);
115+
static const auto pfnSetThreadInformation =
116+
(SetThreadInformation_t)GetProcAddress(
117+
GetModuleHandle(TEXT("kernel32.dll")), "SetThreadInformation");
118+
119+
if (pfnSetThreadInformation) {
120+
if (Priority == ThreadPriority::Background) {
121+
// Use EcoQoS for ThreadPriority::Background available (running on most
122+
// efficent cores at the most efficient cpu frequency):
123+
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadinformation
124+
// https://learn.microsoft.com/en-us/windows/win32/procthread/quality-of-service
125+
THREAD_POWER_THROTTLING_STATE state;
126+
memset(&state, 0, sizeof(state));
127+
state.Version = THREAD_POWER_THROTTLING_CURRENT_VERSION;
128+
state.ControlMask = THREAD_POWER_THROTTLING_EXECUTION_SPEED;
129+
state.StateMask = THREAD_POWER_THROTTLING_EXECUTION_SPEED;
130+
pfnSetThreadInformation(GetCurrentThread(), ThreadPowerThrottling, &state,
131+
sizeof(state));
132+
}
133+
}
134+
110135
// https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-setthreadpriority
111136
// Begin background processing mode. The system lowers the resource scheduling
112137
// priorities of the thread so that it can perform background work without

0 commit comments

Comments
 (0)