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