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

Unified Diff: content/renderer/render_thread_impl.cc

Issue 2350423003: [Tentaive patch for discussion] Add Purge+Suspend metrics as UMA.
Patch Set: Fixed misuse of CanPurgeAndSuspend Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/render_thread_impl.h ('k') | third_party/WebKit/Source/web/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/render_thread_impl.cc
diff --git a/content/renderer/render_thread_impl.cc b/content/renderer/render_thread_impl.cc
index b77e5122c0462e087201af6b4fb7b9121f429123..59d816724a86c93174c97ee874d185871705bc6a 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;
@@ -1759,9 +1766,133 @@ void RenderThreadImpl::OnProcessPurgeAndSuspend() {
ChildThreadImpl::OnProcessPurgeAndSuspend();
if (is_renderer_suspended_)
return;
- // TODO(hajimehoshi): Implement purging e.g. cache (crbug/607077)
+
+ // If PurgeAndSuspendFeature is disabled, do nothing except updating UMA.
+ // The data is used as "Force Disabled" metric.
+ if (base::FeatureList::IsEnabled(features::kPurgeAndSuspend)) {
+ // TODO(tasak): use MemoryCoordinator instead of NotifyMemoryPressure.
+ base::MemoryPressureListener::NotifyMemoryPressure(
+ base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
+ base::allocator::ReleaseFreeMemory();
+
+ renderer_scheduler_->SuspendRenderer();
+ }
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,
+ base::Bind(&RenderThreadImpl::OnDumpMemoryUsage, base::Unretained(this)),
+ 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
+
+// For purge+suspend, report memory usage diff by using UMA_HISTOGRAM.
+void RenderThreadImpl::OnDumpMemoryUsage() 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.
+ 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();
+ }
+ 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);
}
void RenderThreadImpl::OnCreateNewFrame(FrameMsg_NewFrame_Params params) {
« no previous file with comments | « content/renderer/render_thread_impl.h ('k') | third_party/WebKit/Source/web/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698