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 "base/trace_event/process_memory_maps_dump_provider.h" |
6 | 6 |
7 #include <cctype> | 7 #include <cctype> |
8 #include <fstream> | 8 #include <fstream> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; | 53 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; |
54 } | 54 } |
55 if (protection_flags[1] == 'w') { | 55 if (protection_flags[1] == 'w') { |
56 region->protection_flags |= | 56 region->protection_flags |= |
57 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; | 57 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; |
58 } | 58 } |
59 if (protection_flags[2] == 'x') { | 59 if (protection_flags[2] == 'x') { |
60 region->protection_flags |= | 60 region->protection_flags |= |
61 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; | 61 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; |
62 } | 62 } |
63 *smaps >> std::hex >> region->mapped_file_offset; | 63 *smaps >> ignored; // Ignore mapped file offset. |
64 *smaps >> ignored; // Ignore device maj-min (fc:01 in the example above). | 64 *smaps >> ignored; // Ignore device maj-min (fc:01 in the example above). |
65 *smaps >> ignored; // Ignore inode number (1234 in the example above). | 65 *smaps >> ignored; // Ignore inode number (1234 in the example above). |
66 | 66 |
67 while (smaps->peek() == ' ') | 67 while (smaps->peek() == ' ') |
68 smaps->ignore(1); | 68 smaps->ignore(1); |
69 char mapped_file[kMaxLineSize]; | 69 char mapped_file[kMaxLineSize]; |
70 smaps->getline(mapped_file, sizeof(mapped_file)); | 70 smaps->getline(mapped_file, sizeof(mapped_file)); |
71 region->mapped_file = mapped_file; | 71 region->mapped_file = mapped_file; |
72 | 72 |
73 return res; | 73 return res; |
74 } | 74 } |
75 | 75 |
| 76 uint64 ReadCounterBytes(std::istream* smaps) { |
| 77 uint64 counter_value = 0; |
| 78 *smaps >> std::dec >> counter_value; |
| 79 return counter_value * 1024; |
| 80 } |
| 81 |
76 uint32 ParseSmapsCounter(std::istream* smaps, | 82 uint32 ParseSmapsCounter(std::istream* smaps, |
77 ProcessMemoryMaps::VMRegion* region) { | 83 ProcessMemoryMaps::VMRegion* region) { |
78 // e.g., "RSS: 0 Kb\n" | 84 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" |
79 uint32 res = 0; | 85 uint32 res = 0; |
80 std::string counter_name; | 86 std::string counter_name; |
81 *smaps >> counter_name; | 87 *smaps >> counter_name; |
82 | 88 |
83 // TODO(primiano): "Swap" should also be accounted as resident. Check | 89 // TODO(primiano): "Swap" should also be accounted as resident. Check |
84 // whether Rss isn't already counting swapped and fix below if that is | 90 // whether Rss isn't already counting swapped and fix below if that is |
85 // the case. | 91 // the case. |
86 if (counter_name == "Rss:") { | 92 if (counter_name == "Pss:") { |
87 *smaps >> std::dec >> region->byte_stats_resident; | 93 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); |
88 region->byte_stats_resident *= 1024; | |
89 res = 1; | 94 res = 1; |
90 } else if (counter_name == "Anonymous:") { | 95 } else if (counter_name == "Private_Dirty:" || |
91 *smaps >> std::dec >> region->byte_stats_anonymous; | 96 counter_name == "Private_Clean:") { |
92 region->byte_stats_anonymous *= 1024; | 97 // For Private and Shared counters keep the sum of the dirty + clean stats. |
| 98 region->byte_stats_private_resident += ReadCounterBytes(smaps); |
| 99 res = 1; |
| 100 } else if (counter_name == "Shared_Dirty:" || |
| 101 counter_name == "Shared_Clean:") { |
| 102 region->byte_stats_shared_resident += ReadCounterBytes(smaps); |
93 res = 1; | 103 res = 1; |
94 } | 104 } |
95 | 105 |
96 #ifndef NDEBUG | 106 #ifndef NDEBUG |
97 // Paranoid check against changes of the Kernel /proc interface. | 107 // Paranoid check against changes of the Kernel /proc interface. |
98 if (res) { | 108 if (res) { |
99 std::string unit; | 109 std::string unit; |
100 *smaps >> unit; | 110 *smaps >> unit; |
101 DCHECK_EQ("kB", unit); | 111 DCHECK_EQ("kB", unit); |
102 } | 112 } |
103 #endif | 113 #endif |
104 | 114 |
105 smaps->ignore(kMaxLineSize, '\n'); | 115 smaps->ignore(kMaxLineSize, '\n'); |
106 | 116 |
107 return res; | 117 return res; |
108 } | 118 } |
109 | 119 |
110 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { | 120 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { |
111 if (!smaps->good()) | 121 if (!smaps->good()) |
112 return 0; | 122 return 0; |
113 | 123 |
114 const uint32 kNumExpectedCountersPerRegion = 2; | 124 const uint32 kNumExpectedCountersPerRegion = 5; |
115 uint32 counters_parsed_for_current_region = 0; | 125 uint32 counters_parsed_for_current_region = 0; |
116 uint32 num_valid_regions = 0; | 126 uint32 num_valid_regions = 0; |
117 ProcessMemoryMaps::VMRegion region; | 127 ProcessMemoryMaps::VMRegion region; |
118 bool should_add_current_region = false; | 128 bool should_add_current_region = false; |
119 for (;;) { | 129 for (;;) { |
120 int next = smaps->peek(); | 130 int next = smaps->peek(); |
121 if (next == std::ifstream::traits_type::eof() || next == '\n') | 131 if (next == std::ifstream::traits_type::eof() || next == '\n') |
122 break; | 132 break; |
123 if (isxdigit(next) && !isupper(next)) { | 133 if (isxdigit(next) && !isupper(next)) { |
124 region = {0}; | 134 region = {0}; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 188 |
179 return false; | 189 return false; |
180 } | 190 } |
181 | 191 |
182 const char* ProcessMemoryMapsDumpProvider::GetFriendlyName() const { | 192 const char* ProcessMemoryMapsDumpProvider::GetFriendlyName() const { |
183 return kDumperFriendlyName; | 193 return kDumperFriendlyName; |
184 } | 194 } |
185 | 195 |
186 } // namespace trace_event | 196 } // namespace trace_event |
187 } // namespace base | 197 } // namespace base |
OLD | NEW |