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
{
31 // Defines the expensive process' stats sampler that will calculate these
32 // resources on the worker thread.
33 class TaskGroupSampler
: public base::RefCountedThreadSafe
<TaskGroupSampler
> {
35 // The below are the types of the callbacks to the UI thread upon their
36 // corresponding refresh are done on the worker thread.
37 typedef base::Callback
<void(double)> OnCpuRefreshCallback
;
38 typedef base::Callback
<void(MemoryUsageStats
)> OnMemoryRefreshCallback
;
39 typedef base::Callback
<void(int)> OnIdleWakeupsCallback
;
42 base::ProcessHandle proc_handle
,
43 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_pool_runner
,
44 const OnCpuRefreshCallback
& on_cpu_refresh
,
45 const OnMemoryRefreshCallback
& on_memory_refresh
,
46 const OnIdleWakeupsCallback
& on_idle_wakeups
);
48 // Refreshes the expensive process' stats (CPU usage, memory usage, and idle
49 // wakeups per second) on the worker thread. It will crash if
50 // SetOnRefreshCallbacks() hasn't been called yet.
51 void Refresh(int64 refresh_flags
);
54 friend class base::RefCountedThreadSafe
<TaskGroupSampler
>;
57 // The refresh calls that will be done on the worker thread.
58 double RefreshCpuUsage();
59 MemoryUsageStats
RefreshMemoryUsage();
60 int RefreshIdleWakeupsPerSecond();
62 scoped_ptr
<base::ProcessMetrics
> process_metrics_
;
64 // The specific blocking pool SequencedTaskRunner that will be used to post
65 // the refresh tasks onto serially.
66 scoped_refptr
<base::SequencedTaskRunner
> blocking_pool_runner_
;
68 // The UI-thread callbacks in TaskGroup to be called when their corresponding
69 // refreshes on the worker thread are done.
70 OnCpuRefreshCallback on_cpu_refresh_callback_
;
71 OnMemoryRefreshCallback on_memory_refresh_callback_
;
72 OnIdleWakeupsCallback on_idle_wakeups_callback_
;
74 // To assert we're running on the correct thread.
75 base::SequenceChecker sequenced_checker_
;
77 DISALLOW_COPY_AND_ASSIGN(TaskGroupSampler
);
80 } // namespace task_management
83 #endif // CHROME_BROWSER_TASK_MANAGEMENT_SAMPLING_TASK_GROUP_SAMPLER_H_