| 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 "chrome/browser/task_manager/sampling/shared_sampler.h" | 5 #include "chrome/browser/task_manager/sampling/shared_sampler.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/time/time.h" |
| 16 #include "chrome/browser/task_manager/task_manager_observer.h" | 17 #include "chrome/browser/task_manager/task_manager_observer.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace task_manager { | 22 namespace task_manager { |
| 22 | 23 |
| 23 // This test class drives SharedSampler in a way similar to the real | 24 // This test class drives SharedSampler in a way similar to the real |
| 24 // implementation in TaskManagerImpl and TaskGroup. | 25 // implementation in TaskManagerImpl and TaskGroup. |
| 25 class SharedSamplerTest : public testing::Test { | 26 class SharedSamplerTest : public testing::Test { |
| 26 public: | 27 public: |
| 27 SharedSamplerTest() | 28 SharedSamplerTest() |
| 28 : blocking_pool_runner_(GetBlockingPoolRunner()), | 29 : blocking_pool_runner_(GetBlockingPoolRunner()), |
| 29 shared_sampler_(new SharedSampler(blocking_pool_runner_)) { | 30 shared_sampler_(new SharedSampler(blocking_pool_runner_)) { |
| 30 shared_sampler_->RegisterCallbacks( | 31 shared_sampler_->RegisterCallbacks( |
| 31 base::GetCurrentProcId(), | 32 base::GetCurrentProcId(), |
| 32 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone, | 33 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone, |
| 33 base::Unretained(this)), | 34 base::Unretained(this)), |
| 34 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone, | 35 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone, |
| 36 base::Unretained(this)), |
| 37 base::Bind(&SharedSamplerTest::OnStartTimeRefreshDone, |
| 38 base::Unretained(this)), |
| 39 base::Bind(&SharedSamplerTest::OnCpuTimeRefreshDone, |
| 35 base::Unretained(this))); | 40 base::Unretained(this))); |
| 36 } | 41 } |
| 37 | 42 |
| 38 ~SharedSamplerTest() override {} | 43 ~SharedSamplerTest() override {} |
| 39 | 44 |
| 40 protected: | 45 protected: |
| 41 int64_t physical_bytes() const { return physical_bytes_; } | 46 int64_t physical_bytes() const { return physical_bytes_; } |
| 42 | 47 |
| 48 base::Time start_time() const { return start_time_; } |
| 49 |
| 50 base::TimeDelta cpu_time() const { return cpu_time_; } |
| 51 |
| 43 int idle_wakeups_per_second() const { return idle_wakeups_per_second_; } | 52 int idle_wakeups_per_second() const { return idle_wakeups_per_second_; } |
| 44 | 53 |
| 45 int64_t finished_refresh_type() const { return finished_refresh_type_; } | 54 int64_t finished_refresh_type() const { return finished_refresh_type_; } |
| 46 | 55 |
| 47 void StartRefresh(int64_t refresh_flags) { | 56 void StartRefresh(int64_t refresh_flags) { |
| 48 finished_refresh_type_ = 0; | 57 finished_refresh_type_ = 0; |
| 49 expected_refresh_type_ = refresh_flags; | 58 expected_refresh_type_ = refresh_flags; |
| 50 shared_sampler_->Refresh(base::GetCurrentProcId(), refresh_flags); | 59 shared_sampler_->Refresh(base::GetCurrentProcId(), refresh_flags); |
| 51 } | 60 } |
| 52 | 61 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 74 void OnPhysicalMemoryUsageRefreshDone(int64_t physical_bytes) { | 83 void OnPhysicalMemoryUsageRefreshDone(int64_t physical_bytes) { |
| 75 physical_bytes_ = physical_bytes; | 84 physical_bytes_ = physical_bytes; |
| 76 OnRefreshTypeFinished(REFRESH_TYPE_PHYSICAL_MEMORY); | 85 OnRefreshTypeFinished(REFRESH_TYPE_PHYSICAL_MEMORY); |
| 77 } | 86 } |
| 78 | 87 |
| 79 void OnIdleWakeupsRefreshDone(int idle_wakeups_per_second) { | 88 void OnIdleWakeupsRefreshDone(int idle_wakeups_per_second) { |
| 80 idle_wakeups_per_second_ = idle_wakeups_per_second; | 89 idle_wakeups_per_second_ = idle_wakeups_per_second; |
| 81 OnRefreshTypeFinished(REFRESH_TYPE_IDLE_WAKEUPS); | 90 OnRefreshTypeFinished(REFRESH_TYPE_IDLE_WAKEUPS); |
| 82 } | 91 } |
| 83 | 92 |
| 93 void OnStartTimeRefreshDone(base::Time start_time) { |
| 94 start_time_ = start_time; |
| 95 OnRefreshTypeFinished(REFRESH_TYPE_START_TIME); |
| 96 } |
| 97 |
| 98 void OnCpuTimeRefreshDone(base::TimeDelta cpu_time) { |
| 99 cpu_time_ = cpu_time; |
| 100 OnRefreshTypeFinished(REFRESH_TYPE_CPU_TIME); |
| 101 } |
| 102 |
| 84 int64_t expected_refresh_type_ = 0; | 103 int64_t expected_refresh_type_ = 0; |
| 85 int64_t finished_refresh_type_ = 0; | 104 int64_t finished_refresh_type_ = 0; |
| 86 base::Closure quit_closure_; | 105 base::Closure quit_closure_; |
| 87 | 106 |
| 88 int64_t physical_bytes_ = 0; | 107 int64_t physical_bytes_ = 0; |
| 89 int idle_wakeups_per_second_ = -1; | 108 int idle_wakeups_per_second_ = -1; |
| 109 base::Time start_time_; |
| 110 base::TimeDelta cpu_time_; |
| 90 | 111 |
| 91 content::TestBrowserThreadBundle thread_bundle_; | 112 content::TestBrowserThreadBundle thread_bundle_; |
| 92 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_; | 113 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_; |
| 93 scoped_refptr<SharedSampler> shared_sampler_; | 114 scoped_refptr<SharedSampler> shared_sampler_; |
| 94 | 115 |
| 95 DISALLOW_COPY_AND_ASSIGN(SharedSamplerTest); | 116 DISALLOW_COPY_AND_ASSIGN(SharedSamplerTest); |
| 96 }; | 117 }; |
| 97 | 118 |
| 98 // Tests that Idle Wakeups per second value can be obtained from SharedSampler. | 119 // Tests that Idle Wakeups per second value can be obtained from SharedSampler. |
| 99 TEST_F(SharedSamplerTest, IdleWakeups) { | 120 TEST_F(SharedSamplerTest, IdleWakeups) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 129 const int allocated_size = 4 * 1024 * 1024; | 150 const int allocated_size = 4 * 1024 * 1024; |
| 130 std::vector<uint8_t> memory_block(allocated_size); | 151 std::vector<uint8_t> memory_block(allocated_size); |
| 131 | 152 |
| 132 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY); | 153 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY); |
| 133 WaitUntilRefreshDone(); | 154 WaitUntilRefreshDone(); |
| 134 | 155 |
| 135 // Verify that physical bytes has increased accordingly. | 156 // Verify that physical bytes has increased accordingly. |
| 136 EXPECT_GE(physical_bytes(), initial_value + allocated_size); | 157 EXPECT_GE(physical_bytes(), initial_value + allocated_size); |
| 137 } | 158 } |
| 138 | 159 |
| 160 // Tests that process start time can be obtained from SharedSampler. |
| 161 TEST_F(SharedSamplerTest, StartTime) { |
| 162 StartRefresh(REFRESH_TYPE_START_TIME); |
| 163 WaitUntilRefreshDone(); |
| 164 EXPECT_EQ(REFRESH_TYPE_START_TIME, finished_refresh_type()); |
| 165 |
| 166 // Should get a greater than zero now. |
| 167 base::Time start_time_prev = start_time(); |
| 168 EXPECT_GT(start_time_prev, base::Time()); |
| 169 |
| 170 // Do a refresh. |
| 171 StartRefresh(REFRESH_TYPE_START_TIME); |
| 172 WaitUntilRefreshDone(); |
| 173 |
| 174 // Start time should not change. |
| 175 EXPECT_EQ(start_time(), start_time_prev); |
| 176 } |
| 177 |
| 178 // Tests that CPU time can be obtained from SharedSampler. |
| 179 TEST_F(SharedSamplerTest, CpuTime) { |
| 180 StartRefresh(REFRESH_TYPE_CPU_TIME); |
| 181 WaitUntilRefreshDone(); |
| 182 EXPECT_EQ(REFRESH_TYPE_CPU_TIME, finished_refresh_type()); |
| 183 |
| 184 base::TimeDelta cpu_time_prev = cpu_time(); |
| 185 |
| 186 // Do a refresh. |
| 187 StartRefresh(REFRESH_TYPE_CPU_TIME); |
| 188 WaitUntilRefreshDone(); |
| 189 |
| 190 // CPU time should not decrease. |
| 191 EXPECT_GE(cpu_time(), cpu_time_prev); |
| 192 } |
| 193 |
| 139 // Verifies that multiple refresh types can be refreshed at the same time. | 194 // Verifies that multiple refresh types can be refreshed at the same time. |
| 140 TEST_F(SharedSamplerTest, MultipleRefreshTypes) { | 195 TEST_F(SharedSamplerTest, MultipleRefreshTypes) { |
| 141 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY); | 196 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY | |
| 197 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME); |
| 142 WaitUntilRefreshDone(); | 198 WaitUntilRefreshDone(); |
| 143 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY, | 199 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY | |
| 200 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME, |
| 144 finished_refresh_type()); | 201 finished_refresh_type()); |
| 145 } | 202 } |
| 146 | 203 |
| 147 } // namespace task_manager | 204 } // namespace task_manager |
| OLD | NEW |