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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | tools/gn/target.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« 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