OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
_posix is a bit misleading here, as it suggests th
ssid
2016/01/04 20:54:38
Done.
| |
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_maps_dump_provider.h" | 5 #include "components/tracing/process_metrics_memory_dump_provider.h" |
6 | 6 |
7 #include <fcntl.h> | |
8 | |
9 #include "base/files/file_util.h" | |
7 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
8 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/process/process_metrics.h" | |
14 #include "base/strings/string_number_conversions.h" | |
10 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
11 #include "base/trace_event/process_memory_dump.h" | 16 #include "base/trace_event/process_memory_dump.h" |
12 #include "base/trace_event/process_memory_maps.h" | 17 #include "base/trace_event/process_memory_maps.h" |
13 | 18 |
14 namespace base { | 19 namespace tracing { |
15 namespace trace_event { | |
16 | |
17 // static | |
18 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
23 bool kernel_supports_rss_peak_reset = true; | |
24 const char kClearPeakRssCommand[] = "5"; | |
25 | |
22 const uint32 kMaxLineSize = 4096; | 26 const uint32 kMaxLineSize = 4096; |
23 | 27 |
24 bool ParseSmapsHeader(const char* header_line, | 28 bool ParseSmapsHeader(const char* header_line, |
25 ProcessMemoryMaps::VMRegion* region) { | 29 base::trace_event::ProcessMemoryMaps::VMRegion* region) { |
26 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" | 30 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" |
27 bool res = true; // Whether this region should be appended or skipped. | 31 bool res = true; // Whether this region should be appended or skipped. |
28 uint64 end_addr = 0; | 32 uint64 end_addr = 0; |
29 char protection_flags[5] = {0}; | 33 char protection_flags[5] = {0}; |
30 char mapped_file[kMaxLineSize]; | 34 char mapped_file[kMaxLineSize]; |
31 | 35 |
32 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n", | 36 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n", |
33 ®ion->start_address, &end_addr, protection_flags, | 37 ®ion->start_address, &end_addr, protection_flags, |
34 mapped_file) != 4) | 38 mapped_file) != 4) |
35 return false; | 39 return false; |
36 | 40 |
37 if (end_addr > region->start_address) { | 41 if (end_addr > region->start_address) { |
38 region->size_in_bytes = end_addr - region->start_address; | 42 region->size_in_bytes = end_addr - region->start_address; |
39 } else { | 43 } else { |
40 // This is not just paranoia, it can actually happen (See crbug.com/461237). | 44 // This is not just paranoia, it can actually happen (See crbug.com/461237). |
41 region->size_in_bytes = 0; | 45 region->size_in_bytes = 0; |
42 res = false; | 46 res = false; |
43 } | 47 } |
44 | 48 |
45 region->protection_flags = 0; | 49 region->protection_flags = 0; |
46 if (protection_flags[0] == 'r') { | 50 if (protection_flags[0] == 'r') { |
47 region->protection_flags |= | 51 region->protection_flags |= |
48 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; | 52 base::trace_event::ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; |
49 } | 53 } |
50 if (protection_flags[1] == 'w') { | 54 if (protection_flags[1] == 'w') { |
51 region->protection_flags |= | 55 region->protection_flags |= |
52 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; | 56 base::trace_event::ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; |
53 } | 57 } |
54 if (protection_flags[2] == 'x') { | 58 if (protection_flags[2] == 'x') { |
55 region->protection_flags |= | 59 region->protection_flags |= |
56 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; | 60 base::trace_event::ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; |
57 } | 61 } |
58 | 62 |
59 region->mapped_file = mapped_file; | 63 region->mapped_file = mapped_file; |
60 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, ®ion->mapped_file); | 64 base::TrimWhitespaceASCII(region->mapped_file, base::TRIM_ALL, |
65 ®ion->mapped_file); | |
61 | 66 |
62 return res; | 67 return res; |
63 } | 68 } |
64 | 69 |
65 uint64 ReadCounterBytes(char* counter_line) { | 70 uint64 ReadCounterBytes(char* counter_line) { |
66 uint64 counter_value = 0; | 71 uint64 counter_value = 0; |
67 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value); | 72 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value); |
68 DCHECK_EQ(1, res); | 73 DCHECK_EQ(1, res); |
69 return counter_value * 1024; | 74 return counter_value * 1024; |
70 } | 75 } |
71 | 76 |
72 uint32 ParseSmapsCounter(char* counter_line, | 77 uint32 ParseSmapsCounter( |
73 ProcessMemoryMaps::VMRegion* region) { | 78 char* counter_line, |
79 base::trace_event::ProcessMemoryMaps::VMRegion* region) { | |
74 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" | 80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" |
75 uint32 res = 1; | 81 uint32 res = 1; |
76 char counter_name[20]; | 82 char counter_name[20]; |
77 int did_read = sscanf(counter_line, "%19[^\n ]", counter_name); | 83 int did_read = sscanf(counter_line, "%19[^\n ]", counter_name); |
78 DCHECK_EQ(1, did_read); | 84 DCHECK_EQ(1, did_read); |
79 | 85 |
80 if (strcmp(counter_name, "Pss:") == 0) { | 86 if (strcmp(counter_name, "Pss:") == 0) { |
81 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line); | 87 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line); |
82 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { | 88 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { |
83 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line); | 89 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line); |
84 } else if (strcmp(counter_name, "Private_Clean:") == 0) { | 90 } else if (strcmp(counter_name, "Private_Clean:") == 0) { |
85 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line); | 91 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line); |
86 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { | 92 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { |
87 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line); | 93 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line); |
88 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { | 94 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { |
89 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line); | 95 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line); |
90 } else if (strcmp(counter_name, "Swap:") == 0) { | 96 } else if (strcmp(counter_name, "Swap:") == 0) { |
91 region->byte_stats_swapped = ReadCounterBytes(counter_line); | 97 region->byte_stats_swapped = ReadCounterBytes(counter_line); |
92 } else { | 98 } else { |
93 res = 0; | 99 res = 0; |
94 } | 100 } |
95 | 101 |
96 return res; | 102 return res; |
97 } | 103 } |
98 | 104 |
99 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) { | 105 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, |
106 base::trace_event::ProcessMemoryMaps* pmm) { | |
100 if (!smaps_file) | 107 if (!smaps_file) |
101 return 0; | 108 return 0; |
102 | 109 |
103 fseek(smaps_file, 0, SEEK_SET); | 110 fseek(smaps_file, 0, SEEK_SET); |
104 | 111 |
105 char line[kMaxLineSize]; | 112 char line[kMaxLineSize]; |
106 const uint32 kNumExpectedCountersPerRegion = 6; | 113 const uint32 kNumExpectedCountersPerRegion = 6; |
107 uint32 counters_parsed_for_current_region = 0; | 114 uint32 counters_parsed_for_current_region = 0; |
108 uint32 num_valid_regions = 0; | 115 uint32 num_valid_regions = 0; |
109 ProcessMemoryMaps::VMRegion region; | 116 base::trace_event::ProcessMemoryMaps::VMRegion region; |
110 bool should_add_current_region = false; | 117 bool should_add_current_region = false; |
111 for (;;) { | 118 for (;;) { |
112 line[0] = '\0'; | 119 line[0] = '\0'; |
113 if (fgets(line, kMaxLineSize, smaps_file) == nullptr) | 120 if (fgets(line, kMaxLineSize, smaps_file) == nullptr) |
114 break; | 121 break; |
115 DCHECK_GT(strlen(line), 0u); | 122 DCHECK_GT(strlen(line), 0u); |
116 if (isxdigit(line[0]) && !isupper(line[0])) { | 123 if (isxdigit(line[0]) && !isupper(line[0])) { |
117 region = ProcessMemoryMaps::VMRegion(); | 124 region = base::trace_event::ProcessMemoryMaps::VMRegion(); |
118 counters_parsed_for_current_region = 0; | 125 counters_parsed_for_current_region = 0; |
119 should_add_current_region = ParseSmapsHeader(line, ®ion); | 126 should_add_current_region = ParseSmapsHeader(line, ®ion); |
120 } else { | 127 } else { |
121 counters_parsed_for_current_region += ParseSmapsCounter(line, ®ion); | 128 counters_parsed_for_current_region += ParseSmapsCounter(line, ®ion); |
122 DCHECK_LE(counters_parsed_for_current_region, | 129 DCHECK_LE(counters_parsed_for_current_region, |
123 kNumExpectedCountersPerRegion); | 130 kNumExpectedCountersPerRegion); |
124 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | 131 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { |
125 if (should_add_current_region) { | 132 if (should_add_current_region) { |
126 pmm->AddVMRegion(region); | 133 pmm->AddVMRegion(region); |
127 ++num_valid_regions; | 134 ++num_valid_regions; |
128 should_add_current_region = false; | 135 should_add_current_region = false; |
129 } | 136 } |
130 } | 137 } |
131 } | 138 } |
132 } | 139 } |
133 return num_valid_regions; | 140 return num_valid_regions; |
134 } | 141 } |
135 | 142 |
136 } // namespace | 143 } // namespace |
137 | 144 |
138 // static | 145 // static |
139 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { | 146 FILE* ProcessMetricsMemoryDumpProvider::proc_smaps_for_testing = nullptr; |
140 return Singleton<ProcessMemoryMapsDumpProvider, | 147 |
141 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); | 148 void ProcessMetricsMemoryDumpProvider::DumpProcessTotals( |
149 const base::trace_event::MemoryDumpArgs& args, | |
150 base::trace_event::ProcessMemoryDump* pmd) { | |
151 const uint64 rss_bytes = rss_bytes_for_testing | |
152 ? rss_bytes_for_testing | |
153 : process_metrics_->GetWorkingSetSize(); | |
154 | |
155 uint64 peak_rss_bytes = 0; | |
156 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); | |
157 | |
158 // This file cannot be wriiten for other processes. So, do not try. | |
159 if (kernel_supports_rss_peak_reset && process_ == base::kNullProcessHandle) { | |
160 int clear_refs_fd = open("/proc/self/clear_refs", O_WRONLY); | |
161 if (clear_refs_fd > 0 && | |
162 base::WriteFileDescriptor(clear_refs_fd, kClearPeakRssCommand, | |
163 sizeof(kClearPeakRssCommand))) { | |
164 pmd->process_totals()->set_is_peak_rss_resetable(true); | |
165 } else { | |
166 kernel_supports_rss_peak_reset = false; | |
167 } | |
168 close(clear_refs_fd); | |
169 } | |
170 | |
171 DCHECK(rss_bytes); | |
172 pmd->process_totals()->set_resident_set_bytes(rss_bytes); | |
173 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); | |
174 pmd->set_has_process_totals(); | |
142 } | 175 } |
143 | 176 |
144 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() { | 177 void ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( |
145 } | 178 const base::trace_event::MemoryDumpArgs& args, |
146 | 179 base::trace_event::ProcessMemoryDump* pmd) { |
147 ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() { | 180 // Snapshot of memory maps is taken only for detailed dump requests. |
148 } | 181 if (args.level_of_detail != |
149 | 182 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) |
150 // Called at trace dump point time. Creates a snapshot of the memory maps for | 183 return; |
151 // the current process. | |
152 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | |
153 ProcessMemoryDump* pmd) { | |
154 // Snapshot of memory maps is not taken for light dump requests. | |
155 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) | |
156 return true; | |
157 | 184 |
158 uint32 res = 0; | 185 uint32 res = 0; |
159 if (UNLIKELY(proc_smaps_for_testing)) { | 186 if (UNLIKELY(proc_smaps_for_testing)) { |
160 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | 187 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); |
161 } else { | 188 } else { |
162 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); | 189 std::string file_name = "/proc/" + (process_ == base::kNullProcessHandle |
190 ? "self" | |
191 : base::IntToString(process_)) + | |
192 "/smaps"; | |
193 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); | |
163 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 194 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
164 } | 195 } |
165 | 196 |
166 if (res > 0) { | 197 if (res) |
167 pmd->set_has_process_mmaps(); | 198 pmd->set_has_process_mmaps(); |
Primiano Tucci (use gerrit)
2015/12/14 10:52:21
what happens if something fails? we don't get any
ssid
2016/01/04 20:54:38
Done.
| |
168 return true; | |
169 } | |
170 return false; | |
171 } | 199 } |
172 | 200 |
173 } // namespace trace_event | 201 } // namespace tracing |
174 } // namespace base | |
OLD | NEW |