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

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

Issue 2238403003: Task manager: Get physical memory efficiently for all processes from SharedSampler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed test failure Created 4 years, 3 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 "chrome/browser/task_manager/task_manager_observer.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace task_manager {
22
23 // This test class drives SharedSampler in a way similar to the real
24 // implementation in TaskManagerImpl and TaskGroup.
25 class SharedSamplerTest : public testing::Test {
26 public:
27 SharedSamplerTest()
28 : expected_refresh_type_(0), finished_refresh_type_(0),
brucedawson 2016/08/26 01:02:10 We are now allowed to initialize member variables
stanisc 2016/08/26 20:47:56 Yes, I know we can do this now with C++ 11. Initia
brucedawson 2016/08/26 21:47:27 It is possible to initialize all fields in the cla
29 physical_bytes_(0), idle_wakeups_per_second_(-1),
30 blocking_pool_runner_(GetBlockingPoolRunner()),
31 shared_sampler_(new SharedSampler(blocking_pool_runner_)) {
32 shared_sampler_->RegisterCallbacks(
33 base::GetCurrentProcId(),
34 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone,
35 base::Unretained(this)),
36 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone,
37 base::Unretained(this)));
38 }
39
40 ~SharedSamplerTest() override {}
41
42 protected:
43 int64_t physical_bytes() const { return physical_bytes_; }
44
45 int idle_wakeups_per_second() const { return idle_wakeups_per_second_; }
46
47 int64_t finished_refresh_type() const { return finished_refresh_type_; }
48
49 void StartRefresh(int64_t refresh_flags) {
50 finished_refresh_type_ = 0;
51 expected_refresh_type_ = refresh_flags;
52 shared_sampler_->Refresh(base::GetCurrentProcId(), refresh_flags);
53 }
54
55 void WaitUntilRefreshDone() {
56 base::RunLoop run_loop;
57 quit_closure_ = run_loop.QuitWhenIdleClosure();
58 run_loop.Run();
59 }
60
61 private:
62 static scoped_refptr<base::SequencedTaskRunner> GetBlockingPoolRunner() {
63 base::SequencedWorkerPool* blocking_pool =
64 content::BrowserThread::GetBlockingPool();
65 return blocking_pool->GetSequencedTaskRunner(
66 blocking_pool->GetSequenceToken());
67 }
68
69 void OnRefreshTypeFinished(int64_t finished_refresh_type) {
70 finished_refresh_type_ |= finished_refresh_type;
71
72 if (finished_refresh_type_ == expected_refresh_type_)
73 quit_closure_.Run();
74 }
75
76 void OnPhysicalMemoryUsageRefreshDone(int64_t physical_bytes) {
77 physical_bytes_ = physical_bytes;
78 OnRefreshTypeFinished(REFRESH_TYPE_PHYSICAL_MEMORY);
79 }
80
81 void OnIdleWakeupsRefreshDone(int idle_wakeups_per_second) {
82 idle_wakeups_per_second_ = idle_wakeups_per_second;
83 OnRefreshTypeFinished(REFRESH_TYPE_IDLE_WAKEUPS);
84 }
85
86 int64_t expected_refresh_type_;
87 int64_t finished_refresh_type_;
88 base::Closure quit_closure_;
89
90 int64_t physical_bytes_;
91 int idle_wakeups_per_second_;
92
93 content::TestBrowserThreadBundle thread_bundle_;
94 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_;
95 scoped_refptr<SharedSampler> shared_sampler_;
96
97 DISALLOW_COPY_AND_ASSIGN(SharedSamplerTest);
98 };
99
100 // Tests that Idle Wakeups per second value can be obtained from SharedSampler.
101 TEST_F(SharedSamplerTest, IdleWakeups) {
102 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS);
103 WaitUntilRefreshDone();
104 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS, finished_refresh_type());
105
106 // Since idle_wakeups_per_second is an incremental value that reflects delta
107 // between two refreshes, the first refresh always returns zero value. This
108 // is consistent with implementation on other platforms.
109 EXPECT_EQ(0, idle_wakeups_per_second());
110
111 // Do a short sleep - that should trigger a context switch.
112 ::Sleep(1);
113
114 // Do another refresh.
115 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS);
116 WaitUntilRefreshDone();
117
118 // Should get a greater than zero rate now.
119 EXPECT_LT(0, idle_wakeups_per_second());
brucedawson 2016/08/26 01:02:10 If we except a greater than zero rate now then why
stanisc 2016/08/26 20:47:56 The only reason I've done it this way is to keep i
120 }
121
122 // Verifies that Memory (Private WS) value can be obtained from Shared Sampler.
123 TEST_F(SharedSamplerTest, PhysicalMemory) {
124 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY);
125 WaitUntilRefreshDone();
126 EXPECT_EQ(REFRESH_TYPE_PHYSICAL_MEMORY, finished_refresh_type());
127
128 int64_t initial_value = physical_bytes();
129
130 // Allocate a large continuos block of memory.
131 const int allocated_size = 4 * 1024 * 1024;
132 std::unique_ptr<uint8_t[]> memory_block(new uint8_t[allocated_size]);
brucedawson 2016/08/26 01:02:10 On further reflection... This could be simplified
stanisc 2016/08/26 20:47:56 Good suggestion. Done.
133 DCHECK(memory_block.get());
134 // Touch the memory block to initialize it to make sure it is reflected in the
135 // working set.
136 ::memset(memory_block.get(), 0, allocated_size);
137
138 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY);
139 WaitUntilRefreshDone();
140
141 // Verify that physical bytes has increased accordingly.
142 EXPECT_LE(initial_value + allocated_size, physical_bytes());
brucedawson 2016/08/26 01:02:10 I find this hard to read with EXPECT_LE. Have you
stanisc 2016/08/26 20:47:56 Done.
143 }
144
145 // Verifies that multiple refresh types can be refreshed at the same time.
146 TEST_F(SharedSamplerTest, MultipleRefreshTypes) {
147 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY);
148 WaitUntilRefreshDone();
149 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY,
150 finished_refresh_type());
151 }
152
153 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698