Skip to content

Commit 8afd07b

Browse files
author
yangerhang
committed
Add support for changing subprocess priority.
1 parent a0bf8e9 commit 8afd07b

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

include/vcpkg/base/contractual-constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ namespace vcpkg
546546
inline constexpr StringLiteral EnvironmentVariableVcpkgForceSystemBinaries = "VCPKG_FORCE_SYSTEM_BINARIES";
547547
inline constexpr StringLiteral EnvironmentVariableVcpkgKeepEnvVars = "VCPKG_KEEP_ENV_VARS";
548548
inline constexpr StringLiteral EnvironmentVariableVcpkgMaxConcurrency = "VCPKG_MAX_CONCURRENCY";
549+
inline constexpr StringLiteral EnvironmentVariableVcpkgSubprocessPriority = "VCPKG_SUBPROCESS_PRIORITY";
549550
inline constexpr StringLiteral EnvironmentVariableVcpkgNoCi = "VCPKG_NO_CI";
550551
inline constexpr StringLiteral EnvironmentVariableVcpkgNuGetRepository = "VCPKG_NUGET_REPOSITORY";
551552
inline constexpr StringLiteral EnvironmentVariableVcpkgOverlayPorts = "VCPKG_OVERLAY_PORTS";

include/vcpkg/base/message-data.inc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,11 @@ DECLARE_MESSAGE(EnvInvalidMaxConcurrency,
11471147
(msg::env_var, msg::value),
11481148
"{value} is the invalid value of an environment variable",
11491149
"{env_var} is {value}, must be > 0")
1150+
DECLARE_MESSAGE(EnvInvalidPriority,
1151+
(msg::env_var, msg::value),
1152+
"'{value} is a user-supplied value for {env_var} environment variable.'",
1153+
"invalid value \"{value}\" for {env_var}."
1154+
"Valid values are '', 'IDLE', 'BELOW_NORMAL', 'NORMAL', 'ABOVE_NORMAL', 'HIGH' and 'REALTIME'")
11501155
DECLARE_MESSAGE(EnvStrFailedToExtract, (), "", "could not expand the environment string:")
11511156
DECLARE_MESSAGE(EnvPlatformNotSupported, (), "", "Build environment commands are not supported on this platform")
11521157
DECLARE_MESSAGE(EnvVarMustBeAbsolutePath, (msg::path, msg::env_var), "", "{env_var} ({path}) was not an absolute path")

include/vcpkg/base/system.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ namespace vcpkg
6262
const Optional<Path>& get_program_files_platform_bitness();
6363

6464
unsigned int get_concurrency();
65+
66+
#if defined(_WIN32)
67+
DWORD get_subprocess_priority();
68+
#endif
6569

6670
Optional<CPUArchitecture> guess_visual_studio_prompt_target_architecture();
6771
}

src/vcpkg/base/system.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,53 @@ namespace vcpkg
733733
return concurrency;
734734
}
735735

736+
#if defined(_WIN32)
737+
DWORD get_subprocess_priority()
738+
{
739+
static const DWORD priority = []() {
740+
const auto opt_user_defined_priority = get_environment_variable(EnvironmentVariableVcpkgSubprocessPriority);
741+
if (!opt_user_defined_priority.has_value())
742+
{
743+
return IDLE_PRIORITY_CLASS;
744+
}
745+
const auto user_defined_priority = opt_user_defined_priority.value_or_exit(VCPKG_LINE_INFO);
746+
if (user_defined_priority.empty() || Strings::case_insensitive_ascii_equals(user_defined_priority, "idle"))
747+
{
748+
return IDLE_PRIORITY_CLASS;
749+
}
750+
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "below_normal"))
751+
{
752+
return BELOW_NORMAL_PRIORITY_CLASS;
753+
}
754+
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "normal"))
755+
{
756+
return NORMAL_PRIORITY_CLASS;
757+
}
758+
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "above_normal"))
759+
{
760+
return ABOVE_NORMAL_PRIORITY_CLASS;
761+
}
762+
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "high"))
763+
{
764+
return HIGH_PRIORITY_CLASS;
765+
}
766+
else if (Strings::case_insensitive_ascii_equals(user_defined_priority, "realtime"))
767+
{
768+
return REALTIME_PRIORITY_CLASS;
769+
}
770+
else
771+
{
772+
Checks::msg_exit_with_message(VCPKG_LINE_INFO,
773+
msgEnvInvalidPriority,
774+
msg::env_var = EnvironmentVariableVcpkgSubprocessPriority,
775+
msg::value = user_defined_priority);
776+
}
777+
return IDLE_PRIORITY_CLASS;
778+
}();
779+
return priority;
780+
}
781+
#endif
782+
736783
Optional<CPUArchitecture> guess_visual_studio_prompt_target_architecture()
737784
{
738785
// Check for the "vsdevcmd" infrastructure used by Visual Studio 2017 and later

src/vcpkg/base/system.process.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ namespace
818818
call_environment = environment_block.data();
819819
}
820820

821+
DWORD dwPriority = get_subprocess_priority();
822+
821823
// Leaking process information handle 'process_info.proc_info.hProcess'
822824
// /analyze can't tell that we transferred ownership here
823825
VCPKG_MSVC_WARNING(suppress : 6335)
@@ -826,7 +828,7 @@ namespace
826828
nullptr,
827829
nullptr,
828830
bInheritHandles,
829-
IDLE_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT |
831+
dwPriority | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT |
830832
dwCreationFlags,
831833
call_environment,
832834
working_directory_arg,

0 commit comments

Comments
 (0)