Skip to content

Commit

Permalink
Feature flag to set num-threads to host threads
Browse files Browse the repository at this point in the history
Currently, num-threads is hardcoded to 12. This PR sets the value to the
number of cores return by sysctl, or "host threads". This may improve
vertical scaling in some situations for larger core machines.
  • Loading branch information
jerrymarino committed Jun 9, 2021
1 parent 914eff9 commit ec5fdc6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
16 changes: 16 additions & 0 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ load(
"SWIFT_FEATURE_USE_C_MODULES",
"SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE",
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
"SWIFT_FEATURE_USE_HOST_NUM_THREADS",
"SWIFT_FEATURE_VFSOVERLAY",
"SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS",
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
Expand Down Expand Up @@ -767,6 +768,16 @@ def compile_action_configs(
],
not_features = [SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
swift_action_names.DERIVE_FILES,
],
configurators = [_wmo_host_thread_count_configurator],
features = [
[SWIFT_FEATURE_USE_HOST_NUM_THREADS, SWIFT_FEATURE__WMO_IN_SWIFTCOPTS],
],
),
swift_toolchain_config.action_config(
actions = [
swift_action_names.COMPILE,
Expand Down Expand Up @@ -1385,6 +1396,11 @@ def _wmo_thread_count_configurator(should_check_flags, prerequisites, args):
# Force threaded mode for WMO builds.
args.add("-num-threads", str(_DEFAULT_WMO_THREAD_COUNT))

def _wmo_host_thread_count_configurator(prerequisites, args):
"""Sets num-threads to number of threads on the host.
"""
args.add("-Xwrapped-swift=-use-host-num-threads")

def _is_wmo_manually_requested(user_compile_flags):
"""Returns `True` if a WMO flag is in the given list of compiler flags.
Expand Down
3 changes: 3 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,6 @@ SWIFT_FEATURE__WMO_IN_SWIFTCOPTS = "swift._wmo_in_swiftcopts"
# were passed on the command line using `--swiftcopt`. Users should never
# manually enable, disable, or query this feature.
SWIFT_FEATURE__NUM_THREADS_0_IN_SWIFTCOPTS = "swift._num_threads_0_in_swiftcopts"

# If enabled, it passes the number of threads that the host has
SWIFT_FEATURE_USE_HOST_NUM_THREADS = "swift.use_host_num_threads"
35 changes: 34 additions & 1 deletion tools/worker/swift_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "tools/worker/swift_runner.h"

#include <sys/sysctl.h>
#include <sys/types.h>

#include <fstream>

#include "tools/common/bazel_substitutions.h"
Expand Down Expand Up @@ -102,7 +105,7 @@ static bool StripPrefix(const std::string &prefix, std::string &str) {

SwiftRunner::SwiftRunner(const std::vector<std::string> &args,
bool force_response_file)
: force_response_file_(force_response_file) {
: force_response_file_(force_response_file), use_host_num_threads_(false) {
args_ = ProcessArguments(args);
}

Expand Down Expand Up @@ -257,6 +260,26 @@ bool SwiftRunner::ProcessPossibleResponseFile(
return changed;
}

static int GetNumCPU() {
int sysctlarg[4];
int numCPU;
std::size_t len = sizeof(numCPU);

// set the sysctlarg for hw.ncpu
sysctlarg[0] = CTL_HW;
sysctlarg[1] = HW_AVAILCPU;

// Gets NCPU from sysctl
sysctl(sysctlarg, 2, &numCPU, &len, NULL, 0);

if (numCPU < 1) {
sysctlarg[1] = HW_NCPU;
sysctl(sysctlarg, 2, &numCPU, &len, NULL, 0);
if (numCPU < 1) numCPU = 1;
}
return numCPU;
}

template <typename Iterator>
bool SwiftRunner::ProcessArgument(
Iterator &itr, const std::string &arg,
Expand Down Expand Up @@ -318,6 +341,14 @@ bool SwiftRunner::ProcessArgument(
++itr;
new_arg = output_file_map_path_;
changed = true;
} else if (use_host_num_threads_ && arg == "-num-threads") {
consumer("-num-threads");

// If we're using the number of threads on the host, set this value to
// the NumCPU.
++itr;
new_arg = std::to_string(GetNumCPU());
changed = true;
}

// Apply any other text substitutions needed in the argument (i.e., for
Expand Down Expand Up @@ -347,6 +378,8 @@ std::vector<std::string> SwiftRunner::ParseArguments(Iterator itr) {
global_index_store_import_path_ = arg;
} else if (StripPrefix("-generated-header-rewriter=", arg)) {
generated_header_rewriter_path_ = arg;
} else if (StripPrefix("-use-host-num-threads", arg)) {
use_host_num_threads_ = true;
}
} else {
if (arg == "-output-file-map") {
Expand Down
3 changes: 3 additions & 0 deletions tools/worker/swift_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class SwiftRunner {
// `-index-store-path`. After running `swiftc` `index-import` copies relevant
// index outputs into the `index_store_path` to integrate outputs with Bazel.
std::string global_index_store_import_path_;

// If the worker should set num-threads to number of threads on the host.
bool use_host_num_threads_;
};

#endif // BUILD_BAZEL_RULES_SWIFT_TOOLS_WORKER_SWIFT_RUNNER_H_

0 comments on commit ec5fdc6

Please sign in to comment.