| OLD | NEW | 
|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium 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 #include "base/task_scheduler/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" | 
| 6 | 6 | 
| 7 #include <stddef.h> | 7 #include <stddef.h> | 
| 8 | 8 | 
| 9 #include <memory> | 9 #include <memory> | 
| 10 #include <unordered_set> | 10 #include <unordered_set> | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 52 constexpr size_t kNumTasksPostedPerThread = 150; | 52 constexpr size_t kNumTasksPostedPerThread = 150; | 
| 53 // This can't be lower because Windows' WaitableEvent wakes up too early when a | 53 // This can't be lower because Windows' WaitableEvent wakes up too early when a | 
| 54 // small timeout is used. This results in many spurious wake ups before a worker | 54 // small timeout is used. This results in many spurious wake ups before a worker | 
| 55 // is allowed to detach. | 55 // is allowed to detach. | 
| 56 constexpr TimeDelta kReclaimTimeForDetachTests = | 56 constexpr TimeDelta kReclaimTimeForDetachTests = | 
| 57     TimeDelta::FromMilliseconds(500); | 57     TimeDelta::FromMilliseconds(500); | 
| 58 constexpr TimeDelta kExtraTimeToWaitForDetach = | 58 constexpr TimeDelta kExtraTimeToWaitForDetach = | 
| 59     TimeDelta::FromSeconds(1); | 59     TimeDelta::FromSeconds(1); | 
| 60 | 60 | 
| 61 using IORestriction = SchedulerWorkerPoolParams::IORestriction; | 61 using IORestriction = SchedulerWorkerPoolParams::IORestriction; | 
|  | 62 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; | 
| 62 | 63 | 
| 63 class TaskSchedulerWorkerPoolImplTest | 64 class TaskSchedulerWorkerPoolImplTest | 
| 64     : public testing::TestWithParam<test::ExecutionMode> { | 65     : public testing::TestWithParam<test::ExecutionMode> { | 
| 65  protected: | 66  protected: | 
| 66   TaskSchedulerWorkerPoolImplTest() | 67   TaskSchedulerWorkerPoolImplTest() | 
| 67       : service_thread_("TaskSchedulerServiceThread") {} | 68       : service_thread_("TaskSchedulerServiceThread") {} | 
| 68 | 69 | 
| 69   void SetUp() override { | 70   void SetUp() override { | 
| 70     InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); | 71     InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); | 
| 71   } | 72   } | 
| 72 | 73 | 
| 73   void TearDown() override { | 74   void TearDown() override { | 
| 74     service_thread_.Stop(); | 75     service_thread_.Stop(); | 
| 75     worker_pool_->WaitForAllWorkersIdleForTesting(); | 76     worker_pool_->WaitForAllWorkersIdleForTesting(); | 
| 76     worker_pool_->JoinForTesting(); | 77     worker_pool_->JoinForTesting(); | 
| 77   } | 78   } | 
| 78 | 79 | 
| 79   void InitializeWorkerPool(const TimeDelta& suggested_reclaim_time, | 80   void InitializeWorkerPool(const TimeDelta& suggested_reclaim_time, | 
| 80                             size_t num_workers) { | 81                             size_t num_workers) { | 
| 81     ASSERT_FALSE(worker_pool_); | 82     ASSERT_FALSE(worker_pool_); | 
| 82     ASSERT_FALSE(delayed_task_manager_); | 83     ASSERT_FALSE(delayed_task_manager_); | 
| 83     service_thread_.Start(); | 84     service_thread_.Start(); | 
| 84     delayed_task_manager_ = | 85     delayed_task_manager_ = | 
| 85         base::MakeUnique<DelayedTaskManager>(service_thread_.task_runner()); | 86         base::MakeUnique<DelayedTaskManager>(service_thread_.task_runner()); | 
| 86     worker_pool_ = SchedulerWorkerPoolImpl::Create( | 87     worker_pool_ = SchedulerWorkerPoolImpl::Create( | 
| 87         SchedulerWorkerPoolParams("TestWorkerPool", ThreadPriority::NORMAL, | 88         SchedulerWorkerPoolParams( | 
| 88                                   IORestriction::ALLOWED, num_workers, | 89             "TestWorkerPool", ThreadPriority::NORMAL, IORestriction::ALLOWED, | 
| 89                                   suggested_reclaim_time), | 90             StandbyThreadPolicy::LAZY, num_workers, suggested_reclaim_time), | 
| 90         Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback, | 91         Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback, | 
| 91              Unretained(this)), | 92              Unretained(this)), | 
| 92         &task_tracker_, delayed_task_manager_.get()); | 93         &task_tracker_, delayed_task_manager_.get()); | 
| 93     ASSERT_TRUE(worker_pool_); | 94     ASSERT_TRUE(worker_pool_); | 
| 94   } | 95   } | 
| 95 | 96 | 
| 96   std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; | 97   std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; | 
| 97 | 98 | 
| 98   TaskTracker task_tracker_; | 99   TaskTracker task_tracker_; | 
| 99   Thread service_thread_; | 100   Thread service_thread_; | 
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 466 }; | 467 }; | 
| 467 | 468 | 
| 468 }  // namespace | 469 }  // namespace | 
| 469 | 470 | 
| 470 TEST_P(TaskSchedulerWorkerPoolImplIORestrictionTest, IORestriction) { | 471 TEST_P(TaskSchedulerWorkerPoolImplIORestrictionTest, IORestriction) { | 
| 471   TaskTracker task_tracker; | 472   TaskTracker task_tracker; | 
| 472   DelayedTaskManager delayed_task_manager( | 473   DelayedTaskManager delayed_task_manager( | 
| 473       make_scoped_refptr(new TestSimpleTaskRunner)); | 474       make_scoped_refptr(new TestSimpleTaskRunner)); | 
| 474 | 475 | 
| 475   auto worker_pool = SchedulerWorkerPoolImpl::Create( | 476   auto worker_pool = SchedulerWorkerPoolImpl::Create( | 
| 476       SchedulerWorkerPoolParams("TestWorkerPoolWithParam", | 477       SchedulerWorkerPoolParams( | 
| 477                                 ThreadPriority::NORMAL, GetParam(), 1U, | 478           "TestWorkerPoolWithParam", ThreadPriority::NORMAL, GetParam(), | 
| 478                                 TimeDelta::Max()), | 479           StandbyThreadPolicy::LAZY, 1U, TimeDelta::Max()), | 
| 479       Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 480       Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 
| 480       &delayed_task_manager); | 481       &delayed_task_manager); | 
| 481   ASSERT_TRUE(worker_pool); | 482   ASSERT_TRUE(worker_pool); | 
| 482 | 483 | 
| 483   WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 484   WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 
| 484                          WaitableEvent::InitialState::NOT_SIGNALED); | 485                          WaitableEvent::InitialState::NOT_SIGNALED); | 
| 485   worker_pool->CreateTaskRunnerWithTraits(TaskTraits()) | 486   worker_pool->CreateTaskRunnerWithTraits(TaskTraits()) | 
| 486       ->PostTask(FROM_HERE, Bind(&ExpectIORestriction, GetParam(), &task_ran)); | 487       ->PostTask(FROM_HERE, Bind(&ExpectIORestriction, GetParam(), &task_ran)); | 
| 487   task_ran.Wait(); | 488   task_ran.Wait(); | 
| 488 | 489 | 
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 818   PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 819   PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 
| 819   worker_pool_->DisallowWorkerDetachmentForTesting(); | 820   worker_pool_->DisallowWorkerDetachmentForTesting(); | 
| 820 | 821 | 
| 821   // Verify that counts were recorded to the histogram as expected. | 822   // Verify that counts were recorded to the histogram as expected. | 
| 822   const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); | 823   const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); | 
| 823   EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 824   EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 
| 824   EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 825   EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 
| 825   EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 826   EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 
| 826 } | 827 } | 
| 827 | 828 | 
|  | 829 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitLazy) { | 
|  | 830   TaskTracker task_tracker; | 
|  | 831   DelayedTaskManager delayed_task_manager( | 
|  | 832       make_scoped_refptr(new TestSimpleTaskRunner)); | 
|  | 833   auto worker_pool = SchedulerWorkerPoolImpl::Create( | 
|  | 834       SchedulerWorkerPoolParams("LazyPolicyWorkerPool", ThreadPriority::NORMAL, | 
|  | 835                                 IORestriction::DISALLOWED, | 
|  | 836                                 StandbyThreadPolicy::LAZY, 8U, | 
|  | 837                                 TimeDelta::Max()), | 
|  | 838       Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 
|  | 839       &delayed_task_manager); | 
|  | 840   ASSERT_TRUE(worker_pool); | 
|  | 841   EXPECT_EQ(0U, worker_pool->NumberOfAliveWorkersForTesting()); | 
|  | 842   worker_pool->JoinForTesting(); | 
|  | 843 } | 
|  | 844 | 
|  | 845 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitOne) { | 
|  | 846   TaskTracker task_tracker; | 
|  | 847   DelayedTaskManager delayed_task_manager( | 
|  | 848       make_scoped_refptr(new TestSimpleTaskRunner)); | 
|  | 849   auto worker_pool = SchedulerWorkerPoolImpl::Create( | 
|  | 850       SchedulerWorkerPoolParams("LazyPolicyWorkerPool", ThreadPriority::NORMAL, | 
|  | 851                                 IORestriction::DISALLOWED, | 
|  | 852                                 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), | 
|  | 853       Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 
|  | 854       &delayed_task_manager); | 
|  | 855   ASSERT_TRUE(worker_pool); | 
|  | 856   EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); | 
|  | 857   worker_pool->JoinForTesting(); | 
|  | 858 } | 
|  | 859 | 
| 828 }  // namespace internal | 860 }  // namespace internal | 
| 829 }  // namespace base | 861 }  // namespace base | 
| OLD | NEW | 
|---|