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 SystemProfilingStats : public base::debug::ConvertableToTraceFormat { | |
26 public: | |
27 SystemProfilingStats() { } | |
28 virtual ~SystemProfilingStats() { } | |
29 | |
30 // Fills system_stats_ 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(SystemMetrics* prev_system_metrics, | |
33 bool first_profile); | |
34 | |
35 // base::debug::ConvertableToTraceFormat overrides: | |
36 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
37 AppendSystemProfileAsTraceFormat(system_metrics_, out); | |
38 } | |
39 | |
40 private: | |
41 SystemMetrics system_metrics_; | |
42 | |
43 DISALLOW_COPY_AND_ASSIGN(SystemProfilingStats); | |
44 }; | |
45 | |
46 void SystemProfilingStats::GetSystemProfilingStats( | |
47 SystemMetrics* prev_system_metrics, bool first_profile) { | |
48 system_metrics_ = SystemMetrics::Sample(); | |
49 | |
50 // If this is not the first profile dump, compute rates of change. | |
51 // Otherwise, stats that are rates remain zero. | |
52 /* | |
nduca
2013/08/28 23:45:40
dont commit dead code.
jwmak
2013/09/06 22:39:37
Done.
| |
53 if (!first_profile) | |
54 system_metrics_.ComputeRateMetricsFromPreviousState( | |
55 *prev_system_metrics, | |
56 TraceEventSystemStatsMonitor::kSystemStatsMonitorIntervalSeconds); | |
57 */ | |
58 | |
59 // Save the current non-rate stats for the next profile. | |
60 *prev_system_metrics = system_metrics_; | |
nduca
2013/08/28 23:45:40
Remove the prev_blahblah stuff until you add in th
jwmak
2013/09/06 22:39:37
Done.
| |
61 } | |
62 } // namespace | |
63 | |
64 ////////////////////////////////////////////////////////////////////////////// | |
65 | |
66 TraceEventSystemStatsMonitor::TraceEventSystemStatsMonitor( | |
67 scoped_refptr<MessageLoopProxy> message_loop_proxy) | |
68 : message_loop_proxy_(message_loop_proxy), | |
69 weak_factory_(this) { | |
70 // Force the "system_memory_and_disk" category to show up in the trace viewer. | |
71 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("system_memory_and_disk"), "init"); | |
nduca
2013/08/28 23:45:40
You can do this just by getting the category itsel
jwmak
2013/09/06 22:39:37
Done.
| |
72 // Allow this to be instantiated on unsupported platforms, but don't run. | |
73 TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
74 } | |
75 | |
76 TraceEventSystemStatsMonitor::~TraceEventSystemStatsMonitor() { | |
77 if (dump_timer_.IsRunning()) | |
78 StopProfiling(); | |
79 TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
80 } | |
81 | |
82 void TraceEventSystemStatsMonitor::OnTraceLogEnabled() { | |
83 // Check to see if system tracing is enabled. | |
84 bool enabled; | |
85 | |
86 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT( | |
87 "system_memory_and_disk"), &enabled); | |
88 if (!enabled) | |
89 return; | |
90 DVLOG(1) << "OnTraceLogEnabled"; | |
nduca
2013/08/28 23:45:40
dont commit loggin
jwmak
2013/09/06 22:39:37
Done.
| |
91 message_loop_proxy_->PostTask( | |
92 FROM_HERE, | |
93 base::Bind(&TraceEventSystemStatsMonitor::StartProfiling, | |
94 weak_factory_.GetWeakPtr())); | |
95 } | |
96 | |
97 void TraceEventSystemStatsMonitor::OnTraceLogDisabled() { | |
98 DVLOG(1) << "OnTraceLogDisabled"; | |
99 message_loop_proxy_->PostTask( | |
100 FROM_HERE, | |
101 base::Bind(&TraceEventSystemStatsMonitor::StopProfiling, | |
102 weak_factory_.GetWeakPtr())); | |
103 } | |
104 | |
105 void TraceEventSystemStatsMonitor::StartProfiling() { | |
106 // Watch for the tracing framework sending enabling more than once. | |
107 if (dump_timer_.IsRunning()) | |
108 return; | |
109 | |
110 // Do not compute rates of change for the first profile. | |
111 first_profile_ = true; | |
112 | |
113 dump_timer_.Start(FROM_HERE, | |
114 TimeDelta::FromSeconds(TraceEventSystemStatsMonitor:: | |
115 kSystemStatsMonitorIntervalSeconds), | |
116 base::Bind(&TraceEventSystemStatsMonitor:: | |
117 DumpSystemProfile, | |
118 weak_factory_.GetWeakPtr())); | |
119 } | |
120 | |
121 // If system tracing is enabled, dumps a profile to the tracing system. | |
122 void TraceEventSystemStatsMonitor::DumpSystemProfile() { | |
123 DVLOG(1) << "DumpSystemProfile"; | |
nduca
2013/08/28 23:45:40
dont commit logging
jwmak
2013/09/06 22:39:37
Done.
| |
124 | |
125 scoped_ptr<SystemProfilingStats> dump_holder(new SystemProfilingStats()); | |
126 dump_holder->GetSystemProfilingStats(&previous_system_metrics_, | |
127 first_profile_); | |
128 | |
129 // After the first profile dump, compute rates of change for future dumps. | |
130 first_profile_ = false; | |
nduca
2013/08/28 23:45:40
remove this until you add this stuff
jwmak
2013/09/06 22:39:37
Done.
| |
131 | |
132 const int kSnapshotId = 1; | |
133 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | |
134 TRACE_DISABLED_BY_DEFAULT("system_memory_and_disk"), | |
nduca
2013/08/28 23:45:40
you need a name for this system. oyu're calling it
jwmak
2013/09/06 22:39:37
Done.
| |
135 "system_stats", | |
nduca
2013/08/28 23:45:40
this should be base::TraceEventSystemStatsMonitor:
jwmak
2013/09/06 22:39:37
Done.
| |
136 kSnapshotId, | |
137 dump_holder.PassAs<base::debug::ConvertableToTraceFormat>()); | |
138 } | |
139 | |
140 void TraceEventSystemStatsMonitor::StopProfiling() { | |
141 dump_timer_.Stop(); | |
142 } | |
143 | |
144 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const { | |
145 return dump_timer_.IsRunning(); | |
146 } | |
147 | |
148 void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics, | |
149 std::string* output) { | |
150 std::string tmp; | |
151 base::JSONWriter::Write(system_metrics.AsValue(), &tmp); | |
152 *output += tmp; | |
153 } | |
154 | |
155 } // namespace debug | |
156 } // namespace base | |
OLD | NEW |