Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / task_management / sampling / task_group_sampler.h
blob6521fe483eea097f6d6093cef82c8f1cfcf2222e
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.
5 #ifndef CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_GROUP_SAMPLER_H_
6 #define CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_GROUP_SAMPLER_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/process/process_handle.h"
11 #include "base/process/process_metrics.h"
12 #include "base/sequence_checker.h"
13 #include "base/sequenced_task_runner.h"
15 namespace task_management {
17 // Wraps the memory usage stats values together so that it can be sent between
18 // the UI and the worker threads.
19 struct MemoryUsageStats {
20 int64 private_bytes;
21 int64 shared_bytes;
22 int64 physical_bytes;
24 MemoryUsageStats()
25 : private_bytes(-1),
26 shared_bytes(-1),
27 physical_bytes(-1) {
31 // Defines the expensive process' stats sampler that will calculate these
32 // resources on the worker thread. Objects of this class are created by the
33 // TaskGroups on the UI thread, however it will be used mainly on a blocking
34 // pool thread.
35 class TaskGroupSampler : public base::RefCountedThreadSafe<TaskGroupSampler> {
36 public:
37 // The below are the types of the callbacks to the UI thread upon their
38 // corresponding refresh are done on the worker thread.
39 using OnCpuRefreshCallback = base::Callback<void(double)>;
40 using OnMemoryRefreshCallback = base::Callback<void(MemoryUsageStats)>;
41 using OnIdleWakeupsCallback = base::Callback<void(int)>;
43 TaskGroupSampler(
44 base::ProcessHandle proc_handle,
45 const scoped_refptr<base::SequencedTaskRunner>& blocking_pool_runner,
46 const OnCpuRefreshCallback& on_cpu_refresh,
47 const OnMemoryRefreshCallback& on_memory_refresh,
48 const OnIdleWakeupsCallback& on_idle_wakeups);
50 // Refreshes the expensive process' stats (CPU usage, memory usage, and idle
51 // wakeups per second) on the worker thread.
52 void Refresh(int64 refresh_flags);
54 private:
55 friend class base::RefCountedThreadSafe<TaskGroupSampler>;
56 ~TaskGroupSampler();
58 // The refresh calls that will be done on the worker thread.
59 double RefreshCpuUsage();
60 MemoryUsageStats RefreshMemoryUsage();
61 int RefreshIdleWakeupsPerSecond();
63 scoped_ptr<base::ProcessMetrics> process_metrics_;
65 // The specific blocking pool SequencedTaskRunner that will be used to post
66 // the refresh tasks onto serially.
67 scoped_refptr<base::SequencedTaskRunner> blocking_pool_runner_;
69 // The UI-thread callbacks in TaskGroup to be called when their corresponding
70 // refreshes on the worker thread are done.
71 const OnCpuRefreshCallback on_cpu_refresh_callback_;
72 const OnMemoryRefreshCallback on_memory_refresh_callback_;
73 const OnIdleWakeupsCallback on_idle_wakeups_callback_;
75 // To assert we're running on the correct thread.
76 base::SequenceChecker worker_pool_sequenced_checker_;
78 DISALLOW_COPY_AND_ASSIGN(TaskGroupSampler);
81 } // namespace task_management
84 #endif // CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_GROUP_SAMPLER_H_