Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/render_thread_impl.h" | 5 #include "content/renderer/render_thread_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 895 GetConnector()->BindInterface(mojom::kBrowserServiceName, | 895 GetConnector()->BindInterface(mojom::kBrowserServiceName, |
| 896 mojo::MakeRequest(&storage_partition_service_)); | 896 mojo::MakeRequest(&storage_partition_service_)); |
| 897 | 897 |
| 898 #if defined(OS_LINUX) | 898 #if defined(OS_LINUX) |
| 899 ChildProcess::current()->SetIOThreadPriority(base::ThreadPriority::DISPLAY); | 899 ChildProcess::current()->SetIOThreadPriority(base::ThreadPriority::DISPLAY); |
| 900 ChildThreadImpl::current()->SetThreadPriority( | 900 ChildThreadImpl::current()->SetThreadPriority( |
| 901 categorized_worker_pool_->background_worker_thread_id(), | 901 categorized_worker_pool_->background_worker_thread_id(), |
| 902 base::ThreadPriority::BACKGROUND); | 902 base::ThreadPriority::BACKGROUND); |
| 903 #endif | 903 #endif |
| 904 | 904 |
| 905 record_purge_suspend_growth_metric_closure_.Reset( | 905 process_foregrounded_count_ = 0; |
| 906 base::Bind(&RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics, | |
| 907 base::Unretained(this))); | |
| 908 needs_to_record_first_active_paint_ = false; | 906 needs_to_record_first_active_paint_ = false; |
| 909 | 907 |
| 910 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | 908 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
| 911 | 909 |
| 912 // If this renderer doesn't run inside the browser process, enable | 910 // If this renderer doesn't run inside the browser process, enable |
| 913 // SequencedWorkerPool. Otherwise, it should already have been enabled. | 911 // SequencedWorkerPool. Otherwise, it should already have been enabled. |
| 914 // TODO(fdoray): Remove this once the SequencedWorkerPool to TaskScheduler | 912 // TODO(fdoray): Remove this once the SequencedWorkerPool to TaskScheduler |
| 915 // redirection experiment concludes https://crbug.com/622400. | 913 // redirection experiment concludes https://crbug.com/622400. |
| 916 if (!command_line.HasSwitch(switches::kSingleProcess)) | 914 if (!command_line.HasSwitch(switches::kSingleProcess)) |
| 917 base::SequencedWorkerPool::EnableForProcess(); | 915 base::SequencedWorkerPool::EnableForProcess(); |
| (...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1651 } | 1649 } |
| 1652 | 1650 |
| 1653 void RenderThreadImpl::OnProcessBackgrounded(bool backgrounded) { | 1651 void RenderThreadImpl::OnProcessBackgrounded(bool backgrounded) { |
| 1654 ChildThreadImpl::OnProcessBackgrounded(backgrounded); | 1652 ChildThreadImpl::OnProcessBackgrounded(backgrounded); |
| 1655 | 1653 |
| 1656 if (backgrounded) { | 1654 if (backgrounded) { |
| 1657 renderer_scheduler_->OnRendererBackgrounded(); | 1655 renderer_scheduler_->OnRendererBackgrounded(); |
| 1658 needs_to_record_first_active_paint_ = false; | 1656 needs_to_record_first_active_paint_ = false; |
| 1659 } else { | 1657 } else { |
| 1660 renderer_scheduler_->OnRendererForegrounded(); | 1658 renderer_scheduler_->OnRendererForegrounded(); |
| 1661 | 1659 process_foregrounded_count_++; |
| 1662 record_purge_suspend_growth_metric_closure_.Cancel(); | |
| 1663 record_purge_suspend_growth_metric_closure_.Reset( | |
| 1664 base::Bind(&RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics, | |
| 1665 base::Unretained(this))); | |
| 1666 } | 1660 } |
| 1667 } | 1661 } |
| 1668 | 1662 |
| 1669 void RenderThreadImpl::OnProcessPurgeAndSuspend() { | 1663 void RenderThreadImpl::OnProcessPurgeAndSuspend() { |
| 1670 ChildThreadImpl::OnProcessPurgeAndSuspend(); | 1664 ChildThreadImpl::OnProcessPurgeAndSuspend(); |
| 1671 if (!RendererIsHidden()) | 1665 if (!RendererIsHidden()) |
| 1672 return; | 1666 return; |
| 1673 | 1667 |
| 1674 if (!base::FeatureList::IsEnabled(features::kPurgeAndSuspend)) | 1668 if (!base::FeatureList::IsEnabled(features::kPurgeAndSuspend)) |
| 1675 return; | 1669 return; |
| 1676 | 1670 |
| 1677 base::MemoryCoordinatorClientRegistry::GetInstance()->PurgeMemory(); | 1671 base::MemoryCoordinatorClientRegistry::GetInstance()->PurgeMemory(); |
| 1678 needs_to_record_first_active_paint_ = true; | 1672 needs_to_record_first_active_paint_ = true; |
| 1679 | 1673 |
| 1680 RendererMemoryMetrics memory_metrics; | 1674 RendererMemoryMetrics memory_metrics; |
| 1681 if (!GetRendererMemoryMetrics(&memory_metrics)) | 1675 if (!GetRendererMemoryMetrics(&memory_metrics)) |
| 1682 return; | 1676 return; |
| 1683 | 1677 |
| 1684 purge_and_suspend_memory_metrics_ = memory_metrics; | 1678 purge_and_suspend_memory_metrics_ = memory_metrics; |
| 1685 // record how many memory usage increases after purged. | 1679 // record how many memory usage increases after purged. |
| 1680 // Since RenderThreadImpl is kept alive while its render process is alive, | |
| 1681 // so it is possible to use base::Unretained(this) here. | |
|
Avi (use Gerrit)
2017/05/12 15:41:23
grammar nit: drop "so".
"Since RenderThreadImpl i
tasak
2017/05/15 06:50:32
Done.
| |
| 1686 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( | 1682 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( |
| 1687 FROM_HERE, record_purge_suspend_growth_metric_closure_.callback(), | 1683 FROM_HERE, |
| 1688 base::TimeDelta::FromMinutes(5)); | 1684 base::Bind(&RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics, |
| 1685 base::Unretained(this), "30min", process_foregrounded_count_), | |
| 1686 base::TimeDelta::FromMinutes(30)); | |
| 1689 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( | 1687 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( |
| 1690 FROM_HERE, record_purge_suspend_growth_metric_closure_.callback(), | 1688 FROM_HERE, |
| 1691 base::TimeDelta::FromMinutes(10)); | 1689 base::Bind(&RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics, |
| 1690 base::Unretained(this), "60min", process_foregrounded_count_), | |
| 1691 base::TimeDelta::FromMinutes(60)); | |
| 1692 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( | 1692 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( |
| 1693 FROM_HERE, record_purge_suspend_growth_metric_closure_.callback(), | 1693 FROM_HERE, |
| 1694 base::TimeDelta::FromMinutes(15)); | 1694 base::Bind(&RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics, |
| 1695 base::Unretained(this), "90min", process_foregrounded_count_), | |
| 1696 base::TimeDelta::FromMinutes(90)); | |
| 1695 } | 1697 } |
| 1696 | 1698 |
| 1697 // TODO(tasak): Replace the following GetMallocUsage() with memory-infra | 1699 // TODO(tasak): Replace the following GetMallocUsage() with memory-infra |
| 1698 // when it is possible to run memory-infra without tracing. | 1700 // when it is possible to run memory-infra without tracing. |
| 1699 #if defined(OS_WIN) | 1701 #if defined(OS_WIN) |
| 1700 namespace { | 1702 namespace { |
| 1701 | 1703 |
| 1702 static size_t GetMallocUsage() { | 1704 static size_t GetMallocUsage() { |
| 1703 // Iterate through whichever heap the CRT is using. | 1705 // Iterate through whichever heap the CRT is using. |
| 1704 HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle()); | 1706 HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle()); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1786 total_allocated / render_view_count / 1024 / 1024; | 1788 total_allocated / render_view_count / 1024 / 1024; |
| 1787 | 1789 |
| 1788 return true; | 1790 return true; |
| 1789 } | 1791 } |
| 1790 | 1792 |
| 1791 #define GET_MEMORY_GROWTH(current, previous, allocator) \ | 1793 #define GET_MEMORY_GROWTH(current, previous, allocator) \ |
| 1792 (current.allocator > previous.allocator \ | 1794 (current.allocator > previous.allocator \ |
| 1793 ? current.allocator - previous.allocator \ | 1795 ? current.allocator - previous.allocator \ |
| 1794 : 0) | 1796 : 0) |
| 1795 | 1797 |
| 1796 void RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics() const { | 1798 #define UMA_HISTOGRAM_MEMORY_GROWTH_KB(basename, suffix, memory_usage) \ |
| 1799 { \ | |
| 1800 std::string histogram_name = \ | |
| 1801 base::StringPrintf("%s.%s", basename, suffix); \ | |
| 1802 UMA_HISTOGRAM_MEMORY_KB(histogram_name, memory_usage); \ | |
| 1803 } | |
| 1804 | |
| 1805 void RenderThreadImpl::RecordPurgeAndSuspendMemoryGrowthMetrics( | |
| 1806 const char* suffix, | |
| 1807 int foregrounded_count_when_purged) { | |
| 1797 // If this renderer is resumed, we should not update UMA. | 1808 // If this renderer is resumed, we should not update UMA. |
| 1798 if (!RendererIsHidden()) | 1809 if (!RendererIsHidden()) |
| 1799 return; | 1810 return; |
| 1811 if (foregrounded_count_when_purged != process_foregrounded_count_) | |
| 1812 return; | |
| 1800 | 1813 |
| 1801 RendererMemoryMetrics memory_metrics; | 1814 RendererMemoryMetrics memory_metrics; |
| 1802 if (!GetRendererMemoryMetrics(&memory_metrics)) | 1815 if (!GetRendererMemoryMetrics(&memory_metrics)) |
| 1803 return; | 1816 return; |
| 1804 | 1817 |
| 1805 UMA_HISTOGRAM_MEMORY_KB( | 1818 UMA_HISTOGRAM_MEMORY_GROWTH_KB( |
| 1806 "PurgeAndSuspend.Experimental.MemoryGrowth.PartitionAllocKB", | 1819 "PurgeAndSuspend.Experimental.MemoryGrowth.PartitionAllocKB", suffix, |
| 1807 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, | 1820 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, |
| 1808 partition_alloc_kb)); | 1821 partition_alloc_kb)); |
| 1809 UMA_HISTOGRAM_MEMORY_KB( | 1822 UMA_HISTOGRAM_MEMORY_GROWTH_KB( |
| 1810 "PurgeAndSuspend.Experimental.MemoryGrowth.BlinkGCKB", | 1823 "PurgeAndSuspend.Experimental.MemoryGrowth.BlinkGCKB", suffix, |
| 1811 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, | 1824 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, |
| 1812 blink_gc_kb)); | 1825 blink_gc_kb)); |
| 1813 UMA_HISTOGRAM_MEMORY_KB( | 1826 UMA_HISTOGRAM_MEMORY_GROWTH_KB( |
| 1814 "PurgeAndSuspend.Experimental.MemoryGrowth.MallocKB", | 1827 "PurgeAndSuspend.Experimental.MemoryGrowth.MallocKB", suffix, |
| 1815 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, | 1828 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, |
| 1816 malloc_mb) * 1024); | 1829 malloc_mb) * |
| 1817 UMA_HISTOGRAM_MEMORY_KB( | 1830 1024); |
| 1818 "PurgeAndSuspend.Experimental.MemoryGrowth.DiscardableKB", | 1831 UMA_HISTOGRAM_MEMORY_GROWTH_KB( |
| 1832 "PurgeAndSuspend.Experimental.MemoryGrowth.DiscardableKB", suffix, | |
| 1819 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, | 1833 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, |
| 1820 discardable_kb)); | 1834 discardable_kb)); |
| 1821 UMA_HISTOGRAM_MEMORY_KB( | 1835 UMA_HISTOGRAM_MEMORY_GROWTH_KB( |
| 1822 "PurgeAndSuspend.Experimental.MemoryGrowth.V8MainThreadIsolateKB", | 1836 "PurgeAndSuspend.Experimental.MemoryGrowth.V8MainThreadIsolateKB", suffix, |
| 1823 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, | 1837 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, |
| 1824 v8_main_thread_isolate_mb) * 1024); | 1838 v8_main_thread_isolate_mb) * |
| 1825 UMA_HISTOGRAM_MEMORY_KB( | 1839 1024); |
| 1826 "PurgeAndSuspend.Experimental.MemoryGrowth.TotalAllocatedKB", | 1840 UMA_HISTOGRAM_MEMORY_GROWTH_KB( |
| 1841 "PurgeAndSuspend.Experimental.MemoryGrowth.TotalAllocatedKB", suffix, | |
| 1827 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, | 1842 GET_MEMORY_GROWTH(memory_metrics, purge_and_suspend_memory_metrics_, |
| 1828 total_allocated_mb) * 1024); | 1843 total_allocated_mb) * |
| 1844 1024); | |
| 1829 } | 1845 } |
| 1830 | 1846 |
| 1831 scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() { | 1847 scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() { |
| 1832 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync"); | 1848 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync"); |
| 1833 | 1849 |
| 1834 if (gpu_channel_) { | 1850 if (gpu_channel_) { |
| 1835 // Do nothing if we already have a GPU channel or are already | 1851 // Do nothing if we already have a GPU channel or are already |
| 1836 // establishing one. | 1852 // establishing one. |
| 1837 if (!gpu_channel_->IsLost()) | 1853 if (!gpu_channel_->IsLost()) |
| 1838 return gpu_channel_; | 1854 return gpu_channel_; |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2441 } | 2457 } |
| 2442 } | 2458 } |
| 2443 | 2459 |
| 2444 void RenderThreadImpl::OnRendererInterfaceRequest( | 2460 void RenderThreadImpl::OnRendererInterfaceRequest( |
| 2445 mojom::RendererAssociatedRequest request) { | 2461 mojom::RendererAssociatedRequest request) { |
| 2446 DCHECK(!renderer_binding_.is_bound()); | 2462 DCHECK(!renderer_binding_.is_bound()); |
| 2447 renderer_binding_.Bind(std::move(request)); | 2463 renderer_binding_.Bind(std::move(request)); |
| 2448 } | 2464 } |
| 2449 | 2465 |
| 2450 } // namespace content | 2466 } // namespace content |
| OLD | NEW |