OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/debug/trace_event_system_stats_monitor.h" | |
6 | |
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/message_loop/message_loop.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/strings/string_util.h" | |
16 #include "base/threading/thread_local_storage.h" | |
17 | |
18 namespace base { | |
19 namespace debug { | |
20 | |
21 namespace { | |
22 | |
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() { } | |
29 | |
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(); | |
33 | |
34 // base::debug::ConvertableToTraceFormat overrides: | |
35 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
36 AppendSystemProfileAsTraceFormat(system_stats_, out); | |
37 } | |
38 | |
39 private: | |
40 SystemMetrics system_stats_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(SystemStatsHolder); | |
43 }; | |
44 | |
45 void SystemStatsHolder::GetSystemProfilingStats() { | |
46 system_stats_ = SystemMetrics::Sample(); | |
darin (slow to review)
2013/09/12 20:51:15
nit: insert new line before close of namespace.
jwmak
2013/09/13 20:57:11
Done.
| |
47 } | |
48 } // namespace | |
49 | |
50 ////////////////////////////////////////////////////////////////////////////// | |
51 | |
52 TraceEventSystemStatsMonitor::TraceEventSystemStatsMonitor( | |
53 scoped_refptr<MessageLoopProxy> message_loop_proxy) | |
54 : message_loop_proxy_(message_loop_proxy), | |
55 weak_factory_(this) { | |
56 // Force the "system_stats" category to show up in the trace viewer. | |
57 TraceLog::GetCategoryGroupEnabled(TRACE_DISABLED_BY_DEFAULT("system_stats")); | |
58 | |
59 // Allow this to be instantiated on unsupported platforms, but don't run. | |
60 TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
61 } | |
62 | |
63 TraceEventSystemStatsMonitor::~TraceEventSystemStatsMonitor() { | |
64 if (dump_timer_.IsRunning()) | |
65 StopProfiling(); | |
66 TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
67 } | |
68 | |
69 void TraceEventSystemStatsMonitor::OnTraceLogEnabled() { | |
70 // Check to see if system tracing is enabled. | |
71 bool enabled; | |
72 | |
73 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT( | |
74 "system_stats"), &enabled); | |
75 if (!enabled) | |
76 return; | |
77 message_loop_proxy_->PostTask( | |
78 FROM_HERE, | |
79 base::Bind(&TraceEventSystemStatsMonitor::StartProfiling, | |
80 weak_factory_.GetWeakPtr())); | |
81 } | |
82 | |
83 void TraceEventSystemStatsMonitor::OnTraceLogDisabled() { | |
84 message_loop_proxy_->PostTask( | |
85 FROM_HERE, | |
86 base::Bind(&TraceEventSystemStatsMonitor::StopProfiling, | |
87 weak_factory_.GetWeakPtr())); | |
88 } | |
89 | |
90 void TraceEventSystemStatsMonitor::StartProfiling() { | |
91 // Watch for the tracing framework sending enabling more than once. | |
92 if (dump_timer_.IsRunning()) | |
93 return; | |
94 | |
95 dump_timer_.Start(FROM_HERE, | |
96 TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor:: | |
97 kSamplingIntervalMilliseconds), | |
98 base::Bind(&TraceEventSystemStatsMonitor:: | |
99 DumpSystemStats, | |
100 weak_factory_.GetWeakPtr())); | |
101 } | |
102 | |
103 // If system tracing is enabled, dumps a profile to the tracing system. | |
104 void TraceEventSystemStatsMonitor::DumpSystemStats() { | |
105 scoped_ptr<SystemStatsHolder> dump_holder(new SystemStatsHolder()); | |
106 dump_holder->GetSystemProfilingStats(); | |
107 | |
108 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | |
109 TRACE_DISABLED_BY_DEFAULT("system_stats"), | |
110 "base::TraceEventSystemStatsMonitor::SystemStats", | |
111 this, | |
112 dump_holder.PassAs<base::debug::ConvertableToTraceFormat>()); | |
113 } | |
114 | |
115 void TraceEventSystemStatsMonitor::StopProfiling() { | |
116 dump_timer_.Stop(); | |
117 } | |
118 | |
119 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const { | |
120 return dump_timer_.IsRunning(); | |
121 } | |
122 | |
123 void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics, | |
124 std::string* output) { | |
125 std::string tmp; | |
126 base::JSONWriter::Write(system_metrics.ToValue().get(), &tmp); | |
127 *output += tmp; | |
128 } | |
129 | |
130 } // namespace debug | |
131 } // namespace base | |
OLD | NEW |