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

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: Addressed CR feedback 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 : blocking_pool_runner_(GetBlockingPoolRunner()),
29 shared_sampler_(new SharedSampler(blocking_pool_runner_)) {
30 shared_sampler_->RegisterCallbacks(
31 base::GetCurrentProcId(),
32 base::Bind(&SharedSamplerTest::OnIdleWakeupsRefreshDone,
33 base::Unretained(this)),
34 base::Bind(&SharedSamplerTest::OnPhysicalMemoryUsageRefreshDone,
35 base::Unretained(this)));
36 }
37
38 ~SharedSamplerTest() override {}
39
40 protected:
41 int64_t physical_bytes() const { return physical_bytes_; }
42
43 int idle_wakeups_per_second() const { return idle_wakeups_per_second_; }
44
45 int64_t finished_refresh_type() const { return finished_refresh_type_; }
46
47 void StartRefresh(int64_t refresh_flags) {
48 finished_refresh_type_ = 0;
49 expected_refresh_type_ = refresh_flags;
50 shared_sampler_->Refresh(base::GetCurrentProcId(), refresh_flags);
51 }
52
53 void WaitUntilRefreshDone() {
54 base::RunLoop run_loop;
55 quit_closure_ = run_loop.QuitWhenIdleClosure();
56 run_loop.Run();
57 }
58
59 private:
60 static scoped_refptr<base::SequencedTaskRunner> GetBlockingPoolRunner() {
61 base::SequencedWorkerPool* blocking_pool =
62 content::BrowserThread::GetBlockingPool();
63 return blocking_pool->GetSequencedTaskRunner(
64 blocking_pool->GetSequenceToken());
65 }
66
67 void OnRefreshTypeFinished(int64_t finished_refresh_type) {
68 finished_refresh_type_ |= finished_refresh_type;
69
70 if (finished_refresh_type_ == expected_refresh_type_)
71 quit_closure_.Run();
72 }
73
74 void OnPhysicalMemoryUsageRefreshDone(int64_t physical_bytes) {
75 physical_bytes_ = physical_bytes;
76 OnRefreshTypeFinished(REFRESH_TYPE_PHYSICAL_MEMORY);
77 }
78
79 void OnIdleWakeupsRefreshDone(int idle_wakeups_per_second) {
80 idle_wakeups_per_second_ = idle_wakeups_per_second;
81 OnRefreshTypeFinished(REFRESH_TYPE_IDLE_WAKEUPS);
82 }
83
84 int64_t expected_refresh_type_ = 0;
85 int64_t finished_refresh_type_ = 0;
86 base::Closure quit_closure_;
87
88 int64_t physical_bytes_ = 0;
89 int idle_wakeups_per_second_ = -1;
90
91 content::TestBrowserThreadBundle thread_bundle_;
92 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_;
93 scoped_refptr<SharedSampler> shared_sampler_;
94
95 DISALLOW_COPY_AND_ASSIGN(SharedSamplerTest);
96 };
97
98 // Tests that Idle Wakeups per second value can be obtained from SharedSampler.
99 TEST_F(SharedSamplerTest, IdleWakeups) {
100 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS);
101 WaitUntilRefreshDone();
102 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS, finished_refresh_type());
103
104 // Since idle_wakeups_per_second is an incremental value that reflects delta
105 // between two refreshes, the first refresh always returns zero value. This
106 // is consistent with implementation on other platforms.
107 EXPECT_EQ(0, idle_wakeups_per_second());
108
109 // Do a short sleep - that should trigger a context switch.
110 ::Sleep(1);
111
112 // Do another refresh.
113 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS);
114 WaitUntilRefreshDone();
115
116 // Should get a greater than zero rate now.
117 EXPECT_GT(idle_wakeups_per_second(), 0);
118 }
119
120 // Verifies that Memory (Private WS) value can be obtained from Shared Sampler.
121 TEST_F(SharedSamplerTest, PhysicalMemory) {
122 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY);
123 WaitUntilRefreshDone();
124 EXPECT_EQ(REFRESH_TYPE_PHYSICAL_MEMORY, finished_refresh_type());
125
126 int64_t initial_value = physical_bytes();
127
128 // Allocate a large continuos block of memory.
129 const int allocated_size = 4 * 1024 * 1024;
130 std::vector<uint8_t> memory_block(allocated_size);
131
132 StartRefresh(REFRESH_TYPE_PHYSICAL_MEMORY);
133 WaitUntilRefreshDone();
134
135 // Verify that physical bytes has increased accordingly.
136 EXPECT_GE(physical_bytes(), initial_value + allocated_size);
137 }
138
139 // Verifies that multiple refresh types can be refreshed at the same time.
140 TEST_F(SharedSamplerTest, MultipleRefreshTypes) {
141 StartRefresh(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY);
142 WaitUntilRefreshDone();
143 EXPECT_EQ(REFRESH_TYPE_IDLE_WAKEUPS | REFRESH_TYPE_PHYSICAL_MEMORY,
144 finished_refresh_type());
145 }
146
147 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698