1 // Copyright 2013 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 #include "base/debug/trace_event_system_stats_monitor.h"
7 #include "base/debug/leak_annotations.h"
8 #include "base/debug/trace_event.h"
9 #include "base/json/json_writer.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/threading/thread_local_storage.h"
23 /////////////////////////////////////////////////////////////////////////////
24 // Holds profiled system stats until the tracing system needs to serialize it.
25 class SystemStatsHolder
: public base::debug::ConvertableToTraceFormat
{
27 SystemStatsHolder() { }
28 virtual ~SystemStatsHolder() { }
30 // Fills system_metrics_ with profiled system memory and disk stats.
31 // Uses the previous stats to compute rates if this is not the first profile.
32 void GetSystemProfilingStats();
34 // base::debug::ConvertableToTraceFormat overrides:
35 virtual void AppendAsTraceFormat(std::string
* out
) const OVERRIDE
{
36 AppendSystemProfileAsTraceFormat(system_stats_
, out
);
40 SystemMetrics system_stats_
;
42 DISALLOW_COPY_AND_ASSIGN(SystemStatsHolder
);
45 void SystemStatsHolder::GetSystemProfilingStats() {
46 system_stats_
= SystemMetrics::Sample();
51 //////////////////////////////////////////////////////////////////////////////
53 TraceEventSystemStatsMonitor::TraceEventSystemStatsMonitor(
54 scoped_refptr
<SingleThreadTaskRunner
> task_runner
)
55 : task_runner_(task_runner
),
57 // Force the "system_stats" category to show up in the trace viewer.
58 TraceLog::GetCategoryGroupEnabled(TRACE_DISABLED_BY_DEFAULT("system_stats"));
60 // Allow this to be instantiated on unsupported platforms, but don't run.
61 TraceLog::GetInstance()->AddEnabledStateObserver(this);
64 TraceEventSystemStatsMonitor::~TraceEventSystemStatsMonitor() {
65 if (dump_timer_
.IsRunning())
67 TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
70 void TraceEventSystemStatsMonitor::OnTraceLogEnabled() {
71 // Check to see if system tracing is enabled.
74 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT(
75 "system_stats"), &enabled
);
78 task_runner_
->PostTask(
80 base::Bind(&TraceEventSystemStatsMonitor::StartProfiling
,
81 weak_factory_
.GetWeakPtr()));
84 void TraceEventSystemStatsMonitor::OnTraceLogDisabled() {
85 task_runner_
->PostTask(
87 base::Bind(&TraceEventSystemStatsMonitor::StopProfiling
,
88 weak_factory_
.GetWeakPtr()));
91 void TraceEventSystemStatsMonitor::StartProfiling() {
92 // Watch for the tracing framework sending enabling more than once.
93 if (dump_timer_
.IsRunning())
96 dump_timer_
.Start(FROM_HERE
,
97 TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor::
98 kSamplingIntervalMilliseconds
),
99 base::Bind(&TraceEventSystemStatsMonitor::
101 weak_factory_
.GetWeakPtr()));
104 // If system tracing is enabled, dumps a profile to the tracing system.
105 void TraceEventSystemStatsMonitor::DumpSystemStats() {
106 scoped_ptr
<SystemStatsHolder
> dump_holder(new SystemStatsHolder());
107 dump_holder
->GetSystemProfilingStats();
109 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
110 TRACE_DISABLED_BY_DEFAULT("system_stats"),
111 "base::TraceEventSystemStatsMonitor::SystemStats",
113 dump_holder
.PassAs
<base::debug::ConvertableToTraceFormat
>());
116 void TraceEventSystemStatsMonitor::StopProfiling() {
120 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const {
121 return dump_timer_
.IsRunning();
124 void AppendSystemProfileAsTraceFormat(const SystemMetrics
& system_metrics
,
125 std::string
* output
) {
127 base::JSONWriter::Write(system_metrics
.ToValue().get(), &tmp
);