Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: chrome/browser/task_manager/sampling/shared_sampler_unittest_win.cc

Issue 2657393003: Add unit-test for SharedSampler handling of zero-thread processes. (Closed)
Patch Set: Remove anonymous namespace Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/shared_sampler.h"
6
7 #include <windows.h>
8
9 #include <memory>
10
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/run_loop.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/task_manager/task_manager_observer.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace task_manager {
23
24 // This test class drives SharedSampler in a way similar to the real
25 // implementation in TaskManagerImpl and TaskGroup.
26 class SharedSamplerTest : public testing::Test {
27 public:
28 SharedSamplerTest()
29 : blocking_pool_runner_(GetBlockingPoolRunner()),
30 shared_sampler_(new SharedSampler(blocking_pool_runner_)) {
31 shared_sampler_->RegisterCallbacks(
32 base::GetCurrentProcId(),
33 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone,
34 base::Unretained(this)),
35 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone,
36 base::Unretained(this)),
37 base::Bind(&SharedSamplerTest::OnStartTimeRefreshDone,
38 base::Unretained(this)),
39 base::Bind(&SharedSamplerTest::OnCpuTimeRefreshDone,
40 base::Unretained(this)));
41 }
42
43 ~SharedSamplerTest() override {}
44
45 protected:
46 int64_t physical_bytes() const { return physical_bytes_; }
47
48 base::Time start_time() const { return start_time_; }
49
50 base::TimeDelta cpu_time() const { return cpu_time_; }
51
52 int idle_wakeups_per_second() const { return idle_wakeups_per_second_; }
53
54 int64_t finished_refresh_type() const { return finished_refresh_type_; }
55
56 void StartRefresh(int64_t refresh_flags) {
57 finished_refresh_type_ = 0;
58 expected_refresh_type_ = refresh_flags;
59 shared_sampler_->Refresh(base::GetCurrentProcId(), refresh_flags);
60 }
61
62 void WaitUntilRefreshDone() {
63 base::RunLoop run_loop;
64 quit_closure_ = run_loop.QuitWhenIdleClosure();
65 run_loop.Run();
66 }
67
68 private:
69 static scoped_refptr<base::SequencedTaskRunner> GetBlockingPoolRunner() {
70 base::SequencedWorkerPool* blocking_pool =
71 content::BrowserThread::GetBlockingPool();
72 return blocking_pool->GetSequencedTaskRunner(
73 blocking_pool->GetSequenceToken());
74 }
75
76 void OnRefreshTypeFinished(int64_t finished_refresh_type) {
77 finished_refresh_type_ |= finished_refresh_type;
78
79 if (finished_refresh_type_ == expected_refresh_type_)
80 quit_closure_.Run();
81 }
82
83 void OnPhysicalMemoryUsageRefreshDone(int64_t physical_bytes) {
84 physical_bytes_ = physical_bytes;
85 OnRefreshTypeFinished(REFRESH_TYPE_PHYSICAL_MEMORY);
86 }
87
88 void OnIdleWakeupsRefreshDone(int idle_wakeups_per_second) {
89 idle_wakeups_per_second_ = idle_wakeups_per_second;
90 OnRefreshTypeFinished(REFRESH_TYPE_IDLE_WAKEUPS);
91 }
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
103 int64_t expected_refresh_type_ = 0;
104 int64_t finished_refresh_type_ = 0;
105 base::Closure quit_closure_;
106
107 int64_t physical_bytes_ = 0;
108 int idle_wakeups_per_second_ = -1;
109 base::Time start_time_;
110 base::TimeDelta cpu_time_;
111
112 content::TestBrowserThreadBundle thread_bundle_;
113 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_;
114 scoped_refptr<SharedSampler> shared_sampler_;
115
116 DISALLOW_COPY_AND_ASSIGN(SharedSamplerTest);
117 };
118
119 // Tests that Idle Wakeups per second value can be obtained from SharedSampler.
120 TEST_F(SharedSamplerTest, IdleWakeups) {
121 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS);
122 WaitUntilRefreshDone();
123 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS, finished_refresh_type());
124
125 // Since idle_wakeups_per_second is an incremental value that reflects delta
126 // between two refreshes, the first refresh always returns zero value. This
127 // is consistent with implementation on other platforms.
128 EXPECT_EQ(0, idle_wakeups_per_second());
129
130 // Do a short sleep - that should trigger a context switch.
131 ::Sleep(1);
132
133 // Do another refresh.
134 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS);
135 WaitUntilRefreshDone();
136
137 // Should get a greater than zero rate now.
138 EXPECT_GT(idle_wakeups_per_second(), 0);
139 }
140
141 // Verifies that Memory (Private WS) value can be obtained from Shared Sampler.
142 TEST_F(SharedSamplerTest, PhysicalMemory) {
143 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY);
144 WaitUntilRefreshDone();
145 EXPECT_EQ(REFRESH_TYPE_PHYSICAL_MEMORY, finished_refresh_type());
146
147 int64_t initial_value = physical_bytes();
148
149 // Allocate a large continuos block of memory.
150 const int allocated_size = 4 * 1024 * 1024;
151 std::vector<uint8_t> memory_block(allocated_size);
152
153 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY);
154 WaitUntilRefreshDone();
155
156 // Verify that physical bytes has increased accordingly.
157 EXPECT_GE(physical_bytes(), initial_value + allocated_size);
158 }
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
194 // Verifies that multiple refresh types can be refreshed at the same time.
195 TEST_F(SharedSamplerTest, MultipleRefreshTypes) {
196 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY |
197 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME);
198 WaitUntilRefreshDone();
199 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY |
200 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME,
201 finished_refresh_type());
202 }
203
204 } // namespace task_manager
OLDNEW
« no previous file with comments | « chrome/browser/task_manager/sampling/shared_sampler.h ('k') | chrome/browser/task_manager/sampling/shared_sampler_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698