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