Index: content/renderer/v8_heap_statistics_monitor.cc |
diff --git a/content/renderer/v8_heap_statistics_monitor.cc b/content/renderer/v8_heap_statistics_monitor.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8395b3e71221a800eeccd567ab73e0da9d39d682 |
--- /dev/null |
+++ b/content/renderer/v8_heap_statistics_monitor.cc |
@@ -0,0 +1,99 @@ |
+// 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 "content/renderer/v8_heap_statistics_monitor.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/single_thread_task_runner.h" |
+ |
+namespace content { |
+ |
+V8HeapStatisticsMonitor::V8HeapStatisticsMonitor( |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
+ : dump_pending_(false), |
+ task_runner_(task_runner), |
+ weak_factory_(this) { |
+ // Force the "v8_heap_statistics" category to show up in the trace viewer. |
+ base::debug::TraceLog::GetCategoryGroupEnabled( |
+ TRACE_DISABLED_BY_DEFAULT("v8_heap_statistics")); |
+ |
+ // Allow this to be instantiated on unsupported platforms, but don't run. |
+ base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
+} |
+ |
+V8HeapStatisticsMonitor::~V8HeapStatisticsMonitor() { |
+ if (dump_timer_.IsRunning()) |
+ StopProfiling(); |
+ base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
+} |
+ |
+bool V8HeapStatisticsMonitor::IsMonitoring() const { |
+ return dump_timer_.IsRunning(); |
+} |
+ |
+void V8HeapStatisticsMonitor::OnTraceLogEnabled() { |
+ // Check to see if system tracing is enabled. |
+ bool enabled; |
+ |
+ TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT( |
+ "v8_heap_statistics"), &enabled); |
+ if (!enabled) |
+ return; |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&V8HeapStatisticsMonitor::StartProfiling, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+void V8HeapStatisticsMonitor::OnTraceLogDisabled() { |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&V8HeapStatisticsMonitor::StopProfiling, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+void V8HeapStatisticsMonitor::StartProfiling() { |
+ // Watch for the tracing framework sending enabling more than once. |
+ if (dump_timer_.IsRunning()) |
+ return; |
+ |
+ dump_timer_.Start(FROM_HERE, |
+ base::TimeDelta::FromMilliseconds(2000), |
+ base::Bind(&V8HeapStatisticsMonitor:: |
+ BeginDumpingHeapStats, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+// If system tracing is enabled, dumps a profile to the tracing system. |
+void V8HeapStatisticsMonitor::BeginDumpingHeapStats() { |
+ if (dump_pending_) |
+ return; |
+ dump_pending_ = V8HeapStatisticsCollector::Instance()->InitiateCollection( |
+ base::Bind(&V8HeapStatisticsMonitor::FinalizeDumpOfHeapStats, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+void V8HeapStatisticsMonitor::FinalizeDumpOfHeapStats( |
+ const V8HeapStatisticsCollector::Statistics& stats) { |
+ dump_pending_ = false; |
+ // Keep trace buffer size down by only dumping changes. |
+ if (stats.used_bytes == last_dumped_stats_.used_bytes && |
+ stats.total_bytes == last_dumped_stats_.total_bytes) |
+ return; |
+ last_dumped_stats_ = stats; |
+ TRACE_COUNTER2( |
+ TRACE_DISABLED_BY_DEFAULT("v8_heap_statistics"), "v8::Heap", |
+ "used_bytes", stats.used_bytes, |
+ "free_bytes", stats.total_bytes - stats.used_bytes); |
+} |
+ |
+void V8HeapStatisticsMonitor::StopProfiling() { |
+ dump_timer_.Stop(); |
+} |
+ |
+} // namespace content |