Skip to content

Commit

Permalink
Add support for changing subprocess priority.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangerhang committed Jan 18, 2025
1 parent a0bf8e9 commit 8afd07b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
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 @@ -62,6 +62,10 @@ namespace vcpkg
const Optional<Path>& get_program_files_platform_bitness();

unsigned int get_concurrency();

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

Optional<CPUArchitecture> guess_visual_studio_prompt_target_architecture();
}
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
4 changes: 3 additions & 1 deletion 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,7 +828,7 @@ namespace
nullptr,
nullptr,
bInheritHandles,
IDLE_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT |
dwPriority | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT |
dwCreationFlags,
call_environment,
working_directory_arg,
Expand Down

0 comments on commit 8afd07b

Please sign in to comment.