Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/task_manager/sampling/task_group.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/strings/string16.h" | |
| 17 #include "chrome/browser/task_manager/sampling/shared_sampler.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | |
| 20 #include "gpu/ipc/common/memory_stats.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace task_manager { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class FakeTask : public Task { | |
| 28 public: | |
| 29 FakeTask(base::ProcessId process_id, Type type) | |
| 30 : Task(base::string16(), | |
| 31 "FakeTask", | |
| 32 nullptr, | |
| 33 base::kNullProcessHandle, | |
| 34 process_id), | |
| 35 type_(type) {} | |
| 36 | |
| 37 Type GetType() const override { return type_; } | |
| 38 | |
| 39 int GetChildProcessUniqueID() const override { return 0; } | |
| 40 | |
| 41 const Task* GetParentTask() const override { return nullptr; } | |
| 42 | |
| 43 int GetTabId() const override { return 0; } | |
| 44 | |
| 45 private: | |
| 46 Type type_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(FakeTask); | |
| 49 }; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class TaskGroupTest : public testing::Test { | |
| 54 public: | |
| 55 TaskGroupTest() | |
| 56 : io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( | |
| 57 content::BrowserThread::IO)), | |
| 58 run_loop_(base::MakeUnique<base::RunLoop>()), | |
| 59 task_group_(base::Process::Current().Handle(), | |
| 60 base::Process::Current().Pid(), | |
| 61 base::Bind(&TaskGroupTest::OnBackgroundCalculationsDone, | |
| 62 base::Unretained(this)), | |
| 63 new SharedSampler(io_task_runner_), | |
| 64 io_task_runner_) {} | |
| 65 | |
| 66 protected: | |
| 67 void OnBackgroundCalculationsDone() { | |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 69 background_refresh_complete_ = true; | |
| 70 run_loop_->QuitWhenIdle(); | |
| 71 } | |
| 72 | |
| 73 content::TestBrowserThreadBundle browser_threads_; | |
| 74 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | |
| 75 std::unique_ptr<base::RunLoop> run_loop_; | |
| 76 TaskGroup task_group_; | |
| 77 bool background_refresh_complete_ = false; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(TaskGroupTest); | |
| 80 }; | |
| 81 | |
| 82 // Verify that calling TaskGroup::Refresh() without specifying any fields to | |
| 83 // refresh trivially completes, without crashing or leaving things in a weird | |
| 84 // state. | |
| 85 TEST_F(TaskGroupTest, NullRefresh) { | |
| 86 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), 0); | |
| 87 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 88 EXPECT_FALSE(background_refresh_complete_); | |
| 89 } | |
| 90 | |
| 91 // Verify that Refresh() for a field which can be refreshed synchronously | |
| 92 // completes immediately, without leaving any background calculations pending. | |
| 93 TEST_F(TaskGroupTest, SyncRefresh) { | |
| 94 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 95 REFRESH_TYPE_NETWORK_USAGE); | |
| 96 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 97 EXPECT_FALSE(background_refresh_complete_); | |
| 98 } | |
| 99 | |
| 100 // Some fields are refreshed on a per-TaskGroup basis, but require asynchronous | |
| 101 // work (e.g. on another thread) to complete. Memory is such a field, so verify | |
| 102 // that it is correctly reported as requiring background calculations. | |
| 103 TEST_F(TaskGroupTest, AsyncRefresh) { | |
| 104 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 105 REFRESH_TYPE_MEMORY); | |
| 106 EXPECT_FALSE(task_group_.AreBackgroundCalculationsDone()); | |
| 107 | |
| 108 ASSERT_FALSE(background_refresh_complete_); | |
| 109 run_loop_->Run(); | |
| 110 | |
| 111 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 112 EXPECT_TRUE(background_refresh_complete_); | |
| 113 } | |
| 114 | |
| 115 // Some fields are refreshed system-wide, via a SharedSampler, which completes | |
| 116 // asynchronously. Idle wakeups are reported via SharedSampler on some systems | |
| 117 // and via asynchronous refresh on others, so we just that that field always | |
|
afakhry
2017/02/15 02:58:19
Nit: that that?
Wez
2017/02/16 22:44:19
Done.
| |
| 118 // requires background calculations, similarly to the AsyncRefresh test above. | |
| 119 TEST_F(TaskGroupTest, SharedAsyncRefresh) { | |
| 120 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 121 REFRESH_TYPE_IDLE_WAKEUPS); | |
| 122 EXPECT_FALSE(task_group_.AreBackgroundCalculationsDone()); | |
| 123 | |
| 124 ASSERT_FALSE(background_refresh_complete_); | |
| 125 run_loop_->Run(); | |
| 126 | |
| 127 EXPECT_TRUE(background_refresh_complete_); | |
| 128 | |
| 129 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 130 } | |
| 131 | |
| 132 // Ensure that refreshing the NaCl debug ports for a TaskGroup completes | |
| 133 // synchronously if the TaskGroup doesn't actually contain any Tasks. | |
| 134 TEST_F(TaskGroupTest, NaclRefreshZeroTasks) { | |
|
afakhry
2017/02/15 02:58:19
This shouldn't happen but it's good to have a cove
Wez
2017/02/16 22:44:19
Yup, confirmed that; I'll replace this with a gene
| |
| 135 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 136 REFRESH_TYPE_NACL); | |
| 137 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 138 EXPECT_FALSE(background_refresh_complete_); | |
| 139 } | |
| 140 | |
| 141 // Ensure that if NaCl is enabled then calling Refresh with a NaCl Task active | |
| 142 // results in asynchronous completion. Also verifies that if NaCl is disabled | |
| 143 // then completion is synchronous. | |
| 144 TEST_F(TaskGroupTest, NaclRefreshWithTask) { | |
| 145 FakeTask fake_task(base::Process::Current().Pid(), Task::NACL); | |
| 146 task_group_.AddTask(&fake_task); | |
| 147 | |
| 148 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 149 REFRESH_TYPE_NACL); | |
| 150 #if !defined(DISABLE_NACL) | |
| 151 EXPECT_FALSE(task_group_.AreBackgroundCalculationsDone()); | |
| 152 | |
| 153 ASSERT_FALSE(background_refresh_complete_); | |
| 154 run_loop_->Run(); | |
| 155 | |
| 156 EXPECT_TRUE(background_refresh_complete_); | |
| 157 #endif // !defined(DISABLE_NACL) | |
| 158 | |
| 159 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 160 } | |
| 161 | |
| 162 } // namespace task_manager | |
| OLD | NEW |