Chromium Code Reviews| 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 "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 ScopedFILE* 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; | |
| 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 ®ion->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 base::TrimString(region->mapped_file, " ", ®ion->mapped_file); | |
| 68 | 67 |
|
Primiano Tucci (use gerrit)
2015/09/30 09:37:02
use TrimWhitespaceASCII ?
ssid
2015/09/30 11:15:13
Done.
| |
| 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; |
|
Primiano Tucci (use gerrit)
2015/09/30 09:37:02
Is there any need of this function at this point?
ssid
2015/09/30 11:15:12
Yes, This function will only be called when the co
| |
| 74 *smaps >> std::dec >> counter_value; | 73 CHECK_EQ(1, fscanf(smaps_file, "%" SCNu64, &counter_value)); |
| 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 kB", counter_name); |
| 83 DCHECK_EQ(1, did_read); | |
|
Primiano Tucci (use gerrit)
2015/09/30 09:37:02
I think at this point you can do a single scanf he
ssid
2015/09/30 11:15:12
No, see my other comment.
| |
| 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 |
|
Primiano Tucci (use gerrit)
2015/09/30 09:37:02
Since you are here, plz remove this TODO, we're de
ssid
2015/09/30 11:15:12
Done.
| |
| 87 // the case. | 87 // the case. |
| 88 if (counter_name == "Pss:") { | 88 if (strcmp(counter_name, "Pss:") == 0) { |
| 89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); | 89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps_file); |
| 90 } else if (counter_name == "Private_Dirty:") { | 90 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { |
| 91 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps); | 91 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps_file); |
| 92 } else if (counter_name == "Private_Clean:") { | 92 } else if (strcmp(counter_name, "Private_Clean:") == 0) { |
| 93 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps); | 93 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps_file); |
| 94 } else if (counter_name == "Shared_Dirty:") { | 94 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { |
| 95 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps); | 95 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps_file); |
| 96 } else if (counter_name == "Shared_Clean:") { | 96 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { |
| 97 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps); | 97 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps_file); |
| 98 } else if (counter_name == "Swap:") { | 98 } else if (strcmp(counter_name, "Swap:") == 0) { |
| 99 region->byte_stats_swapped = ReadCounterBytes(smaps); | 99 region->byte_stats_swapped = ReadCounterBytes(smaps_file); |
| 100 } else { | 100 } else { |
| 101 res = 0; | 101 res = 0; |
| 102 } | 102 } |
| 103 | 103 |
| 104 #ifndef NDEBUG | 104 // Ignore the rest of the line. |
| 105 // Paranoid check against changes of the Kernel /proc interface. | 105 char ignore[kMaxLineSize]; |
|
Primiano Tucci (use gerrit)
2015/09/30 09:37:02
At this point this should be uneeded (provided tha
ssid
2015/09/30 11:15:12
That scanf cannot be used to swallow everything si
| |
| 106 if (res) { | 106 fgets(ignore, sizeof(ignore), smaps_file); |
| 107 std::string unit; | |
| 108 *smaps >> unit; | |
| 109 DCHECK_EQ("kB", unit); | |
| 110 } | |
| 111 #endif | |
| 112 | |
| 113 smaps->ignore(kMaxLineSize, '\n'); | |
| 114 | 107 |
| 115 return res; | 108 return res; |
| 116 } | 109 } |
| 117 | 110 |
| 118 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { | 111 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) { |
| 119 if (!smaps->good()) | 112 if (!smaps_file) |
| 120 return 0; | 113 return 0; |
| 121 | 114 |
| 115 fseek(smaps_file, 0, SEEK_SET); | |
| 116 | |
| 122 const uint32 kNumExpectedCountersPerRegion = 6; | 117 const uint32 kNumExpectedCountersPerRegion = 6; |
| 123 uint32 counters_parsed_for_current_region = 0; | 118 uint32 counters_parsed_for_current_region = 0; |
| 124 uint32 num_valid_regions = 0; | 119 uint32 num_valid_regions = 0; |
| 125 ProcessMemoryMaps::VMRegion region; | 120 ProcessMemoryMaps::VMRegion region; |
| 126 bool should_add_current_region = false; | 121 bool should_add_current_region = false; |
| 127 for (;;) { | 122 for (;;) { |
| 128 int next = smaps->peek(); | 123 char next = 0; |
| 129 if (next == std::ifstream::traits_type::eof() || next == '\n') | 124 // Peek into the next integer and move pointer back. |
| 125 next = fgetc(smaps_file); | |
|
Primiano Tucci (use gerrit)
2015/09/30 09:37:02
how is this fgetc + ungetc (brrr) different from f
ssid
2015/09/30 11:15:13
feof only returns a boolean. Result of fgetc + ung
Primiano Tucci (use gerrit)
2015/09/30 15:36:35
At this point change this code to be line buffered
| |
| 126 if (next == EOF) | |
| 130 break; | 127 break; |
| 128 ungetc(next, smaps_file); | |
| 131 if (isxdigit(next) && !isupper(next)) { | 129 if (isxdigit(next) && !isupper(next)) { |
| 132 region = ProcessMemoryMaps::VMRegion(); | 130 region = ProcessMemoryMaps::VMRegion(); |
| 133 counters_parsed_for_current_region = 0; | 131 counters_parsed_for_current_region = 0; |
| 134 should_add_current_region = ParseSmapsHeader(smaps, ®ion); | 132 should_add_current_region = ParseSmapsHeader(smaps_file, ®ion); |
| 135 } else { | 133 } else { |
| 136 counters_parsed_for_current_region += ParseSmapsCounter(smaps, ®ion); | 134 counters_parsed_for_current_region += |
| 135 ParseSmapsCounter(smaps_file, ®ion); | |
| 137 DCHECK_LE(counters_parsed_for_current_region, | 136 DCHECK_LE(counters_parsed_for_current_region, |
| 138 kNumExpectedCountersPerRegion); | 137 kNumExpectedCountersPerRegion); |
| 139 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | 138 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { |
| 140 if (should_add_current_region) { | 139 if (should_add_current_region) { |
| 141 pmm->AddVMRegion(region); | 140 pmm->AddVMRegion(region); |
| 142 ++num_valid_regions; | 141 ++num_valid_regions; |
| 143 should_add_current_region = false; | 142 should_add_current_region = false; |
| 144 } | 143 } |
| 145 } | 144 } |
| 146 } | 145 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 168 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | 167 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
| 169 ProcessMemoryDump* pmd) { | 168 ProcessMemoryDump* pmd) { |
| 170 // Snapshot of memory maps is not taken for light dump requests. | 169 // Snapshot of memory maps is not taken for light dump requests. |
| 171 if (args.level_of_detail == MemoryDumpArgs::LevelOfDetail::LOW) | 170 if (args.level_of_detail == MemoryDumpArgs::LevelOfDetail::LOW) |
| 172 return true; | 171 return true; |
| 173 | 172 |
| 174 uint32 res = 0; | 173 uint32 res = 0; |
| 175 | 174 |
| 176 #if defined(OS_LINUX) || defined(OS_ANDROID) | 175 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 177 if (UNLIKELY(proc_smaps_for_testing)) { | 176 if (UNLIKELY(proc_smaps_for_testing)) { |
| 178 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | 177 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing->get(), |
| 178 pmd->process_mmaps()); | |
| 179 } else { | 179 } else { |
| 180 std::ifstream proc_self_smaps("/proc/self/smaps"); | 180 ScopedFILE smaps_file(OpenFile(FilePath("/proc/self/smaps"), "r")); |
| 181 res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps()); | 181 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
| 182 } | 182 } |
| 183 #else | 183 #else |
| 184 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; | 184 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; |
| 185 #endif | 185 #endif |
| 186 | 186 |
| 187 if (res > 0) { | 187 if (res > 0) { |
| 188 pmd->set_has_process_mmaps(); | 188 pmd->set_has_process_mmaps(); |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| 191 | 191 |
| 192 return false; | 192 return false; |
| 193 } | 193 } |
| 194 | 194 |
| 195 } // namespace trace_event | 195 } // namespace trace_event |
| 196 } // namespace base | 196 } // namespace base |
| OLD | NEW |