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

Side by Side Diff: chrome/browser/task_manager/sampling/shared_sampler_win_unittest.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
« no previous file with comments | « chrome/browser/task_manager/sampling/shared_sampler_win_defines.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/path_service.h"
14 #include "base/run_loop.h" 15 #include "base/run_loop.h"
15 #include "base/sequenced_task_runner.h" 16 #include "base/sequenced_task_runner.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "chrome/browser/task_manager/sampling/shared_sampler_win_defines.h"
17 #include "chrome/browser/task_manager/task_manager_observer.h" 19 #include "chrome/browser/task_manager/task_manager_observer.h"
18 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 21 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
22 namespace task_manager { 24 namespace task_manager {
23 25
24 // This test class drives SharedSampler in a way similar to the real 26 // This test class drives SharedSampler in a way similar to the real
25 // implementation in TaskManagerImpl and TaskGroup. 27 // implementation in TaskManagerImpl and TaskGroup.
26 class SharedSamplerTest : public testing::Test { 28 class SharedSamplerTest : public testing::Test {
27 public: 29 public:
28 SharedSamplerTest() 30 SharedSamplerTest()
29 : blocking_pool_runner_(GetBlockingPoolRunner()), 31 : blocking_pool_runner_(GetBlockingPoolRunner()),
30 shared_sampler_(new SharedSampler(blocking_pool_runner_)) { 32 shared_sampler_(new SharedSampler(blocking_pool_runner_)) {
31 shared_sampler_->RegisterCallbacks( 33 shared_sampler_->RegisterCallbacks(
32 base::GetCurrentProcId(), 34 base::GetCurrentProcId(),
33 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone, 35 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone,
34 base::Unretained(this)), 36 base::Unretained(this)),
35 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone, 37 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone,
36 base::Unretained(this)), 38 base::Unretained(this)),
37 base::Bind(&SharedSamplerTest::OnStartTimeRefreshDone, 39 base::Bind(&SharedSamplerTest::OnStartTimeRefreshDone,
38 base::Unretained(this)), 40 base::Unretained(this)),
39 base::Bind(&SharedSamplerTest::OnCpuTimeRefreshDone, 41 base::Bind(&SharedSamplerTest::OnCpuTimeRefreshDone,
40 base::Unretained(this))); 42 base::Unretained(this)));
41 } 43 }
42 44
43 ~SharedSamplerTest() override {} 45 ~SharedSamplerTest() override {}
44 46
45 protected: 47 protected:
46 int64_t physical_bytes() const { return physical_bytes_; } 48 int64_t physical_bytes() const { return physical_bytes_; }
47 49
48 base::Time start_time() const { return start_time_; } 50 base::Time start_time() const { return start_time_; }
49 51
50 base::TimeDelta cpu_time() const { return cpu_time_; } 52 base::TimeDelta cpu_time() const { return cpu_time_; }
51 53
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // Verifies that multiple refresh types can be refreshed at the same time. 196 // Verifies that multiple refresh types can be refreshed at the same time.
195 TEST_F(SharedSamplerTest, MultipleRefreshTypes) { 197 TEST_F(SharedSamplerTest, MultipleRefreshTypes) {
196 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY | 198 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY |
197 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME); 199 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME);
198 WaitUntilRefreshDone(); 200 WaitUntilRefreshDone();
199 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY | 201 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY |
200 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME, 202 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME,
201 finished_refresh_type()); 203 finished_refresh_type());
202 } 204 }
203 205
206 static int ReturnZeroThreadProcessInformation(unsigned char* buffer,
207 int buffer_size) {
208 // Calculate the number of bytes required for the structure, and ImageName.
209 base::FilePath current_exe;
210 CHECK(PathService::Get(base::FILE_EXE, &current_exe));
211 base::string16 image_name = current_exe.BaseName().value();
212
213 const int kImageNameBytes = image_name.length() * sizeof(base::char16);
214 const int kRequiredBytes = sizeof(SYSTEM_PROCESS_INFORMATION) +
215 kImageNameBytes + sizeof(base::char16);
216 if (kRequiredBytes > buffer_size)
217 return kRequiredBytes;
218
219 // Create a zero'd structure, so that fields such as thread count will be zero
220 // by default.
221 // Set process handle and image name, so the SharedSampler will match us.
222 memset(buffer, 0, kRequiredBytes);
223 SYSTEM_PROCESS_INFORMATION* process_info =
224 reinterpret_cast<SYSTEM_PROCESS_INFORMATION*>(buffer);
225 process_info->ProcessId = reinterpret_cast<HANDLE>(base::GetCurrentProcId());
226 process_info->ImageName.Length = process_info->ImageName.MaximumLength =
227 kImageNameBytes;
228 process_info->ImageName.Buffer = reinterpret_cast<LPWSTR>(process_info + 1);
229 process_info->NumberOfThreads = 0u;
230 process_info->WorkingSetPrivateSize = 1024ull;
231 buffer += sizeof(SYSTEM_PROCESS_INFORMATION);
232
233 // Copy the image name into place. The earlier memset() provides null
234 // termination.
235 memcpy(buffer, image_name.data(), kImageNameBytes);
236
237 return kRequiredBytes;
238 }
239
240 // Verifies that the SharedSampler copes with zero-thread processes.
241 TEST_F(SharedSamplerTest, ZeroThreadProcess) {
242 SharedSampler::SetQuerySystemInformationForTest(
243 ReturnZeroThreadProcessInformation);
244
245 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY |
246 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME);
247 WaitUntilRefreshDone();
248 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY |
249 REFRESH_TYPE_START_TIME | REFRESH_TYPE_CPU_TIME,
250 finished_refresh_type());
251
252 EXPECT_EQ(1024ll, physical_bytes());
253
254 SharedSampler::SetQuerySystemInformationForTest(nullptr);
255 }
256
204 } // namespace task_manager 257 } // namespace task_manager
OLDNEW
« no previous file with comments | « chrome/browser/task_manager/sampling/shared_sampler_win_defines.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698