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 zeros from the bitmap. It can save |
50 // some hundred ms. when printing large mmap-ed and non resident regions. | |
Sami
2014/09/09 10:44:26
Trailing == most significant bits, hence likely to
Primiano Tucci (use gerrit)
2014/09/09 11:11:38
Hmm no, least significant bytes, not bits.
Imagine
Sami
2014/09/09 11:16:20
Ah, somehow I was confusing this with addresses in
| |
51 size_t end = data_.size(); | |
52 while (end > 0 && data_[end - 1] == '\0') | |
53 --end; | |
54 std::string bits(&data_[0], end); | |
49 std::string b64_string; | 55 std::string b64_string; |
50 base::Base64Encode(bits, &b64_string); | 56 base::Base64Encode(bits, &b64_string); |
51 return b64_string; | 57 return b64_string; |
52 } | 58 } |
53 | 59 |
54 private: | 60 private: |
55 std::vector<char> data_; | 61 std::vector<char> data_; |
56 }; | 62 }; |
57 | 63 |
58 // An entry in /proc/<pid>/pagemap. | 64 // An entry in /proc/<pid>/pagemap. |
(...skipping 13 matching lines...) Expand all Loading... | |
72 struct PageCount { | 78 struct PageCount { |
73 PageCount() : total_count(0), unevictable_count(0) {} | 79 PageCount() : total_count(0), unevictable_count(0) {} |
74 | 80 |
75 int total_count; | 81 int total_count; |
76 int unevictable_count; | 82 int unevictable_count; |
77 }; | 83 }; |
78 | 84 |
79 struct MemoryMap { | 85 struct MemoryMap { |
80 std::string name; | 86 std::string name; |
81 std::string flags; | 87 std::string flags; |
82 uint start_address; | 88 uint64 start_address; |
83 uint end_address; | 89 uint64 end_address; |
84 uint offset; | 90 uint64 offset; |
85 PageCount private_pages; | 91 PageCount private_pages; |
86 // app_shared_pages[i] contains the number of pages mapped in i+2 processes | 92 // app_shared_pages[i] contains the number of pages mapped in i+2 processes |
87 // (only among the processes that are being analyzed). | 93 // (only among the processes that are being analyzed). |
88 std::vector<PageCount> app_shared_pages; | 94 std::vector<PageCount> app_shared_pages; |
89 PageCount other_shared_pages; | 95 PageCount other_shared_pages; |
90 std::vector<PageInfo> committed_pages; | 96 std::vector<PageInfo> committed_pages; |
91 // committed_pages_bits is a bitset reflecting the present bit for all the | 97 // committed_pages_bits is a bitset reflecting the present bit for all the |
92 // virtual pages of the mapping. | 98 // virtual pages of the mapping. |
93 BitSet committed_pages_bits; | 99 BitSet committed_pages_bits; |
94 }; | 100 }; |
(...skipping 25 matching lines...) Expand all Loading... | |
120 bool ParseMemoryMapLine(const std::string& line, | 126 bool ParseMemoryMapLine(const std::string& line, |
121 std::vector<std::string>* tokens, | 127 std::vector<std::string>* tokens, |
122 MemoryMap* memory_map) { | 128 MemoryMap* memory_map) { |
123 tokens->clear(); | 129 tokens->clear(); |
124 base::SplitString(line, ' ', tokens); | 130 base::SplitString(line, ' ', tokens); |
125 if (tokens->size() < 2) | 131 if (tokens->size() < 2) |
126 return false; | 132 return false; |
127 const std::string& addr_range = tokens->at(0); | 133 const std::string& addr_range = tokens->at(0); |
128 std::vector<std::string> range_tokens; | 134 std::vector<std::string> range_tokens; |
129 base::SplitString(addr_range, '-', &range_tokens); | 135 base::SplitString(addr_range, '-', &range_tokens); |
130 uint64 tmp = 0; | |
131 const std::string& start_address_token = range_tokens.at(0); | 136 const std::string& start_address_token = range_tokens.at(0); |
132 if (!base::HexStringToUInt64(start_address_token, &tmp)) { | 137 if (!base::HexStringToUInt64(start_address_token, |
138 &memory_map->start_address)) { | |
133 return false; | 139 return false; |
134 } | 140 } |
135 memory_map->start_address = static_cast<uint>(tmp); | |
136 const std::string& end_address_token = range_tokens.at(1); | 141 const std::string& end_address_token = range_tokens.at(1); |
137 if (!base::HexStringToUInt64(end_address_token, &tmp)) { | 142 if (!base::HexStringToUInt64(end_address_token, &memory_map->end_address)) { |
138 return false; | 143 return false; |
139 } | 144 } |
140 memory_map->end_address = static_cast<uint>(tmp); | |
141 if (tokens->at(1).size() != strlen("rwxp")) | 145 if (tokens->at(1).size() != strlen("rwxp")) |
142 return false; | 146 return false; |
143 memory_map->flags.swap(tokens->at(1)); | 147 memory_map->flags.swap(tokens->at(1)); |
144 if (!base::HexStringToUInt64(tokens->at(2), &tmp)) | 148 if (!base::HexStringToUInt64(tokens->at(2), &memory_map->offset)) |
145 return false; | 149 return false; |
146 memory_map->offset = static_cast<uint>(tmp); | |
147 memory_map->committed_pages_bits.resize( | 150 memory_map->committed_pages_bits.resize( |
148 (memory_map->end_address - memory_map->start_address) / kPageSize); | 151 (memory_map->end_address - memory_map->start_address) / kPageSize); |
149 const int map_name_index = 5; | 152 const int map_name_index = 5; |
150 if (tokens->size() >= map_name_index + 1) { | 153 if (tokens->size() >= map_name_index + 1) { |
151 for (std::vector<std::string>::const_iterator it = | 154 for (std::vector<std::string>::const_iterator it = |
152 tokens->begin() + map_name_index; it != tokens->end(); ++it) { | 155 tokens->begin() + map_name_index; it != tokens->end(); ++it) { |
153 if (!it->empty()) { | 156 if (!it->empty()) { |
154 if (!memory_map->name.empty()) | 157 if (!memory_map->name.empty()) |
155 memory_map->name.append(" "); | 158 memory_map->name.append(" "); |
156 memory_map->name.append(*it); | 159 memory_map->name.append(*it); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 // provided memory map. | 202 // provided memory map. |
200 bool GetPagesForMemoryMap(int pagemap_fd, | 203 bool GetPagesForMemoryMap(int pagemap_fd, |
201 const MemoryMap& memory_map, | 204 const MemoryMap& memory_map, |
202 std::vector<PageInfo>* committed_pages, | 205 std::vector<PageInfo>* committed_pages, |
203 BitSet* committed_pages_bits) { | 206 BitSet* committed_pages_bits) { |
204 const off64_t offset = memory_map.start_address / kPageSize; | 207 const off64_t offset = memory_map.start_address / kPageSize; |
205 if (lseek64(pagemap_fd, offset * sizeof(PageMapEntry), SEEK_SET) < 0) { | 208 if (lseek64(pagemap_fd, offset * sizeof(PageMapEntry), SEEK_SET) < 0) { |
206 PLOG(ERROR) << "lseek"; | 209 PLOG(ERROR) << "lseek"; |
207 return false; | 210 return false; |
208 } | 211 } |
209 for (uint addr = memory_map.start_address, page_index = 0; | 212 for (uint64 addr = memory_map.start_address, page_index = 0; |
210 addr < memory_map.end_address; | 213 addr < memory_map.end_address; |
211 addr += kPageSize, ++page_index) { | 214 addr += kPageSize, ++page_index) { |
212 DCHECK_EQ(0, addr % kPageSize); | 215 DCHECK_EQ(0, addr % kPageSize); |
213 PageMapEntry page_map_entry = {}; | 216 PageMapEntry page_map_entry = {}; |
214 COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); | 217 COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); |
215 ssize_t bytes = read(pagemap_fd, &page_map_entry, sizeof(page_map_entry)); | 218 ssize_t bytes = read(pagemap_fd, &page_map_entry, sizeof(page_map_entry)); |
216 if (bytes != sizeof(PageMapEntry) && bytes != 0) { | 219 if (bytes != sizeof(PageMapEntry) && bytes != 0) { |
217 PLOG(ERROR) << "read"; | 220 PLOG(ERROR) << "read"; |
218 return false; | 221 return false; |
219 } | 222 } |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 const ProcessMemory& process_memory = *it; | 411 const ProcessMemory& process_memory = *it; |
409 std::cout << "[ PID=" << process_memory.pid << "]" << '\n'; | 412 std::cout << "[ PID=" << process_memory.pid << "]" << '\n'; |
410 const std::vector<MemoryMap>& memory_maps = process_memory.memory_maps; | 413 const std::vector<MemoryMap>& memory_maps = process_memory.memory_maps; |
411 for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); | 414 for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); |
412 it != memory_maps.end(); ++it) { | 415 it != memory_maps.end(); ++it) { |
413 const MemoryMap& memory_map = *it; | 416 const MemoryMap& memory_map = *it; |
414 app_shared_buf.clear(); | 417 app_shared_buf.clear(); |
415 AppendAppSharedField(memory_map.app_shared_pages, &app_shared_buf); | 418 AppendAppSharedField(memory_map.app_shared_pages, &app_shared_buf); |
416 base::SStringPrintf( | 419 base::SStringPrintf( |
417 &buf, | 420 &buf, |
418 "%x-%x %s %x private_unevictable=%d private=%d shared_app=%s " | 421 "%"PRIx64"-%"PRIx64" %s %"PRIx64" private_unevictable=%d private=%d " |
419 "shared_other_unevictable=%d shared_other=%d \"%s\" [%s]\n", | 422 "shared_app=%s shared_other_unevictable=%d shared_other=%d " |
423 "\"%s\" [%s]\n", | |
420 memory_map.start_address, | 424 memory_map.start_address, |
421 memory_map.end_address, | 425 memory_map.end_address, |
422 memory_map.flags.c_str(), | 426 memory_map.flags.c_str(), |
423 memory_map.offset, | 427 memory_map.offset, |
424 memory_map.private_pages.unevictable_count * kPageSize, | 428 memory_map.private_pages.unevictable_count * kPageSize, |
425 memory_map.private_pages.total_count * kPageSize, | 429 memory_map.private_pages.total_count * kPageSize, |
426 app_shared_buf.c_str(), | 430 app_shared_buf.c_str(), |
427 memory_map.other_shared_pages.unevictable_count * kPageSize, | 431 memory_map.other_shared_pages.unevictable_count * kPageSize, |
428 memory_map.other_shared_pages.total_count * kPageSize, | 432 memory_map.other_shared_pages.total_count * kPageSize, |
429 memory_map.name.c_str(), | 433 memory_map.name.c_str(), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 } | 521 } |
518 } | 522 } |
519 | 523 |
520 ClassifyPages(&processes_memory); | 524 ClassifyPages(&processes_memory); |
521 if (short_output) | 525 if (short_output) |
522 DumpProcessesMemoryMapsInShortFormat(processes_memory); | 526 DumpProcessesMemoryMapsInShortFormat(processes_memory); |
523 else | 527 else |
524 DumpProcessesMemoryMapsInExtendedFormat(processes_memory); | 528 DumpProcessesMemoryMapsInExtendedFormat(processes_memory); |
525 return EXIT_SUCCESS; | 529 return EXIT_SUCCESS; |
526 } | 530 } |
OLD | NEW |