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> |
10 #include <utility> | 11 #include <utility> |
11 | 12 |
12 #include "src/base/macros.h" | 13 #include "src/base/macros.h" |
| 14 #include "src/base/platform/condition-variable.h" |
| 15 #include "src/base/platform/mutex.h" |
13 #include "src/globals.h" | 16 #include "src/globals.h" |
14 #include "testing/gtest/include/gtest/gtest_prod.h" | 17 #include "testing/gtest/include/gtest/gtest_prod.h" |
15 | 18 |
16 namespace v8 { | 19 namespace v8 { |
17 | 20 |
18 class Platform; | 21 class Platform; |
19 | 22 |
20 namespace internal { | 23 namespace internal { |
21 | 24 |
| 25 class CancelableTaskManager; |
22 class CompilerDispatcherJob; | 26 class CompilerDispatcherJob; |
23 class CompilerDispatcherTracer; | 27 class CompilerDispatcherTracer; |
24 class Isolate; | 28 class Isolate; |
25 class SharedFunctionInfo; | 29 class SharedFunctionInfo; |
26 | 30 |
27 template <typename T> | 31 template <typename T> |
28 class Handle; | 32 class Handle; |
29 | 33 |
30 // The CompilerDispatcher uses a combination of idle tasks and background tasks | 34 // The CompilerDispatcher uses a combination of idle tasks and background tasks |
31 // to parse and compile lazily parsed functions. | 35 // 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. |
32 class V8_EXPORT_PRIVATE CompilerDispatcher { | 60 class V8_EXPORT_PRIVATE CompilerDispatcher { |
33 public: | 61 public: |
34 enum class BlockingBehavior { kBlock, kDontBlock }; | 62 enum class BlockingBehavior { kBlock, kDontBlock }; |
35 | 63 |
36 CompilerDispatcher(Isolate* isolate, Platform* platform, | 64 CompilerDispatcher(Isolate* isolate, Platform* platform, |
37 size_t max_stack_size); | 65 size_t max_stack_size); |
38 ~CompilerDispatcher(); | 66 ~CompilerDispatcher(); |
39 | 67 |
40 // Returns true if a job was enqueued. | 68 // Returns true if a job was enqueued. |
41 bool Enqueue(Handle<SharedFunctionInfo> function); | 69 bool Enqueue(Handle<SharedFunctionInfo> function); |
42 | 70 |
43 // Returns true if there is a pending job for the given function. | 71 // Returns true if there is a pending job for the given function. |
44 bool IsEnqueued(Handle<SharedFunctionInfo> function) const; | 72 bool IsEnqueued(Handle<SharedFunctionInfo> function) const; |
45 | 73 |
46 // Blocks until the given function is compiled (and does so as fast as | 74 // Blocks until the given function is compiled (and does so as fast as |
47 // possible). Returns true if the compile job was succesful. | 75 // possible). Returns true if the compile job was succesful. |
48 bool FinishNow(Handle<SharedFunctionInfo> function); | 76 bool FinishNow(Handle<SharedFunctionInfo> function); |
49 | 77 |
50 // Aborts a given job. Blocks if requested. | 78 // Aborts a given job. Blocks if requested. |
51 void Abort(Handle<SharedFunctionInfo> function, BlockingBehavior blocking); | 79 void Abort(Handle<SharedFunctionInfo> function, BlockingBehavior blocking); |
52 | 80 |
53 // Aborts all jobs. Blocks if requested. | 81 // Aborts all jobs. Blocks if requested. |
54 void AbortAll(BlockingBehavior blocking); | 82 void AbortAll(BlockingBehavior blocking); |
55 | 83 |
56 private: | 84 private: |
57 FRIEND_TEST(CompilerDispatcherTest, IdleTaskSmallIdleTime); | 85 FRIEND_TEST(CompilerDispatcherTest, IdleTaskSmallIdleTime); |
| 86 FRIEND_TEST(IgnitionCompilerDispatcherTest, CompileOnBackgroundThread); |
| 87 FRIEND_TEST(IgnitionCompilerDispatcherTest, FinishNowWithBackgroundTask); |
58 | 88 |
59 typedef std::multimap<std::pair<int, int>, | 89 typedef std::multimap<std::pair<int, int>, |
60 std::unique_ptr<CompilerDispatcherJob>> | 90 std::unique_ptr<CompilerDispatcherJob>> |
61 JobMap; | 91 JobMap; |
| 92 class BackgroundTask; |
62 class IdleTask; | 93 class IdleTask; |
63 | 94 |
| 95 void WaitForJobIfRunningOnBackground(CompilerDispatcherJob* job); |
64 bool IsEnabled() const; | 96 bool IsEnabled() const; |
65 JobMap::const_iterator GetJobFor(Handle<SharedFunctionInfo> shared) const; | 97 JobMap::const_iterator GetJobFor(Handle<SharedFunctionInfo> shared) const; |
| 98 void ConsiderJobForBackgroundProcessing(CompilerDispatcherJob* job); |
| 99 void ScheduleMoreBackgroundTasksIfNeeded(); |
| 100 void ScheduleIdleTaskFromAnyThread(); |
66 void ScheduleIdleTaskIfNeeded(); | 101 void ScheduleIdleTaskIfNeeded(); |
| 102 void DoBackgroundWork(); |
67 void DoIdleWork(double deadline_in_seconds); | 103 void DoIdleWork(double deadline_in_seconds); |
68 | 104 |
69 Isolate* isolate_; | 105 Isolate* isolate_; |
70 Platform* platform_; | 106 Platform* platform_; |
71 size_t max_stack_size_; | 107 size_t max_stack_size_; |
72 std::unique_ptr<CompilerDispatcherTracer> tracer_; | 108 std::unique_ptr<CompilerDispatcherTracer> tracer_; |
73 | 109 |
74 bool idle_task_scheduled_; | 110 std::unique_ptr<CancelableTaskManager> task_manager_; |
75 | 111 |
76 // Mapping from (script id, function literal id) to job. We use a multimap, | 112 // Mapping from (script id, function literal id) to job. We use a multimap, |
77 // as script id is not necessarily unique. | 113 // as script id is not necessarily unique. |
78 JobMap jobs_; | 114 JobMap jobs_; |
79 | 115 |
| 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 |
80 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcher); | 137 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcher); |
81 }; | 138 }; |
82 | 139 |
83 } // namespace internal | 140 } // namespace internal |
84 } // namespace v8 | 141 } // namespace v8 |
85 | 142 |
86 #endif // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ | 143 #endif // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ |
OLD | NEW |