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

Side by Side Diff: components/tracing/process_metrics_memory_dump_provider.cc

Issue 1417003003: [tracing] Dump child processes' memory metrics in browser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@web_cache2_base
Patch Set: Remove manager Created 5 years, 1 month 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/tracing/process_metrics_memory_dump_provider.h"
6
7 #include "base/containers/scoped_ptr_map.h"
8 #include "base/lazy_instance.h"
9 #include "base/process/process_metrics.h"
10 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/process_memory_dump.h"
12 #include "base/trace_event/process_memory_totals.h"
13 #include "build/build_config.h"
14
15 namespace tracing {
16
17 namespace {
18
19 base::LazyInstance<
20 base::ScopedPtrMap<base::ProcessHandle,
21 scoped_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky
22 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER;
23
24 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.
25 if (process == base::kNullProcessHandle)
26 return base::ProcessMetrics::CreateCurrentProcessMetrics();
27 #if !defined(OS_MACOSX) || defined(OS_IOS)
28 return base::ProcessMetrics::CreateProcessMetrics(process);
29 #else
30 // Creating process metrics for child processes in mac requires PortProvider.
31 NOTREACHED();
32 #endif
33 }
34
35 } // namespace
36
37 // static
38 uint64 ProcessMetricsMemoryDumpProvider::rss_bytes_for_testing = 0;
39
40 // static
41 void ProcessMetricsMemoryDumpProvider::RegisterForProcess(
42 base::ProcessHandle process) {
43 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
44 new ProcessMetricsMemoryDumpProvider(process));
45 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.
46 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
47 metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options);
48 g_dump_providers_map.Get().insert(process, metrics_provider.Pass());
49 }
50
51 // static
52 void ProcessMetricsMemoryDumpProvider::UnregisterForProcess(
53 base::ProcessHandle process) {
54 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.
55 g_dump_providers_map.Get().take_and_erase(process);
56 DCHECK(provider);
57 base::trace_event::MemoryDumpManager::GetInstance()
58 ->UnregisterAndDeleteDumpProviderAsync(provider.Pass());
59 }
60
61 ProcessMetricsMemoryDumpProvider::ProcessMetricsMemoryDumpProvider(
62 base::ProcessHandle process)
63 : process_(process), process_metrics_(CreateProcessMetrics(process)) {}
64
65 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {}
66
67 // Called at trace dump point time. Creates a snapshot of the memory maps for
68 // the current process.
69 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump(
70 const base::trace_event::MemoryDumpArgs& args,
71 base::trace_event::ProcessMemoryDump* pmd) {
72 DumpProcessTotals(args, pmd);
73
74 #if defined(OS_LINUX) || defined(OS_ANDROID)
75 DumpProcessMemoryMaps(args, pmd);
76 #endif
77 return true;
78 }
79
80 #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.
81 void ProcessMetricsMemoryDumpProvider::DumpProcessTotals(
82 const base::trace_event::MemoryDumpArgs& args,
83 base::trace_event::ProcessMemoryDump* pmd) {
84 const uint64 rss_bytes = rss_bytes_for_testing
85 ? rss_bytes_for_testing
86 : process_metrics_->GetWorkingSetSize();
87
88 uint64 peak_rss_bytes = 0;
89
90 #if !defined(OS_IOS)
91 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize();
92 #if defined(MACOSX)
93 size_t private_bytes;
94 bool res = process_metrics_->GetMemoryBytes(&private_bytes,
95 nullptr /* shared_bytes */);
96 if (res) {
97 pmd->process_totals()->SetExtraFieldInBytes("private_bytes", private_bytes);
98 }
99 #endif // defined(MACOSX)
100 #endif // !defined(OS_IOS)
101
102 DCHECK(rss_bytes);
103 pmd->process_totals()->set_resident_set_bytes(rss_bytes);
104 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes);
105 pmd->set_has_process_totals();
106 }
107 #endif // !defined(OS_LINUX) && !defined(OS_ANDROID)
108
109 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698