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 |
| 11 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 12 #include <fcntl.h> |
| 13 |
| 14 #include "base/files/file_util.h" |
| 15 |
| 16 namespace { |
| 17 bool kernel_supports_rss_peak_reset = true; |
| 18 const char kClearPeakRssCommand[] = "5"; |
| 19 } |
| 20 #endif |
| 21 |
11 namespace base { | 22 namespace base { |
12 namespace trace_event { | 23 namespace trace_event { |
13 | 24 |
14 // static | 25 // static |
15 uint64 ProcessMemoryTotalsDumpProvider::rss_bytes_for_testing = 0; | 26 uint64 ProcessMemoryTotalsDumpProvider::rss_bytes_for_testing = 0; |
16 | 27 |
17 namespace { | 28 namespace { |
18 | 29 |
19 ProcessMetrics* CreateProcessMetricsForCurrentProcess() { | 30 ProcessMetrics* CreateProcessMetricsForCurrentProcess() { |
20 #if !defined(OS_MACOSX) || defined(OS_IOS) | 31 #if !defined(OS_MACOSX) || defined(OS_IOS) |
(...skipping 19 matching lines...) Expand all Loading... |
40 ProcessMemoryTotalsDumpProvider::~ProcessMemoryTotalsDumpProvider() { | 51 ProcessMemoryTotalsDumpProvider::~ProcessMemoryTotalsDumpProvider() { |
41 } | 52 } |
42 | 53 |
43 // Called at trace dump point time. Creates a snapshot the memory counters for | 54 // Called at trace dump point time. Creates a snapshot the memory counters for |
44 // the current process. | 55 // the current process. |
45 bool ProcessMemoryTotalsDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) { | 56 bool ProcessMemoryTotalsDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) { |
46 const uint64 rss_bytes = rss_bytes_for_testing | 57 const uint64 rss_bytes = rss_bytes_for_testing |
47 ? rss_bytes_for_testing | 58 ? rss_bytes_for_testing |
48 : process_metrics_->GetWorkingSetSize(); | 59 : process_metrics_->GetWorkingSetSize(); |
49 | 60 |
| 61 uint64 peak_rss_bytes = 0; |
| 62 |
| 63 #if !defined(OS_IOS) |
| 64 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); |
| 65 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 66 if (kernel_supports_rss_peak_reset) { |
| 67 // TODO(ssid): Fix crbug.com/461788 to write to the file from sandboxed |
| 68 // processes. |
| 69 int clear_refs_fd = open("/proc/self/clear_refs", O_WRONLY); |
| 70 if (clear_refs_fd > 0 && |
| 71 WriteFileDescriptor(clear_refs_fd, kClearPeakRssCommand, |
| 72 sizeof(kClearPeakRssCommand))) { |
| 73 pmd->process_totals()->set_is_peak_rss_resetable(true); |
| 74 } else { |
| 75 kernel_supports_rss_peak_reset = false; |
| 76 } |
| 77 close(clear_refs_fd); |
| 78 } |
| 79 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 80 #endif // !defined(OS_IOS) |
| 81 |
50 if (rss_bytes > 0) { | 82 if (rss_bytes > 0) { |
51 pmd->process_totals()->set_resident_set_bytes(rss_bytes); | 83 pmd->process_totals()->set_resident_set_bytes(rss_bytes); |
| 84 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); |
52 pmd->set_has_process_totals(); | 85 pmd->set_has_process_totals(); |
53 return true; | 86 return true; |
54 } | 87 } |
55 | 88 |
56 return false; | 89 return false; |
57 } | 90 } |
58 | 91 |
59 } // namespace trace_event | 92 } // namespace trace_event |
60 } // namespace base | 93 } // namespace base |
OLD | NEW |