Chromium Code Reviews| Index: components/tracing/common/process_metrics_memory_dump_provider.cc |
| diff --git a/components/tracing/common/process_metrics_memory_dump_provider.cc b/components/tracing/common/process_metrics_memory_dump_provider.cc |
| index d2c00c347f0db3f7f9b13286e2af3edc4ac089b3..1ecb35314722e50ed092d8b84ad8e2a7739b2cfb 100644 |
| --- a/components/tracing/common/process_metrics_memory_dump_provider.cc |
| +++ b/components/tracing/common/process_metrics_memory_dump_provider.cc |
| @@ -155,6 +155,29 @@ uint32_t ReadLinuxProcSmapsFile(FILE* smaps_file, |
| } |
| return num_valid_regions; |
| } |
| + |
| +int OpenStatmFile(base::ProcessId process) { |
|
Primiano Tucci (use gerrit)
2016/12/15 15:00:02
Since you kept the full SetFastPollingEnabled(bool
ssid
2016/12/16 03:16:41
Done.
|
| + std::string name = |
| + "/proc/" + |
| + (process == base::kNullProcessId ? "self" : base::IntToString(process)) + |
| + "/statm"; |
| + return open(name.c_str(), O_RDONLY); |
| +} |
| + |
| +bool GetResidentSizesFromStatmFile(int fd, |
| + uint64_t* resident_bytes, |
| + uint64_t* shared_bytes) { |
| + lseek(fd, 0, SEEK_SET); |
| + char line[kMaxLineSize]; |
| + int res = read(fd, line, kMaxLineSize); |
|
Primiano Tucci (use gerrit)
2016/12/15 15:00:02
if you do this, then here you have to pass kMaxLin
ssid
2016/12/16 03:16:41
Done.
|
| + if (res <= 0) |
| + return false; |
| + line[res] = '\0'; |
| + int num_scanned = |
| + sscanf(line, "%*s %" SCNu64 " %" SCNu64, resident_bytes, shared_bytes); |
| + return num_scanned == 2; |
| +} |
| + |
| #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| std::unique_ptr<base::ProcessMetrics> CreateProcessMetrics( |
| @@ -211,6 +234,7 @@ void ProcessMetricsMemoryDumpProvider::RegisterForProcess( |
| new ProcessMetricsMemoryDumpProvider(process)); |
| base::trace_event::MemoryDumpProvider::Options options; |
| options.target_pid = process; |
| + options.is_fast_polling_supported = true; |
| base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); |
| bool did_insert = |
| @@ -229,9 +253,8 @@ void ProcessMetricsMemoryDumpProvider::RegisterForProcess( |
| void ProcessMetricsMemoryDumpProvider::UnregisterForProcess( |
| base::ProcessId process) { |
| auto iter = g_dump_providers_map.Get().find(process); |
| - if (iter == g_dump_providers_map.Get().end()) { |
| + if (iter == g_dump_providers_map.Get().end()) |
| return; |
| - } |
| base::trace_event::MemoryDumpManager::GetInstance() |
| ->UnregisterAndDeleteDumpProviderSoon(std::move(iter->second)); |
| g_dump_providers_map.Get().erase(iter); |
| @@ -241,9 +264,16 @@ ProcessMetricsMemoryDumpProvider::ProcessMetricsMemoryDumpProvider( |
| base::ProcessId process) |
| : process_(process), |
| process_metrics_(CreateProcessMetrics(process)), |
| - is_rss_peak_resettable_(true) {} |
| + is_rss_peak_resettable_(true) { |
| +#if defined(OS_LINUX) || defined(OS_ANDROID) |
| + fast_polling_statm_fd_ = -1; |
| +#endif |
| +} |
| -ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} |
| +ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() { |
| + // Clear files in case polling was not disabled yet. |
| + SetFastMemoryPollingEnabled(false); |
| +} |
| // Called at trace dump point time. Creates a snapshot of the memory maps for |
| // the current process. |
| @@ -309,4 +339,36 @@ bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( |
| return true; |
| } |
| +void ProcessMetricsMemoryDumpProvider::PollFastMemoryTotal( |
| + uint64_t* memory_total) { |
| + *memory_total = 0; |
| +#if defined(OS_LINUX) || defined(OS_ANDROID) |
| + if (fast_polling_statm_fd_ == -1) |
| + fast_polling_statm_fd_ = OpenStatmFile(process_); |
| + if (fast_polling_statm_fd_ < 0) |
| + return; |
| + |
| + uint64_t rss = 0, shared = 0; |
| + if (!GetResidentSizesFromStatmFile(fast_polling_statm_fd_, &rss, &shared)) |
| + return; |
| + |
| + // When adding total for child processes, do not include "shared" since it |
| + // will be included in the current processes' total. |
| + *memory_total = (process_ == base::kNullProcessId ? rss : rss - shared) * |
| + base::GetPageSize(); |
| +#else |
| + *memory_total = process_metrics_->GetWorkingSetSize(); |
| +#endif |
| +} |
| + |
| +void ProcessMetricsMemoryDumpProvider::SetFastMemoryPollingEnabled( |
| + bool enabled) { |
| +#if defined(OS_LINUX) || defined(OS_ANDROID) |
| + if (!enabled && fast_polling_statm_fd_ >= 0) { |
| + close(fast_polling_statm_fd_); |
| + fast_polling_statm_fd_ = -1; |
| + } |
| +#endif |
| +} |
| + |
| } // namespace tracing |