Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: base/trace_event/process_memory_maps_dump_provider.cc

Issue 1332943002: [tracing] Smaps provider uses fscanf instead of istream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/trace_event/process_memory_maps_dump_provider.h"
6 6
7 #include <cctype> 7 #include "base/format_macros.h"
8 #include <fstream>
9
10 #include "base/logging.h" 8 #include "base/logging.h"
11 #include "base/process/process_metrics.h" 9 #include "base/strings/string_util.h"
12 #include "base/trace_event/process_memory_dump.h" 10 #include "base/trace_event/process_memory_dump.h"
13 #include "base/trace_event/process_memory_maps.h" 11 #include "base/trace_event/process_memory_maps.h"
14 12
15 namespace base { 13 namespace base {
16 namespace trace_event { 14 namespace trace_event {
17 15
18 #if defined(OS_LINUX) || defined(OS_ANDROID) 16 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 // static 17 // static
20 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; 18 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr;
21 19
22 namespace { 20 namespace {
23 21
24 const uint32 kMaxLineSize = 4096; 22 const uint32 kMaxLineSize = 4096;
25 23
26 bool ParseSmapsHeader(std::istream* smaps, 24 bool ParseSmapsHeader(const char* header_line,
27 ProcessMemoryMaps::VMRegion* region) { 25 ProcessMemoryMaps::VMRegion* region) {
28 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" 26 // 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. 27 bool res = true; // Whether this region should be appended or skipped.
30 uint64 end_addr; 28 uint64 end_addr = 0;
31 std::string protection_flags; 29 char protection_flags[5] = {0};
32 std::string ignored; 30 char mapped_file[kMaxLineSize];
33 *smaps >> std::hex >> region->start_address; 31
34 smaps->ignore(1); 32 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n",
35 *smaps >> std::hex >> end_addr; 33 &region->start_address, &end_addr, protection_flags,
34 mapped_file) != 4)
35 return false;
36
36 if (end_addr > region->start_address) { 37 if (end_addr > region->start_address) {
37 region->size_in_bytes = end_addr - region->start_address; 38 region->size_in_bytes = end_addr - region->start_address;
38 } else { 39 } else {
39 // This is not just paranoia, it can actually happen (See crbug.com/461237). 40 // This is not just paranoia, it can actually happen (See crbug.com/461237).
40 region->size_in_bytes = 0; 41 region->size_in_bytes = 0;
41 res = false; 42 res = false;
42 } 43 }
43 44
44 region->protection_flags = 0; 45 region->protection_flags = 0;
45 *smaps >> protection_flags;
46 CHECK_EQ(4UL, protection_flags.size());
47 if (protection_flags[0] == 'r') { 46 if (protection_flags[0] == 'r') {
48 region->protection_flags |= 47 region->protection_flags |=
49 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; 48 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
50 } 49 }
51 if (protection_flags[1] == 'w') { 50 if (protection_flags[1] == 'w') {
52 region->protection_flags |= 51 region->protection_flags |=
53 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; 52 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
54 } 53 }
55 if (protection_flags[2] == 'x') { 54 if (protection_flags[2] == 'x') {
56 region->protection_flags |= 55 region->protection_flags |=
57 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; 56 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
58 } 57 }
59 *smaps >> ignored; // Ignore mapped file offset.
60 *smaps >> ignored; // Ignore device maj-min (fc:01 in the example above).
61 *smaps >> ignored; // Ignore inode number (1234 in the example above).
62 58
63 while (smaps->peek() == ' ')
64 smaps->ignore(1);
65 char mapped_file[kMaxLineSize];
66 smaps->getline(mapped_file, sizeof(mapped_file));
67 region->mapped_file = mapped_file; 59 region->mapped_file = mapped_file;
60 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, &region->mapped_file);
68 61
69 return res; 62 return res;
70 } 63 }
71 64
72 uint64 ReadCounterBytes(std::istream* smaps) { 65 uint64 ReadCounterBytes(char* counter_line) {
73 uint64 counter_value = 0; 66 uint64 counter_value = 0;
74 *smaps >> std::dec >> counter_value; 67 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value);
68 DCHECK_EQ(1, res);
75 return counter_value * 1024; 69 return counter_value * 1024;
76 } 70 }
77 71
78 uint32 ParseSmapsCounter(std::istream* smaps, 72 uint32 ParseSmapsCounter(char* counter_line,
79 ProcessMemoryMaps::VMRegion* region) { 73 ProcessMemoryMaps::VMRegion* region) {
80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" 74 // A smaps counter lines looks as follows: "RSS: 0 Kb\n"
81 uint32 res = 1; 75 uint32 res = 1;
82 std::string counter_name; 76 char counter_name[20];
83 *smaps >> counter_name; 77 int did_read = sscanf(counter_line, "%19[^\n ]", counter_name);
78 DCHECK_EQ(1, did_read);
84 79
85 // TODO(primiano): "Swap" should also be accounted as resident. Check 80 if (strcmp(counter_name, "Pss:") == 0) {
86 // whether Rss isn't already counting swapped and fix below if that is 81 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line);
87 // the case. 82 } else if (strcmp(counter_name, "Private_Dirty:") == 0) {
88 if (counter_name == "Pss:") { 83 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line);
89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); 84 } else if (strcmp(counter_name, "Private_Clean:") == 0) {
90 } else if (counter_name == "Private_Dirty:") { 85 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line);
91 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps); 86 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) {
92 } else if (counter_name == "Private_Clean:") { 87 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line);
93 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps); 88 } else if (strcmp(counter_name, "Shared_Clean:") == 0) {
94 } else if (counter_name == "Shared_Dirty:") { 89 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line);
95 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps); 90 } else if (strcmp(counter_name, "Swap:") == 0) {
96 } else if (counter_name == "Shared_Clean:") { 91 region->byte_stats_swapped = ReadCounterBytes(counter_line);
97 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps);
98 } else if (counter_name == "Swap:") {
99 region->byte_stats_swapped = ReadCounterBytes(smaps);
100 } else { 92 } else {
101 res = 0; 93 res = 0;
102 } 94 }
103 95
104 #ifndef NDEBUG
105 // Paranoid check against changes of the Kernel /proc interface.
106 if (res) {
107 std::string unit;
108 *smaps >> unit;
109 DCHECK_EQ("kB", unit);
110 }
111 #endif
112
113 smaps->ignore(kMaxLineSize, '\n');
114
115 return res; 96 return res;
116 } 97 }
117 98
118 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { 99 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) {
119 if (!smaps->good()) 100 if (!smaps_file)
120 return 0; 101 return 0;
121 102
103 fseek(smaps_file, 0, SEEK_SET);
104
105 char line[kMaxLineSize];
122 const uint32 kNumExpectedCountersPerRegion = 6; 106 const uint32 kNumExpectedCountersPerRegion = 6;
123 uint32 counters_parsed_for_current_region = 0; 107 uint32 counters_parsed_for_current_region = 0;
124 uint32 num_valid_regions = 0; 108 uint32 num_valid_regions = 0;
125 ProcessMemoryMaps::VMRegion region; 109 ProcessMemoryMaps::VMRegion region;
126 bool should_add_current_region = false; 110 bool should_add_current_region = false;
127 for (;;) { 111 for (;;) {
128 int next = smaps->peek(); 112 line[0] = '\0';
129 if (next == std::ifstream::traits_type::eof() || next == '\n') 113 if (fgets(line, kMaxLineSize, smaps_file) == nullptr)
130 break; 114 break;
131 if (isxdigit(next) && !isupper(next)) { 115 DCHECK_GT(strlen(line), 0u);
116 if (isxdigit(line[0]) && !isupper(line[0])) {
132 region = ProcessMemoryMaps::VMRegion(); 117 region = ProcessMemoryMaps::VMRegion();
133 counters_parsed_for_current_region = 0; 118 counters_parsed_for_current_region = 0;
134 should_add_current_region = ParseSmapsHeader(smaps, &region); 119 should_add_current_region = ParseSmapsHeader(line, &region);
135 } else { 120 } else {
136 counters_parsed_for_current_region += ParseSmapsCounter(smaps, &region); 121 counters_parsed_for_current_region += ParseSmapsCounter(line, &region);
137 DCHECK_LE(counters_parsed_for_current_region, 122 DCHECK_LE(counters_parsed_for_current_region,
138 kNumExpectedCountersPerRegion); 123 kNumExpectedCountersPerRegion);
139 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { 124 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) {
140 if (should_add_current_region) { 125 if (should_add_current_region) {
141 pmm->AddVMRegion(region); 126 pmm->AddVMRegion(region);
142 ++num_valid_regions; 127 ++num_valid_regions;
143 should_add_current_region = false; 128 should_add_current_region = false;
144 } 129 }
145 } 130 }
146 } 131 }
(...skipping 23 matching lines...) Expand all
170 // Snapshot of memory maps is not taken for light dump requests. 155 // Snapshot of memory maps is not taken for light dump requests.
171 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) 156 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT)
172 return true; 157 return true;
173 158
174 uint32 res = 0; 159 uint32 res = 0;
175 160
176 #if defined(OS_LINUX) || defined(OS_ANDROID) 161 #if defined(OS_LINUX) || defined(OS_ANDROID)
177 if (UNLIKELY(proc_smaps_for_testing)) { 162 if (UNLIKELY(proc_smaps_for_testing)) {
178 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); 163 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps());
179 } else { 164 } else {
180 std::ifstream proc_self_smaps("/proc/self/smaps"); 165 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r"));
181 res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps()); 166 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps());
182 } 167 }
183 #else 168 #else
184 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; 169 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux";
185 #endif 170 #endif
186 171
187 if (res > 0) { 172 if (res > 0) {
188 pmd->set_has_process_mmaps(); 173 pmd->set_has_process_mmaps();
189 return true; 174 return true;
190 } 175 }
191 176
192 return false; 177 return false;
193 } 178 }
194 179
195 } // namespace trace_event 180 } // namespace trace_event
196 } // namespace base 181 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/process_memory_maps_dump_provider.h ('k') | base/trace_event/process_memory_maps_dump_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698