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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions llvm/lib/Support/Windows/Threading.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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?

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) {
auto setThreadInformation = [](ULONG ControlMask, ULONG StateMask) {
THREAD_POWER_THROTTLING_STATE state{};
state.Version = THREAD_POWER_THROTTLING_CURRENT_VERSION;
state.ControlMask = ControlMask;
state.StateMask = StateMask;
return pfnSetThreadInformation(GetCurrentThread(), ThreadPowerThrottling,
&state, sizeof(state));
};

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

THREAD_POWER_THROTTLING_EXECUTION_SPEED);
} else {
setThreadInformation(0, 0);
}
}

// 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
Expand Down
Loading