OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/scheduler.h" | 5 #include "tools/gn/scheduler.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
9 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "build/build_config.h" |
10 #include "tools/gn/standard_out.h" | 13 #include "tools/gn/standard_out.h" |
11 #include "tools/gn/switches.h" | 14 #include "tools/gn/switches.h" |
12 | 15 |
| 16 #if defined(OS_WIN) |
| 17 #include <windows.h> |
| 18 #else |
| 19 #include <unistd.h> |
| 20 #endif |
| 21 |
13 Scheduler* g_scheduler = nullptr; | 22 Scheduler* g_scheduler = nullptr; |
14 | 23 |
15 namespace { | 24 namespace { |
16 | 25 |
| 26 #if defined(OS_WIN) |
| 27 int GetCPUCount() { |
| 28 SYSTEM_INFO sysinfo; |
| 29 ::GetSystemInfo(&sysinfo); |
| 30 return sysinfo.dwNumberOfProcessors; |
| 31 } |
| 32 #else |
| 33 int GetCPUCount() { |
| 34 return static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN)); |
| 35 } |
| 36 #endif |
| 37 |
17 int GetThreadCount() { | 38 int GetThreadCount() { |
18 std::string thread_count = | 39 std::string thread_count = |
19 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 40 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
20 switches::kThreads); | 41 switches::kThreads); |
21 | 42 |
| 43 // See if an override was specified on the command line. |
22 int result; | 44 int result; |
23 if (thread_count.empty() || !base::StringToInt(thread_count, &result)) | 45 if (!thread_count.empty() && base::StringToInt(thread_count, &result)) |
24 return 32; | 46 return result; |
25 return result; | 47 |
| 48 // Base the default number of worker threads on number of cores in the |
| 49 // system. When building large projects, the speed can be limited by how fast |
| 50 // the main thread can dispatch work and connect the dependency graph. If |
| 51 // there are too many worker threads, the main thread can be starved and it |
| 52 // will run slower overall. |
| 53 // |
| 54 // One less worker thread than the number of physical CPUs seems to be a |
| 55 // good value, both theoretically and experimentally. But always use at |
| 56 // least three workers to prevent us from being too sensitive to I/O latency |
| 57 // on low-end systems. |
| 58 int num_cores = GetCPUCount() / 2; // Almost all CPUs now are hyperthreaded. |
| 59 return std::max(num_cores - 1, 3); |
26 } | 60 } |
27 | 61 |
28 } // namespace | 62 } // namespace |
29 | 63 |
30 Scheduler::Scheduler() | 64 Scheduler::Scheduler() |
31 : pool_(new base::SequencedWorkerPool(GetThreadCount(), "worker_")), | 65 : pool_(new base::SequencedWorkerPool(GetThreadCount(), "worker_")), |
32 input_file_manager_(new InputFileManager), | 66 input_file_manager_(new InputFileManager), |
33 verbose_logging_(false), | 67 verbose_logging_(false), |
34 work_count_(0), | 68 work_count_(0), |
35 is_failed_(false), | 69 is_failed_(false), |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 void Scheduler::DoWork(const base::Closure& closure) { | 205 void Scheduler::DoWork(const base::Closure& closure) { |
172 closure.Run(); | 206 closure.Run(); |
173 DecrementWorkCount(); | 207 DecrementWorkCount(); |
174 } | 208 } |
175 | 209 |
176 void Scheduler::OnComplete() { | 210 void Scheduler::OnComplete() { |
177 // Should be called on the main thread. | 211 // Should be called on the main thread. |
178 DCHECK(base::MessageLoop::current() == main_loop()); | 212 DCHECK(base::MessageLoop::current() == main_loop()); |
179 runner_.Quit(); | 213 runner_.Quit(); |
180 } | 214 } |
OLD | NEW |