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. 4 5 #ifndef CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IMPL_H_ 6 #define CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IMPL_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <memory> 13 #include <string> 14 #include <vector> 15 16 #include "base/lazy_instance.h" 17 #include "base/macros.h" 18 #include "base/memory/weak_ptr.h" 19 #include "base/sequenced_task_runner.h" 20 #include "base/time/time.h" 21 #include "chrome/browser/task_manager/providers/task_provider.h" 22 #include "chrome/browser/task_manager/providers/task_provider_observer.h" 23 #include "chrome/browser/task_manager/sampling/task_group.h" 24 #include "chrome/browser/task_manager/task_manager_interface.h" 25 #include "gpu/ipc/common/memory_stats.h" 26 #include "services/network/public/mojom/network_service.mojom.h" 27 #include "services/resource_coordinator/public/cpp/memory_instrumentation/global_memory_dump.h" 28 29 #if defined(OS_CHROMEOS) 30 #include "chrome/browser/task_manager/sampling/arc_shared_sampler.h" 31 #endif // defined(OS_CHROMEOS) 32 33 namespace task_manager { 34 35 class SharedSampler; 36 37 // Identifies the initiator of a network request, by a (child_id, 38 // route_id) tuple. 39 // BytesTransferredKey supports hashing and may be used as an unordered_map key. 40 struct BytesTransferredKey { 41 // The unique ID of the host of the child process requester. 42 int child_id; 43 44 // The ID of the IPC route for the URLRequest (this identifies the 45 // RenderView or like-thing in the renderer that the request gets routed 46 // to). 47 int route_id; 48 49 struct Hasher { 50 size_t operator()(const BytesTransferredKey& key) const; 51 }; 52 53 bool operator==(const BytesTransferredKey& other) const; 54 }; 55 56 // This is the entry of the unordered map that tracks bytes transfered by task. 57 struct BytesTransferredParam { 58 // The number of bytes read. 59 int64_t byte_read_count = 0; 60 61 // The number of bytes sent. 62 int64_t byte_sent_count = 0; 63 }; 64 65 using BytesTransferredMap = std::unordered_map<BytesTransferredKey, 66 BytesTransferredParam, 67 BytesTransferredKey::Hasher>; 68 69 // Defines a concrete implementation of the TaskManagerInterface. 70 class TaskManagerImpl : public TaskManagerInterface, 71 public TaskProviderObserver { 72 public: 73 ~TaskManagerImpl() override; 74 75 static TaskManagerImpl* GetInstance(); 76 77 // task_manager::TaskManagerInterface: 78 void ActivateTask(TaskId task_id) override; 79 bool IsTaskKillable(TaskId task_id) override; 80 void KillTask(TaskId task_id) override; 81 double GetPlatformIndependentCPUUsage(TaskId task_id) const override; 82 base::Time GetStartTime(TaskId task_id) const override; 83 base::TimeDelta GetCpuTime(TaskId task_id) const override; 84 int64_t GetMemoryFootprintUsage(TaskId task_id) const override; 85 int64_t GetSwappedMemoryUsage(TaskId task_id) const override; 86 int64_t GetGpuMemoryUsage(TaskId task_id, 87 bool* has_duplicates) const override; 88 int GetIdleWakeupsPerSecond(TaskId task_id) const override; 89 int GetHardFaultsPerSecond(TaskId task_id) const override; 90 int GetNaClDebugStubPort(TaskId task_id) const override; 91 void GetGDIHandles(TaskId task_id, 92 int64_t* current, 93 int64_t* peak) const override; 94 void GetUSERHandles(TaskId task_id, 95 int64_t* current, 96 int64_t* peak) const override; 97 int GetOpenFdCount(TaskId task_id) const override; 98 bool IsTaskOnBackgroundedProcess(TaskId task_id) const override; 99 const base::string16& GetTitle(TaskId task_id) const override; 100 base::string16 GetProfileName(TaskId task_id) const override; 101 const gfx::ImageSkia& GetIcon(TaskId task_id) const override; 102 const base::ProcessHandle& GetProcessHandle(TaskId task_id) const override; 103 const base::ProcessId& GetProcessId(TaskId task_id) const override; 104 Task::Type GetType(TaskId task_id) const override; 105 SessionID GetTabId(TaskId task_id) const override; 106 int GetChildProcessUniqueId(TaskId task_id) const override; 107 void GetTerminationStatus(TaskId task_id, 108 base::TerminationStatus* out_status, 109 int* out_error_code) const override; 110 int64_t GetNetworkUsage(TaskId task_id) const override; 111 int64_t GetCumulativeNetworkUsage(TaskId task_id) const override; 112 int64_t GetProcessTotalNetworkUsage(TaskId task_id) const override; 113 int64_t GetCumulativeProcessTotalNetworkUsage(TaskId task_id) const override; 114 int64_t GetSqliteMemoryUsed(TaskId task_id) const override; 115 bool GetV8Memory(TaskId task_id, 116 int64_t* allocated, 117 int64_t* used) const override; 118 bool GetWebCacheStats(TaskId task_id, 119 blink::WebCacheResourceTypeStats* stats) const override; 120 int GetKeepaliveCount(TaskId task_id) const override; 121 const TaskIdList& GetTaskIdsList() const override; 122 TaskIdList GetIdsOfTasksSharingSameProcess(TaskId task_id) const override; 123 size_t GetNumberOfTasksOnSameProcess(TaskId task_id) const override; 124 bool IsRunningInVM(TaskId task_id) const override; 125 TaskId GetTaskIdForWebContents( 126 content::WebContents* web_contents) const override; 127 128 // task_manager::TaskProviderObserver: 129 void TaskAdded(Task* task) override; 130 void TaskRemoved(Task* task) override; 131 void TaskUnresponsive(Task* task) override; 132 133 // Used when Network Service is enabled. 134 // Receives total network usages from |NetworkService|. 135 void OnTotalNetworkUsages( 136 std::vector<network::mojom::NetworkUsagePtr> total_network_usages); 137 138 private: 139 using PidToTaskGroupMap = 140 std::map<base::ProcessId, std::unique_ptr<TaskGroup>>; 141 142 friend struct base::LazyInstanceTraitsBase<TaskManagerImpl>; 143 144 TaskManagerImpl(); 145 146 void OnVideoMemoryUsageStatsUpdate( 147 const gpu::VideoMemoryUsageStats& gpu_memory_stats); 148 void OnReceivedMemoryDump( 149 bool success, 150 std::unique_ptr<memory_instrumentation::GlobalMemoryDump> dump); 151 152 // task_manager::TaskManagerInterface: 153 void Refresh() override; 154 void StartUpdating() override; 155 void StopUpdating() override; 156 157 // Lookup a task by child_id and possibly route_id. 158 Task* GetTaskByRoute(int child_id, int route_id) const; 159 160 // Based on |param| the appropriate task will be updated by its network usage. 161 // Returns true if it was able to match |param| to an existing task, returns 162 // false otherwise, at which point the caller must explicitly match these 163 // bytes to the browser process by calling this method again with 164 // |param.origin_pid = 0| and |param.child_id = param.route_id = -1|. 165 bool UpdateTasksWithBytesTransferred(const BytesTransferredKey& key, 166 const BytesTransferredParam& param); 167 168 PidToTaskGroupMap* GetVmPidToTaskGroupMap(Task::Type type); 169 TaskGroup* GetTaskGroupByTaskId(TaskId task_id) const; 170 Task* GetTaskByTaskId(TaskId task_id) const; 171 172 // Called back by a TaskGroup when the resource calculations done on the 173 // background thread has completed. 174 void OnTaskGroupBackgroundCalculationsDone(); 175 176 const base::RepeatingClosure on_background_data_ready_callback_; 177 178 // Map TaskGroups by the IDs of the processes they represent. 179 PidToTaskGroupMap task_groups_by_proc_id_; 180 181 // Map ARC VM PidToTaskGroupMaps by the task type. This should be separate 182 // from the non-VM map |task_groups_by_proc_id_| as there can be conflicting 183 // PIDs. 184 PidToTaskGroupMap arc_vm_task_groups_by_proc_id_; 185 186 // Map each task by its ID to the TaskGroup on which it resides. 187 // Keys are unique but values will have duplicates (i.e. multiple tasks 188 // running on the same process represented by a single TaskGroup). 189 std::map<TaskId, TaskGroup*> task_groups_by_task_id_; 190 191 // A cached sorted list of the task IDs. 192 mutable std::vector<TaskId> sorted_task_ids_; 193 194 // Used when Network Service is enabled. 195 // Stores the total network usages per |process_id, routing_id| from last 196 // refresh. 197 BytesTransferredMap last_refresh_total_network_usages_map_; 198 199 // The list of the task providers that are owned and observed by this task 200 // manager implementation. 201 std::vector<std::unique_ptr<TaskProvider>> task_providers_; 202 203 // The current GPU memory usage stats that was last received from the 204 // GpuDataManager. 205 gpu::VideoMemoryUsageStats gpu_memory_stats_; 206 207 // The specific blocking pool SequencedTaskRunner that will be used to make 208 // sure TaskGroupSampler posts their refreshes serially. 209 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_; 210 211 // A special sampler shared with all instances of TaskGroup that calculates a 212 // subset of resources for all processes at once. 213 scoped_refptr<SharedSampler> shared_sampler_; 214 215 #if defined(OS_CHROMEOS) 216 // A sampler shared with all instances of TaskGroup that hold ARC tasks and 217 // calculates memory footprint for all processes at once. 218 std::unique_ptr<ArcSharedSampler> arc_shared_sampler_; 219 #endif // defined(OS_CHROMEOS) 220 221 // This will be set to true while there are observers and the task manager is 222 // running. 223 bool is_running_; 224 225 // This is set to true while waiting for a global memory dump from 226 // memory_instrumentation. 227 bool waiting_for_memory_dump_; 228 229 base::WeakPtrFactory<TaskManagerImpl> weak_ptr_factory_{this}; 230 DISALLOW_COPY_AND_ASSIGN(TaskManagerImpl); 231 }; 232 233 } // namespace task_manager 234 235 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_TASK_MANAGER_IMPL_H_ 236