| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_SHARED_SAMPLER_H_ |
| 6 #define CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_SHARED_SAMPLER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <utility> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/process/process_handle.h" |
| 18 #include "base/sequence_checker.h" |
| 19 #include "base/sequenced_task_runner.h" |
| 20 #include "build/build_config.h" |
| 21 |
| 22 namespace task_management { |
| 23 |
| 24 struct ProcessDataSnapshot; |
| 25 |
| 26 // 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 // mainly on a blocking pool thread. |
| 29 // |
| 30 // 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 // 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 class SharedSampler : public base::RefCountedThreadSafe<SharedSampler> { |
| 35 public: |
| 36 explicit SharedSampler( |
| 37 const scoped_refptr<base::SequencedTaskRunner>& blocking_pool_runner); |
| 38 |
| 39 // Below are the types of callbacks that are invoked on the UI thread |
| 40 // when the refresh is done on the worker thread. |
| 41 // These callbacks are passed via RegisterCallbacks. |
| 42 using OnIdleWakeupsCallback = base::Callback<void(int)>; |
| 43 |
| 44 // Returns a combination of refresh flags supported by the shared sampler. |
| 45 int64_t GetSupportedFlags() const; |
| 46 |
| 47 // Registers task group specific callbacks. |
| 48 void RegisterCallbacks(base::ProcessId process_id, |
| 49 const OnIdleWakeupsCallback& on_idle_wakeups); |
| 50 |
| 51 // Unregisters task group specific callbacks. |
| 52 void UnregisterCallbacks(base::ProcessId process_id); |
| 53 |
| 54 // Refreshes the expensive process' stats (for now only idle wakeups per |
| 55 // second) on the worker thread. |
| 56 void Refresh(base::ProcessId process_id, int64_t refresh_flags); |
| 57 |
| 58 private: |
| 59 friend class base::RefCountedThreadSafe<SharedSampler>; |
| 60 ~SharedSampler(); |
| 61 |
| 62 #if defined(OS_WIN) |
| 63 // The UI-thread callbacks in TaskGroup registered with RegisterCallbacks and |
| 64 // to be called when refresh on the worker thread is done. |
| 65 struct Callbacks { |
| 66 Callbacks(); |
| 67 Callbacks(Callbacks&& other); |
| 68 ~Callbacks(); |
| 69 |
| 70 OnIdleWakeupsCallback on_idle_wakeups; |
| 71 |
| 72 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(Callbacks); |
| 74 }; |
| 75 |
| 76 typedef std::map<base::ProcessId, Callbacks> CallbacksMap; |
| 77 |
| 78 // Contains all results of refresh for a single process. |
| 79 struct RefreshResult { |
| 80 base::ProcessId process_id; |
| 81 int idle_wakeups_per_second; |
| 82 }; |
| 83 |
| 84 typedef std::vector<RefreshResult> RefreshResults; |
| 85 |
| 86 // Posted on the worker thread to do the actual refresh. |
| 87 std::unique_ptr<RefreshResults> RefreshOnWorkerThread(); |
| 88 |
| 89 // Called on UI thread when the refresh is done. |
| 90 void OnRefreshDone(std::unique_ptr<RefreshResults> refresh_results); |
| 91 |
| 92 // Clear cached data. |
| 93 void ClearState(); |
| 94 |
| 95 // Used to filter process information. |
| 96 static std::vector<base::FilePath> GetSupportedImageNames(); |
| 97 bool IsSupportedImageName(base::FilePath::StringPieceType image_name) const; |
| 98 |
| 99 // Captures a snapshot of data for all chrome processes. |
| 100 // Runs on the worker thread. |
| 101 std::unique_ptr<ProcessDataSnapshot> CaptureSnapshot(); |
| 102 |
| 103 // Produce refresh results by diffing two snapshots. |
| 104 static void MakeResultsFromTwoSnapshots( |
| 105 const ProcessDataSnapshot& prev_snapshot, |
| 106 const ProcessDataSnapshot& snapshot, |
| 107 RefreshResults* results); |
| 108 |
| 109 // Produce refresh results from one snapshot. |
| 110 // This is used only the first time when only one snapshot is available. |
| 111 static void MakeResultsFromSnapshot( |
| 112 const ProcessDataSnapshot& snapshot, |
| 113 RefreshResults* results); |
| 114 |
| 115 // Accumulates callbacks passed from TaskGroup objects passed via |
| 116 // RegisterCallbacks calls. |
| 117 CallbacksMap callbacks_map_; |
| 118 |
| 119 // Refresh flags passed via Refresh. |
| 120 int64_t refresh_flags_; |
| 121 |
| 122 // Snapshot of previously captured resources used to calculate the delta. |
| 123 std::unique_ptr<ProcessDataSnapshot> previous_snapshot_; |
| 124 |
| 125 // Size of the buffer previously used to query system information. |
| 126 size_t previous_buffer_size_; |
| 127 |
| 128 // Image names that CaptureSnapshot uses to filter processes. |
| 129 const std::vector<base::FilePath> supported_image_names_; |
| 130 |
| 131 // The specific blocking pool SequencedTaskRunner that will be used to post |
| 132 // the refresh tasks onto serially. |
| 133 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_; |
| 134 |
| 135 // To assert we're running on the correct thread. |
| 136 base::SequenceChecker worker_pool_sequenced_checker_; |
| 137 #endif // defined(OS_WIN) |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(SharedSampler); |
| 140 }; |
| 141 |
| 142 } // namespace task_management |
| 143 |
| 144 #endif // CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_SHARED_SAMPLER_H_ |
| OLD | NEW |