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 <fstream> | 7 #include <fstream> |
| 8 | 8 |
| 9 #include "base/process/process_metrics.h" | 9 #include "base/process/process_metrics.h" |
| 10 #include "base/trace_event/process_memory_dump.h" | 10 #include "base/trace_event/process_memory_dump.h" |
| 11 #include "base/trace_event/process_memory_maps.h" | 11 #include "base/trace_event/process_memory_maps.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 namespace trace_event { | 14 namespace trace_event { |
| 15 | 15 |
| 16 #if defined(OS_LINUX) || defined(OS_ANDROID) | 16 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 17 // static | 17 // static |
| 18 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | 18 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { | 21 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { |
| 22 if (!smaps->good()) { | 22 if (!smaps->good()) { |
| 23 LOG(ERROR) << "Could not read smaps file."; | 23 LOG(ERROR) << "Could not read smaps file."; |
| 24 return 0; | 24 return 0; |
| 25 } | 25 } |
| 26 const uint32 kNumExpectedCountersPerRegion = 2; | |
| 27 const uint32 kMaxLineSize = 4096; | |
| 28 uint32 counters_parsed_for_current_region = 0; | |
| 26 uint32 num_regions_processed = 0; | 29 uint32 num_regions_processed = 0; |
| 27 // TODO(primiano): in next CLs add the actual code to process the smaps file. | 30 ProcessMemoryMaps::VMRegion region; |
| 31 for (;;) { | |
| 32 int next = smaps->peek(); | |
| 33 if (next == std::ifstream::traits_type::eof() || next == '\n') | |
| 34 break; | |
| 35 if ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f')) { | |
|
dsinclair
2015/02/23 15:37:12
What about A-Z? (There is a isxdigit() in <cctype>
Primiano Tucci (use gerrit)
2015/02/23 16:57:03
![A-F] is deliberate. The kernel always used (so f
| |
| 36 // Parse head line: "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so" | |
|
dsinclair
2015/02/23 15:37:12
Add the \n to the end since, it looks like, it wil
Primiano Tucci (use gerrit)
2015/02/23 16:57:03
Done.
| |
| 37 uint64 end_addr; | |
| 38 std::string prot_flags; | |
| 39 std::string ignored; | |
| 40 region = {0}; | |
| 41 counters_parsed_for_current_region = 0; | |
| 42 | |
| 43 *smaps >> std::hex >> region.start_address; | |
| 44 smaps->ignore(1); | |
| 45 *smaps >> std::hex >> end_addr; | |
| 46 region.size_in_bytes = end_addr - region.start_address; | |
| 47 region.protection_flags = 0; | |
| 48 *smaps >> prot_flags; | |
| 49 CHECK(4UL == prot_flags.size()); | |
| 50 if (prot_flags[0] == 'r') | |
| 51 region.protection_flags |= ProcessMemoryMaps::VMRegion::kProtRead; | |
| 52 if (prot_flags[1] == 'w') | |
| 53 region.protection_flags |= ProcessMemoryMaps::VMRegion::kProtWrite; | |
| 54 if (prot_flags[2] == 'x') | |
| 55 region.protection_flags |= ProcessMemoryMaps::VMRegion::kProtExec; | |
| 56 *smaps >> std::hex >> region.mapped_file_offset; | |
| 57 *smaps >> ignored; // Ignore device maj-min (fc:01 in the example above). | |
| 58 *smaps >> ignored; // Ignore inode number (1234 in the example above). | |
| 59 | |
| 60 while (smaps->peek() == ' ') | |
| 61 smaps->ignore(1); | |
| 62 char mapped_file[kMaxLineSize]; | |
| 63 smaps->getline(mapped_file, sizeof(mapped_file)); | |
| 64 region.mapped_file = mapped_file; | |
|
dsinclair
2015/02/23 15:37:12
Can the bodies of the if/else get pulled out into
Primiano Tucci (use gerrit)
2015/02/23 16:57:03
Done.
| |
| 65 } else { | |
| 66 // Parse counter line: "VmRSS: 0 Kb" | |
|
dsinclair
2015/02/23 15:37:12
If the \n gets consumed below, add it to the examp
Primiano Tucci (use gerrit)
2015/02/23 16:57:03
Done.
| |
| 67 std::string counter_name; | |
| 68 *smaps >> counter_name; | |
| 69 // TODO(primiano): "Swap" should also be accounted as resident. Check | |
| 70 // whether Rss isn't already counting swapped and fix below if that is | |
| 71 // the case. | |
| 72 if (counter_name == "Rss:") { | |
| 73 *smaps >> std::dec >> region.bytes_stats_resident; | |
| 74 region.bytes_stats_resident *= 1024; | |
| 75 ++counters_parsed_for_current_region; | |
| 76 } else if (counter_name == "Anonymous:") { | |
| 77 *smaps >> std::dec >> region.bytes_stats_anonymous; | |
| 78 region.bytes_stats_anonymous *= 1024; | |
| 79 ++counters_parsed_for_current_region; | |
| 80 } | |
| 81 smaps->ignore(kMaxLineSize, '\n'); | |
| 82 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | |
| 83 pmm->AddVMRegion(region); | |
| 84 ++num_regions_processed; | |
| 85 counters_parsed_for_current_region = 0; | |
| 86 } | |
| 87 } | |
| 88 } | |
| 28 return num_regions_processed; | 89 return num_regions_processed; |
| 29 } | 90 } |
| 30 } // namespace | 91 } // namespace |
| 31 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 92 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 32 | 93 |
| 33 // static | 94 // static |
| 34 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { | 95 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { |
| 35 return Singleton<ProcessMemoryMapsDumpProvider, | 96 return Singleton<ProcessMemoryMapsDumpProvider, |
| 36 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); | 97 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); |
| 37 } | 98 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 57 #else | 118 #else |
| 58 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; | 119 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; |
| 59 #endif | 120 #endif |
| 60 | 121 |
| 61 if (res > 0) | 122 if (res > 0) |
| 62 pmd->set_has_process_mmaps(); | 123 pmd->set_has_process_mmaps(); |
| 63 } | 124 } |
| 64 | 125 |
| 65 } // namespace trace_event | 126 } // namespace trace_event |
| 66 } // namespace base | 127 } // namespace base |
| OLD | NEW |