OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project 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 #ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ | 5 #ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ |
6 #define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ | 6 #define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <unordered_set> | |
11 #include <utility> | 10 #include <utility> |
12 | 11 |
13 #include "src/base/macros.h" | 12 #include "src/base/macros.h" |
14 #include "src/base/platform/condition-variable.h" | |
15 #include "src/base/platform/mutex.h" | |
16 #include "src/globals.h" | 13 #include "src/globals.h" |
17 #include "testing/gtest/include/gtest/gtest_prod.h" | 14 #include "testing/gtest/include/gtest/gtest_prod.h" |
18 | 15 |
19 namespace v8 { | 16 namespace v8 { |
20 | 17 |
21 class Platform; | 18 class Platform; |
22 | 19 |
23 namespace internal { | 20 namespace internal { |
24 | 21 |
25 class CancelableTaskManager; | |
26 class CompilerDispatcherJob; | 22 class CompilerDispatcherJob; |
27 class CompilerDispatcherTracer; | 23 class CompilerDispatcherTracer; |
28 class Isolate; | 24 class Isolate; |
29 class SharedFunctionInfo; | 25 class SharedFunctionInfo; |
30 | 26 |
31 template <typename T> | 27 template <typename T> |
32 class Handle; | 28 class Handle; |
33 | 29 |
34 // The CompilerDispatcher uses a combination of idle tasks and background tasks | 30 // The CompilerDispatcher uses a combination of idle tasks and background tasks |
35 // to parse and compile lazily parsed functions. | 31 // to parse and compile lazily parsed functions. |
36 // | |
37 // As both parsing and compilation currently requires a preparation and | |
38 // finalization step that happens on the main thread, every task has to be | |
39 // advanced during idle time first. Depending on the properties of the task, it | |
40 // can then be parsed or compiled on either background threads, or during idle | |
41 // time. Last, it has to be finalized during idle time again. | |
42 // | |
43 // CompilerDispatcher::jobs_ maintains the list of all CompilerDispatcherJobs | |
44 // the CompilerDispatcher knows about. | |
45 // | |
46 // CompilerDispatcher::pending_background_jobs_ contains the set of | |
47 // CompilerDispatcherJobs that can be processed on a background thread. | |
48 // | |
49 // CompilerDispatcher::running_background_jobs_ contains the set of | |
50 // CompilerDispatcherJobs that are currently being processed on a background | |
51 // thread. | |
52 // | |
53 // CompilerDispatcher::DoIdleWork tries to advance as many jobs out of jobs_ as | |
54 // possible during idle time. If a job can't be advanced, but is suitable for | |
55 // background processing, it fires off background threads. | |
56 // | |
57 // CompilerDispatcher::DoBackgroundWork advances one of the pending jobs, and | |
58 // then spins of another idle task to potentially do the final step on the main | |
59 // thread. | |
60 class V8_EXPORT_PRIVATE CompilerDispatcher { | 32 class V8_EXPORT_PRIVATE CompilerDispatcher { |
61 public: | 33 public: |
62 enum class BlockingBehavior { kBlock, kDontBlock }; | 34 enum class BlockingBehavior { kBlock, kDontBlock }; |
63 | 35 |
64 CompilerDispatcher(Isolate* isolate, Platform* platform, | 36 CompilerDispatcher(Isolate* isolate, Platform* platform, |
65 size_t max_stack_size); | 37 size_t max_stack_size); |
66 ~CompilerDispatcher(); | 38 ~CompilerDispatcher(); |
67 | 39 |
68 // Returns true if a job was enqueued. | 40 // Returns true if a job was enqueued. |
69 bool Enqueue(Handle<SharedFunctionInfo> function); | 41 bool Enqueue(Handle<SharedFunctionInfo> function); |
70 | 42 |
71 // Returns true if there is a pending job for the given function. | 43 // Returns true if there is a pending job for the given function. |
72 bool IsEnqueued(Handle<SharedFunctionInfo> function) const; | 44 bool IsEnqueued(Handle<SharedFunctionInfo> function) const; |
73 | 45 |
74 // Blocks until the given function is compiled (and does so as fast as | 46 // Blocks until the given function is compiled (and does so as fast as |
75 // possible). Returns true if the compile job was succesful. | 47 // possible). Returns true if the compile job was succesful. |
76 bool FinishNow(Handle<SharedFunctionInfo> function); | 48 bool FinishNow(Handle<SharedFunctionInfo> function); |
77 | 49 |
78 // Aborts a given job. Blocks if requested. | 50 // Aborts a given job. Blocks if requested. |
79 void Abort(Handle<SharedFunctionInfo> function, BlockingBehavior blocking); | 51 void Abort(Handle<SharedFunctionInfo> function, BlockingBehavior blocking); |
80 | 52 |
81 // Aborts all jobs. Blocks if requested. | 53 // Aborts all jobs. Blocks if requested. |
82 void AbortAll(BlockingBehavior blocking); | 54 void AbortAll(BlockingBehavior blocking); |
83 | 55 |
84 private: | 56 private: |
85 FRIEND_TEST(CompilerDispatcherTest, IdleTaskSmallIdleTime); | 57 FRIEND_TEST(CompilerDispatcherTest, IdleTaskSmallIdleTime); |
86 FRIEND_TEST(IgnitionCompilerDispatcherTest, CompileOnBackgroundThread); | |
87 FRIEND_TEST(IgnitionCompilerDispatcherTest, FinishNowWithBackgroundTask); | |
88 | 58 |
89 typedef std::multimap<std::pair<int, int>, | 59 typedef std::multimap<std::pair<int, int>, |
90 std::unique_ptr<CompilerDispatcherJob>> | 60 std::unique_ptr<CompilerDispatcherJob>> |
91 JobMap; | 61 JobMap; |
92 class BackgroundTask; | |
93 class IdleTask; | 62 class IdleTask; |
94 | 63 |
95 void WaitForJobIfRunningOnBackground(CompilerDispatcherJob* job); | |
96 bool IsEnabled() const; | 64 bool IsEnabled() const; |
97 JobMap::const_iterator GetJobFor(Handle<SharedFunctionInfo> shared) const; | 65 JobMap::const_iterator GetJobFor(Handle<SharedFunctionInfo> shared) const; |
98 void ConsiderJobForBackgroundProcessing(CompilerDispatcherJob* job); | |
99 void ScheduleMoreBackgroundTasksIfNeeded(); | |
100 void ScheduleIdleTaskFromAnyThread(); | |
101 void ScheduleIdleTaskIfNeeded(); | 66 void ScheduleIdleTaskIfNeeded(); |
102 void DoBackgroundWork(); | |
103 void DoIdleWork(double deadline_in_seconds); | 67 void DoIdleWork(double deadline_in_seconds); |
104 | 68 |
105 Isolate* isolate_; | 69 Isolate* isolate_; |
106 Platform* platform_; | 70 Platform* platform_; |
107 size_t max_stack_size_; | 71 size_t max_stack_size_; |
108 std::unique_ptr<CompilerDispatcherTracer> tracer_; | 72 std::unique_ptr<CompilerDispatcherTracer> tracer_; |
109 | 73 |
110 std::unique_ptr<CancelableTaskManager> task_manager_; | 74 bool idle_task_scheduled_; |
111 | 75 |
112 // Mapping from (script id, function literal id) to job. We use a multimap, | 76 // Mapping from (script id, function literal id) to job. We use a multimap, |
113 // as script id is not necessarily unique. | 77 // as script id is not necessarily unique. |
114 JobMap jobs_; | 78 JobMap jobs_; |
115 | 79 |
116 // The following members can be accessed from any thread. Methods need to hold | |
117 // the mutex |mutex_| while accessing them. | |
118 base::Mutex mutex_; | |
119 | |
120 bool idle_task_scheduled_; | |
121 | |
122 // Number of currently scheduled BackgroundTask objects. | |
123 size_t num_scheduled_background_tasks_; | |
124 | |
125 // The set of CompilerDispatcherJobs that can be advanced on any thread. | |
126 std::unordered_set<CompilerDispatcherJob*> pending_background_jobs_; | |
127 | |
128 // The set of CompilerDispatcherJobs currently processed on background | |
129 // threads. | |
130 std::unordered_set<CompilerDispatcherJob*> running_background_jobs_; | |
131 | |
132 // If not nullptr, then the main thread waits for the task processing | |
133 // this job, and blocks on the ConditionVariable main_thread_blocking_signal_. | |
134 CompilerDispatcherJob* main_thread_blocking_on_job_; | |
135 base::ConditionVariable main_thread_blocking_signal_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcher); | 80 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcher); |
138 }; | 81 }; |
139 | 82 |
140 } // namespace internal | 83 } // namespace internal |
141 } // namespace v8 | 84 } // namespace v8 |
142 | 85 |
143 #endif // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ | 86 #endif // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ |
OLD | NEW |