[email protected] | 8291681 | 2012-02-21 04:14:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | a5d1e1e | 2010-09-23 19:34:12 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 8 | #include <map> |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/memory/singleton.h" |
[email protected] | a754453c | 2009-07-15 20:32:51 | [diff] [blame] | 15 | #include "base/observer_list.h" |
[email protected] | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 16 | #include "base/process_util.h" |
[email protected] | 9f0abdb | 2013-06-10 21:49:34 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame^] | 18 | #include "base/timer/timer.h" |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 19 | #include "chrome/browser/renderer_host/web_cache_manager.h" |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 20 | #include "chrome/browser/task_manager/resource_provider.h" |
[email protected] | ddeb0e843 | 2012-12-21 07:27:54 | [diff] [blame] | 21 | #include "chrome/browser/ui/host_desktop.h" |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 22 | #include "content/public/common/gpu_memory_stats.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 23 | #include "third_party/WebKit/public/web/WebCache.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 25 | class PrefRegistrySimple; |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 26 | class TaskManagerModel; |
[email protected] | 2344ca5 | 2012-08-22 22:46:05 | [diff] [blame] | 27 | class TaskManagerModelGpuDataManagerObserver; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 29 | namespace base { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | class ProcessMetrics; |
| 31 | } |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 32 | |
[email protected] | 7ed0e8c | 2012-07-19 23:17:06 | [diff] [blame] | 33 | namespace content { |
| 34 | class WebContents; |
| 35 | } |
| 36 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 37 | namespace extensions { |
| 38 | class Extension; |
| 39 | } |
| 40 | |
[email protected] | 6280b588 | 2012-06-05 21:56:41 | [diff] [blame] | 41 | namespace gfx { |
| 42 | class ImageSkia; |
| 43 | } |
| 44 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 45 | namespace net { |
| 46 | class URLRequest; |
| 47 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | // This class is a singleton. |
[email protected] | f9fdb07 | 2009-05-06 07:27:40 | [diff] [blame] | 50 | class TaskManager { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | public: |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 52 | static void RegisterPrefs(PrefRegistrySimple* registry); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
[email protected] | 8f90afd7 | 2009-06-22 22:44:38 | [diff] [blame] | 54 | // Returns true if the process at the specified index is the browser process. |
| 55 | bool IsBrowserProcess(int index) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
[email protected] | 8f90afd7 | 2009-06-22 22:44:38 | [diff] [blame] | 57 | // Terminates the process at the specified index. |
| 58 | void KillProcess(int index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | |
[email protected] | 8f90afd7 | 2009-06-22 22:44:38 | [diff] [blame] | 60 | // Activates the browser tab associated with the process in the specified |
| 61 | // index. |
| 62 | void ActivateProcess(int index); |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 63 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | // These methods are invoked by the resource providers to add/remove resources |
| 65 | // to the Task Manager. Note that the resources are owned by the |
| 66 | // ResourceProviders and are not valid after StopUpdating() has been called |
| 67 | // on the ResourceProviders. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 68 | void AddResource(task_manager::Resource* resource); |
| 69 | void RemoveResource(task_manager::Resource* resource); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | 9c5e417 | 2009-05-27 08:46:28 | [diff] [blame] | 71 | void OnWindowClosed(); |
| 72 | |
[email protected] | c43207e | 2010-11-15 18:09:52 | [diff] [blame] | 73 | // Invoked when a change to a resource has occurred that should cause any |
| 74 | // observers to completely refresh themselves (for example, the creation of |
| 75 | // a background resource in a process). Results in all observers receiving |
| 76 | // OnModelChanged() events. |
| 77 | void ModelChanged(); |
| 78 | |
[email protected] | 8f90afd7 | 2009-06-22 22:44:38 | [diff] [blame] | 79 | // Returns the singleton instance (and initializes it if necessary). |
| 80 | static TaskManager* GetInstance(); |
| 81 | |
| 82 | TaskManagerModel* model() const { return model_.get(); } |
| 83 | |
[email protected] | ddeb0e843 | 2012-12-21 07:27:54 | [diff] [blame] | 84 | void OpenAboutMemory(chrome::HostDesktopType desktop_type); |
[email protected] | 8f0d1a2 | 2009-10-08 19:55:14 | [diff] [blame] | 85 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | private: |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 87 | FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Basic); |
| 88 | FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Resources); |
| 89 | FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, RefreshCalled); |
[email protected] | 5ec892c | 2010-07-29 20:19:49 | [diff] [blame] | 90 | FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, Init); |
| 91 | FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, Sort); |
[email protected] | c1b7d8cca | 2010-08-07 23:07:58 | [diff] [blame] | 92 | FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, |
| 93 | SelectionAdaptsToSorting); |
[email protected] | 1222cce | 2009-04-27 06:58:27 | [diff] [blame] | 94 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | // Obtain an instance via GetInstance(). |
| 96 | TaskManager(); |
[email protected] | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 97 | friend struct DefaultSingletonTraits<TaskManager>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
| 99 | ~TaskManager(); |
| 100 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 101 | // The model used for gathering and processing task data. It is ref counted |
| 102 | // because it is passed as a parameter to MessageLoop::InvokeLater(). |
| 103 | scoped_refptr<TaskManagerModel> model_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 105 | DISALLOW_COPY_AND_ASSIGN(TaskManager); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | }; |
| 107 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 108 | class TaskManagerModelObserver { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | public: |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 110 | virtual ~TaskManagerModelObserver() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 112 | // Invoked when the model has been completely changed. |
| 113 | virtual void OnModelChanged() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 115 | // Invoked when a range of items has changed. |
| 116 | virtual void OnItemsChanged(int start, int length) = 0; |
| 117 | |
| 118 | // Invoked when new items are added. |
| 119 | virtual void OnItemsAdded(int start, int length) = 0; |
| 120 | |
| 121 | // Invoked when a range of items has been removed. |
| 122 | virtual void OnItemsRemoved(int start, int length) = 0; |
[email protected] | 8291681 | 2012-02-21 04:14:17 | [diff] [blame] | 123 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 124 | // Invoked when a range of items is to be immediately removed. It differs |
| 125 | // from OnItemsRemoved by the fact that the item is still in the task manager, |
| 126 | // so it can be queried for and found. |
| 127 | virtual void OnItemsToBeRemoved(int start, int length) {} |
| 128 | |
[email protected] | 8291681 | 2012-02-21 04:14:17 | [diff] [blame] | 129 | // Invoked when the initialization of the model has been finished and |
| 130 | // periodical updates is started. The first periodical update will be done |
| 131 | // in a few seconds. (depending on platform) |
| 132 | virtual void OnReadyPeriodicalUpdate() {} |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 133 | }; |
| 134 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 135 | // The model used by TaskManager. |
| 136 | // |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 137 | // TaskManagerModel caches the values from all task_manager::Resources. This is |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 138 | // done so the UI sees a consistant view of the resources until it is told a |
| 139 | // value has been updated. |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 140 | class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> { |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 141 | public: |
[email protected] | abd81a52 | 2012-03-02 16:12:45 | [diff] [blame] | 142 | // (start, length) |
| 143 | typedef std::pair<int, int> GroupRange; |
| 144 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 145 | explicit TaskManagerModel(TaskManager* task_manager); |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 146 | |
[email protected] | a754453c | 2009-07-15 20:32:51 | [diff] [blame] | 147 | void AddObserver(TaskManagerModelObserver* observer); |
| 148 | void RemoveObserver(TaskManagerModelObserver* observer); |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 149 | |
| 150 | // Returns number of registered resources. |
| 151 | int ResourceCount() const; |
[email protected] | 9f5d3e18 | 2011-07-11 06:22:29 | [diff] [blame] | 152 | // Returns number of registered groups. |
| 153 | int GroupCount() const; |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 154 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 155 | // Methods to return raw resource information. |
| 156 | int64 GetNetworkUsage(int index) const; |
| 157 | double GetCPUUsage(int index) const; |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 158 | base::ProcessId GetProcessId(int index) const; |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 159 | base::ProcessHandle GetProcess(int index) const; |
[email protected] | ef72ae0 | 2011-10-28 16:28:59 | [diff] [blame] | 160 | int GetResourceUniqueId(int index) const; |
[email protected] | 28989c3 | 2011-12-14 19:41:36 | [diff] [blame] | 161 | // Returns the index of resource that has the given |unique_id|. Returns -1 if |
| 162 | // no resouce has the |unique_id|. |
| 163 | int GetResourceIndexByUniqueId(const int unique_id) const; |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 164 | |
[email protected] | 36cdba0 | 2013-01-12 20:16:40 | [diff] [blame] | 165 | // Catchall method that calls off to the appropriate GetResourceXXX method |
| 166 | // based on |col_id|. |col_id| is an IDS_ value used to identify the column. |
| 167 | string16 GetResourceById(int index, int col_id) const; |
| 168 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 169 | // Methods to return formatted resource information. |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 170 | const string16& GetResourceTitle(int index) const; |
| 171 | const string16& GetResourceProfileName(int index) const; |
[email protected] | 5f2b0ee | 2010-08-14 14:53:43 | [diff] [blame] | 172 | string16 GetResourceNetworkUsage(int index) const; |
| 173 | string16 GetResourceCPUUsage(int index) const; |
| 174 | string16 GetResourcePrivateMemory(int index) const; |
| 175 | string16 GetResourceSharedMemory(int index) const; |
| 176 | string16 GetResourcePhysicalMemory(int index) const; |
| 177 | string16 GetResourceProcessId(int index) const; |
[email protected] | f293747 | 2013-03-18 21:48:34 | [diff] [blame] | 178 | string16 GetResourceGDIHandles(int index) const; |
| 179 | string16 GetResourceUSERHandles(int index) const; |
[email protected] | 5f2b0ee | 2010-08-14 14:53:43 | [diff] [blame] | 180 | string16 GetResourceWebCoreImageCacheSize(int index) const; |
| 181 | string16 GetResourceWebCoreScriptsCacheSize(int index) const; |
| 182 | string16 GetResourceWebCoreCSSCacheSize(int index) const; |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 183 | string16 GetResourceVideoMemory(int index) const; |
[email protected] | 872ae5b | 2011-05-26 20:20:50 | [diff] [blame] | 184 | string16 GetResourceFPS(int index) const; |
[email protected] | 5f2b0ee | 2010-08-14 14:53:43 | [diff] [blame] | 185 | string16 GetResourceSqliteMemoryUsed(int index) const; |
| 186 | string16 GetResourceGoatsTeleported(int index) const; |
| 187 | string16 GetResourceV8MemoryAllocatedSize(int index) const; |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 188 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 189 | // Gets the private memory (in bytes) that should be displayed for the passed |
| 190 | // resource index. Caches the result since this calculation can take time on |
| 191 | // some platforms. |
| 192 | bool GetPrivateMemory(int index, size_t* result) const; |
| 193 | |
| 194 | // Gets the shared memory (in bytes) that should be displayed for the passed |
| 195 | // resource index. Caches the result since this calculation can take time on |
| 196 | // some platforms. |
| 197 | bool GetSharedMemory(int index, size_t* result) const; |
| 198 | |
| 199 | // Gets the physical memory (in bytes) that should be displayed for the passed |
| 200 | // resource index. |
| 201 | bool GetPhysicalMemory(int index, size_t* result) const; |
| 202 | |
[email protected] | f293747 | 2013-03-18 21:48:34 | [diff] [blame] | 203 | // On Windows, get the current and peak number of GDI handles in use. |
| 204 | void GetGDIHandles(int index, size_t* current, size_t* peak) const; |
| 205 | |
| 206 | // On Windows, get the current and peak number of USER handles in use. |
| 207 | void GetUSERHandles(int index, size_t* current, size_t* peak) const; |
| 208 | |
[email protected] | 5d8c0b9 | 2011-07-21 07:53:32 | [diff] [blame] | 209 | // Gets the statuses of webkit. Return false if the resource for the given row |
| 210 | // isn't a renderer. |
| 211 | bool GetWebCoreCacheStats(int index, |
| 212 | WebKit::WebCache::ResourceTypeStats* result) const; |
| 213 | |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 214 | // Gets the GPU memory allocated of the given page. |
| 215 | bool GetVideoMemory(int index, |
| 216 | size_t* video_memory, |
| 217 | bool* has_duplicates) const; |
| 218 | |
[email protected] | 5d8c0b9 | 2011-07-21 07:53:32 | [diff] [blame] | 219 | // Gets the fps of the given page. Return false if the resource for the given |
| 220 | // row isn't a renderer. |
| 221 | bool GetFPS(int index, float* result) const; |
| 222 | |
| 223 | // Gets the sqlite memory (in byte). Return false if the resource for the |
| 224 | // given row doesn't report information. |
| 225 | bool GetSqliteMemoryUsedBytes(int index, size_t* result) const; |
| 226 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 227 | // Gets the amount of memory allocated for javascript. Returns false if the |
| 228 | // resource for the given row isn't a renderer. |
| 229 | bool GetV8Memory(int index, size_t* result) const; |
| 230 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 231 | // Gets the amount of memory used for javascript. Returns false if the |
| 232 | // resource for the given row isn't a renderer. |
| 233 | bool GetV8MemoryUsed(int index, size_t* result) const; |
| 234 | |
[email protected] | 374bf59 | 2011-11-10 15:42:18 | [diff] [blame] | 235 | // Returns true if resource for the given row can be activated. |
| 236 | bool CanActivate(int index) const; |
| 237 | |
[email protected] | 0f62bb2d | 2011-10-26 14:48:33 | [diff] [blame] | 238 | // Returns true if resource for the given row can be inspected using developer |
| 239 | // tools. |
| 240 | bool CanInspect(int index) const; |
| 241 | |
| 242 | // Invokes or reveals developer tools window for resource in the given row. |
| 243 | void Inspect(int index) const; |
| 244 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 245 | // See design doc at http://go/at-teleporter for more information. |
| 246 | int GetGoatsTeleported(int index) const; |
| 247 | |
[email protected] | 9f5d3e18 | 2011-07-11 06:22:29 | [diff] [blame] | 248 | // Returns true if the resource is first/last in its group (resources |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 249 | // rendered by the same process are groupped together). |
| 250 | bool IsResourceFirstInGroup(int index) const; |
[email protected] | 9f5d3e18 | 2011-07-11 06:22:29 | [diff] [blame] | 251 | bool IsResourceLastInGroup(int index) const; |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 252 | |
[email protected] | c43207e | 2010-11-15 18:09:52 | [diff] [blame] | 253 | // Returns true if the resource runs in the background (not visible to the |
| 254 | // user, e.g. extension background pages and BackgroundContents). |
| 255 | bool IsBackgroundResource(int index) const; |
| 256 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 257 | // Returns icon to be used for resource (for example a favicon). |
[email protected] | 6280b588 | 2012-06-05 21:56:41 | [diff] [blame] | 258 | gfx::ImageSkia GetResourceIcon(int index) const; |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 259 | |
[email protected] | abd81a52 | 2012-03-02 16:12:45 | [diff] [blame] | 260 | // Returns the group range of resource. |
| 261 | GroupRange GetGroupRangeForResource(int index) const; |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 262 | |
[email protected] | 9f5d3e18 | 2011-07-11 06:22:29 | [diff] [blame] | 263 | // Returns an index of groups to which the resource belongs. |
| 264 | int GetGroupIndexForResource(int index) const; |
| 265 | |
| 266 | // Returns an index of resource which belongs to the |group_index|th group |
| 267 | // and which is the |index_in_group|th resource in group. |
| 268 | int GetResourceIndexForGroup(int group_index, int index_in_group) const; |
| 269 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 270 | // Compares values in column |col_id| and rows |row1|, |row2|. |
| 271 | // Returns -1 if value in |row1| is less than value in |row2|, |
| 272 | // 0 if they are equal, and 1 otherwise. |
| 273 | int CompareValues(int row1, int row2, int col_id) const; |
| 274 | |
[email protected] | 6bfb6e70 | 2012-04-05 17:18:22 | [diff] [blame] | 275 | // Returns the unique child process ID generated by Chromium, not the OS |
| 276 | // process id. This is used to identify processes internally and for |
| 277 | // extensions. It is not meant to be displayed to the user. |
| 278 | int GetUniqueChildProcessId(int index) const; |
| 279 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 280 | // Returns the type of the given resource. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 281 | task_manager::Resource::Type GetResourceType(int index) const; |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 282 | |
[email protected] | 7ed0e8c | 2012-07-19 23:17:06 | [diff] [blame] | 283 | // Returns WebContents of given resource or NULL if not applicable. |
| 284 | content::WebContents* GetResourceWebContents(int index) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | |
[email protected] | e172584 | 2009-10-20 06:40:15 | [diff] [blame] | 286 | // Returns Extension of given resource or NULL if not applicable. |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 287 | const extensions::Extension* GetResourceExtension(int index) const; |
[email protected] | e172584 | 2009-10-20 06:40:15 | [diff] [blame] | 288 | |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 289 | void AddResource(task_manager::Resource* resource); |
| 290 | void RemoveResource(task_manager::Resource* resource); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 291 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 292 | void StartUpdating(); |
| 293 | void StopUpdating(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 295 | // Listening involves calling StartUpdating on all resource providers. This |
| 296 | // causes all of them to subscribe to notifications and enumerate current |
| 297 | // resources. It differs from StartUpdating that it doesn't start the |
| 298 | // Refresh timer. The end result is that we have a full view of resources, but |
| 299 | // don't spend unneeded time updating, unless we have a real need to. |
| 300 | void StartListening(); |
| 301 | void StopListening(); |
| 302 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 303 | void Clear(); // Removes all items. |
| 304 | |
[email protected] | c43207e | 2010-11-15 18:09:52 | [diff] [blame] | 305 | // Sends OnModelChanged() to all observers to inform them of significant |
| 306 | // changes to the model. |
| 307 | void ModelChanged(); |
| 308 | |
[email protected] | ec977368 | 2009-09-25 17:59:03 | [diff] [blame] | 309 | void NotifyResourceTypeStats( |
[email protected] | 38b48a8 | 2009-11-11 01:51:32 | [diff] [blame] | 310 | base::ProcessId renderer_id, |
[email protected] | ec977368 | 2009-09-25 17:59:03 | [diff] [blame] | 311 | const WebKit::WebCache::ResourceTypeStats& stats); |
| 312 | |
[email protected] | 872ae5b | 2011-05-26 20:20:50 | [diff] [blame] | 313 | void NotifyFPS(base::ProcessId renderer_id, |
| 314 | int routing_id, |
| 315 | float fps); |
| 316 | |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 317 | void NotifyVideoMemoryUsageStats( |
| 318 | const content::GPUVideoMemoryUsageStats& video_memory_usage_stats); |
| 319 | |
[email protected] | 38b48a8 | 2009-11-11 01:51:32 | [diff] [blame] | 320 | void NotifyV8HeapStats(base::ProcessId renderer_id, |
| 321 | size_t v8_memory_allocated, |
| 322 | size_t v8_memory_used); |
| 323 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 324 | void NotifyBytesRead(const net::URLRequest& request, int bytes_read); |
| 325 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 326 | private: |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 327 | friend class base::RefCountedThreadSafe<TaskManagerModel>; |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 328 | friend class TaskManagerBrowserTest; |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 329 | FRIEND_TEST_ALL_PREFIXES(ExtensionApiTest, ProcessesVsTaskManager); |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 330 | FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, RefreshCalled); |
| 331 | FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, |
| 332 | SelectionAdaptsToSorting); |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 333 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 334 | enum UpdateState { |
| 335 | IDLE = 0, // Currently not updating. |
| 336 | TASK_PENDING, // An update task is pending. |
| 337 | STOPPING // A update task is pending and it should stop the update. |
| 338 | }; |
| 339 | |
[email protected] | a9bd323d | 2013-06-17 20:27:56 | [diff] [blame] | 340 | // The delay between updates of the information (in ms). |
| 341 | #if defined(OS_MACOSX) |
| 342 | // Match Activity Monitor's default refresh rate. |
| 343 | static const int kUpdateTimeMs = 2000; |
| 344 | #else |
| 345 | static const int kUpdateTimeMs = 1000; |
| 346 | #endif |
| 347 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 348 | // Values cached per resource. Values are validated on demand. The is_XXX |
| 349 | // members indicate if a value is valid. |
| 350 | struct PerResourceValues { |
| 351 | PerResourceValues(); |
| 352 | ~PerResourceValues(); |
| 353 | |
| 354 | bool is_title_valid; |
| 355 | string16 title; |
| 356 | |
| 357 | bool is_profile_name_valid; |
| 358 | string16 profile_name; |
| 359 | |
| 360 | // No is_network_usage since default (0) is fine. |
| 361 | int64 network_usage; |
| 362 | |
| 363 | bool is_process_id_valid; |
| 364 | base::ProcessId process_id; |
| 365 | |
| 366 | bool is_goats_teleported_valid; |
| 367 | int goats_teleported; |
| 368 | |
| 369 | bool is_webcore_stats_valid; |
| 370 | WebKit::WebCache::ResourceTypeStats webcore_stats; |
| 371 | |
| 372 | bool is_fps_valid; |
| 373 | float fps; |
| 374 | |
| 375 | bool is_sqlite_memory_bytes_valid; |
| 376 | size_t sqlite_memory_bytes; |
| 377 | |
| 378 | bool is_v8_memory_valid; |
| 379 | size_t v8_memory_allocated; |
| 380 | size_t v8_memory_used; |
| 381 | }; |
| 382 | |
| 383 | // Values cached per process. Values are validated on demand. The is_XXX |
| 384 | // members indicate if a value is valid. |
| 385 | struct PerProcessValues { |
| 386 | PerProcessValues(); |
| 387 | ~PerProcessValues(); |
| 388 | |
| 389 | bool is_cpu_usage_valid; |
| 390 | double cpu_usage; |
| 391 | |
| 392 | bool is_private_and_shared_valid; |
| 393 | size_t private_bytes; |
| 394 | size_t shared_bytes; |
| 395 | |
| 396 | bool is_physical_memory_valid; |
| 397 | size_t physical_memory; |
| 398 | |
| 399 | bool is_video_memory_valid; |
| 400 | size_t video_memory; |
| 401 | bool video_memory_has_duplicates; |
[email protected] | f293747 | 2013-03-18 21:48:34 | [diff] [blame] | 402 | |
| 403 | bool is_gdi_handles_valid; |
| 404 | size_t gdi_handles; |
| 405 | size_t gdi_handles_peak; |
| 406 | |
| 407 | bool is_user_handles_valid; |
| 408 | size_t user_handles; |
| 409 | size_t user_handles_peak; |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 410 | }; |
| 411 | |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 412 | typedef std::vector<task_manager::Resource*> ResourceList; |
| 413 | typedef std::vector<scoped_refptr<task_manager::ResourceProvider> > |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 414 | ResourceProviderList; |
| 415 | typedef std::map<base::ProcessHandle, ResourceList*> GroupMap; |
| 416 | typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap; |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 417 | typedef std::map<task_manager::Resource*, int64> ResourceValueMap; |
| 418 | typedef std::map<task_manager::Resource*, |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 419 | PerResourceValues> PerResourceCache; |
| 420 | typedef std::map<base::ProcessHandle, PerProcessValues> PerProcessCache; |
| 421 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 422 | // This struct is used to exchange information between the io and ui threads. |
| 423 | struct BytesReadParam { |
[email protected] | cdf682e | 2011-02-05 05:11:28 | [diff] [blame] | 424 | BytesReadParam(int origin_pid, |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 425 | int render_process_host_child_id, |
| 426 | int routing_id, |
| 427 | int byte_count) |
[email protected] | cdf682e | 2011-02-05 05:11:28 | [diff] [blame] | 428 | : origin_pid(origin_pid), |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 429 | render_process_host_child_id(render_process_host_child_id), |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 430 | routing_id(routing_id), |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 431 | byte_count(byte_count) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 432 | |
[email protected] | cdf682e | 2011-02-05 05:11:28 | [diff] [blame] | 433 | // The process ID that triggered the request. For plugin requests this |
| 434 | // will differ from the renderer process ID. |
| 435 | int origin_pid; |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 436 | |
[email protected] | cdf682e | 2011-02-05 05:11:28 | [diff] [blame] | 437 | // The child ID of the RenderProcessHost this request was routed through. |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 438 | int render_process_host_child_id; |
| 439 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | int routing_id; |
| 441 | int byte_count; |
| 442 | }; |
| 443 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 444 | ~TaskManagerModel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 446 | // Callback from the timer to refresh. Invokes Refresh() as appropriate. |
| 447 | void RefreshCallback(); |
| 448 | |
| 449 | // Updates the values for all rows. |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 450 | void Refresh(); |
| 451 | |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 452 | void RefreshVideoMemoryUsageStats(); |
| 453 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | // Returns the network usage (in bytes per seconds) for the specified |
| 455 | // resource. That's the value retrieved at the last timer's tick. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 456 | int64 GetNetworkUsageForResource(task_manager::Resource* resource) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | // Called on the UI thread when some bytes are read. |
| 459 | void BytesRead(BytesReadParam param); |
| 460 | |
[email protected] | f173d15 | 2012-10-17 21:02:41 | [diff] [blame] | 461 | void MultipleBytesRead(const std::vector<BytesReadParam>* params); |
| 462 | |
| 463 | // Notifies the UI thread about all the bytes read. Allows for coalescing |
| 464 | // multiple bytes read into a single task for the UI thread. This is important |
| 465 | // for when downloading a lot of data on the IO thread, since posting a Task |
| 466 | // for each one is expensive. |
| 467 | void NotifyMultipleBytesRead(); |
| 468 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 469 | // Returns the network usage (in byte per second) that should be displayed for |
| 470 | // the passed |resource|. -1 means the information is not available for that |
| 471 | // resource. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 472 | int64 GetNetworkUsage(task_manager::Resource* resource) const; |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 473 | |
| 474 | // Returns the CPU usage (in %) that should be displayed for the passed |
| 475 | // |resource|. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 476 | double GetCPUUsage(task_manager::Resource* resource) const; |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 477 | |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 478 | // Given a number, this function returns the formatted string that should be |
| 479 | // displayed in the task manager's memory cell. |
[email protected] | 5f2b0ee | 2010-08-14 14:53:43 | [diff] [blame] | 480 | string16 GetMemCellText(int64 number) const; |
[email protected] | 2a5af8a | 2009-01-23 23:13:05 | [diff] [blame] | 481 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 482 | // Verifies the private and shared memory for |handle| is valid in |
| 483 | // |per_process_cache_|. Returns true if the data in |per_process_cache_| is |
| 484 | // valid. |
| 485 | bool CachePrivateAndSharedMemory(base::ProcessHandle handle) const; |
| 486 | |
| 487 | // Verifies |webcore_stats| in |per_resource_cache_|, returning true on |
| 488 | // success. |
| 489 | bool CacheWebCoreStats(int index) const; |
| 490 | |
| 491 | // Verifies |v8_memory_allocated| and |v8_memory_used| in |
| 492 | // |per_resource_cache_|. Returns true if valid, false if not valid. |
| 493 | bool CacheV8Memory(int index) const; |
[email protected] | 98947a0 | 2010-05-11 17:46:08 | [diff] [blame] | 494 | |
[email protected] | 0f4ffe84 | 2011-03-30 18:59:46 | [diff] [blame] | 495 | // Adds a resource provider to be managed. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 496 | void AddResourceProvider(task_manager::ResourceProvider* provider); |
[email protected] | 0f4ffe84 | 2011-03-30 18:59:46 | [diff] [blame] | 497 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 498 | // Returns the PerResourceValues for the specified index. |
| 499 | PerResourceValues& GetPerResourceValues(int index) const; |
| 500 | |
| 501 | // Returns the Resource for the specified index. |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 502 | task_manager::Resource* GetResource(int index) const; |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 503 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | // The list of providers to the task manager. They are ref counted. |
| 505 | ResourceProviderList providers_; |
| 506 | |
| 507 | // The list of all the resources displayed in the task manager. They are owned |
| 508 | // by the ResourceProviders. |
| 509 | ResourceList resources_; |
| 510 | |
| 511 | // A map to keep tracks of the grouped resources (they are grouped if they |
| 512 | // share the same process). The groups (the Resources vectors) are owned by |
| 513 | // the model (but the actual Resources are owned by the ResourceProviders). |
| 514 | GroupMap group_map_; |
| 515 | |
| 516 | // A map to retrieve the process metrics for a process. The ProcessMetrics are |
| 517 | // owned by the model. |
| 518 | MetricsMap metrics_map_; |
| 519 | |
| 520 | // A map that keeps track of the number of bytes read per process since last |
| 521 | // tick. The Resources are owned by the ResourceProviders. |
| 522 | ResourceValueMap current_byte_count_map_; |
| 523 | |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 524 | // A map that contains the video memory usage for a process |
| 525 | content::GPUVideoMemoryUsageStats video_memory_usage_stats_; |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 526 | |
| 527 | // Set to true when we've requested video stats and false once we get them. |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 528 | bool pending_video_memory_usage_stats_update_; |
| 529 | |
[email protected] | 2344ca5 | 2012-08-22 22:46:05 | [diff] [blame] | 530 | // An observer waiting for video memory usage stats updates from the GPU |
| 531 | // process |
| 532 | scoped_ptr<TaskManagerModelGpuDataManagerObserver> |
| 533 | video_memory_usage_stats_observer_; |
| 534 | |
[email protected] | a754453c | 2009-07-15 20:32:51 | [diff] [blame] | 535 | ObserverList<TaskManagerModelObserver> observer_list_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 536 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 537 | // How many calls to StartUpdating have been made without matching calls to |
| 538 | // StopUpdating. |
| 539 | int update_requests_; |
| 540 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 541 | // How many calls to StartListening have been made without matching calls to |
| 542 | // StopListening. |
| 543 | int listen_requests_; |
| 544 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame] | 545 | // Whether we are currently in the process of updating. |
| 546 | UpdateState update_state_; |
| 547 | |
[email protected] | 96373d0 | 2010-04-21 01:16:14 | [diff] [blame] | 548 | // A salt lick for the goats. |
[email protected] | abd81a52 | 2012-03-02 16:12:45 | [diff] [blame] | 549 | uint64 goat_salt_; |
[email protected] | 96373d0 | 2010-04-21 01:16:14 | [diff] [blame] | 550 | |
[email protected] | ef72ae0 | 2011-10-28 16:28:59 | [diff] [blame] | 551 | // Resource identifier that is unique within single session. |
| 552 | int last_unique_id_; |
| 553 | |
[email protected] | f173d15 | 2012-10-17 21:02:41 | [diff] [blame] | 554 | // Buffer for coalescing BytesReadParam so we don't have to post a task on |
| 555 | // each NotifyBytesRead() call. |
| 556 | std::vector<BytesReadParam> bytes_read_buffer_; |
| 557 | |
[email protected] | b6eaebd7 | 2013-01-15 23:04:09 | [diff] [blame] | 558 | // All per-Resource values are stored here. |
| 559 | mutable PerResourceCache per_resource_cache_; |
| 560 | |
| 561 | // All per-Process values are stored here. |
| 562 | mutable PerProcessCache per_process_cache_; |
| 563 | |
[email protected] | 0860b9b9 | 2009-05-11 18:49:18 | [diff] [blame] | 564 | DISALLOW_COPY_AND_ASSIGN(TaskManagerModel); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 565 | }; |
| 566 | |
[email protected] | a5d1e1e | 2010-09-23 19:34:12 | [diff] [blame] | 567 | #endif // CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_ |