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(
if (in_process_stack_dumping == InProcessStackDumping::kEnabled) {
v8::base::debug::EnableInProcessStackDumping();
}
std::unique_ptr<DefaultPlatform> platform(
new DefaultPlatform(idle_task_support, std::move(tracing_controller)));
platform->SetThreadPoolSize(thread_pool_size);
auto platform = std::make_unique<DefaultPlatform>(
thread_pool_size, idle_task_support, std::move(tracing_controller));
platform->EnsureBackgroundTaskRunnerInitialized();
return std::move(platform);
return platform;
}
bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate,
......@@ -65,16 +64,25 @@ void SetTracingController(
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(
IdleTaskSupport idle_task_support,
int thread_pool_size, IdleTaskSupport idle_task_support,
std::unique_ptr<v8::TracingController> tracing_controller)
: thread_pool_size_(0),
: thread_pool_size_(GetActualThreadPoolSize(thread_pool_size)),
idle_task_support_(idle_task_support),
tracing_controller_(std::move(tracing_controller)),
page_allocator_(new v8::base::PageAllocator()),
time_function_for_testing_(nullptr) {
page_allocator_(std::make_unique<v8::base::PageAllocator>()) {
if (!tracing_controller_) {
tracing::TracingController* controller = new tracing::TracingController();
#if !defined(V8_USE_PERFETTO)
......@@ -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 {
double DefaultTimeFunction() {
......
......@@ -32,13 +32,12 @@ class DefaultPageAllocator;
class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
public:
explicit DefaultPlatform(
int thread_pool_size = 0,
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
std::unique_ptr<v8::TracingController> tracing_controller = {});
~DefaultPlatform() override;
void SetThreadPoolSize(int thread_pool_size);
void EnsureBackgroundTaskRunnerInitialized();
bool PumpMessageLoop(
......@@ -71,10 +70,8 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
v8::PageAllocator* GetPageAllocator() override;
private:
static const int kMaxThreadPoolSize;
base::Mutex lock_;
int thread_pool_size_;
const int thread_pool_size_;
IdleTaskSupport idle_task_support_;
std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runner_;
std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>>
......@@ -83,7 +80,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
std::unique_ptr<TracingController> tracing_controller_;
std::unique_ptr<PageAllocator> page_allocator_;
TimeFunction time_function_for_testing_;
TimeFunction time_function_for_testing_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
};
......
......@@ -13,20 +13,12 @@ namespace v8 {
namespace platform {
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
// current workers to yield.
TEST_F(DefaultJobTest, CancelJob) {
TEST(DefaultJobTest, CancelJob) {
static constexpr size_t kTooManyTasks = 1000;
static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask);
DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and loops until
// ShouldYield() returns true, and then returns.
......@@ -57,7 +49,7 @@ TEST_F(DefaultJobTest, CancelJob) {
auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask);
&platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease();
{
......@@ -73,9 +65,9 @@ TEST_F(DefaultJobTest, CancelJob) {
// Verify that Join() on a job contributes to max concurrency and waits for all
// workers to return.
TEST_F(DefaultJobTest, JoinJobContributes) {
TEST(DefaultJobTest, JoinJobContributes) {
static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask);
DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and blocks on a barrier
// until kMaxTask + 1 threads reach that point, and then returns.
......@@ -104,7 +96,7 @@ TEST_F(DefaultJobTest, JoinJobContributes) {
auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask);
&platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease();
// The main thread contributing is necessary for |worker_count| to reach
......@@ -115,9 +107,9 @@ TEST_F(DefaultJobTest, JoinJobContributes) {
// Verify that calling NotifyConcurrencyIncrease() (re-)schedules tasks with the
// intended concurrency.
TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) {
TEST(DefaultJobTest, JobNotifyConcurrencyIncrease) {
static constexpr size_t kMaxTask = 4;
platform()->SetThreadPoolSize(kMaxTask);
DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and blocks on a barrier
// until kMaxTask threads reach that point, and then returns.
......@@ -148,7 +140,7 @@ TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) {
auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask);
&platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease();
{
......@@ -167,9 +159,9 @@ TEST_F(DefaultJobTest, JobNotifyConcurrencyIncrease) {
}
// 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;
platform()->SetThreadPoolSize(kMaxTask);
DefaultPlatform platform(kMaxTask);
// This Job notifies |threads_running| once started and returns.
class JobTest : public JobTask {
......@@ -199,7 +191,7 @@ TEST_F(DefaultJobTest, FinishBeforeJoin) {
auto job = std::make_unique<JobTest>();
JobTest* job_raw = job.get();
auto state = std::make_shared<DefaultJobState>(
platform(), std::move(job), TaskPriority::kUserVisible, kMaxTask);
&platform, std::move(job), TaskPriority::kUserVisible, kMaxTask);
state->NotifyConcurrencyIncrease();
{
......@@ -215,7 +207,7 @@ TEST_F(DefaultJobTest, FinishBeforeJoin) {
// Verify that destroying a DefaultJobHandle triggers a DCHECK if neither Join()
// or Cancel() was called.
TEST_F(DefaultJobTest, LeakHandle) {
TEST(DefaultJobTest, LeakHandle) {
class JobTest : public JobTask {
public:
~JobTest() override = default;
......@@ -225,8 +217,9 @@ TEST_F(DefaultJobTest, LeakHandle) {
size_t GetMaxConcurrency() const override { return 0; }
};
DefaultPlatform platform(0);
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);
auto handle = std::make_unique<DefaultJobHandle>(std::move(state));
#ifdef DEBUG
......
......@@ -32,8 +32,8 @@ struct MockIdleTask : public IdleTask {
class DefaultPlatformWithMockTime : public DefaultPlatform {
public:
DefaultPlatformWithMockTime()
: DefaultPlatform(IdleTaskSupport::kEnabled, nullptr) {
explicit DefaultPlatformWithMockTime(int thread_pool_size = 0)
: DefaultPlatform(thread_pool_size, IdleTaskSupport::kEnabled, nullptr) {
mock_time_ = 0.0;
SetTimeFunctionForTesting([]() { return mock_time_; });
}
......@@ -240,8 +240,7 @@ class TestBackgroundTask : public Task {
} // namespace
TEST(CustomDefaultPlatformTest, RunBackgroundTask) {
DefaultPlatform platform;
platform.SetThreadPoolSize(1);
DefaultPlatform platform(1);
base::Semaphore sem(0);
bool task_executed = false;
......@@ -256,12 +255,11 @@ TEST(CustomDefaultPlatformTest, RunBackgroundTask) {
TEST(CustomDefaultPlatformTest, PostForegroundTaskAfterPlatformTermination) {
std::shared_ptr<TaskRunner> foreground_taskrunner;
{
DefaultPlatformWithMockTime platform;
DefaultPlatformWithMockTime platform(1);
int dummy;
Isolate* isolate = reinterpret_cast<Isolate*>(&dummy);
platform.SetThreadPoolSize(1);
foreground_taskrunner = platform.GetForegroundTaskRunner(isolate);
}
// 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