Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Side by Side Diff: content/renderer/render_thread_impl.cc

Issue 2391423004: Add UMA for PurgeAndSuspend. (Closed)
Patch Set: Fixed. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 664 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 class ScopedHeapLock {
Primiano Tucci (use gerrit) 2016/10/07 16:30:00 Looks like you are creating this ScopedHeapLock cl
tasak 2016/10/11 08:15:10 Done.
1802 public:
1803 explicit ScopedHeapLock(HANDLE handle) : handle_(handle) {
1804 ::HeapLock(handle_);
1805 }
1806 ~ScopedHeapLock() { ::HeapUnlock(handle_); }
1807
1808 private:
1809 ScopedHeapLock() = delete;
1810 ScopedHeapLock(const ScopedHeapLock&) = delete;
1811
1812 HANDLE handle_;
1813 };
1814
1815 static size_t GetMallocUsage() {
1816 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
1817 if (number_of_heaps <= 0)
1818 return 0;
1819
1820 size_t malloc_usage = 0;
1821 std::unique_ptr<HANDLE[]> heaps(new HANDLE[number_of_heaps]);
1822 ::GetProcessHeaps(number_of_heaps, heaps.get());
1823 for (unsigned i = 0; i < number_of_heaps; i++) {
1824 PROCESS_HEAP_ENTRY heap_entry;
1825 heap_entry.lpData = NULL;
1826 {
1827 ScopedHeapLock lock(heaps[i]);
1828 while (::HeapWalk(heaps[i], &heap_entry) != 0) {
1829 if (heap_entry.lpData == heaps.get())
1830 continue;
1831 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0)
1832 malloc_usage += heap_entry.cbData;
1833 }
1834 }
1835 }
1836 return malloc_usage;
1837 }
1838
1839 } // namespace
1840 #endif
1841
1842 #if defined(OS_MACOSX) || defined(OS_IOS)
1843 namespace {
1844
1845 static size_t GetMallocUsage() {
1846 malloc_statistics_t stats = {0};
1847 malloc_zone_statistics(nullptr, &stats);
1848 return stats.size_in_use;
1849 }
1850
1851 } // namespace
1852 #endif
1853
1854 // TODO(tasak): Once it is possible to use memory-infra without tracing,
1855 // we should collect the metrics using memory-infra.
1856 // TODO(tasak): We should also report a difference between the memory usages
1857 // before and after purging by using memory-infra.
1858 void RenderThreadImpl::RecordPurgeAndSuspendMetrics() const {
1859 // If this renderer is resumed, we should not update UMA.
1860 if (!is_renderer_suspended_)
1861 return;
1862
1863 // TODO(tasak): Compare memory metrics between purge-enabled renderers and
1864 // purge-disabled renderers (A/B testing).
1865 blink::WebMemoryStatistics blink_stats = blink::WebMemoryStatistics::Get();
1866 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.PartitionAllocKB",
Ilya Sherman 2016/10/11 00:58:23 Note that the range of UMA_HISTOGRAM_MEMORY_KB goe
tasak 2016/10/11 08:15:10 I see. I changed V8, Malloc and Total to use MB in
1867 blink_stats.partitionAllocTotalAllocatedBytes);
1868 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.BlinkGCKB",
1869 blink_stats.blinkGCTotalAllocatedBytes);
1870 #if defined(OS_LINUX) || defined(OS_ANDROID)
1871 struct mallinfo minfo = mallinfo();
1872 #if defined(USE_TCMALLOC)
1873 size_t malloc_usage = minfo.uordblks;
1874 #else
1875 size_t malloc_usage = minfo.hblkhd + minfo.arena;
1876 #endif
1877 #else
1878 size_t malloc_usage = GetMallocUsage();
1879 #endif
1880 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.MallocKB", malloc_usage);
1881
1882 ChildDiscardableSharedMemoryManager::Statistics discardable_stats =
1883 ChildThreadImpl::discardable_shared_memory_manager()->GetStatistics();
1884 size_t discardable_usage =
1885 discardable_stats.total_size - discardable_stats.freelist_size;
1886 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.DiscardableKB",
1887 discardable_usage);
1888
1889 size_t v8_usage = 0;
1890 if (v8::Isolate* isolate = blink::mainThreadIsolate()) {
1891 v8::HeapStatistics v8_heap_statistics;
1892 isolate->GetHeapStatistics(&v8_heap_statistics);
1893 v8_usage = v8_heap_statistics.total_heap_size();
1894 }
1895 // TODO(tasak): Currently only memory usage of mainThreadIsolate() is
1896 // reported. We should collect memory usages of all isolates using
1897 // memory-infra.
1898 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.V8MainThreadIsolateKB",
1899 v8_usage);
1900 UMA_HISTOGRAM_MEMORY_KB("Memory.PurgeAndSuspend.TotalAllocatedKB",
1901 blink_stats.partitionAllocTotalAllocatedBytes +
1902 blink_stats.blinkGCTotalAllocatedBytes +
1903 malloc_usage + v8_usage + discardable_usage);
Primiano Tucci (use gerrit) 2016/10/07 16:30:00 I think at this point would be also interesting to
tasak 2016/10/11 08:15:10 I agree with you. I would like to monitor resident
1775 } 1904 }
1776 1905
1777 scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() { 1906 scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() {
1778 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync"); 1907 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
1779 1908
1780 if (gpu_channel_) { 1909 if (gpu_channel_) {
1781 // Do nothing if we already have a GPU channel or are already 1910 // Do nothing if we already have a GPU channel or are already
1782 // establishing one. 1911 // establishing one.
1783 if (!gpu_channel_->IsLost()) 1912 if (!gpu_channel_->IsLost())
1784 return gpu_channel_; 1913 return gpu_channel_;
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
2329 } 2458 }
2330 } 2459 }
2331 2460
2332 void RenderThreadImpl::OnRendererInterfaceRequest( 2461 void RenderThreadImpl::OnRendererInterfaceRequest(
2333 mojom::RendererAssociatedRequest request) { 2462 mojom::RendererAssociatedRequest request) {
2334 DCHECK(!renderer_binding_.is_bound()); 2463 DCHECK(!renderer_binding_.is_bound());
2335 renderer_binding_.Bind(std::move(request)); 2464 renderer_binding_.Bind(std::move(request));
2336 } 2465 }
2337 2466
2338 } // namespace content 2467 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698