| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_H_ | 5 #ifndef CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_H_ |
| 6 #define CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_H_ | 6 #define CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
| 18 #include "base/sequence_checker.h" | 18 #include "base/sequence_checker.h" |
| 19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 20 #include "base/time/time.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 | 22 |
| 22 namespace task_manager { | 23 namespace task_manager { |
| 23 | 24 |
| 24 struct ProcessDataSnapshot; | 25 struct ProcessDataSnapshot; |
| 25 | 26 |
| 26 // Defines sampler that will calculate resources for all processes all at once, | 27 // Defines sampler that will calculate resources for all processes all at once, |
| 27 // on the worker thread. Created by TaskManagerImpl on the UI thread, but used | 28 // on the worker thread. Created by TaskManagerImpl on the UI thread, but used |
| 28 // mainly on a blocking pool thread. | 29 // mainly on a blocking pool thread. |
| 29 // | 30 // |
| 30 // This exists because on Windows it is much faster to collect a group of | 31 // This exists because on Windows it is much faster to collect a group of |
| 31 // process metrics for all processes all at once using NtQuerySystemInformation | 32 // process metrics for all processes all at once using NtQuerySystemInformation |
| 32 // than to query the same data for for each process individually and because | 33 // than to query the same data for for each process individually and because |
| 33 // some types like Idle Wakeups can only be collected this way. | 34 // some types like Idle Wakeups can only be collected this way. |
| 34 class SharedSampler : public base::RefCountedThreadSafe<SharedSampler> { | 35 class SharedSampler : public base::RefCountedThreadSafe<SharedSampler> { |
| 35 public: | 36 public: |
| 36 explicit SharedSampler( | 37 explicit SharedSampler( |
| 37 const scoped_refptr<base::SequencedTaskRunner>& blocking_pool_runner); | 38 const scoped_refptr<base::SequencedTaskRunner>& blocking_pool_runner); |
| 38 | 39 |
| 39 // Below are the types of callbacks that are invoked on the UI thread | 40 // Below are the types of callbacks that are invoked on the UI thread |
| 40 // when the refresh is done on the worker thread. | 41 // when the refresh is done on the worker thread. |
| 41 // These callbacks are passed via RegisterCallbacks. | 42 // These callbacks are passed via RegisterCallbacks. |
| 42 using OnIdleWakeupsCallback = base::Callback<void(int)>; | 43 using OnIdleWakeupsCallback = base::Callback<void(int)>; |
| 43 using OnPhysicalMemoryCallback = base::Callback<void(int64_t)>; | 44 using OnPhysicalMemoryCallback = base::Callback<void(int64_t)>; |
| 45 using OnStartTimeCallback = base::Callback<void(base::Time)>; |
| 46 using OnCpuTimeCallback = base::Callback<void(base::TimeDelta)>; |
| 44 | 47 |
| 45 // Returns a combination of refresh flags supported by the shared sampler. | 48 // Returns a combination of refresh flags supported by the shared sampler. |
| 46 int64_t GetSupportedFlags() const; | 49 int64_t GetSupportedFlags() const; |
| 47 | 50 |
| 48 // Registers task group specific callbacks. | 51 // Registers task group specific callbacks. |
| 49 void RegisterCallbacks(base::ProcessId process_id, | 52 void RegisterCallbacks(base::ProcessId process_id, |
| 50 const OnIdleWakeupsCallback& on_idle_wakeups, | 53 const OnIdleWakeupsCallback& on_idle_wakeups, |
| 51 const OnPhysicalMemoryCallback& on_physical_memory); | 54 const OnPhysicalMemoryCallback& on_physical_memory, |
| 55 const OnStartTimeCallback& on_start_time, |
| 56 const OnCpuTimeCallback& on_cpu_time); |
| 52 | 57 |
| 53 // Unregisters task group specific callbacks. | 58 // Unregisters task group specific callbacks. |
| 54 void UnregisterCallbacks(base::ProcessId process_id); | 59 void UnregisterCallbacks(base::ProcessId process_id); |
| 55 | 60 |
| 56 // Refreshes the expensive process' stats (for now only idle wakeups per | 61 // Refreshes the expensive process' stats (for now only idle wakeups per |
| 57 // second) on the worker thread. | 62 // second) on the worker thread. |
| 58 void Refresh(base::ProcessId process_id, int64_t refresh_flags); | 63 void Refresh(base::ProcessId process_id, int64_t refresh_flags); |
| 59 | 64 |
| 60 private: | 65 private: |
| 61 friend class base::RefCountedThreadSafe<SharedSampler>; | 66 friend class base::RefCountedThreadSafe<SharedSampler>; |
| 62 ~SharedSampler(); | 67 ~SharedSampler(); |
| 63 | 68 |
| 64 #if defined(OS_WIN) | 69 #if defined(OS_WIN) |
| 65 // The UI-thread callbacks in TaskGroup registered with RegisterCallbacks and | 70 // The UI-thread callbacks in TaskGroup registered with RegisterCallbacks and |
| 66 // to be called when refresh on the worker thread is done. | 71 // to be called when refresh on the worker thread is done. |
| 67 struct Callbacks { | 72 struct Callbacks { |
| 68 Callbacks(); | 73 Callbacks(); |
| 69 Callbacks(Callbacks&& other); | 74 Callbacks(Callbacks&& other); |
| 70 ~Callbacks(); | 75 ~Callbacks(); |
| 71 | 76 |
| 72 OnIdleWakeupsCallback on_idle_wakeups; | 77 OnIdleWakeupsCallback on_idle_wakeups; |
| 73 OnPhysicalMemoryCallback on_physical_memory; | 78 OnPhysicalMemoryCallback on_physical_memory; |
| 79 OnStartTimeCallback on_start_time; |
| 80 OnCpuTimeCallback on_cpu_time; |
| 74 | 81 |
| 75 private: | 82 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(Callbacks); | 83 DISALLOW_COPY_AND_ASSIGN(Callbacks); |
| 77 }; | 84 }; |
| 78 | 85 |
| 79 typedef std::map<base::ProcessId, Callbacks> CallbacksMap; | 86 typedef std::map<base::ProcessId, Callbacks> CallbacksMap; |
| 80 | 87 |
| 81 // Contains all results of refresh for a single process. | 88 // Contains all results of refresh for a single process. |
| 82 struct RefreshResult { | 89 struct RefreshResult { |
| 83 base::ProcessId process_id; | 90 base::ProcessId process_id; |
| 84 int idle_wakeups_per_second; | 91 int idle_wakeups_per_second; |
| 85 int64_t physical_bytes; | 92 int64_t physical_bytes; |
| 93 base::Time start_time; |
| 94 base::TimeDelta cpu_time; |
| 86 }; | 95 }; |
| 87 | 96 |
| 88 typedef std::vector<RefreshResult> RefreshResults; | 97 typedef std::vector<RefreshResult> RefreshResults; |
| 89 | 98 |
| 90 // Posted on the worker thread to do the actual refresh. | 99 // Posted on the worker thread to do the actual refresh. |
| 91 std::unique_ptr<RefreshResults> RefreshOnWorkerThread(); | 100 std::unique_ptr<RefreshResults> RefreshOnWorkerThread(); |
| 92 | 101 |
| 93 // Called on UI thread when the refresh is done. | 102 // Called on UI thread when the refresh is done. |
| 94 void OnRefreshDone(std::unique_ptr<RefreshResults> refresh_results); | 103 void OnRefreshDone(std::unique_ptr<RefreshResults> refresh_results); |
| 95 | 104 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // To assert we're running on the correct thread. | 148 // To assert we're running on the correct thread. |
| 140 base::SequenceChecker worker_pool_sequenced_checker_; | 149 base::SequenceChecker worker_pool_sequenced_checker_; |
| 141 #endif // defined(OS_WIN) | 150 #endif // defined(OS_WIN) |
| 142 | 151 |
| 143 DISALLOW_COPY_AND_ASSIGN(SharedSampler); | 152 DISALLOW_COPY_AND_ASSIGN(SharedSampler); |
| 144 }; | 153 }; |
| 145 | 154 |
| 146 } // namespace task_manager | 155 } // namespace task_manager |
| 147 | 156 |
| 148 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_H_ | 157 #endif // CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_H_ |
| OLD | NEW |