Skip to content
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

Change subprocesses to run at normal priority (#42715) #1555

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions include/vcpkg/base/contractual-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ namespace vcpkg
inline constexpr StringLiteral EnvironmentVariableVcpkgForceSystemBinaries = "VCPKG_FORCE_SYSTEM_BINARIES";
inline constexpr StringLiteral EnvironmentVariableVcpkgKeepEnvVars = "VCPKG_KEEP_ENV_VARS";
inline constexpr StringLiteral EnvironmentVariableVcpkgMaxConcurrency = "VCPKG_MAX_CONCURRENCY";
inline constexpr StringLiteral EnvironmentVariableVcpkgSubprocessPriority = "VCPKG_SUBPROCESS_PRIORITY";
inline constexpr StringLiteral EnvironmentVariableVcpkgNoCi = "VCPKG_NO_CI";
inline constexpr StringLiteral EnvironmentVariableVcpkgNuGetRepository = "VCPKG_NUGET_REPOSITORY";
inline constexpr StringLiteral EnvironmentVariableVcpkgOverlayPorts = "VCPKG_OVERLAY_PORTS";
Expand Down
5 changes: 5 additions & 0 deletions include/vcpkg/base/message-data.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,11 @@ DECLARE_MESSAGE(EnvInvalidMaxConcurrency,
(msg::env_var, msg::value),
"{value} is the invalid value of an environment variable",
"{env_var} is {value}, must be > 0")
DECLARE_MESSAGE(EnvInvalidPriority,
(msg::env_var, msg::value),
"'{value} is a user-supplied value for {env_var} environment variable.'",
"invalid value \"{value}\" for {env_var}."
"Valid values are '', 'IDLE', 'BELOW_NORMAL', 'NORMAL', 'ABOVE_NORMAL', 'HIGH' and 'REALTIME'")
DECLARE_MESSAGE(EnvStrFailedToExtract, (), "", "could not expand the environment string:")
DECLARE_MESSAGE(EnvPlatformNotSupported, (), "", "Build environment commands are not supported on this platform")
DECLARE_MESSAGE(EnvVarMustBeAbsolutePath, (msg::path, msg::env_var), "", "{env_var} ({path}) was not an absolute path")
Expand Down
4 changes: 4 additions & 0 deletions include/vcpkg/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ namespace vcpkg

unsigned int get_concurrency();

#if defined(_WIN32)
DWORD get_subprocess_priority();
#endif

Optional<CPUArchitecture> guess_visual_studio_prompt_target_architecture();
}

Expand Down
2 changes: 2 additions & 0 deletions locales/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@
"EndOfStringInCodeUnit": "found end of string in middle of code point",
"EnvInvalidMaxConcurrency": "{env_var} is {value}, must be > 0",
"_EnvInvalidMaxConcurrency.comment": "{value} is the invalid value of an environment variable An example of {env_var} is VCPKG_DEFAULT_TRIPLET.",
"EnvInvalidPriority": "invalid value \"{value}\" for {env_var}.Valid values are '', 'IDLE', 'BELOW_NORMAL', 'NORMAL', 'ABOVE_NORMAL', 'HIGH' and 'REALTIME'",
"_EnvInvalidPriority.comment": "'{value} is a user-supplied value for {env_var} environment variable.' An example of {env_var} is VCPKG_DEFAULT_TRIPLET.",
"EnvPlatformNotSupported": "Build environment commands are not supported on this platform",
"EnvStrFailedToExtract": "could not expand the environment string:",
"EnvVarMustBeAbsolutePath": "{env_var} ({path}) was not an absolute path",
Expand Down
47 changes: 47 additions & 0 deletions src/vcpkg/base/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,53 @@ namespace vcpkg
return concurrency;
}

#if defined(_WIN32)
DWORD get_subprocess_priority()
{
static const DWORD priority = []() {
const auto opt_user_defined_priority = get_environment_variable(EnvironmentVariableVcpkgSubprocessPriority);
if (!opt_user_defined_priority.has_value())
{
return IDLE_PRIORITY_CLASS;
}
const auto user_defined_priority = opt_user_defined_priority.value_or_exit(VCPKG_LINE_INFO);
if (user_defined_priority.empty() || Strings::case_insensitive_ascii_equals(user_defined_priority, "idle"))
{
return IDLE_PRIORITY_CLASS;
}
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "below_normal"))
{
return BELOW_NORMAL_PRIORITY_CLASS;
}
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "normal"))
{
return NORMAL_PRIORITY_CLASS;
}
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "above_normal"))
{
return ABOVE_NORMAL_PRIORITY_CLASS;
}
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "high"))
{
return HIGH_PRIORITY_CLASS;
}
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "realtime"))
{
return REALTIME_PRIORITY_CLASS;
}
else
{
Checks::msg_exit_with_message(VCPKG_LINE_INFO,
msgEnvInvalidPriority,
msg::env_var = EnvironmentVariableVcpkgSubprocessPriority,
msg::value = user_defined_priority);
}
return IDLE_PRIORITY_CLASS;
}();
return priority;
}
#endif

Optional<CPUArchitecture> guess_visual_studio_prompt_target_architecture()
{
// Check for the "vsdevcmd" infrastructure used by Visual Studio 2017 and later
Expand Down
5 changes: 3 additions & 2 deletions src/vcpkg/base/system.process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ namespace
call_environment = environment_block.data();
}

DWORD dwPriority = get_subprocess_priority();

// Leaking process information handle 'process_info.proc_info.hProcess'
// /analyze can't tell that we transferred ownership here
VCPKG_MSVC_WARNING(suppress : 6335)
Expand All @@ -826,8 +828,7 @@ namespace
nullptr,
nullptr,
bInheritHandles,
IDLE_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT |
dwCreationFlags,
dwPriority | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT | dwCreationFlags,
call_environment,
working_directory_arg,
&startup_info.StartupInfo,
Expand Down