OLD | NEW |
---|---|
(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 <map> | |
8 | |
9 #include "base/lazy_instance.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/process/process_metrics.h" | |
12 #include "base/trace_event/memory_dump_manager.h" | |
13 #include "base/trace_event/process_memory_dump.h" | |
14 #include "base/trace_event/process_memory_totals.h" | |
15 #include "build/build_config.h" | |
16 | |
17 namespace tracing { | |
18 | |
19 namespace { | |
20 | |
21 base::LazyInstance<std::map<base::ProcessHandle, | |
22 scoped_ptr<ProcessMetricsMemoryDumpProvider>>>:: | |
23 Leaky g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; | |
24 | |
25 scoped_ptr<base::ProcessMetrics> CreateProcessMetrics( | |
26 base::ProcessHandle process) { | |
27 if (process == base::kNullProcessHandle) | |
28 return make_scoped_ptr(base::ProcessMetrics::CreateCurrentProcessMetrics()); | |
29 #if !defined(OS_MACOSX) || defined(OS_IOS) | |
30 return make_scoped_ptr(base::ProcessMetrics::CreateProcessMetrics(process)); | |
31 #else | |
32 // Creating process metrics for child processes in mac requires PortProvider. | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
I'd add "and is a non needed use case" to the comm
ssid
2016/01/06 20:26:33
Done.
| |
33 NOTREACHED(); | |
34 #endif | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 // static | |
40 uint64_t ProcessMetricsMemoryDumpProvider::rss_bytes_for_testing = 0; | |
41 | |
42 // static | |
43 void ProcessMetricsMemoryDumpProvider::RegisterForProcess( | |
44 base::ProcessHandle process) { | |
45 scoped_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( | |
46 new ProcessMetricsMemoryDumpProvider(process)); | |
47 base::trace_event::MemoryDumpProvider::Options options( | |
48 base::GetProcId(process)); | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
If you need a process_id at the end, doesn't it ma
ssid
2016/01/06 20:26:33
Done.
| |
49 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
50 metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); | |
51 g_dump_providers_map.Get().insert( | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
maybe make this a
bool did_insert = g_dump_provi
ssid
2016/01/06 20:26:33
hm, there are nacl processes in chromeos builds wh
Primiano Tucci (use gerrit)
2016/01/07 11:34:34
Ahh I see. Agree not worth wasting time for those.
| |
52 std::make_pair(process, std::move(metrics_provider))); | |
53 } | |
54 | |
55 // static | |
56 void ProcessMetricsMemoryDumpProvider::UnregisterForProcess( | |
57 base::ProcessHandle process) { | |
58 auto iter = g_dump_providers_map.Get().find(process); | |
59 DCHECK(iter != g_dump_providers_map.Get().end()); | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
I'd make this a bit safer and do:
if (iter == g_d
ssid
2016/01/06 20:26:33
Done.
| |
60 base::trace_event::MemoryDumpManager::GetInstance() | |
61 ->UnregisterAndDeleteDumpProviderAsync(std::move(iter->second)); | |
62 g_dump_providers_map.Get().erase(iter); | |
63 } | |
64 | |
65 ProcessMetricsMemoryDumpProvider::ProcessMetricsMemoryDumpProvider( | |
66 base::ProcessHandle process) | |
67 : process_(process), process_metrics_(CreateProcessMetrics(process)) {} | |
68 | |
69 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} | |
70 | |
71 // Called at trace dump point time. Creates a snapshot of the memory maps for | |
72 // the current process. | |
73 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( | |
74 const base::trace_event::MemoryDumpArgs& args, | |
75 base::trace_event::ProcessMemoryDump* pmd) { | |
76 bool res = DumpProcessTotals(args, pmd); | |
77 | |
78 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
79 res &= DumpProcessMemoryMaps(args, pmd); | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
shouldn't this check for heavy vs light dump here?
ssid
2016/01/06 20:26:33
Moved it here. It was inside the function.
| |
80 #endif | |
81 return res; | |
82 } | |
83 | |
84 // Linux and Android have specialized function in | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
I'd reword this as:
// See process_metrics_memory
ssid
2016/01/06 20:26:33
removed.
| |
85 // process_metrics_memory_dump_provider_linux.cc because kernel supports rss | |
86 // peak resetting. | |
87 #if !defined(OS_LINUX) && !defined(OS_ANDROID) | |
88 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( | |
89 const base::trace_event::MemoryDumpArgs& args, | |
90 base::trace_event::ProcessMemoryDump* pmd) { | |
91 const uint64_t rss_bytes = rss_bytes_for_testing | |
92 ? rss_bytes_for_testing | |
93 : process_metrics_->GetWorkingSetSize(); | |
94 | |
95 // rss_bytes will be 0 if the process ended while dumping. | |
96 if (!rss_bytes) | |
97 return false; | |
98 | |
99 uint64_t peak_rss_bytes = 0; | |
100 | |
101 #if !defined(OS_IOS) | |
102 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); | |
103 #if defined(MACOSX) | |
104 size_t private_bytes; | |
105 bool res = process_metrics_->GetMemoryBytes(&private_bytes, | |
106 nullptr /* shared_bytes */); | |
107 if (res) { | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
nit: you can remove these braces here
ssid
2016/01/06 20:26:33
Done.
| |
108 pmd->process_totals()->SetExtraFieldInBytes("private_bytes", private_bytes); | |
109 } | |
110 #endif // defined(MACOSX) | |
111 #endif // !defined(OS_IOS) | |
112 | |
113 pmd->process_totals()->set_resident_set_bytes(rss_bytes); | |
114 pmd->set_has_process_totals(); | |
115 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); | |
116 | |
117 // Returns true even if other metrics failed, since rss is reported. | |
Primiano Tucci (use gerrit)
2016/01/05 11:55:17
/reported/always reported/
ssid
2016/01/06 20:26:33
The rss is not reported, and function fails if chi
| |
118 return true; | |
119 } | |
120 #endif // !defined(OS_LINUX) && !defined(OS_ANDROID) | |
121 | |
122 } // namespace tracing | |
OLD | NEW |