Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: tools/gn/scheduler.cc

Issue 1464463003: Reduce the number of worker threads GN uses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/gn/target.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/scheduler.cc
diff --git a/tools/gn/scheduler.cc b/tools/gn/scheduler.cc
index 622019ef05635af0769107e0f7fa4e01ae31d451..dfc877d190bc1522f6f035432c8f6469fd4e2af0 100644
--- a/tools/gn/scheduler.cc
+++ b/tools/gn/scheduler.cc
@@ -4,25 +4,59 @@
#include "tools/gn/scheduler.h"
+#include <algorithm>
+
#include "base/bind.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
+#include "build/build_config.h"
#include "tools/gn/standard_out.h"
#include "tools/gn/switches.h"
+#if defined(OS_WIN)
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
Scheduler* g_scheduler = nullptr;
namespace {
+#if defined(OS_WIN)
+int GetCPUCount() {
+ SYSTEM_INFO sysinfo;
+ ::GetSystemInfo(&sysinfo);
+ return sysinfo.dwNumberOfProcessors;
+}
+#else
+int GetCPUCount() {
+ return static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
+}
+#endif
+
int GetThreadCount() {
std::string thread_count =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kThreads);
+ // See if an override was specified on the command line.
int result;
- if (thread_count.empty() || !base::StringToInt(thread_count, &result))
- return 32;
- return result;
+ if (!thread_count.empty() && base::StringToInt(thread_count, &result))
+ return result;
+
+ // Base the default number of worker threads on number of cores in the
+ // system. When building large projects, the speed can be limited by how fast
+ // the main thread can dispatch work and connect the dependency graph. If
+ // there are too many worker threads, the main thread can be starved and it
+ // will run slower overall.
+ //
+ // One less worker thread than the number of physical CPUs seems to be a
+ // good value, both theoretically and experimentally. But always use at
+ // least three workers to prevent us from being too sensitive to I/O latency
+ // on low-end systems.
+ int num_cores = GetCPUCount() / 2; // Almost all CPUs now are hyperthreaded.
+ return std::max(num_cores - 1, 3);
}
} // namespace
« no previous file with comments | « no previous file | tools/gn/target.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698