Commit e337fc68 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[platform] Remove DefaultPlatform::SetThreadPoolSize

This function is always called right after creating the DefaultPlatform,
hence merge it into the constructor.

R=mlippautz@chromium.org

Change-Id: I4afb14c83740224056157665db6b854c659da0c1
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2182635Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67573}
parent 5f7bc600
...@@ -39,11 +39,10 @@ std::unique_ptr<v8::Platform> NewDefaultPlatform( ...@@ -39,11 +39,10 @@ std::unique_ptr<v8::Platform> NewDefaultPlatform(
if (in_process_stack_dumping == InProcessStackDumping::kEnabled) { if (in_process_stack_dumping == InProcessStackDumping::kEnabled) {
v8::base::debug::EnableInProcessStackDumping(); v8::base::debug::EnableInProcessStackDumping();
} }
std::unique_ptr<DefaultPlatform> platform( auto platform = std::make_unique<DefaultPlatform>(
new DefaultPlatform(idle_task_support, std::move(tracing_controller))); thread_pool_size, idle_task_support, std::move(tracing_controller));
platform->SetThreadPoolSize(thread_pool_size);
platform->EnsureBackgroundTaskRunnerInitialized(); platform->EnsureBackgroundTaskRunnerInitialized();
return std::move(platform); return platform;
} }
bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate, bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate,
...@@ -65,16 +64,25 @@ void SetTracingController( ...@@ -65,16 +64,25 @@ void SetTracingController(
std::unique_ptr<v8::TracingController>(tracing_controller)); std::unique_ptr<v8::TracingController>(tracing_controller));
} }
const int DefaultPlatform::kMaxThreadPoolSize = 8; namespace {
constexpr int kMaxThreadPoolSize = 8;
int GetActualThreadPoolSize(int thread_pool_size) {
DCHECK_GE(thread_pool_size, 0);
if (thread_pool_size < 1) {
thread_pool_size = base::SysInfo::NumberOfProcessors() - 1;
}
return std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
}
} // namespace
DefaultPlatform::DefaultPlatform( DefaultPlatform::DefaultPlatform(
IdleTaskSupport idle_task_support, int thread_pool_size, IdleTaskSupport idle_task_support,
std::unique_ptr<v8::TracingController> tracing_controller) std::unique_ptr<v8::TracingController> tracing_controller)
: thread_pool_size_(0), : thread_pool_size_(GetActualThreadPoolSize(thread_pool_size)),
idle_task_support_(idle_task_support), idle_task_support_(idle_task_support),
tracing_controller_(std::move(tracing_controller)), tracing_controller_(std::move(tracing_controller)),
page_allocator_(new v8::base::PageAllocator()), page_allocator_(std::make_unique<v8::base::PageAllocator>()) {
time_function_for_testing_(nullptr) {
if (!tracing_controller_) { if (!tracing_controller_) {
tracing::TracingController* controller = new tracing::TracingController(); tracing::TracingController* controller = new tracing::TracingController();
#if !defined(V8_USE_PERFETTO) #if !defined(V8_USE_PERFETTO)
...@@ -92,16 +100,6 @@ DefaultPlatform::~DefaultPlatform() { ...@@ -92,16 +100,6 @@ DefaultPlatform::~DefaultPlatform() {
} }
} }
void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
base::MutexGuard guard(&lock_);
DCHECK_GE(thread_pool_size, 0);
if (thread_pool_size < 1) {
thread_pool_size = base::SysInfo::NumberOfProcessors() - 1;
}
thread_pool_size_ =
std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
}
namespace { namespace {
double DefaultTimeFunction() { double DefaultTimeFunction() {
......
...@@ -32,13 +32,12 @@ class DefaultPageAllocator; ...@@ -32,13 +32,12 @@ class DefaultPageAllocator;
class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) { class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
public: public:
explicit DefaultPlatform( explicit DefaultPlatform(
int thread_pool_size = 0,
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled, IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
std::unique_ptr<v8::TracingController> tracing_controller = {}); std::unique_ptr<v8::TracingController> tracing_controller = {});
~DefaultPlatform() override; ~DefaultPlatform() override;
void SetThreadPoolSize(int thread_pool_size);
void EnsureBackgroundTaskRunnerInitialized(); void EnsureBackgroundTaskRunnerInitialized();
bool PumpMessageLoop( bool PumpMessageLoop(
...@@ -71,10 +70,8 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) { ...@@ -71,10 +70,8 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
v8::PageAllocator* GetPageAllocator() override; v8::PageAllocator* GetPageAllocator() override;
private: private:
static const int kMaxThreadPoolSize;
base::Mutex lock_; base::Mutex lock_;
int thread_pool_size_; const int thread_pool_size_;
IdleTaskSupport idle_task_support_; IdleTaskSupport idle_task_support_;
std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runner_; std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runner_;
std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>> std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>>
...@@ -83,7 +80,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) { ...@@ -83,7 +80,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
std::unique_ptr<TracingController> tracing_controller_; std::unique_ptr<TracingController> tracing_controller_;
std::unique_ptr<PageAllocator> page_allocator_; std::unique_ptr<PageAllocator> page_allocator_;
TimeFunction time_function_for_testing_; TimeFunction time_function_for_testing_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(DefaultPlatform); DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
}; };
......
...@@ -13,20 +13,12 @@ namespace v8 { ...@@ -13,20 +13,12 @@ namespace v8 {
namespace platform { namespace platform {
namespace default_job_unittest { namespace default_job_unittest {
class DefaultJobTest : public ::testing::Test {
public:
DefaultPlatform* platform() { return &platform_; }
private:
DefaultPlatform platform_;
};
// Verify that Cancel() on a job stops running the worker task and causes // Verify that Cancel() on a job stops running the worker task and causes
// current workers to yield. // current workers to yield.
TEST_F(DefaultJobTest, CancelJob) { TEST(DefaultJobTest, CancelJob) {
static constexpr size_t kTooManyTasks = 1000; static constexpr size_t kTooManyTasks = 1000;
static constexpr size_t kMaxTask = 4; static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask); DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and loops until // This Job notifies |threads_running| once started and loops until
// ShouldYield() returns true, and then returns. // ShouldYield() returns true, and then returns.
...@@ -57,7 +49,7 @@ TEST_F(DefaultJobTest, CancelJob) { ...@@ -57,7 +49,7 @@ TEST_F(DefaultJobTest, CancelJob) {
auto job = std::make_unique<JobTest>(); auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get(); JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>( auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask); &platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease(); state->NotifyConcurrencyIncrease();
{ {
...@@ -73,9 +65,9 @@ TEST_F(DefaultJobTest, CancelJob) { ...@@ -73,9 +65,9 @@ TEST_F(DefaultJobTest, CancelJob) {
// Verify that Join() on a job contributes to max concurrency and waits for all // Verify that Join() on a job contributes to max concurrency and waits for all
// workers to return. // workers to return.
TEST_F(DefaultJobTest, JoinJobContributes) { TEST(DefaultJobTest, JoinJobContributes) {
static constexpr size_t kMaxTask = 4; static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask); DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and blocks on a barrier // This Job notifies |threads_running| once started and blocks on a barrier
// until kMaxTask + 1 threads reach that point, and then returns. // until kMaxTask + 1 threads reach that point, and then returns.
...@@ -104,7 +96,7 @@ TEST_F(DefaultJobTest, JoinJobContributes) { ...@@ -104,7 +96,7 @@ TEST_F(DefaultJobTest, JoinJobContributes) {
auto job = std::make_unique<JobTest>(); auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get(); JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>( auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask); &platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease(); state->NotifyConcurrencyIncrease();
// The main thread contributing is necessary for |worker_count| to reach // The main thread contributing is necessary for |worker_count| to reach
...@@ -115,9 +107,9 @@ TEST_F(DefaultJobTest, JoinJobContributes) { ...@@ -115,9 +107,9 @@ TEST_F(DefaultJobTest, JoinJobContributes) {
// Verify that calling NotifyConcurrencyIncrease() (re-)schedules tasks with the // Verify that calling NotifyConcurrencyIncrease() (re-)schedules tasks with the
// intended concurrency. // intended concurrency.
TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) { TEST(DefaultJobTest, JobNotifyConcurrencyIncrease) {
static constexpr size_t kMaxTask = 4; static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask); DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and blocks on a barrier // This Job notifies |threads_running| once started and blocks on a barrier
// until kMaxTask threads reach that point, and then returns. // until kMaxTask threads reach that point, and then returns.
...@@ -148,7 +140,7 @@ TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) { ...@@ -148,7 +140,7 @@ TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) {
auto job = std::make_unique<JobTest>(); auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get(); JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>( auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask); &platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease(); state->NotifyConcurrencyIncrease();
{ {
...@@ -167,9 +159,9 @@ TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) { ...@@ -167,9 +159,9 @@ TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) {
} }
// Verify that Join() doesn't contribute if the Job is already finished. // Verify that Join() doesn't contribute if the Job is already finished.
TEST_F(DefaultJobTest, FinishBeforeJoin) { TEST(DefaultJobTest, FinishBeforeJoin) {
static constexpr size_t kMaxTask = 4; static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask); DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and returns. // This Job notifies |threads_running| once started and returns.
class JobTest : public JobTask { class JobTest : public JobTask {
...@@ -199,7 +191,7 @@ TEST_F(DefaultJobTest, FinishBeforeJoin) { ...@@ -199,7 +191,7 @@ TEST_F(DefaultJobTest, FinishBeforeJoin) {
auto job = std::make_unique<JobTest>(); auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get(); JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>( auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask); &platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease(); state->NotifyConcurrencyIncrease();
{ {
...@@ -215,7 +207,7 @@ TEST_F(DefaultJobTest, FinishBeforeJoin) { ...@@ -215,7 +207,7 @@ TEST_F(DefaultJobTest, FinishBeforeJoin) {
// Verify that destroying a DefaultJobHandle triggers a DCHECK if neither Join() // Verify that destroying a DefaultJobHandle triggers a DCHECK if neither Join()
// or Cancel() was called. // or Cancel() was called.
TEST_F(DefaultJobTest, LeakHandle) { TEST(DefaultJobTest, LeakHandle) {
class JobTest : public JobTask { class JobTest : public JobTask {
public: public:
~JobTest() override = default; ~JobTest() override = default;
...@@ -225,8 +217,9 @@ TEST_F(DefaultJobTest, LeakHandle) { ...@@ -225,8 +217,9 @@ TEST_F(DefaultJobTest, LeakHandle) {
size_t GetMaxConcurrency() const override { return 0; } size_t GetMaxConcurrency() const override { return 0; }
}; };
DefaultPlatform platform(0);
auto job = std::make_unique<JobTest>(); auto job = std::make_unique<JobTest>();
auto state = std::make_shared<DefaultJobState>(platform(), std::move(job), auto state = std::make_shared<DefaultJobState>(&platform, std::move(job),
TaskPriority::kUserVisible, 1); TaskPriority::kUserVisible, 1);
auto handle = std::make_unique<DefaultJobHandle>(std::move(state)); auto handle = std::make_unique<DefaultJobHandle>(std::move(state));
#ifdef DEBUG #ifdef DEBUG
......
...@@ -32,8 +32,8 @@ struct MockIdleTask : public IdleTask { ...@@ -32,8 +32,8 @@ struct MockIdleTask : public IdleTask {
class DefaultPlatformWithMockTime : public DefaultPlatform { class DefaultPlatformWithMockTime : public DefaultPlatform {
public: public:
DefaultPlatformWithMockTime() explicit DefaultPlatformWithMockTime(int thread_pool_size = 0)
: DefaultPlatform(IdleTaskSupport::kEnabled, nullptr) { : DefaultPlatform(thread_pool_size, IdleTaskSupport::kEnabled, nullptr) {
mock_time_ = 0.0; mock_time_ = 0.0;
SetTimeFunctionForTesting([]() { return mock_time_; }); SetTimeFunctionForTesting([]() { return mock_time_; });
} }
...@@ -240,8 +240,7 @@ class TestBackgroundTask : public Task { ...@@ -240,8 +240,7 @@ class TestBackgroundTask : public Task {
} // namespace } // namespace
TEST(CustomDefaultPlatformTest, RunBackgroundTask) { TEST(CustomDefaultPlatformTest, RunBackgroundTask) {
DefaultPlatform platform; DefaultPlatform platform(1);
platform.SetThreadPoolSize(1);
base::Semaphore sem(0); base::Semaphore sem(0);
bool task_executed = false; bool task_executed = false;
...@@ -256,12 +255,11 @@ TEST(CustomDefaultPlatformTest, RunBackgroundTask) { ...@@ -256,12 +255,11 @@ TEST(CustomDefaultPlatformTest, RunBackgroundTask) {
TEST(CustomDefaultPlatformTest, PostForegroundTaskAfterPlatformTermination) { TEST(CustomDefaultPlatformTest, PostForegroundTaskAfterPlatformTermination) {
std::shared_ptr<TaskRunner> foreground_taskrunner; std::shared_ptr<TaskRunner> foreground_taskrunner;
{ {
DefaultPlatformWithMockTime platform; DefaultPlatformWithMockTime platform(1);
int dummy; int dummy;
Isolate* isolate = reinterpret_cast<Isolate*>(&dummy); Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
platform.SetThreadPoolSize(1);
foreground_taskrunner = platform.GetForegroundTaskRunner(isolate); foreground_taskrunner = platform.GetForegroundTaskRunner(isolate);
} }
// It should still be possible to post foreground tasks, even when the // It should still be possible to post foreground tasks, even when the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment