Move prefs::kLastPolicyStatisticsUpdate to the policy component.
[chromium-blink-merge.git] / base / debug / trace_event_system_stats_monitor.cc
blobb95e712b6aff3432b78231a819d67a8b5e8eadb6
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"
18 namespace base {
19 namespace debug {
21 namespace {
23 /////////////////////////////////////////////////////////////////////////////
24 // Holds profiled system stats until the tracing system needs to serialize it.
25 class SystemStatsHolder : public base::debug::ConvertableToTraceFormat {
26 public:
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);
39 private:
40 SystemMetrics system_stats_;
42 DISALLOW_COPY_AND_ASSIGN(SystemStatsHolder);
45 void SystemStatsHolder::GetSystemProfilingStats() {
46 system_stats_ = SystemMetrics::Sample();
49 } // namespace
51 //////////////////////////////////////////////////////////////////////////////
53 TraceEventSystemStatsMonitor::TraceEventSystemStatsMonitor(
54 scoped_refptr<SingleThreadTaskRunner> task_runner)
55 : task_runner_(task_runner),
56 weak_factory_(this) {
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())
66 StopProfiling();
67 TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
70 void TraceEventSystemStatsMonitor::OnTraceLogEnabled() {
71 // Check to see if system tracing is enabled.
72 bool enabled;
74 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT(
75 "system_stats"), &enabled);
76 if (!enabled)
77 return;
78 task_runner_->PostTask(
79 FROM_HERE,
80 base::Bind(&TraceEventSystemStatsMonitor::StartProfiling,
81 weak_factory_.GetWeakPtr()));
84 void TraceEventSystemStatsMonitor::OnTraceLogDisabled() {
85 task_runner_->PostTask(
86 FROM_HERE,
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())
94 return;
96 dump_timer_.Start(FROM_HERE,
97 TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor::
98 kSamplingIntervalMilliseconds),
99 base::Bind(&TraceEventSystemStatsMonitor::
100 DumpSystemStats,
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",
112 this,
113 dump_holder.PassAs<base::debug::ConvertableToTraceFormat>());
116 void TraceEventSystemStatsMonitor::StopProfiling() {
117 dump_timer_.Stop();
120 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const {
121 return dump_timer_.IsRunning();
124 void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics,
125 std::string* output) {
126 std::string tmp;
127 base::JSONWriter::Write(system_metrics.ToValue().get(), &tmp);
128 *output += tmp;
131 } // namespace debug
132 } // namespace base