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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/allocator/allocator_extension.h" | 13 #include "base/allocator/allocator_extension.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/debug/crash_logging.h" | 15 #include "base/debug/crash_logging.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/discardable_memory_allocator.h" | 19 #include "base/memory/discardable_memory_allocator.h" |
| 20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 21 #include "base/memory/shared_memory.h" | 21 #include "base/memory/shared_memory.h" |
| 22 #include "base/metrics/field_trial.h" | 22 #include "base/metrics/field_trial.h" |
| 23 #include "base/metrics/histogram_macros.h" | 23 #include "base/metrics/histogram_macros.h" |
| 24 #include "base/path_service.h" | 24 #include "base/path_service.h" |
| 25 #include "base/process/process_metrics.h" | |
| 25 #include "base/run_loop.h" | 26 #include "base/run_loop.h" |
| 26 #include "base/strings/string16.h" | 27 #include "base/strings/string16.h" |
| 27 #include "base/strings/string_number_conversions.h" | 28 #include "base/strings/string_number_conversions.h" |
| 28 #include "base/strings/string_split.h" | 29 #include "base/strings/string_split.h" |
| 29 #include "base/strings/sys_string_conversions.h" | 30 #include "base/strings/sys_string_conversions.h" |
| 30 #include "base/strings/utf_string_conversions.h" | 31 #include "base/strings/utf_string_conversions.h" |
| 31 #include "base/threading/simple_thread.h" | 32 #include "base/threading/simple_thread.h" |
| 32 #include "base/threading/thread_local.h" | 33 #include "base/threading/thread_local.h" |
| 33 #include "base/threading/thread_restrictions.h" | 34 #include "base/threading/thread_restrictions.h" |
| 34 #include "base/threading/thread_task_runner_handle.h" | 35 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 #include "content/public/common/service_manager_connection.h" | 205 #include "content/public/common/service_manager_connection.h" |
| 205 #include "content/renderer/mus/render_widget_mus_connection.h" | 206 #include "content/renderer/mus/render_widget_mus_connection.h" |
| 206 #include "content/renderer/mus/render_widget_window_tree_client_factory.h" | 207 #include "content/renderer/mus/render_widget_window_tree_client_factory.h" |
| 207 #include "services/ui/public/cpp/gpu_service.h" | 208 #include "services/ui/public/cpp/gpu_service.h" |
| 208 #endif | 209 #endif |
| 209 | 210 |
| 210 #if defined(ENABLE_IPC_FUZZER) | 211 #if defined(ENABLE_IPC_FUZZER) |
| 211 #include "content/common/external_ipc_dumper.h" | 212 #include "content/common/external_ipc_dumper.h" |
| 212 #endif | 213 #endif |
| 213 | 214 |
| 215 #if defined(OS_MACOSX) | |
| 216 #include <malloc/malloc.h> | |
| 217 #else | |
| 218 #include <malloc.h> | |
| 219 #endif | |
| 220 | |
| 214 using base::ThreadRestrictions; | 221 using base::ThreadRestrictions; |
| 215 using blink::WebDocument; | 222 using blink::WebDocument; |
| 216 using blink::WebFrame; | 223 using blink::WebFrame; |
| 217 using blink::WebNetworkStateNotifier; | 224 using blink::WebNetworkStateNotifier; |
| 218 using blink::WebRuntimeFeatures; | 225 using blink::WebRuntimeFeatures; |
| 219 using blink::WebScriptController; | 226 using blink::WebScriptController; |
| 220 using blink::WebSecurityPolicy; | 227 using blink::WebSecurityPolicy; |
| 221 using blink::WebString; | 228 using blink::WebString; |
| 222 using blink::WebView; | 229 using blink::WebView; |
| 223 using blink::scheduler::WebThreadImplForWorkerScheduler; | 230 using blink::scheduler::WebThreadImplForWorkerScheduler; |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 888 time_zone_monitor->AddClient( | 895 time_zone_monitor->AddClient( |
| 889 time_zone_monitor_binding_.CreateInterfacePtrAndBind()); | 896 time_zone_monitor_binding_.CreateInterfacePtrAndBind()); |
| 890 | 897 |
| 891 #if defined(OS_LINUX) | 898 #if defined(OS_LINUX) |
| 892 ChildProcess::current()->SetIOThreadPriority(base::ThreadPriority::DISPLAY); | 899 ChildProcess::current()->SetIOThreadPriority(base::ThreadPriority::DISPLAY); |
| 893 ChildThreadImpl::current()->SetThreadPriority( | 900 ChildThreadImpl::current()->SetThreadPriority( |
| 894 categorized_worker_pool_->background_worker_thread_id(), | 901 categorized_worker_pool_->background_worker_thread_id(), |
| 895 base::ThreadPriority::BACKGROUND); | 902 base::ThreadPriority::BACKGROUND); |
| 896 #endif | 903 #endif |
| 897 | 904 |
| 905 record_purge_suspend_metric_closure_.Reset(base::Bind( | |
| 906 &RenderThreadImpl::RecordPurgeAndSuspendMetrics, base::Unretained(this))); | |
| 898 is_renderer_suspended_ = false; | 907 is_renderer_suspended_ = false; |
| 899 } | 908 } |
| 900 | 909 |
| 901 RenderThreadImpl::~RenderThreadImpl() { | 910 RenderThreadImpl::~RenderThreadImpl() { |
| 902 } | 911 } |
| 903 | 912 |
| 904 void RenderThreadImpl::Shutdown() { | 913 void RenderThreadImpl::Shutdown() { |
| 905 FOR_EACH_OBSERVER( | 914 FOR_EACH_OBSERVER( |
| 906 RenderThreadObserver, observers_, OnRenderProcessShutdown()); | 915 RenderThreadObserver, observers_, OnRenderProcessShutdown()); |
| 907 | 916 |
| (...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1754 return handled; | 1763 return handled; |
| 1755 } | 1764 } |
| 1756 | 1765 |
| 1757 void RenderThreadImpl::OnProcessBackgrounded(bool backgrounded) { | 1766 void RenderThreadImpl::OnProcessBackgrounded(bool backgrounded) { |
| 1758 ChildThreadImpl::OnProcessBackgrounded(backgrounded); | 1767 ChildThreadImpl::OnProcessBackgrounded(backgrounded); |
| 1759 | 1768 |
| 1760 if (backgrounded) { | 1769 if (backgrounded) { |
| 1761 renderer_scheduler_->OnRendererBackgrounded(); | 1770 renderer_scheduler_->OnRendererBackgrounded(); |
| 1762 } else { | 1771 } else { |
| 1763 renderer_scheduler_->OnRendererForegrounded(); | 1772 renderer_scheduler_->OnRendererForegrounded(); |
| 1773 record_purge_suspend_metric_closure_.Cancel(); | |
| 1764 is_renderer_suspended_ = false; | 1774 is_renderer_suspended_ = false; |
| 1765 } | 1775 } |
| 1766 } | 1776 } |
| 1767 | 1777 |
| 1768 void RenderThreadImpl::OnProcessPurgeAndSuspend() { | 1778 void RenderThreadImpl::OnProcessPurgeAndSuspend() { |
| 1769 ChildThreadImpl::OnProcessPurgeAndSuspend(); | 1779 ChildThreadImpl::OnProcessPurgeAndSuspend(); |
| 1770 if (is_renderer_suspended_) | 1780 if (is_renderer_suspended_) |
| 1771 return; | 1781 return; |
| 1772 // TODO(hajimehoshi): Implement purging e.g. cache (crbug/607077) | 1782 // TODO(hajimehoshi): Implement purging e.g. cache (crbug/607077) |
| 1773 is_renderer_suspended_ = true; | 1783 is_renderer_suspended_ = true; |
| 1774 renderer_scheduler_->SuspendRenderer(); | 1784 renderer_scheduler_->SuspendRenderer(); |
| 1785 | |
| 1786 // Since purging is not a synchronouse task (e.g. v8 GC, oilpan GC, ...), | |
| 1787 // we need to wait until the task is finished. So wait 15 seconds and | |
| 1788 // update purge+suspend UMA histogram. | |
| 1789 // TODO(tasak): use MemoryCoordinator's callback to report purge+suspend | |
| 1790 // UMA when MemoryCoordinator is available. | |
| 1791 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( | |
| 1792 FROM_HERE, record_purge_suspend_metric_closure_.callback(), | |
| 1793 base::TimeDelta::FromSeconds(15)); | |
| 1794 } | |
| 1795 | |
| 1796 // TODO(tasak): Replace the following GetMallocUsage() with memory-infra | |
| 1797 // when it is possible to run memory-infra without tracing. | |
| 1798 #if defined(OS_WIN) | |
| 1799 namespace { | |
| 1800 | |
| 1801 static size_t GetMallocUsage() { | |
| 1802 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); | |
| 1803 if (number_of_heaps <= 0) | |
| 1804 return 0; | |
| 1805 | |
| 1806 size_t malloc_usage = 0; | |
| 1807 std::unique_ptr<HANDLE[]> heaps(new HANDLE[number_of_heaps]); | |
| 1808 ::GetProcessHeaps(number_of_heaps, heaps.get()); | |
| 1809 for (unsigned i = 0; i < number_of_heaps; i++) { | |
| 1810 PROCESS_HEAP_ENTRY heap_entry; | |
| 1811 ::HeapLock(heaps[i]); | |
| 1812 heap_entry.lpData = NULL; | |
| 1813 while (::HeapWalk(heaps[i], &heap_entry) != 0) { | |
| 1814 if (heap_entry.lpData == heaps.get()) | |
| 1815 continue; | |
| 1816 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) | |
| 1817 malloc_usage += heap_entry.cbData; | |
| 1818 } | |
| 1819 ::HeapUnlock(heaps[i]); | |
| 1820 } | |
| 1821 return malloc_usage; | |
| 1822 } | |
| 1823 | |
| 1824 } // namespace | |
| 1825 #elif defined(OS_MACOSX) || defined(OS_IOS) | |
| 1826 namespace { | |
| 1827 | |
| 1828 static size_t GetMallocUsage() { | |
| 1829 malloc_statistics_t stats = {0}; | |
| 1830 malloc_zone_statistics(nullptr, &stats); | |
| 1831 return stats.size_in_use; | |
| 1832 } | |
| 1833 | |
| 1834 } // namespace | |
| 1835 #endif | |
| 1836 | |
| 1837 // TODO(tasak): Once it is possible to use memory-infra without tracing, | |
| 1838 // we should collect the metrics using memory-infra. | |
| 1839 // TODO(tasak): We should also report a difference between the memory usages | |
| 1840 // before and after purging by using memory-infra. | |
| 1841 void RenderThreadImpl::RecordPurgeAndSuspendMetrics() const { | |
| 1842 // If this renderer is resumed, we should not update UMA. | |
| 1843 if (!is_renderer_suspended_) | |
| 1844 return; | |
| 1845 | |
| 1846 const char* status = base::FeatureList::IsEnabled(features::kPurgeAndSuspend) | |
| 1847 ? "Enabled" | |
| 1848 : "Disabled"; | |
|
Ilya Sherman
2016/10/11 21:28:56
You shouldn't need to add these suffixes manually.
tasak
2016/10/12 07:37:23
I see.
Done.
| |
| 1849 // TODO(tasak): Compare memory metrics between purge-enabled renderers and | |
| 1850 // purge-disabled renderers (A/B testing). | |
| 1851 blink::WebMemoryStatistics blink_stats = blink::WebMemoryStatistics::Get(); | |
| 1852 UMA_HISTOGRAM_MEMORY_KB( | |
| 1853 base::StringPrintf("Memory.PurgeAndSuspend.PartitionAllocKB.%s", status), | |
| 1854 blink_stats.partitionAllocTotalAllocatedBytes); | |
|
tasak
2016/10/12 07:37:23
I missed "/1024".
| |
| 1855 UMA_HISTOGRAM_MEMORY_KB( | |
| 1856 base::StringPrintf("Memory.PurgeAndSuspend.BlinkGCKB.%s", status), | |
| 1857 blink_stats.blinkGCTotalAllocatedBytes); | |
| 1858 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 1859 struct mallinfo minfo = mallinfo(); | |
| 1860 #if defined(USE_TCMALLOC) | |
| 1861 size_t malloc_usage = minfo.uordblks; | |
| 1862 #else | |
| 1863 size_t malloc_usage = minfo.hblkhd + minfo.arena; | |
| 1864 #endif | |
| 1865 #else | |
| 1866 size_t malloc_usage = GetMallocUsage(); | |
| 1867 #endif | |
| 1868 UMA_HISTOGRAM_MEMORY_MB( | |
| 1869 base::StringPrintf("Memory.PurgeAndSuspend.MallocMB.%s", status), | |
| 1870 malloc_usage); | |
|
tasak
2016/10/12 07:37:23
I missed "/1024/1024".
| |
| 1871 | |
| 1872 ChildDiscardableSharedMemoryManager::Statistics discardable_stats = | |
| 1873 ChildThreadImpl::discardable_shared_memory_manager()->GetStatistics(); | |
| 1874 size_t discardable_usage = | |
| 1875 discardable_stats.total_size - discardable_stats.freelist_size; | |
| 1876 UMA_HISTOGRAM_MEMORY_KB( | |
| 1877 base::StringPrintf("Memory.PurgeAndSuspend.DiscardableKB.%s", status), | |
| 1878 discardable_usage); | |
| 1879 | |
| 1880 size_t v8_usage = 0; | |
| 1881 if (v8::Isolate* isolate = blink::mainThreadIsolate()) { | |
| 1882 v8::HeapStatistics v8_heap_statistics; | |
| 1883 isolate->GetHeapStatistics(&v8_heap_statistics); | |
| 1884 v8_usage = v8_heap_statistics.total_heap_size(); | |
| 1885 } | |
| 1886 // TODO(tasak): Currently only memory usage of mainThreadIsolate() is | |
| 1887 // reported. We should collect memory usages of all isolates using | |
| 1888 // memory-infra. | |
| 1889 UMA_HISTOGRAM_MEMORY_MB( | |
| 1890 base::StringPrintf("Memory.PurgeAndSuspend.V8MainThreadIsolateMB.%s", | |
| 1891 status), | |
| 1892 v8_usage); | |
| 1893 UMA_HISTOGRAM_MEMORY_MB( | |
| 1894 base::StringPrintf("Memory.PurgeAndSuspend.TotalAllocatedMB.%s", status), | |
| 1895 blink_stats.partitionAllocTotalAllocatedBytes + | |
| 1896 blink_stats.blinkGCTotalAllocatedBytes + malloc_usage + v8_usage + | |
| 1897 discardable_usage); | |
| 1775 } | 1898 } |
| 1776 | 1899 |
| 1777 scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() { | 1900 scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() { |
| 1778 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync"); | 1901 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync"); |
| 1779 | 1902 |
| 1780 if (gpu_channel_) { | 1903 if (gpu_channel_) { |
| 1781 // Do nothing if we already have a GPU channel or are already | 1904 // Do nothing if we already have a GPU channel or are already |
| 1782 // establishing one. | 1905 // establishing one. |
| 1783 if (!gpu_channel_->IsLost()) | 1906 if (!gpu_channel_->IsLost()) |
| 1784 return gpu_channel_; | 1907 return gpu_channel_; |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2329 } | 2452 } |
| 2330 } | 2453 } |
| 2331 | 2454 |
| 2332 void RenderThreadImpl::OnRendererInterfaceRequest( | 2455 void RenderThreadImpl::OnRendererInterfaceRequest( |
| 2333 mojom::RendererAssociatedRequest request) { | 2456 mojom::RendererAssociatedRequest request) { |
| 2334 DCHECK(!renderer_binding_.is_bound()); | 2457 DCHECK(!renderer_binding_.is_bound()); |
| 2335 renderer_binding_.Bind(std::move(request)); | 2458 renderer_binding_.Bind(std::move(request)); |
| 2336 } | 2459 } |
| 2337 | 2460 |
| 2338 } // namespace content | 2461 } // namespace content |
| OLD | NEW |