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 <fstream> | 8 #include <fstream> |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/process/process_metrics.h" | 11 #include "base/process/process_metrics.h" |
| 11 #include "base/trace_event/process_memory_dump.h" | 12 #include "base/trace_event/process_memory_dump.h" |
| 12 #include "base/trace_event/process_memory_maps.h" | 13 #include "base/trace_event/process_memory_maps.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 namespace trace_event { | 16 namespace trace_event { |
| 16 | 17 |
| 17 #if defined(OS_LINUX) || defined(OS_ANDROID) | 18 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 18 // static | 19 // static |
| 19 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | 20 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 23 | |
| 24 const uint32 kMaxLineSize = 4096; | |
| 25 | |
| 26 bool ParseSmapsHeader(std::istream* smaps, | |
| 27 ProcessMemoryMaps::VMRegion* region) { | |
| 28 // 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. | |
| 30 uint64 end_addr; | |
| 31 std::string protection_flags; | |
| 32 std::string ignored; | |
| 33 *smaps >> std::hex >> region->start_address; | |
| 34 smaps->ignore(1); | |
| 35 *smaps >> std::hex >> end_addr; | |
| 36 if (end_addr > region->start_address) { | |
| 37 region->size_in_bytes = end_addr - region->start_address; | |
| 38 } else { | |
| 39 // This is not just paranoia, it can actually happen (See crbug.com/461237). | |
| 40 region->size_in_bytes = 0; | |
| 41 res = false; | |
| 42 } | |
| 43 | |
| 44 region->protection_flags = 0; | |
| 45 *smaps >> protection_flags; | |
| 46 CHECK(4UL == protection_flags.size()); | |
| 47 if (protection_flags[0] == 'r') { | |
| 48 region->protection_flags |= | |
| 49 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; | |
| 50 } | |
| 51 if (protection_flags[1] == 'w') { | |
| 52 region->protection_flags |= | |
| 53 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; | |
| 54 } | |
| 55 if (protection_flags[2] == 'x') { | |
| 56 region->protection_flags |= | |
| 57 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; | |
| 58 } | |
| 59 *smaps >> std::hex >> region->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 | |
| 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; | |
| 68 | |
| 69 return res; | |
| 70 } | |
| 71 | |
| 72 uint32 ParseSmapsCounter(std::istream* smaps, | |
| 73 ProcessMemoryMaps::VMRegion* region) { | |
| 74 // e.g., "RSS: 0 Kb\n" | |
| 75 uint32 res = 0; | |
| 76 std::string counter_name; | |
| 77 *smaps >> counter_name; | |
| 78 | |
| 79 // TODO(primiano): "Swap" should also be accounted as resident. Check | |
| 80 // whether Rss isn't already counting swapped and fix below if that is | |
| 81 // the case. | |
| 82 if (counter_name == "Rss:") { | |
| 83 *smaps >> std::dec >> region->byte_stats_resident; | |
| 84 region->byte_stats_resident *= 1024; | |
| 85 res = 1; | |
| 86 } else if (counter_name == "Anonymous:") { | |
| 87 *smaps >> std::dec >> region->byte_stats_anonymous; | |
| 88 region->byte_stats_anonymous *= 1024; | |
| 89 res = 1; | |
| 90 } | |
| 91 smaps->ignore(kMaxLineSize, '\n'); | |
|
Sami
2015/02/24 11:11:25
I guess the kernel format is set in stone by now s
Primiano Tucci (use gerrit)
2015/02/24 13:17:07
I would expect tons of things to break in that cas
| |
| 92 | |
| 93 return res; | |
| 94 } | |
| 95 | |
| 22 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { | 96 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { |
| 23 if (!smaps->good()) { | 97 if (!smaps->good()) { |
| 24 LOG(ERROR) << "Could not read smaps file."; | 98 LOG(ERROR) << "Could not read smaps file."; |
| 25 return 0; | 99 return 0; |
| 26 } | 100 } |
| 101 const uint32 kNumExpectedCountersPerRegion = 2; | |
| 102 uint32 counters_parsed_for_current_region = 0; | |
| 27 uint32 num_regions_processed = 0; | 103 uint32 num_regions_processed = 0; |
| 28 // TODO(primiano): in next CLs add the actual code to process the smaps file. | 104 ProcessMemoryMaps::VMRegion region; |
| 105 bool is_current_region_valid = false; | |
| 106 for (;;) { | |
| 107 int next = smaps->peek(); | |
| 108 if (next == std::ifstream::traits_type::eof() || next == '\n') | |
| 109 break; | |
| 110 if (isxdigit(next) && !isupper(next)) { | |
| 111 region = {0}; | |
| 112 counters_parsed_for_current_region = 0; | |
| 113 is_current_region_valid = ParseSmapsHeader(smaps, ®ion); | |
| 114 } else { | |
| 115 counters_parsed_for_current_region += ParseSmapsCounter(smaps, ®ion); | |
| 116 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | |
| 117 if (is_current_region_valid) | |
| 118 pmm->AddVMRegion(region); | |
| 119 ++num_regions_processed; | |
| 120 counters_parsed_for_current_region = 0; | |
| 121 } | |
| 122 } | |
| 123 } | |
| 29 return num_regions_processed; | 124 return num_regions_processed; |
| 30 } | 125 } |
| 126 | |
| 31 } // namespace | 127 } // namespace |
| 32 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 128 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 33 | 129 |
| 34 // static | 130 // static |
| 35 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { | 131 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { |
| 36 return Singleton<ProcessMemoryMapsDumpProvider, | 132 return Singleton<ProcessMemoryMapsDumpProvider, |
| 37 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); | 133 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); |
| 38 } | 134 } |
| 39 | 135 |
| 40 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() { | 136 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 58 #else | 154 #else |
| 59 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; | 155 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; |
| 60 #endif | 156 #endif |
| 61 | 157 |
| 62 if (res > 0) | 158 if (res > 0) |
| 63 pmd->set_has_process_mmaps(); | 159 pmd->set_has_process_mmaps(); |
| 64 } | 160 } |
| 65 | 161 |
| 66 } // namespace trace_event | 162 } // namespace trace_event |
| 67 } // namespace base | 163 } // namespace base |
| OLD | NEW |