OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "components/tracing/common/process_metrics_memory_dump_provider.h" | 5 #include "components/tracing/common/process_metrics_memory_dump_provider.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 if (should_add_current_region) { | 148 if (should_add_current_region) { |
149 pmm->AddVMRegion(region); | 149 pmm->AddVMRegion(region); |
150 ++num_valid_regions; | 150 ++num_valid_regions; |
151 should_add_current_region = false; | 151 should_add_current_region = false; |
152 } | 152 } |
153 } | 153 } |
154 } | 154 } |
155 } | 155 } |
156 return num_valid_regions; | 156 return num_valid_regions; |
157 } | 157 } |
158 | |
159 bool GetResidentSizeFromStatmFile(int fd, uint64_t* resident_pages) { | |
160 lseek(fd, 0, SEEK_SET); | |
161 char line[kMaxLineSize]; | |
162 int res = read(fd, line, kMaxLineSize - 1); | |
163 if (res <= 0) | |
164 return false; | |
165 line[res] = '\0'; | |
166 int num_scanned = sscanf(line, "%*s %" SCNu64, resident_pages); | |
167 return num_scanned == 1; | |
168 } | |
169 | |
158 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 170 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
159 | 171 |
160 std::unique_ptr<base::ProcessMetrics> CreateProcessMetrics( | 172 std::unique_ptr<base::ProcessMetrics> CreateProcessMetrics( |
161 base::ProcessId process) { | 173 base::ProcessId process) { |
162 if (process == base::kNullProcessId) | 174 if (process == base::kNullProcessId) |
163 return base::ProcessMetrics::CreateCurrentProcessMetrics(); | 175 return base::ProcessMetrics::CreateCurrentProcessMetrics(); |
164 #if defined(OS_LINUX) || defined(OS_ANDROID) | 176 #if defined(OS_LINUX) || defined(OS_ANDROID) |
165 // Just pass ProcessId instead of handle since they are the same in linux and | 177 // Just pass ProcessId instead of handle since they are the same in linux and |
166 // android. | 178 // android. |
167 return base::ProcessMetrics::CreateProcessMetrics(process); | 179 return base::ProcessMetrics::CreateProcessMetrics(process); |
168 #else | 180 #else |
169 // Creating process metrics for child processes in mac or windows requires | 181 // Creating process metrics for child processes in mac or windows requires |
170 // additional information like ProcessHandle or port provider. | 182 // additional information like ProcessHandle or port provider. |
171 NOTREACHED(); | 183 NOTREACHED(); |
172 return std::unique_ptr<base::ProcessMetrics>(); | 184 return std::unique_ptr<base::ProcessMetrics>(); |
173 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 185 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
174 } | 186 } |
175 | 187 |
176 } // namespace | 188 } // namespace |
177 | 189 |
178 // static | 190 // static |
179 uint64_t ProcessMetricsMemoryDumpProvider::rss_bytes_for_testing = 0; | 191 uint64_t ProcessMetricsMemoryDumpProvider::rss_bytes_for_testing = 0; |
180 | 192 |
181 #if defined(OS_LINUX) || defined(OS_ANDROID) | 193 #if defined(OS_LINUX) || defined(OS_ANDROID) |
182 | 194 |
183 // static | 195 // static |
184 FILE* ProcessMetricsMemoryDumpProvider::proc_smaps_for_testing = nullptr; | 196 FILE* ProcessMetricsMemoryDumpProvider::proc_smaps_for_testing = nullptr; |
185 | 197 |
198 // static | |
199 int ProcessMetricsMemoryDumpProvider::fast_polling_statm_fd_for_testing = -1; | |
200 | |
186 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( | 201 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( |
187 const base::trace_event::MemoryDumpArgs& args, | 202 const base::trace_event::MemoryDumpArgs& args, |
188 base::trace_event::ProcessMemoryDump* pmd) { | 203 base::trace_event::ProcessMemoryDump* pmd) { |
189 uint32_t res = 0; | 204 uint32_t res = 0; |
190 if (proc_smaps_for_testing) { | 205 if (proc_smaps_for_testing) { |
191 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | 206 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); |
192 } else { | 207 } else { |
193 std::string file_name = "/proc/" + (process_ == base::kNullProcessId | 208 std::string file_name = "/proc/" + (process_ == base::kNullProcessId |
194 ? "self" | 209 ? "self" |
195 : base::IntToString(process_)) + | 210 : base::IntToString(process_)) + |
196 "/smaps"; | 211 "/smaps"; |
197 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); | 212 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); |
198 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 213 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
199 } | 214 } |
200 | 215 |
201 if (res) | 216 if (res) |
202 pmd->set_has_process_mmaps(); | 217 pmd->set_has_process_mmaps(); |
203 return res; | 218 return res; |
204 } | 219 } |
205 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 220 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
206 | 221 |
207 // static | 222 // static |
208 void ProcessMetricsMemoryDumpProvider::RegisterForProcess( | 223 void ProcessMetricsMemoryDumpProvider::RegisterForProcess( |
209 base::ProcessId process) { | 224 base::ProcessId process) { |
210 std::unique_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( | 225 std::unique_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( |
211 new ProcessMetricsMemoryDumpProvider(process)); | 226 new ProcessMetricsMemoryDumpProvider(process)); |
212 base::trace_event::MemoryDumpProvider::Options options; | 227 base::trace_event::MemoryDumpProvider::Options options; |
213 options.target_pid = process; | 228 options.target_pid = process; |
229 options.is_fast_polling_supported = true; | |
214 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 230 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
215 metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); | 231 metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); |
216 bool did_insert = | 232 bool did_insert = |
217 g_dump_providers_map.Get() | 233 g_dump_providers_map.Get() |
218 .insert(std::make_pair(process, std::move(metrics_provider))) | 234 .insert(std::make_pair(process, std::move(metrics_provider))) |
219 .second; | 235 .second; |
220 if (!did_insert) { | 236 if (!did_insert) { |
221 DLOG(ERROR) << "ProcessMetricsMemoryDumpProvider already registered for " | 237 DLOG(ERROR) << "ProcessMetricsMemoryDumpProvider already registered for " |
222 << (process == base::kNullProcessId | 238 << (process == base::kNullProcessId |
223 ? "current process" | 239 ? "current process" |
224 : "process id " + base::IntToString(process)); | 240 : "process id " + base::IntToString(process)); |
225 } | 241 } |
226 } | 242 } |
227 | 243 |
228 // static | 244 // static |
229 void ProcessMetricsMemoryDumpProvider::UnregisterForProcess( | 245 void ProcessMetricsMemoryDumpProvider::UnregisterForProcess( |
230 base::ProcessId process) { | 246 base::ProcessId process) { |
231 auto iter = g_dump_providers_map.Get().find(process); | 247 auto iter = g_dump_providers_map.Get().find(process); |
232 if (iter == g_dump_providers_map.Get().end()) { | 248 if (iter == g_dump_providers_map.Get().end()) |
233 return; | 249 return; |
234 } | |
235 base::trace_event::MemoryDumpManager::GetInstance() | 250 base::trace_event::MemoryDumpManager::GetInstance() |
236 ->UnregisterAndDeleteDumpProviderSoon(std::move(iter->second)); | 251 ->UnregisterAndDeleteDumpProviderSoon(std::move(iter->second)); |
237 g_dump_providers_map.Get().erase(iter); | 252 g_dump_providers_map.Get().erase(iter); |
238 } | 253 } |
239 | 254 |
240 ProcessMetricsMemoryDumpProvider::ProcessMetricsMemoryDumpProvider( | 255 ProcessMetricsMemoryDumpProvider::ProcessMetricsMemoryDumpProvider( |
241 base::ProcessId process) | 256 base::ProcessId process) |
242 : process_(process), | 257 : process_(process), |
243 process_metrics_(CreateProcessMetrics(process)), | 258 process_metrics_(CreateProcessMetrics(process)), |
244 is_rss_peak_resettable_(true) {} | 259 is_rss_peak_resettable_(true) { |
260 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
261 fast_polling_statm_fd_ = -1; | |
262 #endif | |
263 } | |
245 | 264 |
246 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} | 265 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() { |
266 // Clear files in case polling was not disabled yet. | |
267 SetFastMemoryPollingEnabled(false); | |
268 } | |
247 | 269 |
248 // Called at trace dump point time. Creates a snapshot of the memory maps for | 270 // Called at trace dump point time. Creates a snapshot of the memory maps for |
249 // the current process. | 271 // the current process. |
250 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( | 272 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( |
251 const base::trace_event::MemoryDumpArgs& args, | 273 const base::trace_event::MemoryDumpArgs& args, |
252 base::trace_event::ProcessMemoryDump* pmd) { | 274 base::trace_event::ProcessMemoryDump* pmd) { |
253 bool res = DumpProcessTotals(args, pmd); | 275 bool res = DumpProcessTotals(args, pmd); |
254 | 276 |
255 #if defined(OS_LINUX) || defined(OS_ANDROID) | 277 #if defined(OS_LINUX) || defined(OS_ANDROID) |
256 if (args.level_of_detail == | 278 if (args.level_of_detail == |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 #endif // !defined(OS_IOS) | 337 #endif // !defined(OS_IOS) |
316 | 338 |
317 pmd->process_totals()->set_resident_set_bytes(rss_bytes); | 339 pmd->process_totals()->set_resident_set_bytes(rss_bytes); |
318 pmd->set_has_process_totals(); | 340 pmd->set_has_process_totals(); |
319 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); | 341 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); |
320 | 342 |
321 // Returns true even if other metrics failed, since rss is reported. | 343 // Returns true even if other metrics failed, since rss is reported. |
322 return true; | 344 return true; |
323 } | 345 } |
324 | 346 |
347 void ProcessMetricsMemoryDumpProvider::PollFastMemoryTotal( | |
348 uint64_t* memory_total) { | |
349 *memory_total = 0; | |
350 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
351 int statm_fd = fast_polling_statm_fd_for_testing == -1 | |
352 ? fast_polling_statm_fd_ | |
353 : fast_polling_statm_fd_for_testing; | |
354 if (statm_fd < 0) | |
355 return; | |
356 | |
357 uint64_t rss_pages = 0; | |
358 if (!GetResidentSizeFromStatmFile(statm_fd, &rss_pages)) | |
359 return; | |
360 | |
361 // When adding total for child processes, do not include "shared" since it | |
362 // will be included in the current processes' total. | |
363 *memory_total = rss_pages * base::GetPageSize(); | |
Primiano Tucci (use gerrit)
2016/12/16 11:50:40
base::GetPageSize() is a bit silly because makes a
ssid
2016/12/16 19:48:44
Done.
| |
364 #else | |
365 *memory_total = process_metrics_->GetWorkingSetSize(); | |
366 #endif | |
367 } | |
368 | |
369 void ProcessMetricsMemoryDumpProvider::SetFastMemoryPollingEnabled( | |
370 bool enabled) { | |
371 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
372 if (enabled) { | |
373 DCHECK(fast_polling_statm_fd_); | |
Primiano Tucci (use gerrit)
2016/12/16 11:50:40
I think you really mean here:
DCHECK_EQ(-1, fast_p
ssid
2016/12/16 19:48:44
Yes i meant eq -1. But removed this now.
| |
374 std::string name = "/proc/" + (process_ == base::kNullProcessId | |
375 ? "self" | |
376 : base::IntToString(process_)) + | |
377 "/statm"; | |
378 fast_polling_statm_fd_ = open(name.c_str(), O_RDONLY); | |
Primiano Tucci (use gerrit)
2016/12/16 11:50:40
I'd add a DCHECK_GE(fast_polling_statm_fd_, 0);
at
ssid
2016/12/16 19:48:44
Done.
| |
379 } else if (fast_polling_statm_fd_ >= 0) { | |
380 close(fast_polling_statm_fd_); | |
381 fast_polling_statm_fd_ = -1; | |
382 } | |
383 #endif | |
384 } | |
385 | |
325 } // namespace tracing | 386 } // namespace tracing |
OLD | NEW |