OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <fcntl.h> | 5 #include <fcntl.h> |
6 #include <signal.h> | 6 #include <signal.h> |
7 #include <sys/types.h> | 7 #include <sys/types.h> |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <cstring> | 11 #include <cstring> |
12 #include <fstream> | 12 #include <fstream> |
13 #include <iostream> | 13 #include <iostream> |
14 #include <limits> | 14 #include <limits> |
15 #include <string> | 15 #include <string> |
16 #include <utility> | 16 #include <utility> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/base64.h" | 19 #include "base/base64.h" |
20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
21 #include "base/bind.h" | 21 #include "base/bind.h" |
22 #include "base/callback_helpers.h" | 22 #include "base/callback_helpers.h" |
23 #include "base/containers/hash_tables.h" | 23 #include "base/containers/hash_tables.h" |
24 #include "base/file_util.h" | 24 #include "base/file_util.h" |
25 #include "base/files/scoped_file.h" | 25 #include "base/files/scoped_file.h" |
| 26 #include "base/format_macros.h" |
26 #include "base/logging.h" | 27 #include "base/logging.h" |
27 #include "base/strings/string_number_conversions.h" | 28 #include "base/strings/string_number_conversions.h" |
28 #include "base/strings/string_split.h" | 29 #include "base/strings/string_split.h" |
29 #include "base/strings/stringprintf.h" | 30 #include "base/strings/stringprintf.h" |
30 | 31 |
31 const unsigned int kPageSize = getpagesize(); | 32 const unsigned int kPageSize = getpagesize(); |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 class BitSet { | 36 class BitSet { |
36 public: | 37 public: |
37 void resize(size_t nbits) { | 38 void resize(size_t nbits) { |
38 data_.resize((nbits + 7) / 8); | 39 data_.resize((nbits + 7) / 8); |
39 } | 40 } |
40 | 41 |
41 void set(uint32 bit) { | 42 void set(uint32 bit) { |
42 const uint32 byte_idx = bit / 8; | 43 const uint32 byte_idx = bit / 8; |
43 CHECK(byte_idx < data_.size()); | 44 CHECK(byte_idx < data_.size()); |
44 data_[byte_idx] |= (1 << (bit & 7)); | 45 data_[byte_idx] |= (1 << (bit & 7)); |
45 } | 46 } |
46 | 47 |
47 std::string AsB64String() const { | 48 std::string AsB64String() const { |
48 std::string bits(&data_[0], data_.size()); | 49 // Simple optimization: strip trailing zero bytes from the bitmap. |
| 50 // For instance, if a region has 32 pages but only the first 9 are resident, |
| 51 // The full bitmap would be 0xff 0x01 0x00 0x00, the stripped one 0xff 0x01. |
| 52 // It can save up to some seconds when printing large mmaps, in particular |
| 53 // in presence of large virtual address space reservations (where none of |
| 54 // the pages are resident). |
| 55 size_t end = data_.size(); |
| 56 while (end > 0 && data_[end - 1] == '\0') |
| 57 --end; |
| 58 std::string bits(&data_[0], end); |
49 std::string b64_string; | 59 std::string b64_string; |
50 base::Base64Encode(bits, &b64_string); | 60 base::Base64Encode(bits, &b64_string); |
51 return b64_string; | 61 return b64_string; |
52 } | 62 } |
53 | 63 |
54 private: | 64 private: |
55 std::vector<char> data_; | 65 std::vector<char> data_; |
56 }; | 66 }; |
57 | 67 |
58 // An entry in /proc/<pid>/pagemap. | 68 // An entry in /proc/<pid>/pagemap. |
(...skipping 13 matching lines...) Expand all Loading... |
72 struct PageCount { | 82 struct PageCount { |
73 PageCount() : total_count(0), unevictable_count(0) {} | 83 PageCount() : total_count(0), unevictable_count(0) {} |
74 | 84 |
75 int total_count; | 85 int total_count; |
76 int unevictable_count; | 86 int unevictable_count; |
77 }; | 87 }; |
78 | 88 |
79 struct MemoryMap { | 89 struct MemoryMap { |
80 std::string name; | 90 std::string name; |
81 std::string flags; | 91 std::string flags; |
82 uint start_address; | 92 uint64 start_address; |
83 uint end_address; | 93 uint64 end_address; |
84 uint offset; | 94 uint64 offset; |
85 PageCount private_pages; | 95 PageCount private_pages; |
86 // app_shared_pages[i] contains the number of pages mapped in i+2 processes | 96 // app_shared_pages[i] contains the number of pages mapped in i+2 processes |
87 // (only among the processes that are being analyzed). | 97 // (only among the processes that are being analyzed). |
88 std::vector<PageCount> app_shared_pages; | 98 std::vector<PageCount> app_shared_pages; |
89 PageCount other_shared_pages; | 99 PageCount other_shared_pages; |
90 std::vector<PageInfo> committed_pages; | 100 std::vector<PageInfo> committed_pages; |
91 // committed_pages_bits is a bitset reflecting the present bit for all the | 101 // committed_pages_bits is a bitset reflecting the present bit for all the |
92 // virtual pages of the mapping. | 102 // virtual pages of the mapping. |
93 BitSet committed_pages_bits; | 103 BitSet committed_pages_bits; |
94 }; | 104 }; |
(...skipping 25 matching lines...) Expand all Loading... |
120 bool ParseMemoryMapLine(const std::string& line, | 130 bool ParseMemoryMapLine(const std::string& line, |
121 std::vector<std::string>* tokens, | 131 std::vector<std::string>* tokens, |
122 MemoryMap* memory_map) { | 132 MemoryMap* memory_map) { |
123 tokens->clear(); | 133 tokens->clear(); |
124 base::SplitString(line, ' ', tokens); | 134 base::SplitString(line, ' ', tokens); |
125 if (tokens->size() < 2) | 135 if (tokens->size() < 2) |
126 return false; | 136 return false; |
127 const std::string& addr_range = tokens->at(0); | 137 const std::string& addr_range = tokens->at(0); |
128 std::vector<std::string> range_tokens; | 138 std::vector<std::string> range_tokens; |
129 base::SplitString(addr_range, '-', &range_tokens); | 139 base::SplitString(addr_range, '-', &range_tokens); |
130 uint64 tmp = 0; | |
131 const std::string& start_address_token = range_tokens.at(0); | 140 const std::string& start_address_token = range_tokens.at(0); |
132 if (!base::HexStringToUInt64(start_address_token, &tmp)) { | 141 if (!base::HexStringToUInt64(start_address_token, |
| 142 &memory_map->start_address)) { |
133 return false; | 143 return false; |
134 } | 144 } |
135 memory_map->start_address = static_cast<uint>(tmp); | |
136 const std::string& end_address_token = range_tokens.at(1); | 145 const std::string& end_address_token = range_tokens.at(1); |
137 if (!base::HexStringToUInt64(end_address_token, &tmp)) { | 146 if (!base::HexStringToUInt64(end_address_token, &memory_map->end_address)) { |
138 return false; | 147 return false; |
139 } | 148 } |
140 memory_map->end_address = static_cast<uint>(tmp); | |
141 if (tokens->at(1).size() != strlen("rwxp")) | 149 if (tokens->at(1).size() != strlen("rwxp")) |
142 return false; | 150 return false; |
143 memory_map->flags.swap(tokens->at(1)); | 151 memory_map->flags.swap(tokens->at(1)); |
144 if (!base::HexStringToUInt64(tokens->at(2), &tmp)) | 152 if (!base::HexStringToUInt64(tokens->at(2), &memory_map->offset)) |
145 return false; | 153 return false; |
146 memory_map->offset = static_cast<uint>(tmp); | |
147 memory_map->committed_pages_bits.resize( | 154 memory_map->committed_pages_bits.resize( |
148 (memory_map->end_address - memory_map->start_address) / kPageSize); | 155 (memory_map->end_address - memory_map->start_address) / kPageSize); |
149 const int map_name_index = 5; | 156 const int map_name_index = 5; |
150 if (tokens->size() >= map_name_index + 1) { | 157 if (tokens->size() >= map_name_index + 1) { |
151 for (std::vector<std::string>::const_iterator it = | 158 for (std::vector<std::string>::const_iterator it = |
152 tokens->begin() + map_name_index; it != tokens->end(); ++it) { | 159 tokens->begin() + map_name_index; it != tokens->end(); ++it) { |
153 if (!it->empty()) { | 160 if (!it->empty()) { |
154 if (!memory_map->name.empty()) | 161 if (!memory_map->name.empty()) |
155 memory_map->name.append(" "); | 162 memory_map->name.append(" "); |
156 memory_map->name.append(*it); | 163 memory_map->name.append(*it); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // provided memory map. | 206 // provided memory map. |
200 bool GetPagesForMemoryMap(int pagemap_fd, | 207 bool GetPagesForMemoryMap(int pagemap_fd, |
201 const MemoryMap& memory_map, | 208 const MemoryMap& memory_map, |
202 std::vector<PageInfo>* committed_pages, | 209 std::vector<PageInfo>* committed_pages, |
203 BitSet* committed_pages_bits) { | 210 BitSet* committed_pages_bits) { |
204 const off64_t offset = memory_map.start_address / kPageSize; | 211 const off64_t offset = memory_map.start_address / kPageSize; |
205 if (lseek64(pagemap_fd, offset * sizeof(PageMapEntry), SEEK_SET) < 0) { | 212 if (lseek64(pagemap_fd, offset * sizeof(PageMapEntry), SEEK_SET) < 0) { |
206 PLOG(ERROR) << "lseek"; | 213 PLOG(ERROR) << "lseek"; |
207 return false; | 214 return false; |
208 } | 215 } |
209 for (uint addr = memory_map.start_address, page_index = 0; | 216 for (uint64 addr = memory_map.start_address, page_index = 0; |
210 addr < memory_map.end_address; | 217 addr < memory_map.end_address; |
211 addr += kPageSize, ++page_index) { | 218 addr += kPageSize, ++page_index) { |
212 DCHECK_EQ(0, addr % kPageSize); | 219 DCHECK_EQ(0, addr % kPageSize); |
213 PageMapEntry page_map_entry = {}; | 220 PageMapEntry page_map_entry = {}; |
214 COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); | 221 COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); |
215 ssize_t bytes = read(pagemap_fd, &page_map_entry, sizeof(page_map_entry)); | 222 ssize_t bytes = read(pagemap_fd, &page_map_entry, sizeof(page_map_entry)); |
216 if (bytes != sizeof(PageMapEntry) && bytes != 0) { | 223 if (bytes != sizeof(PageMapEntry) && bytes != 0) { |
217 PLOG(ERROR) << "read"; | 224 PLOG(ERROR) << "read"; |
218 return false; | 225 return false; |
219 } | 226 } |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 const ProcessMemory& process_memory = *it; | 415 const ProcessMemory& process_memory = *it; |
409 std::cout << "[ PID=" << process_memory.pid << "]" << '\n'; | 416 std::cout << "[ PID=" << process_memory.pid << "]" << '\n'; |
410 const std::vector<MemoryMap>& memory_maps = process_memory.memory_maps; | 417 const std::vector<MemoryMap>& memory_maps = process_memory.memory_maps; |
411 for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); | 418 for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); |
412 it != memory_maps.end(); ++it) { | 419 it != memory_maps.end(); ++it) { |
413 const MemoryMap& memory_map = *it; | 420 const MemoryMap& memory_map = *it; |
414 app_shared_buf.clear(); | 421 app_shared_buf.clear(); |
415 AppendAppSharedField(memory_map.app_shared_pages, &app_shared_buf); | 422 AppendAppSharedField(memory_map.app_shared_pages, &app_shared_buf); |
416 base::SStringPrintf( | 423 base::SStringPrintf( |
417 &buf, | 424 &buf, |
418 "%x-%x %s %x private_unevictable=%d private=%d shared_app=%s " | 425 "%"PRIx64"-%"PRIx64" %s %"PRIx64" private_unevictable=%d private=%d " |
419 "shared_other_unevictable=%d shared_other=%d \"%s\" [%s]\n", | 426 "shared_app=%s shared_other_unevictable=%d shared_other=%d " |
| 427 "\"%s\" [%s]\n", |
420 memory_map.start_address, | 428 memory_map.start_address, |
421 memory_map.end_address, | 429 memory_map.end_address, |
422 memory_map.flags.c_str(), | 430 memory_map.flags.c_str(), |
423 memory_map.offset, | 431 memory_map.offset, |
424 memory_map.private_pages.unevictable_count * kPageSize, | 432 memory_map.private_pages.unevictable_count * kPageSize, |
425 memory_map.private_pages.total_count * kPageSize, | 433 memory_map.private_pages.total_count * kPageSize, |
426 app_shared_buf.c_str(), | 434 app_shared_buf.c_str(), |
427 memory_map.other_shared_pages.unevictable_count * kPageSize, | 435 memory_map.other_shared_pages.unevictable_count * kPageSize, |
428 memory_map.other_shared_pages.total_count * kPageSize, | 436 memory_map.other_shared_pages.total_count * kPageSize, |
429 memory_map.name.c_str(), | 437 memory_map.name.c_str(), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 } | 525 } |
518 } | 526 } |
519 | 527 |
520 ClassifyPages(&processes_memory); | 528 ClassifyPages(&processes_memory); |
521 if (short_output) | 529 if (short_output) |
522 DumpProcessesMemoryMapsInShortFormat(processes_memory); | 530 DumpProcessesMemoryMapsInShortFormat(processes_memory); |
523 else | 531 else |
524 DumpProcessesMemoryMapsInExtendedFormat(processes_memory); | 532 DumpProcessesMemoryMapsInExtendedFormat(processes_memory); |
525 return EXIT_SUCCESS; | 533 return EXIT_SUCCESS; |
526 } | 534 } |
OLD | NEW |