Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: base/debug/trace_event_system_stats_monitor.cc

Issue 22836004: Chrome tracing for system-wide performance stats. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@asvalue
Patch Set: rebase Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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();
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::FromSeconds(TraceEventSystemStatsMonitor::
97 kSystemStatsMonitorIntervalSeconds),
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 const int kSnapshotId = 1;
nduca 2013/09/07 07:19:20 just use this as your id. it wont collide that way
jwmak 2013/09/08 09:26:05 Done.
109 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
110 TRACE_DISABLED_BY_DEFAULT("system_stats"),
111 "base::TraceEventSystemStatsMonitor::SystemStats",
112 kSnapshotId,
113 dump_holder.PassAs<base::debug::ConvertableToTraceFormat>());
114 }
115
116 void TraceEventSystemStatsMonitor::StopProfiling() {
117 dump_timer_.Stop();
118 }
119
120 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const {
121 return dump_timer_.IsRunning();
122 }
123
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;
129 }
130
131 } // namespace debug
132 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698