2017-05-16 20:57:05 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/heap/item-parallel-job.h"
|
|
|
|
|
|
|
|
#include "src/isolate.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class ItemParallelJobTest : public TestWithIsolate {
|
|
|
|
public:
|
|
|
|
ItemParallelJobTest() : parallel_job_semaphore_(0) {}
|
|
|
|
|
|
|
|
base::Semaphore* parallel_job_semaphore() { return ¶llel_job_semaphore_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::Semaphore parallel_job_semaphore_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ItemParallelJobTest);
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-01-30 16:40:55 +00:00
|
|
|
class SimpleTask : public ItemParallelJob::Task {
|
2017-05-16 20:57:05 +00:00
|
|
|
public:
|
2018-01-30 16:40:55 +00:00
|
|
|
SimpleTask(Isolate* isolate, bool* did_run)
|
2017-05-16 20:57:05 +00:00
|
|
|
: ItemParallelJob::Task(isolate), did_run_(did_run) {}
|
|
|
|
|
2018-01-30 16:40:55 +00:00
|
|
|
void RunInParallel() override {
|
|
|
|
ItemParallelJob::Item* item = nullptr;
|
|
|
|
while ((item = GetItem<ItemParallelJob::Item>()) != nullptr) {
|
|
|
|
item->MarkFinished();
|
|
|
|
}
|
|
|
|
*did_run_ = true;
|
|
|
|
}
|
2017-05-16 20:57:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool* did_run_;
|
|
|
|
};
|
|
|
|
|
2018-01-30 16:40:55 +00:00
|
|
|
// A simple work item which sets |was_processed| to true, if non-null, when it
|
|
|
|
// is processed.
|
2017-05-16 20:57:05 +00:00
|
|
|
class SimpleItem : public ItemParallelJob::Item {
|
|
|
|
public:
|
2018-01-30 16:40:55 +00:00
|
|
|
explicit SimpleItem(bool* was_processed = nullptr)
|
2017-05-16 20:57:05 +00:00
|
|
|
: ItemParallelJob::Item(), was_processed_(was_processed) {}
|
2018-01-30 16:40:55 +00:00
|
|
|
void Process() {
|
|
|
|
if (was_processed_) *was_processed_ = true;
|
|
|
|
}
|
2017-05-16 20:57:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool* was_processed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EagerTask : public ItemParallelJob::Task {
|
|
|
|
public:
|
|
|
|
explicit EagerTask(Isolate* isolate) : ItemParallelJob::Task(isolate) {}
|
|
|
|
|
|
|
|
void RunInParallel() override {
|
|
|
|
SimpleItem* item = nullptr;
|
|
|
|
while ((item = GetItem<SimpleItem>()) != nullptr) {
|
|
|
|
item->Process();
|
|
|
|
item->MarkFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-01 08:36:38 +00:00
|
|
|
// A OneShotBarrier is meant to be passed to |counter| users. Users should
|
|
|
|
// either Signal() or Wait() when done (based on whether they want to return
|
|
|
|
// immediately or wait until others are also done).
|
2017-05-17 07:58:33 +00:00
|
|
|
class OneShotBarrier {
|
|
|
|
public:
|
|
|
|
explicit OneShotBarrier(size_t counter) : counter_(counter) {
|
|
|
|
DCHECK_GE(counter_, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Wait() {
|
|
|
|
DCHECK_NE(counter_, 0);
|
|
|
|
mutex_.Lock();
|
|
|
|
counter_--;
|
|
|
|
if (counter_ == 0) {
|
|
|
|
condition_.NotifyAll();
|
|
|
|
} else {
|
|
|
|
while (counter_ > 0) {
|
|
|
|
condition_.Wait(&mutex_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_.Unlock();
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:36:38 +00:00
|
|
|
void Signal() {
|
|
|
|
mutex_.Lock();
|
|
|
|
counter_--;
|
|
|
|
if (counter_ == 0) {
|
|
|
|
condition_.NotifyAll();
|
|
|
|
}
|
|
|
|
mutex_.Unlock();
|
|
|
|
}
|
|
|
|
|
2017-05-17 07:58:33 +00:00
|
|
|
private:
|
|
|
|
base::Mutex mutex_;
|
|
|
|
base::ConditionVariable condition_;
|
|
|
|
size_t counter_;
|
|
|
|
};
|
|
|
|
|
2018-02-01 08:36:38 +00:00
|
|
|
// A task that only processes a single item. Signals |barrier| when done; if
|
|
|
|
// |wait_when_done|, will blocks until all other tasks have signaled |barrier|.
|
|
|
|
// If |did_process_an_item| is non-null, will set it to true if it does process
|
|
|
|
// an item. Otherwise, it will expect to get an item to process (and will report
|
|
|
|
// a failure if it doesn't).
|
2017-05-16 20:57:05 +00:00
|
|
|
class TaskProcessingOneItem : public ItemParallelJob::Task {
|
|
|
|
public:
|
2018-01-30 16:40:55 +00:00
|
|
|
TaskProcessingOneItem(Isolate* isolate, OneShotBarrier* barrier,
|
2018-02-01 08:36:38 +00:00
|
|
|
bool wait_when_done,
|
2018-01-30 16:40:55 +00:00
|
|
|
bool* did_process_an_item = nullptr)
|
|
|
|
: ItemParallelJob::Task(isolate),
|
|
|
|
barrier_(barrier),
|
2018-02-01 08:36:38 +00:00
|
|
|
wait_when_done_(wait_when_done),
|
2018-01-30 16:40:55 +00:00
|
|
|
did_process_an_item_(did_process_an_item) {}
|
2017-05-16 20:57:05 +00:00
|
|
|
|
|
|
|
void RunInParallel() override {
|
|
|
|
SimpleItem* item = GetItem<SimpleItem>();
|
2018-01-30 16:40:55 +00:00
|
|
|
|
|
|
|
if (did_process_an_item_) {
|
|
|
|
*did_process_an_item_ = item != nullptr;
|
|
|
|
} else {
|
|
|
|
EXPECT_NE(nullptr, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
item->Process();
|
|
|
|
item->MarkFinished();
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:36:38 +00:00
|
|
|
if (wait_when_done_) {
|
|
|
|
barrier_->Wait();
|
|
|
|
} else {
|
|
|
|
barrier_->Signal();
|
|
|
|
}
|
2017-05-16 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-17 07:58:33 +00:00
|
|
|
OneShotBarrier* barrier_;
|
2018-02-01 08:36:38 +00:00
|
|
|
bool wait_when_done_;
|
2018-01-30 16:40:55 +00:00
|
|
|
bool* did_process_an_item_;
|
2017-05-16 20:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TaskForDifferentItems;
|
|
|
|
|
|
|
|
class BaseItem : public ItemParallelJob::Item {
|
|
|
|
public:
|
|
|
|
virtual ~BaseItem() {}
|
|
|
|
virtual void ProcessItem(TaskForDifferentItems* task) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TaskForDifferentItems : public ItemParallelJob::Task {
|
|
|
|
public:
|
|
|
|
explicit TaskForDifferentItems(Isolate* isolate, bool* processed_a,
|
|
|
|
bool* processed_b)
|
|
|
|
: ItemParallelJob::Task(isolate),
|
|
|
|
processed_a_(processed_a),
|
|
|
|
processed_b_(processed_b) {}
|
|
|
|
virtual ~TaskForDifferentItems() {}
|
|
|
|
|
|
|
|
void RunInParallel() override {
|
|
|
|
BaseItem* item = nullptr;
|
|
|
|
while ((item = GetItem<BaseItem>()) != nullptr) {
|
|
|
|
item->ProcessItem(this);
|
|
|
|
item->MarkFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessA() { *processed_a_ = true; }
|
|
|
|
void ProcessB() { *processed_b_ = true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool* processed_a_;
|
|
|
|
bool* processed_b_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ItemA : public BaseItem {
|
|
|
|
public:
|
|
|
|
virtual ~ItemA() {}
|
|
|
|
void ProcessItem(TaskForDifferentItems* task) override { task->ProcessA(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class ItemB : public BaseItem {
|
|
|
|
public:
|
|
|
|
virtual ~ItemB() {}
|
|
|
|
void ProcessItem(TaskForDifferentItems* task) override { task->ProcessB(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-30 16:40:55 +00:00
|
|
|
// ItemParallelJob runs tasks even without work items (as requested tasks may be
|
|
|
|
// responsible for post-processing).
|
|
|
|
TEST_F(ItemParallelJobTest, SimpleTaskWithNoItemsRuns) {
|
2018-01-30 11:23:58 +00:00
|
|
|
bool did_run = false;
|
|
|
|
ItemParallelJob job(i_isolate()->cancelable_task_manager(),
|
|
|
|
parallel_job_semaphore());
|
2018-01-30 16:40:55 +00:00
|
|
|
job.AddTask(new SimpleTask(i_isolate(), &did_run));
|
|
|
|
|
2018-02-06 13:51:16 +00:00
|
|
|
job.Run(i_isolate()->async_counters());
|
2018-01-30 11:23:58 +00:00
|
|
|
EXPECT_TRUE(did_run);
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:40:55 +00:00
|
|
|
TEST_F(ItemParallelJobTest, SimpleTaskWithSimpleItemRuns) {
|
|
|
|
bool did_run = false;
|
|
|
|
ItemParallelJob job(i_isolate()->cancelable_task_manager(),
|
|
|
|
parallel_job_semaphore());
|
|
|
|
job.AddTask(new SimpleTask(i_isolate(), &did_run));
|
|
|
|
|
|
|
|
job.AddItem(new ItemParallelJob::Item);
|
|
|
|
|
2018-02-06 13:51:16 +00:00
|
|
|
job.Run(i_isolate()->async_counters());
|
2018-01-30 16:40:55 +00:00
|
|
|
EXPECT_TRUE(did_run);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ItemParallelJobTest, MoreTasksThanItems) {
|
2018-02-01 08:36:38 +00:00
|
|
|
const int kNumTasks = 128;
|
|
|
|
const int kNumItems = kNumTasks - 4;
|
2018-01-30 16:40:55 +00:00
|
|
|
|
|
|
|
TaskProcessingOneItem* tasks[kNumTasks] = {};
|
|
|
|
bool did_process_an_item[kNumTasks] = {};
|
|
|
|
|
|
|
|
ItemParallelJob job(i_isolate()->cancelable_task_manager(),
|
|
|
|
parallel_job_semaphore());
|
|
|
|
|
|
|
|
// The barrier ensures that all tasks run. But only the first kNumItems tasks
|
|
|
|
// should be assigned an item to execute.
|
|
|
|
OneShotBarrier barrier(kNumTasks);
|
|
|
|
for (int i = 0; i < kNumTasks; i++) {
|
2018-02-01 08:36:38 +00:00
|
|
|
// Block the main thread when done to prevent it from returning control to
|
|
|
|
// the job (which could cancel tasks that have yet to be scheduled).
|
|
|
|
const bool wait_when_done = i == 0;
|
|
|
|
tasks[i] = new TaskProcessingOneItem(i_isolate(), &barrier, wait_when_done,
|
2018-01-30 16:40:55 +00:00
|
|
|
&did_process_an_item[i]);
|
|
|
|
job.AddTask(tasks[i]);
|
2018-01-30 16:31:12 +00:00
|
|
|
}
|
2018-01-30 16:40:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumItems; i++) {
|
|
|
|
job.AddItem(new SimpleItem);
|
|
|
|
}
|
|
|
|
|
2018-02-06 13:51:16 +00:00
|
|
|
job.Run(i_isolate()->async_counters());
|
2018-01-30 16:40:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumTasks; i++) {
|
|
|
|
// Only the first kNumItems tasks should have been assigned a work item.
|
|
|
|
EXPECT_EQ(i < kNumItems, did_process_an_item[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ItemParallelJobTest, SingleThreadProcessing) {
|
|
|
|
const int kItems = 111;
|
|
|
|
bool was_processed[kItems] = {};
|
2017-05-16 20:57:05 +00:00
|
|
|
ItemParallelJob job(i_isolate()->cancelable_task_manager(),
|
|
|
|
parallel_job_semaphore());
|
|
|
|
job.AddTask(new EagerTask(i_isolate()));
|
|
|
|
for (int i = 0; i < kItems; i++) {
|
|
|
|
job.AddItem(new SimpleItem(&was_processed[i]));
|
|
|
|
}
|
2018-02-06 13:51:16 +00:00
|
|
|
job.Run(i_isolate()->async_counters());
|
2017-05-16 20:57:05 +00:00
|
|
|
for (int i = 0; i < kItems; i++) {
|
|
|
|
EXPECT_TRUE(was_processed[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 07:58:33 +00:00
|
|
|
TEST_F(ItemParallelJobTest, DistributeItemsMultipleTasks) {
|
2018-02-01 08:36:38 +00:00
|
|
|
const int kItemsAndTasks = 256;
|
2018-01-30 16:40:55 +00:00
|
|
|
bool was_processed[kItemsAndTasks] = {};
|
2017-05-17 07:58:33 +00:00
|
|
|
OneShotBarrier barrier(kItemsAndTasks);
|
2017-05-16 20:57:05 +00:00
|
|
|
ItemParallelJob job(i_isolate()->cancelable_task_manager(),
|
|
|
|
parallel_job_semaphore());
|
2017-05-17 07:58:33 +00:00
|
|
|
for (int i = 0; i < kItemsAndTasks; i++) {
|
2017-05-16 20:57:05 +00:00
|
|
|
job.AddItem(new SimpleItem(&was_processed[i]));
|
2018-02-01 08:36:38 +00:00
|
|
|
|
|
|
|
// Block the main thread when done to prevent it from returning control to
|
|
|
|
// the job (which could cancel tasks that have yet to be scheduled).
|
|
|
|
const bool wait_when_done = i == 0;
|
|
|
|
job.AddTask(
|
|
|
|
new TaskProcessingOneItem(i_isolate(), &barrier, wait_when_done));
|
2017-05-16 20:57:05 +00:00
|
|
|
}
|
2018-02-06 13:51:16 +00:00
|
|
|
job.Run(i_isolate()->async_counters());
|
2017-05-17 07:58:33 +00:00
|
|
|
for (int i = 0; i < kItemsAndTasks; i++) {
|
2017-05-16 20:57:05 +00:00
|
|
|
EXPECT_TRUE(was_processed[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ItemParallelJobTest, DifferentItems) {
|
|
|
|
bool item_a = false;
|
|
|
|
bool item_b = false;
|
|
|
|
ItemParallelJob job(i_isolate()->cancelable_task_manager(),
|
|
|
|
parallel_job_semaphore());
|
|
|
|
job.AddItem(new ItemA());
|
|
|
|
job.AddItem(new ItemB());
|
|
|
|
job.AddTask(new TaskForDifferentItems(i_isolate(), &item_a, &item_b));
|
2018-02-06 13:51:16 +00:00
|
|
|
job.Run(i_isolate()->async_counters());
|
2017-05-16 20:57:05 +00:00
|
|
|
EXPECT_TRUE(item_a);
|
|
|
|
EXPECT_TRUE(item_b);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|