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 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 } | 576 } |
577 | 577 |
578 namespace { | 578 namespace { |
579 | 579 |
580 class TaskSchedulerWorkerPoolHistogramTest | 580 class TaskSchedulerWorkerPoolHistogramTest |
581 : public TaskSchedulerWorkerPoolImplTest { | 581 : public TaskSchedulerWorkerPoolImplTest { |
582 public: | 582 public: |
583 TaskSchedulerWorkerPoolHistogramTest() = default; | 583 TaskSchedulerWorkerPoolHistogramTest() = default; |
584 | 584 |
585 protected: | 585 protected: |
| 586 // Override SetUp() to allow every test case to initialize a worker pool with |
| 587 // its own arguments. |
586 void SetUp() override {} | 588 void SetUp() override {} |
587 | 589 |
588 void TearDown() override { | |
589 // |worker_pool_| initialization is done in test body. | |
590 if (worker_pool_) | |
591 worker_pool_->JoinForTesting(); | |
592 } | |
593 | |
594 private: | 590 private: |
595 std::unique_ptr<StatisticsRecorder> statistics_recorder_ = | 591 std::unique_ptr<StatisticsRecorder> statistics_recorder_ = |
596 StatisticsRecorder::CreateTemporaryForTesting(); | 592 StatisticsRecorder::CreateTemporaryForTesting(); |
597 | 593 |
598 private: | |
599 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolHistogramTest); | 594 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolHistogramTest); |
600 }; | 595 }; |
601 | 596 |
602 } // namespace | 597 } // namespace |
603 | 598 |
604 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) { | 599 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) { |
605 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 600 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
606 WaitableEvent::InitialState::NOT_SIGNALED); | 601 WaitableEvent::InitialState::NOT_SIGNALED); |
607 InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); | 602 InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); |
608 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( | 603 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 // beginning of the test. | 697 // beginning of the test. |
703 EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool), | 698 EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool), |
704 histogram->SnapshotSamples()->GetCount(1)); | 699 histogram->SnapshotSamples()->GetCount(1)); |
705 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 700 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); |
706 | 701 |
707 tasks_can_exit_event.Signal(); | 702 tasks_can_exit_event.Signal(); |
708 worker_pool_->WaitForAllWorkersIdleForTesting(); | 703 worker_pool_->WaitForAllWorkersIdleForTesting(); |
709 worker_pool_->DisallowWorkerDetachmentForTesting(); | 704 worker_pool_->DisallowWorkerDetachmentForTesting(); |
710 } | 705 } |
711 | 706 |
| 707 namespace { |
| 708 |
| 709 void CaptureThreadId(PlatformThreadId* thread_id) { |
| 710 ASSERT_TRUE(thread_id); |
| 711 *thread_id = PlatformThread::CurrentId(); |
| 712 } |
| 713 |
| 714 void VerifyThreadIdIsNot(PlatformThreadId thread_id) { |
| 715 EXPECT_NE(thread_id, PlatformThread::CurrentId()); |
| 716 } |
| 717 |
| 718 } // namespace |
| 719 |
712 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBeforeDetach) { | 720 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBeforeDetach) { |
713 InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool); | 721 InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool); |
| 722 |
| 723 // This test assumes that the TaskRunners aren't assigned to the same worker. |
714 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( | 724 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( |
715 TaskTraits(), ExecutionMode::SINGLE_THREADED); | 725 TaskTraits(), ExecutionMode::SINGLE_THREADED); |
716 auto other_task_runner = worker_pool_->CreateTaskRunnerWithTraits( | 726 auto other_task_runner = worker_pool_->CreateTaskRunnerWithTraits( |
717 TaskTraits(), ExecutionMode::SINGLE_THREADED); | 727 TaskTraits(), ExecutionMode::SINGLE_THREADED); |
718 | 728 |
719 // Post 3 tasks and wait until they run. | 729 // Post 3 tasks and wait until they run. |
720 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 730 PlatformThreadId thread_id; |
| 731 task_runner->PostTask(FROM_HERE, |
| 732 Bind(&CaptureThreadId, Unretained(&thread_id))); |
721 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 733 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); |
722 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 734 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); |
723 worker_pool_->WaitForAllWorkersIdleForTesting(); | 735 worker_pool_->WaitForAllWorkersIdleForTesting(); |
724 | 736 |
725 // To allow the SchedulerWorker associated with |task_runner| to detach: | 737 // To allow the SchedulerWorker associated with |task_runner| to detach: |
726 // - Make sure it isn't on top of the idle stack by waking up another | 738 // - Make sure it isn't on top of the idle stack by waking up another |
727 // SchedulerWorker. | 739 // SchedulerWorker and waiting until it goes back to sleep. |
728 // - Release |task_runner|. | 740 // - Release |task_runner|. |
729 other_task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 741 other_task_runner->PostTask(FROM_HERE, Bind(&VerifyThreadIdIsNot, thread_id)); |
| 742 worker_pool_->WaitForAllWorkersIdleForTesting(); |
730 task_runner = nullptr; | 743 task_runner = nullptr; |
731 | 744 |
732 // Allow the SchedulerWorker to detach. | 745 // Allow the SchedulerWorker that was associated with |task_runner| to detach. |
733 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 746 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); |
734 | |
735 // Join the SchedulerWorkerPool. This forces SchedulerWorkers that detached | |
736 // during the test to record to the histogram. | |
737 worker_pool_->WaitForAllWorkersIdleForTesting(); | |
738 worker_pool_->DisallowWorkerDetachmentForTesting(); | 747 worker_pool_->DisallowWorkerDetachmentForTesting(); |
739 worker_pool_->JoinForTesting(); | |
740 const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); | |
741 other_task_runner = nullptr; | |
742 worker_pool_.reset(); | |
743 | 748 |
744 // Verify that counts were recorded to the histogram as expected. | 749 // Verify that counts were recorded to the histogram as expected. |
| 750 const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); |
745 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 751 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); |
746 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 752 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); |
747 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 753 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); |
748 } | 754 } |
749 | 755 |
750 } // namespace internal | 756 } // namespace internal |
751 } // namespace base | 757 } // namespace base |
OLD | NEW |