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

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/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"
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
49 } // namespace
50
51 //////////////////////////////////////////////////////////////////////////////
52
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"));
59
60 // Allow this to be instantiated on unsupported platforms, but don't run.
61 TraceLog::GetInstance()->AddEnabledStateObserver(this);
62 }
63
64 TraceEventSystemStatsMonitor::~TraceEventSystemStatsMonitor() {
65 if (dump_timer_.IsRunning())
66 StopProfiling();
67 TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
68 }
69
70 void TraceEventSystemStatsMonitor::OnTraceLogEnabled() {
71 // Check to see if system tracing is enabled.
72 bool enabled;
73
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()));
82 }
83
84 void TraceEventSystemStatsMonitor::OnTraceLogDisabled() {
85 task_runner_->PostTask(
86 FROM_HERE,
87 base::Bind(&TraceEventSystemStatsMonitor::StopProfiling,
88 weak_factory_.GetWeakPtr()));
89 }
90
91 void TraceEventSystemStatsMonitor::StartProfiling() {
92 // Watch for the tracing framework sending enabling more than once.
93 if (dump_timer_.IsRunning())
94 return;
95
96 dump_timer_.Start(FROM_HERE,
97 TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor::
98 kSamplingIntervalMilliseconds),
99 base::Bind(&TraceEventSystemStatsMonitor::
100 DumpSystemStats,
101 weak_factory_.GetWeakPtr()));
102 }
103
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();
108
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>());
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
« no previous file with comments | « base/debug/trace_event_system_stats_monitor.h ('k') | base/debug/trace_event_system_stats_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698