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_collector_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "content/renderer/render_thread_impl.h" |
| 10 #include "v8/include/v8.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 static const int kWaitForWorkersStatsTimeoutMS = 20; |
| 15 |
| 16 V8HeapStatisticsCollector* V8HeapStatisticsCollector::Instance() { |
| 17 return V8HeapStatisticsCollectorImpl::Instance(); |
| 18 } |
| 19 |
| 20 V8HeapStatisticsCollector* V8HeapStatisticsCollectorImpl::Instance() { |
| 21 CR_DEFINE_STATIC_LOCAL(V8HeapStatisticsCollectorImpl, instance, ()); |
| 22 return &instance; |
| 23 } |
| 24 |
| 25 V8HeapStatisticsCollectorImpl::V8HeapStatisticsCollectorImpl() |
| 26 : round_id_(0) {} |
| 27 |
| 28 V8HeapStatisticsCollectorImpl::~V8HeapStatisticsCollectorImpl() { |
| 29 } |
| 30 |
| 31 bool V8HeapStatisticsCollectorImpl::InitiateCollection( |
| 32 const ResultCallback& callback) { |
| 33 DCHECK(RenderThreadImpl::Get()); |
| 34 if (pending_result_callback_) |
| 35 return false; |
| 36 pending_result_callback_.reset(new ResultCallback(callback)); |
| 37 |
| 38 v8::HeapStatistics heap_stats; |
| 39 v8::Isolate::GetCurrent()->GetHeapStatistics(&heap_stats); |
| 40 pending_stats_.total_bytes = heap_stats.total_heap_size(); |
| 41 pending_stats_.used_bytes = heap_stats.used_heap_size(); |
| 42 base::Closure collect = base::Bind( |
| 43 &V8HeapStatisticsCollectorImpl::CollectOnWorkerThread, |
| 44 base::Unretained(this), |
| 45 base::MessageLoopProxy::current(), |
| 46 round_id_); |
| 47 workers_to_go_ = RenderThreadImpl::Get()->PostTaskToAllWebWorkers(collect); |
| 48 if (workers_to_go_) { |
| 49 // The guard task to send out partial stats |
| 50 // in case some workers are not responsive. |
| 51 base::MessageLoopProxy::current()->PostDelayedTask( |
| 52 FROM_HERE, |
| 53 base::Bind(&V8HeapStatisticsCollectorImpl::SendStatsToCallback, |
| 54 base::Unretained(this), |
| 55 round_id_), |
| 56 base::TimeDelta::FromMilliseconds(kWaitForWorkersStatsTimeoutMS)); |
| 57 } else { |
| 58 // No worker threads so just send out the main thread data immediately, via |
| 59 // a postTask so that the behavior of sync completion matches the |
| 60 // asynchronous behavior. |
| 61 base::MessageLoopProxy::current()->PostTask( |
| 62 FROM_HERE, |
| 63 base::Bind(&V8HeapStatisticsCollectorImpl::SendStatsToCallback, |
| 64 base::Unretained(this), |
| 65 round_id_)); |
| 66 } |
| 67 return true; |
| 68 } |
| 69 |
| 70 void V8HeapStatisticsCollectorImpl::CollectOnWorkerThread( |
| 71 scoped_refptr<base::TaskRunner> master, |
| 72 int round_id) { |
| 73 |
| 74 size_t total_bytes = 0; |
| 75 size_t used_bytes = 0; |
| 76 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 77 if (isolate) { |
| 78 v8::HeapStatistics heap_stats; |
| 79 isolate->GetHeapStatistics(&heap_stats); |
| 80 total_bytes = heap_stats.total_heap_size(); |
| 81 used_bytes = heap_stats.used_heap_size(); |
| 82 } |
| 83 master->PostTask( |
| 84 FROM_HERE, |
| 85 base::Bind(&V8HeapStatisticsCollectorImpl::ReceiveStats, |
| 86 base::Unretained(this), |
| 87 round_id, |
| 88 total_bytes, |
| 89 used_bytes)); |
| 90 } |
| 91 |
| 92 void V8HeapStatisticsCollectorImpl::ReceiveStats(int round_id, |
| 93 size_t total_bytes, |
| 94 size_t used_bytes) { |
| 95 if (round_id != round_id_) |
| 96 return; |
| 97 pending_stats_.total_bytes += total_bytes; |
| 98 pending_stats_.used_bytes += used_bytes; |
| 99 if (!--workers_to_go_) |
| 100 SendStatsToCallback(round_id); |
| 101 } |
| 102 |
| 103 void V8HeapStatisticsCollectorImpl::SendStatsToCallback(int round_id) { |
| 104 if (round_id != round_id_) |
| 105 return; |
| 106 |
| 107 // TODO(alph): Do caching heap stats and use the cache if we haven't got |
| 108 // reply from a worker. |
| 109 // Currently a busy worker stats are not counted. |
| 110 pending_result_callback_->Run(pending_stats_); |
| 111 pending_result_callback_.reset(); |
| 112 ++round_id_; |
| 113 } |
| 114 |
| 115 } // namespace content |
OLD | NEW |