| 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/mojo_shell_connection.h" | 205 #include "content/public/common/mojo_shell_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 1528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1752 } else { | 1759 } else { |
| 1753 renderer_scheduler_->OnRendererForegrounded(); | 1760 renderer_scheduler_->OnRendererForegrounded(); |
| 1754 is_renderer_suspended_ = false; | 1761 is_renderer_suspended_ = false; |
| 1755 } | 1762 } |
| 1756 } | 1763 } |
| 1757 | 1764 |
| 1758 void RenderThreadImpl::OnProcessPurgeAndSuspend() { | 1765 void RenderThreadImpl::OnProcessPurgeAndSuspend() { |
| 1759 ChildThreadImpl::OnProcessPurgeAndSuspend(); | 1766 ChildThreadImpl::OnProcessPurgeAndSuspend(); |
| 1760 if (is_renderer_suspended_) | 1767 if (is_renderer_suspended_) |
| 1761 return; | 1768 return; |
| 1762 // TODO(hajimehoshi): Implement purging e.g. cache (crbug/607077) | 1769 |
| 1770 // If PurgeAndSuspendFeature is disabled, do nothing except updating UMA. |
| 1771 // The data is used as "Force Disabled" metric. |
| 1772 if (base::FeatureList::IsEnabled(features::kPurgeAndSuspend)) { |
| 1773 // TODO(tasak): use MemoryCoordinator instead of NotifyMemoryPressure. |
| 1774 base::MemoryPressureListener::NotifyMemoryPressure( |
| 1775 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 1776 base::allocator::ReleaseFreeMemory(); |
| 1777 |
| 1778 renderer_scheduler_->SuspendRenderer(); |
| 1779 } |
| 1763 is_renderer_suspended_ = true; | 1780 is_renderer_suspended_ = true; |
| 1764 renderer_scheduler_->SuspendRenderer(); | 1781 |
| 1782 // Since purging is not a synchronouse task (e.g. v8 GC, oilpan GC, ...), |
| 1783 // we need to wait until the task is finished. So wait 15 seconds and |
| 1784 // update purge+suspend UMA histogram. |
| 1785 // TODO(tasak): use MemoryCoordinator's callback to report purge+suspend |
| 1786 // UMA when MemoryCoordinator is available. |
| 1787 GetRendererScheduler()->DefaultTaskRunner()->PostDelayedTask( |
| 1788 FROM_HERE, |
| 1789 base::Bind(&RenderThreadImpl::OnDumpMemoryUsage, base::Unretained(this)), |
| 1790 base::TimeDelta::FromSeconds(15)); |
| 1791 } |
| 1792 |
| 1793 // TODO(tasak): Replace the following GetMallocUsage() with memory-infra |
| 1794 // when it is possible to run memory-infra without tracing. |
| 1795 #if defined(OS_WIN) |
| 1796 namespace { |
| 1797 |
| 1798 class ScopedHeapLock { |
| 1799 public: |
| 1800 explicit ScopedHeapLock(HANDLE handle) : handle_(handle) { |
| 1801 ::HeapLock(handle_); |
| 1802 } |
| 1803 ~ScopedHeapLock() { ::HeapUnlock(handle_); } |
| 1804 |
| 1805 private: |
| 1806 ScopedHeapLock() = delete; |
| 1807 ScopedHeapLock(const ScopedHeapLock&) = delete; |
| 1808 |
| 1809 HANDLE handle_; |
| 1810 }; |
| 1811 |
| 1812 static size_t GetMallocUsage() { |
| 1813 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); |
| 1814 if (number_of_heaps <= 0) |
| 1815 return 0; |
| 1816 |
| 1817 size_t malloc_usage = 0; |
| 1818 std::unique_ptr<HANDLE[]> heaps(new HANDLE[number_of_heaps]); |
| 1819 ::GetProcessHeaps(number_of_heaps, heaps.get()); |
| 1820 for (unsigned i = 0; i < number_of_heaps; i++) { |
| 1821 PROCESS_HEAP_ENTRY heap_entry; |
| 1822 heap_entry.lpData = NULL; |
| 1823 { |
| 1824 ScopedHeapLock lock(heaps[i]); |
| 1825 while (::HeapWalk(heaps[i], &heap_entry) != 0) { |
| 1826 if (heap_entry.lpData == heaps.get()) |
| 1827 continue; |
| 1828 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) |
| 1829 malloc_usage += heap_entry.cbData; |
| 1830 } |
| 1831 } |
| 1832 } |
| 1833 return malloc_usage; |
| 1834 } |
| 1835 |
| 1836 } // namespace |
| 1837 #endif |
| 1838 |
| 1839 #if defined(OS_MACOSX) || defined(OS_IOS) |
| 1840 namespace { |
| 1841 |
| 1842 static size_t GetMallocUsage() { |
| 1843 malloc_statistics_t stats = {0}; |
| 1844 malloc_zone_statistics(nullptr, &stats); |
| 1845 return stats.size_in_use; |
| 1846 } |
| 1847 |
| 1848 } // namespace |
| 1849 #endif |
| 1850 |
| 1851 // For purge+suspend, report memory usage diff by using UMA_HISTOGRAM. |
| 1852 void RenderThreadImpl::OnDumpMemoryUsage() const { |
| 1853 // If this renderer is resumed, we should not update UMA. |
| 1854 if (!is_renderer_suspended_) |
| 1855 return; |
| 1856 |
| 1857 // TODO(tasak): firstly we are trying to compare metrics from enabled and |
| 1858 // disabled renderers (A/B testing). However we also need to gather |
| 1859 // metrics which measures how many memory usages are reduced. |
| 1860 blink::WebMemoryStatistics blink_stats = blink::WebMemoryStatistics::Get(); |
| 1861 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.PartitionAllocKB", |
| 1862 blink_stats.partitionAllocTotalAllocatedBytes); |
| 1863 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.BlinkGCKB", |
| 1864 blink_stats.blinkGCTotalAllocatedBytes); |
| 1865 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 1866 struct mallinfo minfo = mallinfo(); |
| 1867 #if defined(USE_TCMALLOC) |
| 1868 size_t malloc_usage = minfo.uordblks; |
| 1869 #else |
| 1870 size_t malloc_usage = minfo.hblkhd + minfo.arena; |
| 1871 #endif |
| 1872 #else |
| 1873 size_t malloc_usage = GetMallocUsage(); |
| 1874 #endif |
| 1875 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.MallocKB", malloc_usage); |
| 1876 |
| 1877 ChildDiscardableSharedMemoryManager::Statistics discardable_stats = |
| 1878 ChildThreadImpl::discardable_shared_memory_manager()->GetStatistics(); |
| 1879 size_t discardable_usage = |
| 1880 discardable_stats.total_size - discardable_stats.freelist_size; |
| 1881 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.DiscardableKB", |
| 1882 discardable_usage); |
| 1883 |
| 1884 size_t v8_usage = 0; |
| 1885 if (v8::Isolate* isolate = blink::mainThreadIsolate()) { |
| 1886 v8::HeapStatistics v8_heap_statistics; |
| 1887 isolate->GetHeapStatistics(&v8_heap_statistics); |
| 1888 v8_usage = v8_heap_statistics.total_heap_size(); |
| 1889 } |
| 1890 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.V8MainThreadIsolateKB", |
| 1891 v8_usage); |
| 1892 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.TotalAllocatedKB", |
| 1893 blink_stats.partitionAllocTotalAllocatedBytes + |
| 1894 blink_stats.blinkGCTotalAllocatedBytes + |
| 1895 malloc_usage + v8_usage + discardable_usage); |
| 1765 } | 1896 } |
| 1766 | 1897 |
| 1767 void RenderThreadImpl::OnCreateNewFrame(FrameMsg_NewFrame_Params params) { | 1898 void RenderThreadImpl::OnCreateNewFrame(FrameMsg_NewFrame_Params params) { |
| 1768 // Debug cases of https://crbug.com/626802. | 1899 // Debug cases of https://crbug.com/626802. |
| 1769 base::debug::SetCrashKeyValue("newframe_routing_id", | 1900 base::debug::SetCrashKeyValue("newframe_routing_id", |
| 1770 base::IntToString(params.routing_id)); | 1901 base::IntToString(params.routing_id)); |
| 1771 base::debug::SetCrashKeyValue("newframe_proxy_id", | 1902 base::debug::SetCrashKeyValue("newframe_proxy_id", |
| 1772 base::IntToString(params.proxy_routing_id)); | 1903 base::IntToString(params.proxy_routing_id)); |
| 1773 base::debug::SetCrashKeyValue("newframe_opener_id", | 1904 base::debug::SetCrashKeyValue("newframe_opener_id", |
| 1774 base::IntToString(params.opener_routing_id)); | 1905 base::IntToString(params.opener_routing_id)); |
| (...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2309 if (blink::mainThreadIsolate()) { | 2440 if (blink::mainThreadIsolate()) { |
| 2310 blink::mainThreadIsolate()->MemoryPressureNotification( | 2441 blink::mainThreadIsolate()->MemoryPressureNotification( |
| 2311 v8::MemoryPressureLevel::kCritical); | 2442 v8::MemoryPressureLevel::kCritical); |
| 2312 blink::MemoryPressureNotificationToWorkerThreadIsolates( | 2443 blink::MemoryPressureNotificationToWorkerThreadIsolates( |
| 2313 v8::MemoryPressureLevel::kCritical); | 2444 v8::MemoryPressureLevel::kCritical); |
| 2314 } | 2445 } |
| 2315 } | 2446 } |
| 2316 | 2447 |
| 2317 | 2448 |
| 2318 } // namespace content | 2449 } // namespace content |
| OLD | NEW |