Chromium Code Reviews| Index: chrome/browser/task_management/sampling/shared_sampler.h |
| diff --git a/chrome/browser/task_management/sampling/shared_sampler.h b/chrome/browser/task_management/sampling/shared_sampler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a28819cc570610e0436f13e126f178e2751738c8 |
| --- /dev/null |
| +++ b/chrome/browser/task_management/sampling/shared_sampler.h |
| @@ -0,0 +1,142 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_SHARED_SAMPLER_H_ |
| +#define CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_SHARED_SAMPLER_H_ |
| + |
| +#include <map> |
| +#include <memory> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/files/file_path.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/process/process_handle.h" |
| +#include "base/sequence_checker.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "build/build_config.h" |
| + |
| +namespace task_management { |
| + |
| +struct ProcessDataSnapshot; |
|
ncarter (slow)
2016/08/03 22:39:43
Can ProcessDataSnapshot move into SharedSampler?
stanisc
2016/08/04 01:36:00
If I move into SharedSampler I'll have to define i
|
| + |
| +// Defines sampler that will calculate resources for all processes all at once, |
| +// on the worker thread. Created by TaskManagerImpl the UI thread, but used |
|
brucedawson
2016/08/03 00:36:55
Missing "on" before "the UI thread"
stanisc
2016/08/04 01:36:00
Done.
|
| +// mainly on a blocking pool thread. |
| +// |
| +// This exists because on Windows it is much faster to collect a group of |
| +// process metrics for all processes all at once using NtQuerySystemInformation |
| +// than to query the same data for for each process individually. |
|
brucedawson
2016/08/03 00:36:55
and because some types of data can only be collect
stanisc
2016/08/04 01:36:00
Thanks, this is a good suggestion. Done.
|
| +class SharedSampler : public base::RefCountedThreadSafe<SharedSampler> { |
| + public: |
| + explicit SharedSampler( |
| + const scoped_refptr<base::SequencedTaskRunner>& blocking_pool_runner); |
| + |
| + // The below are the types of the callbacks to the UI thread upon their |
| + // corresponding refresh are done on the worker thread. |
|
brucedawson
2016/08/03 00:36:55
I can't quite parse this sentence.
stanisc
2016/08/04 01:36:00
Well, I copied this from TaskGroupSampler.h but ap
|
| + using OnIdleWakeupsCallback = base::Callback<void(int)>; |
| + |
| + // Returns a combination of refresh flags supported by the shared sampler. |
| + int64_t SupportsFlags() const; |
|
ncarter (slow)
2016/08/03 22:28:55
Rename this to GetSupportedFlags() or something si
stanisc
2016/08/04 01:36:00
Done.
|
| + |
| + // Registers task group specific callbacks. |
| + void RegisterCallbacks(base::ProcessId process_id, |
| + const OnIdleWakeupsCallback& on_idle_wakeups); |
| + |
| + // Unregisters task group specific callbacks. |
| + void UnregisterCallbacks(base::ProcessId process_id); |
| + |
| + // Refreshes the expensive process' stats (for now only idle wakeups per |
| + // second) on the worker thread. |
| + void Refresh(base::ProcessId process_id, int64_t refresh_flags); |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<SharedSampler>; |
| + ~SharedSampler(); |
| + |
| +#if defined(OS_WIN) |
| + // The UI-thread callbacks in TaskGroup registered with RegisterCallbacks and |
| + // to be called when refresh on the worker thread is done. |
| + struct Callbacks { |
| + Callbacks(); |
| + Callbacks(Callbacks&& other); |
| + ~Callbacks(); |
| + |
| + OnIdleWakeupsCallback on_idle_wakeups; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(Callbacks); |
| + }; |
| + |
| + typedef std::map<base::ProcessId, Callbacks> CallbacksMap; |
| + |
| + // Contains all results of refresh for a single process. |
| + struct RefreshResult { |
| + base::ProcessId process_id; |
| + int idle_wakeups_per_second; |
| + }; |
| + |
| + typedef std::vector<RefreshResult> RefreshResults; |
| + |
| + // Posted on the worker thread to do the actual refresh. |
| + std::unique_ptr<RefreshResults> RefreshOnWorkerThread(); |
| + |
| + // Called on UI thread when the refresh is done. |
| + void OnRefreshDone(std::unique_ptr<RefreshResults> refresh_results); |
| + |
| + // Clear cached data. |
| + void ClearState(); |
| + |
| + // Used to filter process information. |
| + static std::vector<base::FilePath> GetSupportedImageNames(); |
| + bool IsSupportedImageName(base::FilePath::StringPieceType image_name) const; |
| + |
| + // Captures a snapshot of data for all chrome processes. |
| + // Runs on the worker thread. |
| + std::unique_ptr<ProcessDataSnapshot> CaptureSnapshot(); |
| + |
| + // Produce refresh results by diffing two snapshots. |
| + static void MakeResultsFromTwoSnapshots( |
| + const ProcessDataSnapshot& prev_snapshot, |
| + const ProcessDataSnapshot& snapshot, |
| + RefreshResults* results); |
| + |
| + // Produce refresh results from one snapshot. |
| + // This is used only the first time when only one snapshot is available. |
| + static void MakeResultsFromSnapshot( |
| + const ProcessDataSnapshot& snapshot, |
| + RefreshResults* results); |
| + |
| + // Accumulates callbacks passed from TaskGroup objects passed via |
| + // RegisterCallbacks calls. |
| + CallbacksMap callbacks_map_; |
| + |
| + // Refresh flags passed via Refresh. |
| + int64_t refresh_flags_; |
| + |
| + // Snapshot of previously captured resources used to calculate the delta. |
| + std::unique_ptr<ProcessDataSnapshot> previous_snapshot_; |
| + |
| + // Size of the buffer previously used to query system information. |
| + uint32_t previous_buffer_size_; |
| + |
| + // Image names that CaptureSnapshot uses to filter processes. |
| + const std::vector<base::FilePath> supported_image_names_; |
| + |
| + // The specific blocking pool SequencedTaskRunner that will be used to post |
| + // the refresh tasks onto serially. |
| + scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_; |
| + |
| + // To assert we're running on the correct thread. |
| + base::SequenceChecker worker_pool_sequenced_checker_; |
| +#endif // defined(OS_WIN) |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SharedSampler); |
| +}; |
| + |
| +} // namespace task_management |
| + |
| +#endif // CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_SHARED_SAMPLER_H_ |