Chromium Code Reviews| Index: content/renderer/render_thread_impl.cc |
| diff --git a/content/renderer/render_thread_impl.cc b/content/renderer/render_thread_impl.cc |
| index d1545473645b206b6758af47ee6a20a28bd2df17..0e4388d74b9ba4578472c24fb255d73d6eeb92ef 100644 |
| --- a/content/renderer/render_thread_impl.cc |
| +++ b/content/renderer/render_thread_impl.cc |
| @@ -22,6 +22,7 @@ |
| #include "base/metrics/field_trial.h" |
| #include "base/metrics/histogram_macros.h" |
| #include "base/path_service.h" |
| +#include "base/process/process_metrics.h" |
| #include "base/run_loop.h" |
| #include "base/strings/string16.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -211,6 +212,12 @@ |
| #include "content/common/external_ipc_dumper.h" |
| #endif |
| +#if defined(OS_MACOSX) |
| +#include <malloc/malloc.h> |
| +#else |
| +#include <malloc.h> |
| +#endif |
| + |
| using base::ThreadRestrictions; |
| using blink::WebDocument; |
| using blink::WebFrame; |
| @@ -895,6 +902,8 @@ void RenderThreadImpl::Init( |
| base::ThreadPriority::BACKGROUND); |
| #endif |
| + record_purge_suspend_metric_closure_.Reset(base::Bind( |
| + &RenderThreadImpl::RecordPurgeAndSuspendMetrics, base::Unretained(this))); |
| is_renderer_suspended_ = false; |
| } |
| @@ -1761,6 +1770,7 @@ void RenderThreadImpl::OnProcessBackgrounded(bool backgrounded) { |
| renderer_scheduler_->OnRendererBackgrounded(); |
| } else { |
| renderer_scheduler_->OnRendererForegrounded(); |
| + record_purge_suspend_metric_closure_.Cancel(); |
| is_renderer_suspended_ = false; |
| } |
| } |
| @@ -1772,6 +1782,125 @@ void RenderThreadImpl::OnProcessPurgeAndSuspend() { |
| // TODO(hajimehoshi): Implement purging e.g. cache (crbug/607077) |
| is_renderer_suspended_ = true; |
| renderer_scheduler_->SuspendRenderer(); |
| + |
| + // Since purging is not a synchronouse task (e.g. v8 GC, oilpan GC, ...), |
| + // we need to wait until the task is finished. So wait 15 seconds and |
| + // update purge+suspend UMA histogram. |
| + // TODO(tasak): use MemoryCoordinator's callback to report purge+suspend |
| + // UMA when MemoryCoordinator is available. |
| + GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( |
| + FROM_HERE, record_purge_suspend_metric_closure_.callback(), |
| + base::TimeDelta::FromSeconds(15)); |
| +} |
| + |
| +// TODO(tasak): Replace the following GetMallocUsage() with memory-infra |
| +// when it is possible to run memory-infra without tracing. |
| +#if defined(OS_WIN) |
| +namespace { |
| + |
| +class ScopedHeapLock { |
| + public: |
| + explicit ScopedHeapLock(HANDLE handle) : handle_(handle) { |
| + ::HeapLock(handle_); |
| + } |
| + ~ScopedHeapLock() { ::HeapUnlock(handle_); } |
| + |
| + private: |
| + ScopedHeapLock() = delete; |
| + ScopedHeapLock(const ScopedHeapLock&) = delete; |
| + |
| + HANDLE handle_; |
| +}; |
| + |
| +static size_t GetMallocUsage() { |
| + DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); |
| + if (number_of_heaps <= 0) |
| + return 0; |
| + |
| + size_t malloc_usage = 0; |
| + std::unique_ptr<HANDLE[]> heaps(new HANDLE[number_of_heaps]); |
| + ::GetProcessHeaps(number_of_heaps, heaps.get()); |
| + for (unsigned i = 0; i < number_of_heaps; i++) { |
| + PROCESS_HEAP_ENTRY heap_entry; |
| + heap_entry.lpData = NULL; |
| + { |
| + ScopedHeapLock lock(heaps[i]); |
| + while (::HeapWalk(heaps[i], &heap_entry) != 0) { |
| + if (heap_entry.lpData == heaps.get()) |
| + continue; |
| + if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) |
| + malloc_usage += heap_entry.cbData; |
| + } |
| + } |
| + } |
| + return malloc_usage; |
| +} |
| + |
| +} // namespace |
| +#endif |
| + |
| +#if defined(OS_MACOSX) || defined(OS_IOS) |
| +namespace { |
| + |
| +static size_t GetMallocUsage() { |
| + malloc_statistics_t stats = {0}; |
| + malloc_zone_statistics(nullptr, &stats); |
| + return stats.size_in_use; |
| +} |
| + |
| +} // namespace |
| +#endif |
| + |
| +// TODO(tasak): Use memory-infra when tracing v2 is enabled. |
|
haraken
2016/10/06 08:03:16
Tracing v2 is not that related. Just say "We shoul
tasak
2016/10/06 09:47:39
Done.
|
| +// TODO(tasak): Report difference of memory usage betweeen before and after |
| +// purging memory when tracing v2 is enabled. |
|
haraken
2016/10/06 08:03:17
Report a difference between the memory usages befo
tasak
2016/10/06 09:47:40
Done.
|
| +void RenderThreadImpl::RecordPurgeAndSuspendMetrics() const { |
| + // If this renderer is resumed, we should not update UMA. |
| + if (!is_renderer_suspended_) |
| + return; |
| + |
| + // TODO(tasak): firstly we are trying to compare metrics from enabled and |
| + // disabled renderers (A/B testing). However we also need to gather |
| + // metrics which measures how many memory usages are reduced. |
|
haraken
2016/10/06 08:03:17
TODO: Compare memory metrics between purge-enabled
tasak
2016/10/06 09:47:40
Done.
|
| + blink::WebMemoryStatistics blink_stats = blink::WebMemoryStatistics::Get(); |
| + UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.PartitionAllocKB", |
| + blink_stats.partitionAllocTotalAllocatedBytes); |
| + UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.BlinkGCKB", |
| + blink_stats.blinkGCTotalAllocatedBytes); |
| +#if defined(OS_LINUX) || defined(OS_ANDROID) |
| + struct mallinfo minfo = mallinfo(); |
| +#if defined(USE_TCMALLOC) |
| + size_t malloc_usage = minfo.uordblks; |
| +#else |
| + size_t malloc_usage = minfo.hblkhd + minfo.arena; |
| +#endif |
| +#else |
| + size_t malloc_usage = GetMallocUsage(); |
| +#endif |
| + UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.MallocKB", malloc_usage); |
| + |
| + ChildDiscardableSharedMemoryManager::Statistics discardable_stats = |
| + ChildThreadImpl::discardable_shared_memory_manager()->GetStatistics(); |
| + size_t discardable_usage = |
| + discardable_stats.total_size - discardable_stats.freelist_size; |
| + UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.DiscardableKB", |
| + discardable_usage); |
| + |
| + size_t v8_usage = 0; |
| + if (v8::Isolate* isolate = blink::mainThreadIsolate()) { |
| + v8::HeapStatistics v8_heap_statistics; |
| + isolate->GetHeapStatistics(&v8_heap_statistics); |
| + v8_usage = v8_heap_statistics.total_heap_size(); |
| + } |
| + // TODO(tasak): currently only blink::mainThreadIsolate()'s memory usage |
| + // is reported. When tracing v2 is enabled, use memory-infra to report |
| + // all isolates' memory usages. |
|
haraken
2016/10/06 08:03:16
TODO: Currently only memory usage of mainThreadIso
tasak
2016/10/06 09:47:40
Done.
|
| + UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.V8MainThreadIsolateKB", |
| + v8_usage); |
| + UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.TotalAllocatedKB", |
| + blink_stats.partitionAllocTotalAllocatedBytes + |
| + blink_stats.blinkGCTotalAllocatedBytes + |
| + malloc_usage + v8_usage + discardable_usage); |
| } |
| scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() { |