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 "content/renderer/v8_heap_statistics_monitor.h" |
| 6 |
| 7 #include "base/debug/trace_event.h" |
| 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 V8HeapStatisticsMonitor::V8HeapStatisticsMonitor( |
| 17 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 18 : dump_pending_(false), |
| 19 task_runner_(task_runner), |
| 20 weak_factory_(this) { |
| 21 // Force the "v8_heap_statistics" category to show up in the trace viewer. |
| 22 base::debug::TraceLog::GetCategoryGroupEnabled( |
| 23 TRACE_DISABLED_BY_DEFAULT("v8_heap_statistics")); |
| 24 |
| 25 // Allow this to be instantiated on unsupported platforms, but don't run. |
| 26 base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 27 } |
| 28 |
| 29 V8HeapStatisticsMonitor::~V8HeapStatisticsMonitor() { |
| 30 if (dump_timer_.IsRunning()) |
| 31 StopProfiling(); |
| 32 base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| 33 } |
| 34 |
| 35 bool V8HeapStatisticsMonitor::IsMonitoring() const { |
| 36 return dump_timer_.IsRunning(); |
| 37 } |
| 38 |
| 39 void V8HeapStatisticsMonitor::OnTraceLogEnabled() { |
| 40 // Check to see if system tracing is enabled. |
| 41 bool enabled; |
| 42 |
| 43 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT( |
| 44 "v8_heap_statistics"), &enabled); |
| 45 if (!enabled) |
| 46 return; |
| 47 task_runner_->PostTask( |
| 48 FROM_HERE, |
| 49 base::Bind(&V8HeapStatisticsMonitor::StartProfiling, |
| 50 weak_factory_.GetWeakPtr())); |
| 51 } |
| 52 |
| 53 void V8HeapStatisticsMonitor::OnTraceLogDisabled() { |
| 54 task_runner_->PostTask( |
| 55 FROM_HERE, |
| 56 base::Bind(&V8HeapStatisticsMonitor::StopProfiling, |
| 57 weak_factory_.GetWeakPtr())); |
| 58 } |
| 59 |
| 60 void V8HeapStatisticsMonitor::StartProfiling() { |
| 61 // Watch for the tracing framework sending enabling more than once. |
| 62 if (dump_timer_.IsRunning()) |
| 63 return; |
| 64 |
| 65 dump_timer_.Start(FROM_HERE, |
| 66 base::TimeDelta::FromMilliseconds(2000), |
| 67 base::Bind(&V8HeapStatisticsMonitor:: |
| 68 BeginDumpingHeapStats, |
| 69 weak_factory_.GetWeakPtr())); |
| 70 } |
| 71 |
| 72 // If system tracing is enabled, dumps a profile to the tracing system. |
| 73 void V8HeapStatisticsMonitor::BeginDumpingHeapStats() { |
| 74 if (dump_pending_) |
| 75 return; |
| 76 dump_pending_ = V8HeapStatisticsCollector::Instance()->InitiateCollection( |
| 77 base::Bind(&V8HeapStatisticsMonitor::FinalizeDumpOfHeapStats, |
| 78 weak_factory_.GetWeakPtr())); |
| 79 } |
| 80 |
| 81 void V8HeapStatisticsMonitor::FinalizeDumpOfHeapStats( |
| 82 const V8HeapStatisticsCollector::Statistics& stats) { |
| 83 dump_pending_ = false; |
| 84 // Keep trace buffer size down by only dumping changes. |
| 85 if (stats.used_bytes == last_dumped_stats_.used_bytes && |
| 86 stats.total_bytes == last_dumped_stats_.total_bytes) |
| 87 return; |
| 88 last_dumped_stats_ = stats; |
| 89 TRACE_COUNTER2( |
| 90 TRACE_DISABLED_BY_DEFAULT("v8_heap_statistics"), "v8::Heap", |
| 91 "used_bytes", stats.used_bytes, |
| 92 "free_bytes", stats.total_bytes - stats.used_bytes); |
| 93 } |
| 94 |
| 95 void V8HeapStatisticsMonitor::StopProfiling() { |
| 96 dump_timer_.Stop(); |
| 97 } |
| 98 |
| 99 } // namespace content |
OLD | NEW |