OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef V8_HEAP_PAGE_PARALLEL_JOB_ | |
6 #define V8_HEAP_PAGE_PARALLEL_JOB_ | |
7 | |
8 #include "src/allocation.h" | |
9 #include "src/cancelable-task.h" | |
10 #include "src/utils.h" | |
11 #include "src/v8.h" | |
12 | |
13 namespace v8 { | |
14 namespace internal { | |
15 | |
16 class Heap; | |
17 class Isolate; | |
18 | |
19 // This class manages background tasks that process set of pages in parallel. | |
20 // The JobTraits class needs to define: | |
21 // - PerPageData type - state associated with each page. | |
22 // - PerTaskData type - state associated with each task. | |
23 // - static bool ProcessPageInParallel(Heap* heap, | |
24 // PerTaskData task_data, | |
25 // MemoryChunk* page, | |
26 // PerPageData page_data) | |
27 // The function should return true iff processing succeeded. | |
28 // - static const bool NeedSequentialFinalization | |
29 // - static void FinalizePageSequentially(Heap* heap, | |
30 // bool processing_succeeded, | |
31 // MemoryChunk* page, | |
32 // PerPageData page_data) | |
33 template <typename JobTraits> | |
34 class PageParallelJob { | |
35 public: | |
36 PageParallelJob(Heap* heap, CancelableTaskManager* cancelable_task_manager) | |
37 : heap_(heap), | |
38 cancelable_task_manager_(cancelable_task_manager), | |
39 items_(nullptr), | |
40 num_items_(0), | |
41 pending_tasks_(0) {} | |
42 | |
43 ~PageParallelJob() { | |
44 Item* item = items_; | |
45 while (item != nullptr) { | |
46 Item* next = item->next; | |
47 delete item; | |
48 item = next; | |
49 } | |
50 } | |
51 | |
52 void AddPage(MemoryChunk* chunk, typename JobTraits::PerPageData data) { | |
53 Item* item = new Item(chunk, data, items_); | |
54 items_ = item; | |
55 ++num_items_; | |
56 } | |
57 | |
58 int NumberOfPages() { return num_items_; } | |
59 | |
60 // Runs the given number of tasks in parallel and processes the previosly | |
61 // added pages. This function blocks until all tasks finish. | |
62 // The callback takes the index of a task and returns data for that task. | |
63 template <typename Callback> | |
64 void Run(int num_tasks, Callback per_task_data_callback) { | |
65 if (num_items_ == 0) return; | |
Michael Lippautz
2016/03/09 09:48:53
Maybe add
DCHECK_GE(num_tasks, 1)
after the bail
ulan
2016/03/09 17:34:18
Done.
| |
66 uint32_t task_ids[kMaxNumberOfTasks]; | |
67 const int max_num_tasks = Min( | |
68 kMaxNumberOfTasks, | |
69 static_cast<int>( | |
70 V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())); | |
71 num_tasks = Max(1, Min(num_tasks, max_num_tasks)); | |
72 int items_per_task = (num_items_ + num_tasks - 1) / num_tasks; | |
73 int start_index = 0; | |
74 Task* main_task = nullptr; | |
75 for (int i = 0; i < num_tasks; i++, start_index += items_per_task) { | |
76 if (start_index >= num_items_) { | |
77 start_index -= num_items_; | |
78 } | |
79 Task* task = new Task(heap_, items_, num_items_, start_index, | |
80 &pending_tasks_, per_task_data_callback(i)); | |
81 task_ids[i] = task->id(); | |
82 if (i > 0) { | |
83 V8::GetCurrentPlatform()->CallOnBackgroundThread( | |
84 task, v8::Platform::kShortRunningTask); | |
85 } else { | |
86 main_task = task; | |
87 } | |
88 } | |
89 // Contribute on main thread. | |
90 main_task->Run(); | |
91 delete main_task; | |
92 // Wait for background tasks. | |
93 for (int i = 0; i < num_tasks; i++) { | |
94 if (!cancelable_task_manager_->TryAbort(task_ids[i])) { | |
95 pending_tasks_.Wait(); | |
96 } | |
97 } | |
98 if (JobTraits::NeedSequentialFinalization) { | |
99 Item* item = items_; | |
100 while (item != nullptr) { | |
101 bool success = (item->state.Value() == kFinished); | |
102 JobTraits::FinalizePageSequentially(heap_, item->chunk, success, | |
103 item->data); | |
104 item = item->next; | |
105 } | |
106 } | |
107 } | |
108 | |
109 private: | |
110 static const int kMaxNumberOfTasks = 10; | |
111 | |
112 enum ProcessingState { kAvailable, kProcessing, kFinished, kFailed }; | |
113 | |
114 struct Item : public Malloced { | |
115 MemoryChunk* chunk; | |
116 AtomicValue<ProcessingState> state; | |
117 typename JobTraits::PerPageData data; | |
118 Item* next; | |
119 Item(MemoryChunk* chunk, typename JobTraits::PerPageData data, Item* next) | |
Michael Lippautz
2016/03/09 09:48:53
nit: move constructor to top
ulan
2016/03/09 17:34:18
Done.
| |
120 : chunk(chunk), state(kAvailable), data(data), next(next) {} | |
121 }; | |
122 | |
123 class Task : public CancelableTask { | |
124 public: | |
125 Task(Heap* heap, Item* items, int num_items, int start_index, | |
126 base::Semaphore* on_finish, typename JobTraits::PerTaskData data) | |
127 : CancelableTask(heap->isolate()), | |
128 heap_(heap), | |
129 items_(items), | |
130 num_items_(num_items), | |
131 start_index_(start_index), | |
132 on_finish_(on_finish), | |
133 data_(data) {} | |
134 | |
135 virtual ~Task() {} | |
136 | |
137 private: | |
138 // v8::internal::CancelableTask overrides. | |
139 void RunInternal() override { | |
140 // Each task starts at a different index to improve parallelization. | |
141 Item* current = items_; | |
142 int skip = start_index_; | |
143 while (skip-- > 0) { | |
144 current = current->next; | |
145 } | |
146 for (int i = 0; i < num_items_; i++) { | |
147 if (current->state.TrySetValue(kAvailable, kProcessing)) { | |
148 bool success = JobTraits::ProcessPageInParallel( | |
149 heap_, data_, current->chunk, current->data); | |
150 current->state.SetValue(success ? kFinished : kFailed); | |
151 } | |
152 current = current->next; | |
153 // Wrap around if needed. | |
154 if (current == nullptr) { | |
155 current = items_; | |
156 } | |
157 } | |
158 on_finish_->Signal(); | |
159 } | |
160 | |
161 Heap* heap_; | |
162 Item* items_; | |
163 int num_items_; | |
164 int start_index_; | |
165 base::Semaphore* on_finish_; | |
166 typename JobTraits::PerTaskData data_; | |
167 DISALLOW_COPY_AND_ASSIGN(Task); | |
168 }; | |
169 | |
170 Heap* heap_; | |
171 CancelableTaskManager* cancelable_task_manager_; | |
172 Item* items_; | |
173 int num_items_; | |
174 base::Semaphore pending_tasks_; | |
175 DISALLOW_COPY_AND_ASSIGN(PageParallelJob); | |
176 }; | |
177 | |
178 } // namespace internal | |
179 } // namespace v8 | |
180 | |
181 #endif // V8_HEAP_PAGE_PARALLEL_JOB_ | |
OLD | NEW |