blob: 2c76a7dcb6c68833573235f245ee18d3620fff18 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
[email protected]bfac5ac2008-10-09 17:55:385#ifndef CHROME_BROWSER_TASK_MANAGER_H_
6#define CHROME_BROWSER_TASK_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:297
[email protected]b7937d5b2008-09-10 22:12:198#include <map>
[email protected]bfac5ac2008-10-09 17:55:389#include <string>
[email protected]0860b9b92009-05-11 18:49:1810#include <utility>
[email protected]b7937d5b2008-09-10 22:12:1911#include <vector>
12
[email protected]5d438dbad2009-04-30 08:59:3913#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/lock.h"
[email protected]a754453c2009-07-15 20:32:5115#include "base/observer_list.h"
[email protected]5d438dbad2009-04-30 08:59:3916#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/ref_counted.h"
[email protected]5d438dbad2009-04-30 08:59:3918#include "base/singleton.h"
[email protected]2d316662008-09-03 18:18:1419#include "base/timer.h"
[email protected]2c434b32009-03-19 06:27:4720#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]f3ec7742009-01-15 00:59:1621#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:2922#include "net/url_request/url_request_job_tracker.h"
[email protected]1222cce2009-04-27 06:58:2723#include "testing/gtest/include/gtest/gtest_prod.h"
[email protected]ec9773682009-09-25 17:59:0324#include "webkit/api/public/WebCache.h"
initial.commit09911bf2008-07-26 23:55:2925
[email protected]e1725842009-10-20 06:40:1526class Extension;
initial.commit09911bf2008-07-26 23:55:2927class SkBitmap;
initial.commit09911bf2008-07-26 23:55:2928class TaskManager;
[email protected]0860b9b92009-05-11 18:49:1829class TaskManagerModel;
initial.commit09911bf2008-07-26 23:55:2930
31struct BytesReadParam;
32
[email protected]176aa482008-11-14 03:25:1533namespace base {
initial.commit09911bf2008-07-26 23:55:2934class ProcessMetrics;
35}
36
37// This class is a singleton.
[email protected]f9fdb072009-05-06 07:27:4038class TaskManager {
initial.commit09911bf2008-07-26 23:55:2939 public:
40 // A resource represents one row in the task manager.
41 // Resources from similar processes are grouped together by the task manager.
42 class Resource {
43 public:
[email protected]b7937d5b2008-09-10 22:12:1944 virtual ~Resource() {}
45
initial.commit09911bf2008-07-26 23:55:2946 virtual std::wstring GetTitle() const = 0;
47 virtual SkBitmap GetIcon() const = 0;
[email protected]5d438dbad2009-04-30 08:59:3948 virtual base::ProcessHandle GetProcess() const = 0;
initial.commit09911bf2008-07-26 23:55:2949
[email protected]149fd6df2009-09-30 18:28:4450 virtual bool ReportsCacheStats() const { return false; }
51 virtual WebKit::WebCache::ResourceTypeStats GetWebCoreCacheStats() const {
52 return WebKit::WebCache::ResourceTypeStats();
53 }
[email protected]ec9773682009-09-25 17:59:0354
[email protected]f5451062009-10-15 20:37:2455 virtual bool ReportsSqliteMemoryUsed() const { return false; }
56 virtual size_t SqliteMemoryUsedBytes() const { return 0; }
57
[email protected]e1725842009-10-20 06:40:1558 // Return extension associated with the resource, or NULL
59 // if not applicable.
60 virtual const Extension* GetExtension() const { return NULL; }
61
[email protected]38b48a82009-11-11 01:51:3262 virtual bool ReportsV8MemoryStats() const { return false; }
63 virtual size_t GetV8MemoryAllocated() const { return 0; }
64 virtual size_t GetV8MemoryUsed() const { return 0; }
65
[email protected]b7937d5b2008-09-10 22:12:1966 // A helper function for ActivateFocusedTab. Returns NULL by default
[email protected]e1725842009-10-20 06:40:1567 // because not all resources have an associated tab.
[email protected]149fd6df2009-09-30 18:28:4468 virtual TabContents* GetTabContents() const { return NULL; }
[email protected]b7937d5b2008-09-10 22:12:1969
initial.commit09911bf2008-07-26 23:55:2970 // Whether this resource does report the network usage accurately.
71 // This controls whether 0 or N/A is displayed when no bytes have been
72 // reported as being read. This is because some plugins do not report the
73 // bytes read and we don't want to display a misleading 0 value in that
74 // case.
75 virtual bool SupportNetworkUsage() const = 0;
76
77 // Called when some bytes have been read and support_network_usage returns
[email protected]bfac5ac2008-10-09 17:55:3878 // false (meaning we do have network usage support).
initial.commit09911bf2008-07-26 23:55:2979 virtual void SetSupportNetworkUsage() = 0;
[email protected]ec9773682009-09-25 17:59:0380
[email protected]d277d432009-10-13 17:27:0181 // The TaskManagerModel periodically refreshes its data and call this
82 // on all live resources.
83 virtual void Refresh() {}
84
[email protected]ec9773682009-09-25 17:59:0385 virtual void NotifyResourceTypeStats(
86 const WebKit::WebCache::ResourceTypeStats& stats) {}
[email protected]38b48a82009-11-11 01:51:3287 virtual void NotifyV8HeapStats(size_t v8_memory_allocated,
88 size_t v8_memory_used) {}
initial.commit09911bf2008-07-26 23:55:2989 };
90
91 // ResourceProviders are responsible for adding/removing resources to the task
92 // manager. The task manager notifies the ResourceProvider that it is ready
93 // to receive resource creation/termination notifications with a call to
94 // StartUpdating(). At that point, the resource provider should call
95 // AddResource with all the existing resources, and after that it should call
96 // AddResource/RemoveResource as resources are created/terminated.
97 // The provider remains the owner of the resource objects and is responsible
98 // for deleting them (when StopUpdating() is called).
99 // After StopUpdating() is called the provider should also stop reporting
100 // notifications to the task manager.
101 // Note: ResourceProviders have to be ref counted as they are used in
102 // MessageLoop::InvokeLater().
[email protected]4090a282009-09-29 22:20:49103 class ResourceProvider : public base::RefCountedThreadSafe<ResourceProvider> {
initial.commit09911bf2008-07-26 23:55:29104 public:
initial.commit09911bf2008-07-26 23:55:29105 // Should return the resource associated to the specified ids, or NULL if
106 // the resource does not belong to this provider.
107 virtual TaskManager::Resource* GetResource(int process_id,
108 int render_process_host_id,
109 int routing_id) = 0;
110 virtual void StartUpdating() = 0;
111 virtual void StopUpdating() = 0;
[email protected]e6e6ba42009-11-07 01:56:19112
113 protected:
114 friend class base::RefCountedThreadSafe<ResourceProvider>;
115
116 virtual ~ResourceProvider() {}
initial.commit09911bf2008-07-26 23:55:29117 };
118
119 static void RegisterPrefs(PrefService* prefs);
120
[email protected]8f90afd72009-06-22 22:44:38121 // Returns true if the process at the specified index is the browser process.
122 bool IsBrowserProcess(int index) const;
initial.commit09911bf2008-07-26 23:55:29123
[email protected]8f90afd72009-06-22 22:44:38124 // Terminates the process at the specified index.
125 void KillProcess(int index);
initial.commit09911bf2008-07-26 23:55:29126
[email protected]8f90afd72009-06-22 22:44:38127 // Activates the browser tab associated with the process in the specified
128 // index.
129 void ActivateProcess(int index);
[email protected]b7937d5b2008-09-10 22:12:19130
initial.commit09911bf2008-07-26 23:55:29131 void AddResourceProvider(ResourceProvider* provider);
132 void RemoveResourceProvider(ResourceProvider* provider);
133
134 // These methods are invoked by the resource providers to add/remove resources
135 // to the Task Manager. Note that the resources are owned by the
136 // ResourceProviders and are not valid after StopUpdating() has been called
137 // on the ResourceProviders.
138 void AddResource(Resource* resource);
139 void RemoveResource(Resource* resource);
140
[email protected]9c5e4172009-05-27 08:46:28141 void OnWindowClosed();
142
[email protected]8f90afd72009-06-22 22:44:38143 // Returns the singleton instance (and initializes it if necessary).
144 static TaskManager* GetInstance();
145
146 TaskManagerModel* model() const { return model_.get(); }
147
[email protected]8f0d1a22009-10-08 19:55:14148 void OpenAboutMemory();
149
initial.commit09911bf2008-07-26 23:55:29150 private:
[email protected]4327e9a2009-06-04 22:05:44151 FRIEND_TEST(TaskManagerTest, Basic);
152 FRIEND_TEST(TaskManagerTest, Resources);
[email protected]d277d432009-10-13 17:27:01153 FRIEND_TEST(TaskManagerTest, RefreshCalled);
[email protected]1222cce2009-04-27 06:58:27154
initial.commit09911bf2008-07-26 23:55:29155 // Obtain an instance via GetInstance().
156 TaskManager();
[email protected]5d438dbad2009-04-30 08:59:39157 friend struct DefaultSingletonTraits<TaskManager>;
initial.commit09911bf2008-07-26 23:55:29158
159 ~TaskManager();
160
[email protected]0860b9b92009-05-11 18:49:18161 // The model used for gathering and processing task data. It is ref counted
162 // because it is passed as a parameter to MessageLoop::InvokeLater().
163 scoped_refptr<TaskManagerModel> model_;
initial.commit09911bf2008-07-26 23:55:29164
[email protected]bfac5ac2008-10-09 17:55:38165 DISALLOW_COPY_AND_ASSIGN(TaskManager);
initial.commit09911bf2008-07-26 23:55:29166};
167
[email protected]0860b9b92009-05-11 18:49:18168class TaskManagerModelObserver {
initial.commit09911bf2008-07-26 23:55:29169 public:
[email protected]0860b9b92009-05-11 18:49:18170 virtual ~TaskManagerModelObserver() {}
initial.commit09911bf2008-07-26 23:55:29171
[email protected]0860b9b92009-05-11 18:49:18172 // Invoked when the model has been completely changed.
173 virtual void OnModelChanged() = 0;
initial.commit09911bf2008-07-26 23:55:29174
[email protected]0860b9b92009-05-11 18:49:18175 // Invoked when a range of items has changed.
176 virtual void OnItemsChanged(int start, int length) = 0;
177
178 // Invoked when new items are added.
179 virtual void OnItemsAdded(int start, int length) = 0;
180
181 // Invoked when a range of items has been removed.
182 virtual void OnItemsRemoved(int start, int length) = 0;
183};
184
185// The model that the TaskManager is using.
186class TaskManagerModel : public URLRequestJobTracker::JobObserver,
[email protected]4090a282009-09-29 22:20:49187 public base::RefCountedThreadSafe<TaskManagerModel> {
[email protected]0860b9b92009-05-11 18:49:18188 public:
189 explicit TaskManagerModel(TaskManager* task_manager);
[email protected]0860b9b92009-05-11 18:49:18190
[email protected]a754453c2009-07-15 20:32:51191 void AddObserver(TaskManagerModelObserver* observer);
192 void RemoveObserver(TaskManagerModelObserver* observer);
[email protected]0860b9b92009-05-11 18:49:18193
194 // Returns number of registered resources.
195 int ResourceCount() const;
196
197 // Methods to return formatted resource information.
198 std::wstring GetResourceTitle(int index) const;
199 std::wstring GetResourceNetworkUsage(int index) const;
200 std::wstring GetResourceCPUUsage(int index) const;
201 std::wstring GetResourcePrivateMemory(int index) const;
202 std::wstring GetResourceSharedMemory(int index) const;
203 std::wstring GetResourcePhysicalMemory(int index) const;
204 std::wstring GetResourceProcessId(int index) const;
205 std::wstring GetResourceStatsValue(int index, int col_id) const;
[email protected]ec9773682009-09-25 17:59:03206 std::wstring GetResourceWebCoreImageCacheSize(int index) const;
207 std::wstring GetResourceWebCoreScriptsCacheSize(int index) const;
208 std::wstring GetResourceWebCoreCSSCacheSize(int index) const;
[email protected]f5451062009-10-15 20:37:24209 std::wstring GetResourceSqliteMemoryUsed(int index) const;
[email protected]0860b9b92009-05-11 18:49:18210 std::wstring GetResourceGoatsTeleported(int index) const;
[email protected]38b48a82009-11-11 01:51:32211 std::wstring GetResourceV8MemoryAllocatedSize(int index) const;
[email protected]0860b9b92009-05-11 18:49:18212
213 // Returns true if the resource is first in its group (resources
214 // rendered by the same process are groupped together).
215 bool IsResourceFirstInGroup(int index) const;
216
217 // Returns icon to be used for resource (for example a favicon).
218 SkBitmap GetResourceIcon(int index) const;
219
220 // Returns a pair (start, length) of the group range of resource.
221 std::pair<int, int> GetGroupRangeForResource(int index) const;
222
223 // Compares values in column |col_id| and rows |row1|, |row2|.
224 // Returns -1 if value in |row1| is less than value in |row2|,
225 // 0 if they are equal, and 1 otherwise.
226 int CompareValues(int row1, int row2, int col_id) const;
227
228 // Returns process handle for given resource.
229 base::ProcessHandle GetResourceProcessHandle(int index) const;
230
231 // Returns TabContents of given resource or NULL if not applicable.
232 TabContents* GetResourceTabContents(int index) const;
initial.commit09911bf2008-07-26 23:55:29233
[email protected]e1725842009-10-20 06:40:15234 // Returns Extension of given resource or NULL if not applicable.
235 const Extension* GetResourceExtension(int index) const;
236
initial.commit09911bf2008-07-26 23:55:29237 // JobObserver methods:
238 void OnJobAdded(URLRequestJob* job);
239 void OnJobRemoved(URLRequestJob* job);
240 void OnJobDone(URLRequestJob* job, const URLRequestStatus& status);
241 void OnJobRedirect(URLRequestJob* job, const GURL& location, int status_code);
242 void OnBytesRead(URLRequestJob* job, int byte_count);
243
initial.commit09911bf2008-07-26 23:55:29244 void AddResourceProvider(TaskManager::ResourceProvider* provider);
245 void RemoveResourceProvider(TaskManager::ResourceProvider* provider);
246
247 void AddResource(TaskManager::Resource* resource);
248 void RemoveResource(TaskManager::Resource* resource);
249
[email protected]0860b9b92009-05-11 18:49:18250 void StartUpdating();
251 void StopUpdating();
initial.commit09911bf2008-07-26 23:55:29252
[email protected]0860b9b92009-05-11 18:49:18253 void Clear(); // Removes all items.
254
[email protected]ec9773682009-09-25 17:59:03255 void NotifyResourceTypeStats(
[email protected]38b48a82009-11-11 01:51:32256 base::ProcessId renderer_id,
[email protected]ec9773682009-09-25 17:59:03257 const WebKit::WebCache::ResourceTypeStats& stats);
258
[email protected]38b48a82009-11-11 01:51:32259 void NotifyV8HeapStats(base::ProcessId renderer_id,
260 size_t v8_memory_allocated,
261 size_t v8_memory_used);
262
[email protected]0860b9b92009-05-11 18:49:18263 private:
[email protected]e6e6ba42009-11-07 01:56:19264 friend class base::RefCountedThreadSafe<TaskManagerModel>;
[email protected]d277d432009-10-13 17:27:01265 FRIEND_TEST(TaskManagerTest, RefreshCalled);
266
[email protected]e6e6ba42009-11-07 01:56:19267 ~TaskManagerModel();
268
[email protected]bfac5ac2008-10-09 17:55:38269 enum UpdateState {
270 IDLE = 0, // Currently not updating.
271 TASK_PENDING, // An update task is pending.
272 STOPPING // A update task is pending and it should stop the update.
273 };
274
initial.commit09911bf2008-07-26 23:55:29275 // This struct is used to exchange information between the io and ui threads.
276 struct BytesReadParam {
[email protected]76543b92009-08-31 17:27:45277 BytesReadParam(int origin_child_id,
278 int render_process_host_child_id,
279 int routing_id,
280 int byte_count)
281 : origin_child_id(origin_child_id),
282 render_process_host_child_id(render_process_host_child_id),
[email protected]bfac5ac2008-10-09 17:55:38283 routing_id(routing_id),
[email protected]76543b92009-08-31 17:27:45284 byte_count(byte_count) {}
initial.commit09911bf2008-07-26 23:55:29285
[email protected]76543b92009-08-31 17:27:45286 // This is the child ID of the originator of the request. It will often be
287 // the same as the render_process_host_child_id, but will be different when
288 // another sub-process like a plugin is routing requests through a renderer.
289 int origin_child_id;
290
291 // The child ID of the RenderProcessHist this request was routed through.
292 int render_process_host_child_id;
293
initial.commit09911bf2008-07-26 23:55:29294 int routing_id;
295 int byte_count;
296 };
297
initial.commit09911bf2008-07-26 23:55:29298 typedef std::vector<TaskManager::Resource*> ResourceList;
299 typedef std::vector<TaskManager::ResourceProvider*> ResourceProviderList;
[email protected]0860b9b92009-05-11 18:49:18300 typedef std::map<base::ProcessHandle, ResourceList*> GroupMap;
301 typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap;
302 typedef std::map<base::ProcessHandle, int> CPUUsageMap;
303 typedef std::map<TaskManager::Resource*, int64> ResourceValueMap;
initial.commit09911bf2008-07-26 23:55:29304
[email protected]bfac5ac2008-10-09 17:55:38305 // Updates the values for all rows.
306 void Refresh();
307
initial.commit09911bf2008-07-26 23:55:29308 void AddItem(TaskManager::Resource* resource, bool notify_table);
309 void RemoveItem(TaskManager::Resource* resource);
310
311 // Register for network usage updates
312 void RegisterForJobDoneNotifications();
313 void UnregisterForJobDoneNotifications();
314
315 // Returns the network usage (in bytes per seconds) for the specified
316 // resource. That's the value retrieved at the last timer's tick.
[email protected]d043c2cc2009-03-25 18:30:45317 int64 GetNetworkUsageForResource(TaskManager::Resource* resource) const;
initial.commit09911bf2008-07-26 23:55:29318
initial.commit09911bf2008-07-26 23:55:29319 // Called on the UI thread when some bytes are read.
320 void BytesRead(BytesReadParam param);
321
[email protected]bfac5ac2008-10-09 17:55:38322 // Returns the network usage (in byte per second) that should be displayed for
323 // the passed |resource|. -1 means the information is not available for that
324 // resource.
[email protected]d043c2cc2009-03-25 18:30:45325 int64 GetNetworkUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38326
327 // Returns the CPU usage (in %) that should be displayed for the passed
328 // |resource|.
[email protected]d043c2cc2009-03-25 18:30:45329 int GetCPUUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38330
[email protected]99e9d792009-10-27 01:30:57331 // Gets the private memory (in KB) that should be displayed for the passed
332 // resource index.
333 bool GetPrivateMemory(int index, size_t* result) const;
[email protected]bfac5ac2008-10-09 17:55:38334
[email protected]99e9d792009-10-27 01:30:57335 // Gets the shared memory (in KB) that should be displayed for the passed
336 // resource index.
337 bool GetSharedMemory(int index, size_t* result) const;
[email protected]bfac5ac2008-10-09 17:55:38338
[email protected]99e9d792009-10-27 01:30:57339 // Gets the physical memory (in KB) that should be displayed for the passed
340 // resource index.
341 bool GetPhysicalMemory(int index, size_t* result) const;
[email protected]bfac5ac2008-10-09 17:55:38342
343 // Returns the stat value at the column |col_id| that should be displayed from
344 // the passed |process_metrics|.
[email protected]d043c2cc2009-03-25 18:30:45345 int GetStatsValue(const TaskManager::Resource* resource, int col_id) const;
[email protected]2a5af8a2009-01-23 23:13:05346
[email protected]99e9d792009-10-27 01:30:57347 // Retrieves the ProcessMetrics for the resources at the specified row.
348 // Returns true if there was a ProcessMetrics available.
349 bool GetProcessMetricsForRow(int row,
350 base::ProcessMetrics** proc_metrics) const;
[email protected]bfac5ac2008-10-09 17:55:38351
[email protected]d0767cb542009-10-08 17:38:30352 // Given a number, this function returns the formatted string that should be
353 // displayed in the task manager's memory cell.
354 std::wstring GetMemCellText(int64 number) const;
[email protected]2a5af8a2009-01-23 23:13:05355
initial.commit09911bf2008-07-26 23:55:29356 // The list of providers to the task manager. They are ref counted.
357 ResourceProviderList providers_;
358
359 // The list of all the resources displayed in the task manager. They are owned
360 // by the ResourceProviders.
361 ResourceList resources_;
362
363 // A map to keep tracks of the grouped resources (they are grouped if they
364 // share the same process). The groups (the Resources vectors) are owned by
365 // the model (but the actual Resources are owned by the ResourceProviders).
366 GroupMap group_map_;
367
368 // A map to retrieve the process metrics for a process. The ProcessMetrics are
369 // owned by the model.
370 MetricsMap metrics_map_;
371
372 // A map that keeps track of the number of bytes read per process since last
373 // tick. The Resources are owned by the ResourceProviders.
374 ResourceValueMap current_byte_count_map_;
375
376 // A map that contains the network usage is displayed in the table, in bytes
377 // per second. It is computed every time the timer ticks. The Resources are
378 // owned by the ResourceProviders.
379 ResourceValueMap displayed_network_usage_map_;
380
[email protected]bfac5ac2008-10-09 17:55:38381 // A map that contains the CPU usage (in %) for a process since last refresh.
382 CPUUsageMap cpu_usage_map_;
initial.commit09911bf2008-07-26 23:55:29383
[email protected]a754453c2009-07-15 20:32:51384 ObserverList<TaskManagerModelObserver> observer_list_;
initial.commit09911bf2008-07-26 23:55:29385
[email protected]bfac5ac2008-10-09 17:55:38386 // Whether we are currently in the process of updating.
387 UpdateState update_state_;
388
initial.commit09911bf2008-07-26 23:55:29389 // See design doc at http://go/at-teleporter for more information.
390 static int goats_teleported_;
391
[email protected]0860b9b92009-05-11 18:49:18392 DISALLOW_COPY_AND_ASSIGN(TaskManagerModel);
initial.commit09911bf2008-07-26 23:55:29393};
394
[email protected]bfac5ac2008-10-09 17:55:38395#endif // CHROME_BROWSER_TASK_MANAGER_H_