Chromium Code Reviews| Index: base/debug/trace_event_system_stats_monitor.cc |
| diff --git a/base/debug/trace_event_system_stats_monitor.cc b/base/debug/trace_event_system_stats_monitor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..23820086e88746dad6fa0935b98c62a21961ce94 |
| --- /dev/null |
| +++ b/base/debug/trace_event_system_stats_monitor.cc |
| @@ -0,0 +1,156 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/debug/trace_event_system_stats_monitor.h" |
| + |
| +#include "base/debug/leak_annotations.h" |
| +#include "base/debug/trace_event.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/threading/thread_local_storage.h" |
| + |
| +namespace base { |
| +namespace debug { |
| + |
| +namespace { |
| + |
| +///////////////////////////////////////////////////////////////////////////// |
| +// Holds profiled system stats until the tracing system needs to serialize it. |
| +class SystemProfilingStats : public base::debug::ConvertableToTraceFormat { |
| + public: |
| + SystemProfilingStats() { } |
| + virtual ~SystemProfilingStats() { } |
| + |
| + // Fills system_stats_ with profiled system memory and disk stats. |
| + // Uses the previous stats to compute rates if this is not the first profile. |
| + void GetSystemProfilingStats(SystemMetrics* prev_system_metrics, |
| + bool first_profile); |
| + |
| + // base::debug::ConvertableToTraceFormat overrides: |
| + virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { |
| + AppendSystemProfileAsTraceFormat(system_metrics_, out); |
| + } |
| + |
| + private: |
| + SystemMetrics system_metrics_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SystemProfilingStats); |
| +}; |
| + |
| +void SystemProfilingStats::GetSystemProfilingStats( |
| + SystemMetrics* prev_system_metrics, bool first_profile) { |
| + system_metrics_ = SystemMetrics::Sample(); |
| + |
| + // If this is not the first profile dump, compute rates of change. |
| + // Otherwise, stats that are rates remain zero. |
| +/* |
|
nduca
2013/08/28 23:45:40
dont commit dead code.
jwmak
2013/09/06 22:39:37
Done.
|
| + if (!first_profile) |
| + system_metrics_.ComputeRateMetricsFromPreviousState( |
| + *prev_system_metrics, |
| + TraceEventSystemStatsMonitor::kSystemStatsMonitorIntervalSeconds); |
| +*/ |
| + |
| + // Save the current non-rate stats for the next profile. |
| + *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.
|
| +} |
| +} // namespace |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +TraceEventSystemStatsMonitor::TraceEventSystemStatsMonitor( |
| + scoped_refptr<MessageLoopProxy> message_loop_proxy) |
| + : message_loop_proxy_(message_loop_proxy), |
| + weak_factory_(this) { |
| + // Force the "system_memory_and_disk" category to show up in the trace viewer. |
| + 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.
|
| + // Allow this to be instantiated on unsupported platforms, but don't run. |
| + TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| +} |
| + |
| +TraceEventSystemStatsMonitor::~TraceEventSystemStatsMonitor() { |
| + if (dump_timer_.IsRunning()) |
| + StopProfiling(); |
| + TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| +} |
| + |
| +void TraceEventSystemStatsMonitor::OnTraceLogEnabled() { |
| + // Check to see if system tracing is enabled. |
| + bool enabled; |
| + |
| + TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT( |
| + "system_memory_and_disk"), &enabled); |
| + if (!enabled) |
| + return; |
| + DVLOG(1) << "OnTraceLogEnabled"; |
|
nduca
2013/08/28 23:45:40
dont commit loggin
jwmak
2013/09/06 22:39:37
Done.
|
| + message_loop_proxy_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&TraceEventSystemStatsMonitor::StartProfiling, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void TraceEventSystemStatsMonitor::OnTraceLogDisabled() { |
| + DVLOG(1) << "OnTraceLogDisabled"; |
| + message_loop_proxy_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&TraceEventSystemStatsMonitor::StopProfiling, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void TraceEventSystemStatsMonitor::StartProfiling() { |
| + // Watch for the tracing framework sending enabling more than once. |
| + if (dump_timer_.IsRunning()) |
| + return; |
| + |
| + // Do not compute rates of change for the first profile. |
| + first_profile_ = true; |
| + |
| + dump_timer_.Start(FROM_HERE, |
| + TimeDelta::FromSeconds(TraceEventSystemStatsMonitor:: |
| + kSystemStatsMonitorIntervalSeconds), |
| + base::Bind(&TraceEventSystemStatsMonitor:: |
| + DumpSystemProfile, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +// If system tracing is enabled, dumps a profile to the tracing system. |
| +void TraceEventSystemStatsMonitor::DumpSystemProfile() { |
| + DVLOG(1) << "DumpSystemProfile"; |
|
nduca
2013/08/28 23:45:40
dont commit logging
jwmak
2013/09/06 22:39:37
Done.
|
| + |
| + scoped_ptr<SystemProfilingStats> dump_holder(new SystemProfilingStats()); |
| + dump_holder->GetSystemProfilingStats(&previous_system_metrics_, |
| + first_profile_); |
| + |
| + // After the first profile dump, compute rates of change for future dumps. |
| + 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.
|
| + |
| + const int kSnapshotId = 1; |
| + TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
| + 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.
|
| + "system_stats", |
|
nduca
2013/08/28 23:45:40
this should be base::TraceEventSystemStatsMonitor:
jwmak
2013/09/06 22:39:37
Done.
|
| + kSnapshotId, |
| + dump_holder.PassAs<base::debug::ConvertableToTraceFormat>()); |
| +} |
| + |
| +void TraceEventSystemStatsMonitor::StopProfiling() { |
| + dump_timer_.Stop(); |
| +} |
| + |
| +bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const { |
| + return dump_timer_.IsRunning(); |
| +} |
| + |
| +void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics, |
| + std::string* output) { |
| + std::string tmp; |
| + base::JSONWriter::Write(system_metrics.AsValue(), &tmp); |
| + *output += tmp; |
| +} |
| + |
| +} // namespace debug |
| +} // namespace base |