Index: components/tracing/process_metrics_memory_dump_provider.cc |
diff --git a/components/tracing/process_metrics_memory_dump_provider.cc b/components/tracing/process_metrics_memory_dump_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..143a9f0cc4f242bb672d0c0916088ab9aed85e30 |
--- /dev/null |
+++ b/components/tracing/process_metrics_memory_dump_provider.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/tracing/process_metrics_memory_dump_provider.h" |
+ |
+#include "base/containers/scoped_ptr_map.h" |
+#include "base/lazy_instance.h" |
+#include "base/process/process_metrics.h" |
+#include "base/trace_event/memory_dump_manager.h" |
+#include "base/trace_event/process_memory_dump.h" |
+#include "base/trace_event/process_memory_totals.h" |
+#include "build/build_config.h" |
+ |
+namespace tracing { |
+ |
+namespace { |
+ |
+base::LazyInstance< |
+ base::ScopedPtrMap<base::ProcessHandle, |
+ scoped_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky |
+ g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; |
+ |
+base::ProcessMetrics* CreateProcessMetrics(base::ProcessHandle process) { |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
why not returning a scoped_ptr here?
ssid
2016/01/04 20:54:37
Done.
|
+ if (process == base::kNullProcessHandle) |
+ return base::ProcessMetrics::CreateCurrentProcessMetrics(); |
+#if !defined(OS_MACOSX) || defined(OS_IOS) |
+ return base::ProcessMetrics::CreateProcessMetrics(process); |
+#else |
+ // Creating process metrics for child processes in mac requires PortProvider. |
+ NOTREACHED(); |
+#endif |
+} |
+ |
+} // namespace |
+ |
+// static |
+uint64 ProcessMetricsMemoryDumpProvider::rss_bytes_for_testing = 0; |
+ |
+// static |
+void ProcessMetricsMemoryDumpProvider::RegisterForProcess( |
+ base::ProcessHandle process) { |
+ scoped_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
Can we have a DCHECK_CURRENTLY_ON to make sure tha
ssid
2016/01/04 20:54:38
This would require adding deps of content/public/b
|
+ new ProcessMetricsMemoryDumpProvider(process)); |
+ base::trace_event::MemoryDumpProvider::Options options(process); |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
technically options takes a ProcessId, and now you
ssid
2016/01/04 20:54:37
Fixed this.
|
+ base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
+ metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); |
+ g_dump_providers_map.Get().insert(process, metrics_provider.Pass()); |
+} |
+ |
+// static |
+void ProcessMetricsMemoryDumpProvider::UnregisterForProcess( |
+ base::ProcessHandle process) { |
+ scoped_ptr<ProcessMetricsMemoryDumpProvider> provider = |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
same here about the DCHECK_CURRENTLY_ON
ssid
2016/01/04 20:54:38
same as above.
|
+ g_dump_providers_map.Get().take_and_erase(process); |
+ DCHECK(provider); |
+ base::trace_event::MemoryDumpManager::GetInstance() |
+ ->UnregisterAndDeleteDumpProviderAsync(provider.Pass()); |
+} |
+ |
+ProcessMetricsMemoryDumpProvider::ProcessMetricsMemoryDumpProvider( |
+ base::ProcessHandle process) |
+ : process_(process), process_metrics_(CreateProcessMetrics(process)) {} |
+ |
+ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} |
+ |
+// Called at trace dump point time. Creates a snapshot of the memory maps for |
+// the current process. |
+bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( |
+ const base::trace_event::MemoryDumpArgs& args, |
+ base::trace_event::ProcessMemoryDump* pmd) { |
+ DumpProcessTotals(args, pmd); |
+ |
+#if defined(OS_LINUX) || defined(OS_ANDROID) |
+ DumpProcessMemoryMaps(args, pmd); |
+#endif |
+ return true; |
+} |
+ |
+#if !defined(OS_LINUX) && !defined(OS_ANDROID) |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
Can you add a comment explaining that on linux/and
ssid
2016/01/04 20:54:38
Done.
|
+void ProcessMetricsMemoryDumpProvider::DumpProcessTotals( |
+ const base::trace_event::MemoryDumpArgs& args, |
+ base::trace_event::ProcessMemoryDump* pmd) { |
+ const uint64 rss_bytes = rss_bytes_for_testing |
+ ? rss_bytes_for_testing |
+ : process_metrics_->GetWorkingSetSize(); |
+ |
+ uint64 peak_rss_bytes = 0; |
+ |
+#if !defined(OS_IOS) |
+ peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); |
+#if defined(MACOSX) |
+ size_t private_bytes; |
+ bool res = process_metrics_->GetMemoryBytes(&private_bytes, |
+ nullptr /* shared_bytes */); |
+ if (res) { |
+ pmd->process_totals()->SetExtraFieldInBytes("private_bytes", private_bytes); |
+ } |
+#endif // defined(MACOSX) |
+#endif // !defined(OS_IOS) |
+ |
+ DCHECK(rss_bytes); |
+ pmd->process_totals()->set_resident_set_bytes(rss_bytes); |
+ pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); |
+ pmd->set_has_process_totals(); |
+} |
+#endif // !defined(OS_LINUX) && !defined(OS_ANDROID) |
+ |
+} // namespace tracing |