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

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: Using FILE*. 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/logging.h"
8 #include <fstream> 8 #include "base/trace_event/process_memory_dump.h"
9 9
10 #include "base/logging.h" 10 #if defined(OS_LINUX) || defined(OS_ANDROID)
11 #include "base/process/process_metrics.h" 11 #include "base/files/file_util.h"
12 #include "base/trace_event/process_memory_dump.h" 12 #include "base/format_macros.h"
13 #include "base/posix/eintr_wrapper.h"
14 #include "base/strings/string_util.h"
13 #include "base/trace_event/process_memory_maps.h" 15 #include "base/trace_event/process_memory_maps.h"
16 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
14 17
15 namespace base { 18 namespace base {
16 namespace trace_event { 19 namespace trace_event {
17 20
18 #if defined(OS_LINUX) || defined(OS_ANDROID) 21 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 // static 22 // static
20 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; 23 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr;
21 24
22 namespace { 25 namespace {
23 26
24 const uint32 kMaxLineSize = 4096; 27 const uint32 kMaxLineSize = 4096;
28 const uint32 kMaxCounterNameSize = 20;
Primiano Tucci (use gerrit) 2015/09/30 15:36:35 move this constant down in the only place where it
ssid 2015/10/02 17:07:19 Done.
25 29
26 bool ParseSmapsHeader(std::istream* smaps, 30 bool ParseSmapsHeader(FILE* smaps_file, ProcessMemoryMaps::VMRegion* region) {
27 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 end_addr; 33 uint64 end_addr = 0;
31 std::string protection_flags; 34 char protection_flags[5] = {0};
32 std::string ignored; 35 char mapped_file[kMaxLineSize];
33 *smaps >> std::hex >> region->start_address; 36
34 smaps->ignore(1); 37 if (fscanf(smaps_file,
35 *smaps >> std::hex >> end_addr; 38 "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4096[^\n]\n" SCNx64,
39 &region->start_address, &end_addr, protection_flags,
40 mapped_file) != 4)
41 return false;
42
36 if (end_addr > region->start_address) { 43 if (end_addr > region->start_address) {
37 region->size_in_bytes = end_addr - region->start_address; 44 region->size_in_bytes = end_addr - region->start_address;
38 } else { 45 } else {
39 // This is not just paranoia, it can actually happen (See crbug.com/461237). 46 // This is not just paranoia, it can actually happen (See crbug.com/461237).
40 region->size_in_bytes = 0; 47 region->size_in_bytes = 0;
41 res = false; 48 res = false;
42 } 49 }
43 50
44 region->protection_flags = 0; 51 region->protection_flags = 0;
45 *smaps >> protection_flags;
46 CHECK_EQ(4UL, protection_flags.size());
47 if (protection_flags[0] == 'r') { 52 if (protection_flags[0] == 'r') {
48 region->protection_flags |= 53 region->protection_flags |=
49 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; 54 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
50 } 55 }
51 if (protection_flags[1] == 'w') { 56 if (protection_flags[1] == 'w') {
52 region->protection_flags |= 57 region->protection_flags |=
53 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; 58 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
54 } 59 }
55 if (protection_flags[2] == 'x') { 60 if (protection_flags[2] == 'x') {
56 region->protection_flags |= 61 region->protection_flags |=
57 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; 62 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
58 } 63 }
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 64
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; 65 region->mapped_file = mapped_file;
66 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, &region->mapped_file);
68 67
69 return res; 68 return res;
70 } 69 }
71 70
72 uint64 ReadCounterBytes(std::istream* smaps) { 71 uint64 ReadCounterBytes(FILE* smaps_file) {
73 uint64 counter_value = 0; 72 uint64 counter_value = 0;
74 *smaps >> std::dec >> counter_value; 73 CHECK_EQ(1, fscanf(smaps_file, "%" SCNu64 " kB", &counter_value));
Primiano Tucci (use gerrit) 2015/09/30 15:36:35 Please don't put operations with side-effects in C
ssid 2015/10/02 17:07:19 Done. Thanks.
75 return counter_value * 1024; 74 return counter_value * 1024;
76 } 75 }
77 76
78 uint32 ParseSmapsCounter(std::istream* smaps, 77 uint32 ParseSmapsCounter(FILE* smaps_file,
79 ProcessMemoryMaps::VMRegion* region) { 78 ProcessMemoryMaps::VMRegion* region) {
80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" 79 // A smaps counter lines looks as follows: "RSS: 0 Kb\n"
81 uint32 res = 1; 80 uint32 res = 1;
82 std::string counter_name; 81 char counter_name[kMaxCounterNameSize];
83 *smaps >> counter_name; 82 int did_read = fscanf(smaps_file, "%s", counter_name);
83 DCHECK_EQ(1, did_read);
84 84
85 // TODO(primiano): "Swap" should also be accounted as resident. Check 85 if (strcmp(counter_name, "Pss:") == 0) {
86 // whether Rss isn't already counting swapped and fix below if that is 86 region->byte_stats_proportional_resident = ReadCounterBytes(smaps_file);
87 // the case. 87 } else if (strcmp(counter_name, "Private_Dirty:") == 0) {
88 if (counter_name == "Pss:") { 88 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps_file);
89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); 89 } else if (strcmp(counter_name, "Private_Clean:") == 0) {
90 } else if (counter_name == "Private_Dirty:") { 90 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps_file);
91 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps); 91 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) {
92 } else if (counter_name == "Private_Clean:") { 92 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps_file);
93 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps); 93 } else if (strcmp(counter_name, "Shared_Clean:") == 0) {
94 } else if (counter_name == "Shared_Dirty:") { 94 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps_file);
95 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps); 95 } else if (strcmp(counter_name, "Swap:") == 0) {
96 } else if (counter_name == "Shared_Clean:") { 96 region->byte_stats_swapped = ReadCounterBytes(smaps_file);
97 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps);
98 } else if (counter_name == "Swap:") {
99 region->byte_stats_swapped = ReadCounterBytes(smaps);
100 } else { 97 } else {
101 res = 0; 98 res = 0;
102 } 99 }
103 100
104 #ifndef NDEBUG 101 // Ignore the rest of the line.
105 // Paranoid check against changes of the Kernel /proc interface. 102 char ignore[kMaxLineSize];
106 if (res) { 103 fgets(ignore, sizeof(ignore), smaps_file);
Primiano Tucci (use gerrit) 2015/09/30 15:36:35 Just fscanf with %*4096[^\n]\n or similar will be
ssid 2015/10/02 17:07:19 Done.
107 std::string unit;
108 *smaps >> unit;
109 DCHECK_EQ("kB", unit);
110 }
111 #endif
112
113 smaps->ignore(kMaxLineSize, '\n');
114 104
115 return res; 105 return res;
116 } 106 }
117 107
118 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { 108 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) {
119 if (!smaps->good()) 109 if (!smaps_file)
120 return 0; 110 return 0;
121 111
112 fseek(smaps_file, 0, SEEK_SET);
113
122 const uint32 kNumExpectedCountersPerRegion = 6; 114 const uint32 kNumExpectedCountersPerRegion = 6;
123 uint32 counters_parsed_for_current_region = 0; 115 uint32 counters_parsed_for_current_region = 0;
124 uint32 num_valid_regions = 0; 116 uint32 num_valid_regions = 0;
125 ProcessMemoryMaps::VMRegion region; 117 ProcessMemoryMaps::VMRegion region;
126 bool should_add_current_region = false; 118 bool should_add_current_region = false;
127 for (;;) { 119 for (;;) {
128 int next = smaps->peek(); 120 char next = 0;
129 if (next == std::ifstream::traits_type::eof() || next == '\n') 121 // Peek into the next integer and move pointer back.
122 next = fgetc(smaps_file);
123 if (next == EOF)
130 break; 124 break;
125 ungetc(next, smaps_file);
131 if (isxdigit(next) && !isupper(next)) { 126 if (isxdigit(next) && !isupper(next)) {
132 region = ProcessMemoryMaps::VMRegion(); 127 region = ProcessMemoryMaps::VMRegion();
133 counters_parsed_for_current_region = 0; 128 counters_parsed_for_current_region = 0;
134 should_add_current_region = ParseSmapsHeader(smaps, &region); 129 should_add_current_region = ParseSmapsHeader(smaps_file, &region);
135 } else { 130 } else {
136 counters_parsed_for_current_region += ParseSmapsCounter(smaps, &region); 131 counters_parsed_for_current_region +=
132 ParseSmapsCounter(smaps_file, &region);
137 DCHECK_LE(counters_parsed_for_current_region, 133 DCHECK_LE(counters_parsed_for_current_region,
138 kNumExpectedCountersPerRegion); 134 kNumExpectedCountersPerRegion);
139 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { 135 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) {
140 if (should_add_current_region) { 136 if (should_add_current_region) {
141 pmm->AddVMRegion(region); 137 pmm->AddVMRegion(region);
142 ++num_valid_regions; 138 ++num_valid_regions;
143 should_add_current_region = false; 139 should_add_current_region = false;
144 } 140 }
145 } 141 }
146 } 142 }
(...skipping 23 matching lines...) Expand all
170 // Snapshot of memory maps is not taken for light dump requests. 166 // Snapshot of memory maps is not taken for light dump requests.
171 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) 167 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT)
172 return true; 168 return true;
173 169
174 uint32 res = 0; 170 uint32 res = 0;
175 171
176 #if defined(OS_LINUX) || defined(OS_ANDROID) 172 #if defined(OS_LINUX) || defined(OS_ANDROID)
177 if (UNLIKELY(proc_smaps_for_testing)) { 173 if (UNLIKELY(proc_smaps_for_testing)) {
178 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); 174 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps());
179 } else { 175 } else {
180 std::ifstream proc_self_smaps("/proc/self/smaps"); 176 ScopedFILE smaps_file(OpenFile(FilePath("/proc/self/smaps"), "r"));
181 res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps()); 177 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps());
182 } 178 }
183 #else 179 #else
184 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; 180 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux";
185 #endif 181 #endif
186 182
187 if (res > 0) { 183 if (res > 0) {
188 pmd->set_has_process_mmaps(); 184 pmd->set_has_process_mmaps();
189 return true; 185 return true;
190 } 186 }
191 187
192 return false; 188 return false;
193 } 189 }
194 190
195 } // namespace trace_event 191 } // namespace trace_event
196 } // namespace base 192 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698