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 "base/trace_event/process_memory_totals_dump_provider.h" | 5 #include "base/trace_event/process_memory_totals_dump_provider.h" |
6 | 6 |
7 #include "base/process/process_metrics.h" | 7 #include "base/process/process_metrics.h" |
8 #include "base/trace_event/process_memory_dump.h" | 8 #include "base/trace_event/process_memory_dump.h" |
9 #include "base/trace_event/process_memory_totals.h" | 9 #include "base/trace_event/process_memory_totals.h" |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 ProcessMetrics* CreateProcessMetricsForCurrentProcess() { | 30 ProcessMetrics* CreateProcessMetricsForCurrentProcess() { |
31 #if !defined(OS_MACOSX) || defined(OS_IOS) | 31 #if !defined(OS_MACOSX) || defined(OS_IOS) |
32 return ProcessMetrics::CreateProcessMetrics(GetCurrentProcessHandle()); | 32 return ProcessMetrics::CreateProcessMetrics(GetCurrentProcessHandle()); |
33 #else | 33 #else |
34 return ProcessMetrics::CreateProcessMetrics(GetCurrentProcessHandle(), NULL); | 34 return ProcessMetrics::CreateProcessMetrics(GetCurrentProcessHandle(), NULL); |
35 #endif | 35 #endif |
36 } | 36 } |
37 | |
38 bool ReadStatusFile(FILE* status_file, std::string* data) { | |
39 if (fseek(status_file, 0, SEEK_SET)) | |
40 return false; | |
41 char buffer[4096]; | |
42 buffer[0] = '\0'; | |
43 int ret = fread(buffer, 1, sizeof(buffer), status_file); | |
Primiano Tucci (use gerrit)
2015/10/07 10:25:34
Use base::ReadFromFD, which is protected from EINT
| |
44 if (ret <= 0) | |
45 return false; | |
46 data->clear(); | |
47 data->append(buffer, ret); | |
48 return true; | |
49 } | |
50 | |
37 } // namespace | 51 } // namespace |
38 | 52 |
39 // static | 53 // static |
40 ProcessMemoryTotalsDumpProvider* | 54 ProcessMemoryTotalsDumpProvider* |
41 ProcessMemoryTotalsDumpProvider::GetInstance() { | 55 ProcessMemoryTotalsDumpProvider::GetInstance() { |
42 return Singleton< | 56 return Singleton< |
43 ProcessMemoryTotalsDumpProvider, | 57 ProcessMemoryTotalsDumpProvider, |
44 LeakySingletonTraits<ProcessMemoryTotalsDumpProvider>>::get(); | 58 LeakySingletonTraits<ProcessMemoryTotalsDumpProvider>>::get(); |
45 } | 59 } |
46 | 60 |
47 ProcessMemoryTotalsDumpProvider::ProcessMemoryTotalsDumpProvider() | 61 ProcessMemoryTotalsDumpProvider::ProcessMemoryTotalsDumpProvider() |
48 : process_metrics_(CreateProcessMetricsForCurrentProcess()) { | 62 : process_metrics_(CreateProcessMetricsForCurrentProcess()) { |
49 } | 63 } |
50 | 64 |
51 ProcessMemoryTotalsDumpProvider::~ProcessMemoryTotalsDumpProvider() { | 65 ProcessMemoryTotalsDumpProvider::~ProcessMemoryTotalsDumpProvider() { |
52 } | 66 } |
53 | 67 |
54 // Called at trace dump point time. Creates a snapshot the memory counters for | 68 // Called at trace dump point time. Creates a snapshot the memory counters for |
55 // the current process. | 69 // the current process. |
56 bool ProcessMemoryTotalsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | 70 bool ProcessMemoryTotalsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
57 ProcessMemoryDump* pmd) { | 71 ProcessMemoryDump* pmd) { |
58 const uint64 rss_bytes = rss_bytes_for_testing | 72 uint64 rss_bytes; |
59 ? rss_bytes_for_testing | 73 uint64 peak_rss_bytes = 0; |
60 : process_metrics_->GetWorkingSetSize(); | 74 std::string status_contents; |
61 | 75 |
62 uint64 peak_rss_bytes = 0; | 76 if (UNLIKELY(rss_bytes_for_testing)) { |
77 rss_bytes = rss_bytes_for_testing; | |
78 } else if (proc_status_file && | |
79 ReadStatusFile(proc_status_file.get(), &status_contents)) { | |
80 #if defined(OS_LINUX) || defeind(OS_ANDROID) | |
81 int res = ParseProcStatusAndGetField(status_contents, "VmRSS", &rss_bytes); | |
82 DCHECK(res); | |
83 res = ParseProcStatusAndGetField(status_contents, "VmHWM", &peak_rss_bytes); | |
84 DCHECK(res); | |
85 #else | |
86 NOTREACHED(); | |
87 #endif | |
88 } else { | |
89 rss_bytes = process_metrics_->GetWorkingSetSize(); | |
90 #if !defined(OS_IOS) | |
91 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); | |
92 #endif | |
93 } | |
63 | 94 |
64 #if !defined(OS_IOS) | |
65 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); | |
66 #if defined(OS_LINUX) || defined(OS_ANDROID) | 95 #if defined(OS_LINUX) || defined(OS_ANDROID) |
67 if (kernel_supports_rss_peak_reset) { | 96 if (kernel_supports_rss_peak_reset) { |
68 // TODO(ssid): Fix crbug.com/461788 to write to the file from sandboxed | 97 // TODO(ssid): Fix crbug.com/461788 to write to the file from sandboxed |
69 // processes. | 98 // processes. |
70 int clear_refs_fd = open("/proc/self/clear_refs", O_WRONLY); | 99 int clear_refs_fd = open("/proc/self/clear_refs", O_WRONLY); |
71 if (clear_refs_fd > 0 && | 100 if (clear_refs_fd > 0 && |
72 WriteFileDescriptor(clear_refs_fd, kClearPeakRssCommand, | 101 WriteFileDescriptor(clear_refs_fd, kClearPeakRssCommand, |
73 sizeof(kClearPeakRssCommand))) { | 102 sizeof(kClearPeakRssCommand))) { |
74 pmd->process_totals()->set_is_peak_rss_resetable(true); | 103 pmd->process_totals()->set_is_peak_rss_resetable(true); |
75 } else { | 104 } else { |
76 kernel_supports_rss_peak_reset = false; | 105 kernel_supports_rss_peak_reset = false; |
77 } | 106 } |
78 close(clear_refs_fd); | 107 close(clear_refs_fd); |
79 } | 108 } |
80 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 109 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
81 #endif // !defined(OS_IOS) | |
82 | 110 |
83 if (rss_bytes > 0) { | 111 if (rss_bytes > 0) { |
84 pmd->process_totals()->set_resident_set_bytes(rss_bytes); | 112 pmd->process_totals()->set_resident_set_bytes(rss_bytes); |
85 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); | 113 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); |
86 pmd->set_has_process_totals(); | 114 pmd->set_has_process_totals(); |
87 return true; | 115 return true; |
88 } | 116 } |
89 | 117 |
90 return false; | 118 return false; |
91 } | 119 } |
92 | 120 |
93 } // namespace trace_event | 121 } // namespace trace_event |
94 } // namespace base | 122 } // namespace base |
OLD | NEW |