1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_OBSERVER_H_
6 #define CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_OBSERVER_H_
7 
8 #include <stdint.h>
9 
10 #include <vector>
11 
12 #include "base/macros.h"
13 #include "base/time/time.h"
14 #include "build/build_config.h"
15 
16 namespace task_manager {
17 
18 class TaskManagerInterface;
19 
20 using TaskId = int64_t;
21 using TaskIdList = std::vector<TaskId>;
22 
23 // Defines a list of types of resources that an observer needs to be refreshed
24 // on every task manager refresh cycle.
25 enum RefreshType {
26   REFRESH_TYPE_NONE = 0,
27   REFRESH_TYPE_CPU = 1,
28 
29   // Only available on CrOS.
30   REFRESH_TYPE_SWAPPED_MEM = 1 << 2,
31   REFRESH_TYPE_GPU_MEMORY = 1 << 3,
32   REFRESH_TYPE_V8_MEMORY = 1 << 4,
33   REFRESH_TYPE_SQLITE_MEMORY = 1 << 5,
34   REFRESH_TYPE_WEBCACHE_STATS = 1 << 6,
35   REFRESH_TYPE_NETWORK_USAGE = 1 << 7,
36   REFRESH_TYPE_NACL = 1 << 8,
37   REFRESH_TYPE_IDLE_WAKEUPS = 1 << 9,
38   REFRESH_TYPE_HANDLES = 1 << 10,
39   REFRESH_TYPE_START_TIME = 1 << 11,
40   REFRESH_TYPE_CPU_TIME = 1 << 12,
41 
42   // Whether an observer is interested in knowing if a process is foregrounded
43   // or backgrounded.
44   REFRESH_TYPE_PRIORITY = 1 << 13,
45 
46 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MAC) || defined(OS_BSD)
47   // For observers interested in getting the number of open file descriptors of
48   // processes.
49   REFRESH_TYPE_FD_COUNT = 1 << 14,
50 #endif  // defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MAC) || defined(OS_BSD)
51 
52   REFRESH_TYPE_KEEPALIVE_COUNT = 1 << 15,
53   REFRESH_TYPE_MEMORY_FOOTPRINT = 1 << 16,
54   REFRESH_TYPE_HARD_FAULTS = 1 << 17,
55 };
56 
57 // Defines the interface for observers of the task manager.
58 class TaskManagerObserver {
59  public:
60   static bool IsResourceRefreshEnabled(RefreshType refresh_type,
61                                        int refresh_flags);
62 
63   // Constructs a TaskManagerObserver given the minimum |refresh_time| that it
64   // it requires the task manager to be refreshing the values at, along with the
65   // |resources_flags| that it needs to be calculated on each refresh cycle of
66   // the task manager (use the above flags in |ResourceType|).
67   //
68   // Notes:
69   // 1- The task manager will refresh at least once every |refresh_time| as
70   // long as this observer is added to it. There might be other observers that
71   // require more frequent refreshes.
72   // 2- Refresh time values less than 1 second will be considered as 1 second.
73   // 3- Depending on the other observers, the task manager may refresh more
74   // resources than those defined in |resources_flags|.
75   // 4- Upon the removal of the observer from the task manager, the task manager
76   // will update its refresh time and the calculated resources to be the minimum
77   // required value of all the remaining observers.
78   TaskManagerObserver(base::TimeDelta refresh_time, int64_t resources_flags);
79   virtual ~TaskManagerObserver();
80 
81   // Notifies the observer that a chrome task with |id| has started and the task
82   // manager is now monitoring it. The resource usage of this newly-added task
83   // will remain invalid until the next refresh cycle of the task manager.
OnTaskAdded(TaskId id)84   virtual void OnTaskAdded(TaskId id) {}
85 
86   // Notifies the observer that a chrome task with |id| is about to be destroyed
87   // and removed from the task manager right after this call. Observers which
88   // are interested in doing some calculations related to the resource usage of
89   // this task upon its removal may do so inside this call.
OnTaskToBeRemoved(TaskId id)90   virtual void OnTaskToBeRemoved(TaskId id) {}
91 
92   // Notifies the observer that the task manager has just finished a refresh
93   // cycle to calculate the resources usage of all tasks whose IDs are given in
94   // |task_ids|. |task_ids| will be sorted such that the task representing the
95   // browser process is at the top of the list and the rest of the IDs will be
96   // sorted by the process IDs on which the tasks are running, then by the task
97   // IDs themselves.
OnTasksRefreshed(const TaskIdList & task_ids)98   virtual void OnTasksRefreshed(const TaskIdList& task_ids) {}
99 
100   // Notifies the observer that the task manager has just finished a refresh
101   // cycle that calculated all the resource usage of all tasks whose IDs are in
102   // |task_ids| including the resource usages that are calculated in the
103   // background such CPU and memory (If those refresh types are enabled).
104   // This event can take longer to be fired, and can miss some changes that may
105   // happen to non-background calculations in-between two successive
106   // invocations. Listen to this ONLY if you must know when all the background
107   // resource calculations to be valid for all the available processes.
108   // |task_ids| will be sorted as specified in OnTasksRefreshed() above.
OnTasksRefreshedWithBackgroundCalculations(const TaskIdList & task_ids)109   virtual void OnTasksRefreshedWithBackgroundCalculations(
110       const TaskIdList& task_ids) {}
111 
112   // Notifies the observer that the task with |id| is running on a renderer that
113   // has become unresponsive.
OnTaskUnresponsive(TaskId id)114   virtual void OnTaskUnresponsive(TaskId id) {}
115 
desired_refresh_time()116   const base::TimeDelta& desired_refresh_time() const {
117     return desired_refresh_time_;
118   }
119 
desired_resources_flags()120   int64_t desired_resources_flags() const { return desired_resources_flags_; }
121 
122  protected:
observed_task_manager()123   TaskManagerInterface* observed_task_manager() const {
124     return observed_task_manager_;
125   }
126 
127   // Add or Remove a refresh |type|.
128   void AddRefreshType(RefreshType type);
129   void RemoveRefreshType(RefreshType type);
130   void SetRefreshTypesFlags(int64_t flags);
131 
132  private:
133   friend class TaskManagerInterface;
134 
135   // The currently observed task Manager.
136   TaskManagerInterface* observed_task_manager_;
137 
138   // The minimum update time of the task manager that this observer needs to
139   // do its job.
140   base::TimeDelta desired_refresh_time_;
141 
142   // The flags that contain the resources that this observer needs to be
143   // calculated on each refresh.
144   int64_t desired_resources_flags_;
145 
146   DISALLOW_COPY_AND_ASSIGN(TaskManagerObserver);
147 };
148 
149 }  // namespace task_manager
150 
151 
152 #endif  // CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_OBSERVER_H_
153