1 // Copyright 2015 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.
5 #include "chrome/browser/task_management/sampling/task_manager_impl.h"
7 #include "base/stl_util.h"
8 #include "chrome/browser/task_management/sampling/task_group.h"
9 #include "content/public/browser/browser_thread.h"
11 namespace task_management
{
15 inline scoped_refptr
<base::SequencedTaskRunner
> GetBlockingPoolRunner() {
16 base::SequencedWorkerPool
* blocking_pool
=
17 content::BrowserThread::GetBlockingPool();
18 return blocking_pool
->GetSequencedTaskRunner(
19 blocking_pool
->GetSequenceToken());
24 TaskManagerImpl::TaskManagerImpl()
25 : task_groups_by_proc_id_(),
26 task_groups_by_task_id_(),
28 blocking_pool_runner_(GetBlockingPoolRunner()) {
31 TaskManagerImpl::~TaskManagerImpl() {
32 STLDeleteValues(&task_groups_by_proc_id_
);
35 void TaskManagerImpl::ActivateTask(TaskId task_id
) {
36 GetTaskByTaskId(task_id
)->Activate();
39 double TaskManagerImpl::GetCpuUsage(TaskId task_id
) const {
40 return GetTaskGroupByTaskId(task_id
)->cpu_usage();
43 int64
TaskManagerImpl::GetPhysicalMemoryUsage(TaskId task_id
) const {
44 return GetTaskGroupByTaskId(task_id
)->physical_bytes();
47 int64
TaskManagerImpl::GetPrivateMemoryUsage(TaskId task_id
) const {
48 return GetTaskGroupByTaskId(task_id
)->private_bytes();
51 int64
TaskManagerImpl::GetSharedMemoryUsage(TaskId task_id
) const {
52 return GetTaskGroupByTaskId(task_id
)->shared_bytes();
55 int64
TaskManagerImpl::GetGpuMemoryUsage(TaskId task_id
,
56 bool* has_duplicates
) const {
57 const TaskGroup
* task_group
= GetTaskGroupByTaskId(task_id
);
59 *has_duplicates
= task_group
->gpu_memory_has_duplicates();
60 return task_group
->gpu_memory();
63 int TaskManagerImpl::GetIdleWakeupsPerSecond(TaskId task_id
) const {
64 return GetTaskGroupByTaskId(task_id
)->idle_wakeups_per_second();
67 int TaskManagerImpl::GetNaClDebugStubPort(TaskId task_id
) const {
68 #if !defined(DISABLE_NACL)
69 return GetTaskGroupByTaskId(task_id
)->nacl_debug_stub_port();
72 #endif // !defined(DISABLE_NACL)
75 void TaskManagerImpl::GetGDIHandles(TaskId task_id
,
79 const TaskGroup
* task_group
= GetTaskGroupByTaskId(task_id
);
80 *current
= task_group
->gdi_current_handles();
81 *peak
= task_group
->gdi_peak_handles();
85 #endif // defined(OS_WIN)
88 void TaskManagerImpl::GetUSERHandles(TaskId task_id
,
92 const TaskGroup
* task_group
= GetTaskGroupByTaskId(task_id
);
93 *current
= task_group
->user_current_handles();
94 *peak
= task_group
->user_peak_handles();
98 #endif // defined(OS_WIN)
101 const base::string16
& TaskManagerImpl::GetTitle(TaskId task_id
) const {
102 return GetTaskByTaskId(task_id
)->title();
105 base::string16
TaskManagerImpl::GetProfileName(TaskId task_id
) const {
106 return GetTaskByTaskId(task_id
)->GetProfileName();
109 const gfx::ImageSkia
& TaskManagerImpl::GetIcon(TaskId task_id
) const {
110 return GetTaskByTaskId(task_id
)->icon();
113 const base::ProcessHandle
& TaskManagerImpl::GetProcessHandle(
114 TaskId task_id
) const {
115 return GetTaskByTaskId(task_id
)->process_handle();
118 const base::ProcessId
& TaskManagerImpl::GetProcessId(TaskId task_id
) const {
119 return GetTaskByTaskId(task_id
)->process_id();
122 Task::Type
TaskManagerImpl::GetType(TaskId task_id
) const {
123 return GetTaskByTaskId(task_id
)->GetType();
126 int64
TaskManagerImpl::GetNetworkUsage(TaskId task_id
) const {
127 return GetTaskByTaskId(task_id
)->network_usage();
130 int64
TaskManagerImpl::GetSqliteMemoryUsed(TaskId task_id
) const {
131 return GetTaskByTaskId(task_id
)->GetSqliteMemoryUsed();
134 bool TaskManagerImpl::GetV8Memory(TaskId task_id
,
137 const Task
* task
= GetTaskByTaskId(task_id
);
138 if (!task
->ReportsV8Memory())
141 *allocated
= task
->GetV8MemoryAllocated();
142 *used
= task
->GetV8MemoryUsed();
147 bool TaskManagerImpl::GetWebCacheStats(
149 blink::WebCache::ResourceTypeStats
* stats
) const {
150 const Task
* task
= GetTaskByTaskId(task_id
);
151 if (!task
->ReportsWebCacheStats())
154 *stats
= task
->GetWebCacheStats();
159 void TaskManagerImpl::TaskAdded(Task
* task
) {
162 TaskGroup
* task_group
= nullptr;
163 const base::ProcessId proc_id
= task
->process_id();
164 const TaskId task_id
= task
->task_id();
166 auto itr
= task_groups_by_proc_id_
.find(proc_id
);
167 if (itr
== task_groups_by_proc_id_
.end()) {
168 task_group
= new TaskGroup(task
->process_handle(),
170 blocking_pool_runner_
.get());
171 task_groups_by_proc_id_
[proc_id
] = task_group
;
173 task_group
= itr
->second
;
176 task_group
->AddTask(task
);
177 task_groups_by_task_id_
[task_id
] = task_group
;
179 NotifyObserversOnTaskAdded(task_id
);
182 void TaskManagerImpl::TaskRemoved(Task
* task
) {
185 const base::ProcessId proc_id
= task
->process_id();
186 const TaskId task_id
= task
->task_id();
188 DCHECK(ContainsKey(task_groups_by_proc_id_
, proc_id
));
189 DCHECK(ContainsKey(task_groups_by_task_id_
, task_id
));
191 NotifyObserversOnTaskToBeRemoved(task_id
);
193 TaskGroup
* task_group
= task_groups_by_proc_id_
.at(proc_id
);
194 task_group
->RemoveTask(task
);
195 task_groups_by_task_id_
.erase(task_id
);
197 if (task_group
->empty()) {
198 task_groups_by_proc_id_
.erase(proc_id
);
203 void TaskManagerImpl::OnVideoMemoryUsageStatsUpdate(
204 const content::GPUVideoMemoryUsageStats
& gpu_memory_stats
) {
205 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
207 gpu_memory_stats_
= gpu_memory_stats
;
210 void TaskManagerImpl::Refresh() {
211 for (auto& groups_itr
: task_groups_by_proc_id_
) {
212 groups_itr
.second
->Refresh(gpu_memory_stats_
,
213 GetCurrentRefreshTime(),
214 enabled_resources_flags());
217 std::vector
<TaskId
> task_ids
;
218 task_ids
.reserve(task_groups_by_task_id_
.size());
219 for (auto& ids_itr
: task_groups_by_task_id_
)
220 task_ids
.push_back(ids_itr
.first
);
222 NotifyObserversOnRefresh(task_ids
);
225 TaskGroup
* TaskManagerImpl::GetTaskGroupByTaskId(TaskId task_id
) const {
226 DCHECK(ContainsKey(task_groups_by_task_id_
, task_id
));
228 return task_groups_by_task_id_
.at(task_id
);
231 Task
* TaskManagerImpl::GetTaskByTaskId(TaskId task_id
) const {
232 return GetTaskGroupByTaskId(task_id
)->GetTaskById(task_id
);
235 } // namespace task_management