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 #ifndef CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_MANAGER_IMPL_H_
10 #include "base/lazy_instance.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/sequenced_task_runner.h"
13 #include "chrome/browser/task_management/providers/task_provider.h"
14 #include "chrome/browser/task_management/providers/task_provider_observer.h"
15 #include "chrome/browser/task_management/sampling/task_group.h"
16 #include "chrome/browser/task_management/sampling/task_manager_io_thread_helper.h"
17 #include "chrome/browser/task_management/task_manager_interface.h"
18 #include "content/public/browser/gpu_data_manager_observer.h"
20 namespace task_management
{
22 // Defines a concrete implementation of the TaskManagerInterface.
23 class TaskManagerImpl
:
24 public TaskManagerInterface
,
25 public TaskProviderObserver
,
26 content::GpuDataManagerObserver
{
28 ~TaskManagerImpl() override
;
30 static TaskManagerImpl
* GetInstance();
32 // task_management::TaskManagerInterface:
33 void ActivateTask(TaskId task_id
) override
;
34 double GetCpuUsage(TaskId task_id
) const override
;
35 int64
GetPhysicalMemoryUsage(TaskId task_id
) const override
;
36 int64
GetPrivateMemoryUsage(TaskId task_id
) const override
;
37 int64
GetSharedMemoryUsage(TaskId task_id
) const override
;
38 int64
GetGpuMemoryUsage(TaskId task_id
, bool* has_duplicates
) const override
;
39 int GetIdleWakeupsPerSecond(TaskId task_id
) const override
;
40 int GetNaClDebugStubPort(TaskId task_id
) const override
;
41 void GetGDIHandles(TaskId task_id
,
43 int64
* peak
) const override
;
44 void GetUSERHandles(TaskId task_id
,
46 int64
* peak
) const override
;
47 const base::string16
& GetTitle(TaskId task_id
) const override
;
48 base::string16
GetProfileName(TaskId task_id
) const override
;
49 const gfx::ImageSkia
& GetIcon(TaskId task_id
) const override
;
50 const base::ProcessHandle
& GetProcessHandle(TaskId task_id
) const override
;
51 const base::ProcessId
& GetProcessId(TaskId task_id
) const override
;
52 Task::Type
GetType(TaskId task_id
) const override
;
53 int64
GetNetworkUsage(TaskId task_id
) const override
;
54 int64
GetSqliteMemoryUsed(TaskId task_id
) const override
;
55 bool GetV8Memory(TaskId task_id
,
57 int64
* used
) const override
;
58 bool GetWebCacheStats(
60 blink::WebCache::ResourceTypeStats
* stats
) const override
;
61 const TaskIdList
& GetTaskIdsList() const override
;
62 size_t GetNumberOfTasksOnSameProcess(TaskId task_id
) const override
;
64 // task_management::TaskProviderObserver:
65 void TaskAdded(Task
* task
) override
;
66 void TaskRemoved(Task
* task
) override
;
68 // content::GpuDataManagerObserver:
69 void OnVideoMemoryUsageStatsUpdate(
70 const content::GPUVideoMemoryUsageStats
& gpu_memory_stats
) override
;
72 // The notification method on the UI thread when multiple bytes are read
73 // from URLRequests. This will be called by the |io_thread_helper_|
74 static void OnMultipleBytesReadUI(std::vector
<BytesReadParam
>* params
);
77 friend struct base::DefaultLazyInstanceTraits
<TaskManagerImpl
>;
81 // task_management::TaskManagerInterface:
82 void Refresh() override
;
83 void StartUpdating() override
;
84 void StopUpdating() override
;
86 // Based on |param| the appropriate task will be updated by its network usage.
87 // Returns true if it was able to match |param| to an existing task, returns
88 // false otherwise, at which point the caller must explicitly match these
89 // bytes to the browser process by calling this method again with
90 // |param.origin_pid = 0| and |param.child_id = param.route_id = -1|.
91 bool UpdateTasksWithBytesRead(const BytesReadParam
& param
);
93 TaskGroup
* GetTaskGroupByTaskId(TaskId task_id
) const;
94 Task
* GetTaskByTaskId(TaskId task_id
) const;
96 // Map TaskGroups by the IDs of the processes they represent.
97 // Keys and values are unique (no duplicates).
98 std::map
<base::ProcessId
, TaskGroup
*> task_groups_by_proc_id_
;
100 // Map each task by its ID to the TaskGroup on which it resides.
101 // Keys are unique but values will have duplicates (i.e. multiple tasks
102 // running on the same process represented by a single TaskGroup).
103 std::map
<TaskId
, TaskGroup
*> task_groups_by_task_id_
;
105 // A cached sorted list of the task IDs.
106 mutable std::vector
<TaskId
> sorted_task_ids_
;
108 // The manager of the IO thread helper used to handle network bytes
109 // notifications on IO thread. The manager itself lives on the UI thread, but
110 // the IO thread helper lives entirely on the IO thread.
111 scoped_ptr
<IoThreadHelperManager
> io_thread_helper_manager_
;
113 // The list of the task providers that are owned and observed by this task
114 // manager implementation.
115 ScopedVector
<TaskProvider
> task_providers_
;
117 // The current GPU memory usage stats that was last received from the
119 content::GPUVideoMemoryUsageStats gpu_memory_stats_
;
121 // The specific blocking pool SequencedTaskRunner that will be used to make
122 // sure TaskGroupSampler posts their refreshes serially.
123 scoped_refptr
<base::SequencedTaskRunner
> blocking_pool_runner_
;
125 // This will be set to true while there are observers and the task manager is
129 DISALLOW_COPY_AND_ASSIGN(TaskManagerImpl
);
132 } // namespace task_management
134 #endif // CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_MANAGER_IMPL_H_