| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 uint64 ReadCounterBytes(std::istream* smaps) { | 72 uint64 ReadCounterBytes(std::istream* smaps) { |
| 73 uint64 counter_value = 0; | 73 uint64 counter_value = 0; |
| 74 *smaps >> std::dec >> counter_value; | 74 *smaps >> std::dec >> counter_value; |
| 75 return counter_value * 1024; | 75 return counter_value * 1024; |
| 76 } | 76 } |
| 77 | 77 |
| 78 uint32 ParseSmapsCounter(std::istream* smaps, | 78 uint32 ParseSmapsCounter(std::istream* smaps, |
| 79 ProcessMemoryMaps::VMRegion* region) { | 79 ProcessMemoryMaps::VMRegion* region) { |
| 80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" | 80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" |
| 81 uint32 res = 0; | 81 uint32 res = 1; |
| 82 std::string counter_name; | 82 std::string counter_name; |
| 83 *smaps >> counter_name; | 83 *smaps >> counter_name; |
| 84 | 84 |
| 85 // TODO(primiano): "Swap" should also be accounted as resident. Check | 85 // TODO(primiano): "Swap" should also be accounted as resident. Check |
| 86 // whether Rss isn't already counting swapped and fix below if that is | 86 // whether Rss isn't already counting swapped and fix below if that is |
| 87 // the case. | 87 // the case. |
| 88 if (counter_name == "Pss:") { | 88 if (counter_name == "Pss:") { |
| 89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); | 89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); |
| 90 res = 1; | 90 } else if (counter_name == "Private_Dirty:") { |
| 91 } else if (counter_name == "Private_Dirty:" || | 91 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps); |
| 92 counter_name == "Private_Clean:") { | 92 } else if (counter_name == "Private_Clean:") { |
| 93 // For Private and Shared counters keep the sum of the dirty + clean stats. | 93 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps); |
| 94 region->byte_stats_private_resident += ReadCounterBytes(smaps); | 94 } else if (counter_name == "Shared_Dirty:") { |
| 95 res = 1; | 95 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps); |
| 96 } else if (counter_name == "Shared_Dirty:" || | 96 } else if (counter_name == "Shared_Clean:") { |
| 97 counter_name == "Shared_Clean:") { | 97 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps); |
| 98 region->byte_stats_shared_resident += ReadCounterBytes(smaps); | 98 } else if (counter_name == "Swap:") { |
| 99 res = 1; | 99 region->byte_stats_swapped = ReadCounterBytes(smaps); |
| 100 } else { |
| 101 res = 0; |
| 100 } | 102 } |
| 101 | 103 |
| 102 #ifndef NDEBUG | 104 #ifndef NDEBUG |
| 103 // Paranoid check against changes of the Kernel /proc interface. | 105 // Paranoid check against changes of the Kernel /proc interface. |
| 104 if (res) { | 106 if (res) { |
| 105 std::string unit; | 107 std::string unit; |
| 106 *smaps >> unit; | 108 *smaps >> unit; |
| 107 DCHECK_EQ("kB", unit); | 109 DCHECK_EQ("kB", unit); |
| 108 } | 110 } |
| 109 #endif | 111 #endif |
| 110 | 112 |
| 111 smaps->ignore(kMaxLineSize, '\n'); | 113 smaps->ignore(kMaxLineSize, '\n'); |
| 112 | 114 |
| 113 return res; | 115 return res; |
| 114 } | 116 } |
| 115 | 117 |
| 116 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { | 118 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { |
| 117 if (!smaps->good()) | 119 if (!smaps->good()) |
| 118 return 0; | 120 return 0; |
| 119 | 121 |
| 120 const uint32 kNumExpectedCountersPerRegion = 5; | 122 const uint32 kNumExpectedCountersPerRegion = 6; |
| 121 uint32 counters_parsed_for_current_region = 0; | 123 uint32 counters_parsed_for_current_region = 0; |
| 122 uint32 num_valid_regions = 0; | 124 uint32 num_valid_regions = 0; |
| 123 ProcessMemoryMaps::VMRegion region; | 125 ProcessMemoryMaps::VMRegion region; |
| 124 bool should_add_current_region = false; | 126 bool should_add_current_region = false; |
| 125 for (;;) { | 127 for (;;) { |
| 126 int next = smaps->peek(); | 128 int next = smaps->peek(); |
| 127 if (next == std::ifstream::traits_type::eof() || next == '\n') | 129 if (next == std::ifstream::traits_type::eof() || next == '\n') |
| 128 break; | 130 break; |
| 129 if (isxdigit(next) && !isupper(next)) { | 131 if (isxdigit(next) && !isupper(next)) { |
| 130 region = {0}; | 132 region = ProcessMemoryMaps::VMRegion(); |
| 131 counters_parsed_for_current_region = 0; | 133 counters_parsed_for_current_region = 0; |
| 132 should_add_current_region = ParseSmapsHeader(smaps, ®ion); | 134 should_add_current_region = ParseSmapsHeader(smaps, ®ion); |
| 133 } else { | 135 } else { |
| 134 counters_parsed_for_current_region += ParseSmapsCounter(smaps, ®ion); | 136 counters_parsed_for_current_region += ParseSmapsCounter(smaps, ®ion); |
| 135 DCHECK_LE(counters_parsed_for_current_region, | 137 DCHECK_LE(counters_parsed_for_current_region, |
| 136 kNumExpectedCountersPerRegion); | 138 kNumExpectedCountersPerRegion); |
| 137 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | 139 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { |
| 138 if (should_add_current_region) { | 140 if (should_add_current_region) { |
| 139 pmm->AddVMRegion(region); | 141 pmm->AddVMRegion(region); |
| 140 ++num_valid_regions; | 142 ++num_valid_regions; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 if (res > 0) { | 182 if (res > 0) { |
| 181 pmd->set_has_process_mmaps(); | 183 pmd->set_has_process_mmaps(); |
| 182 return true; | 184 return true; |
| 183 } | 185 } |
| 184 | 186 |
| 185 return false; | 187 return false; |
| 186 } | 188 } |
| 187 | 189 |
| 188 } // namespace trace_event | 190 } // namespace trace_event |
| 189 } // namespace base | 191 } // namespace base |
| OLD | NEW |