| 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/basictypes.h" | 20 #include "base/basictypes.h" |
| 20 #include "base/bind.h" | 21 #include "base/bind.h" |
| 21 #include "base/bind_helpers.h" | 22 #include "base/bind_helpers.h" |
| 22 #include "base/containers/hash_tables.h" | 23 #include "base/containers/hash_tables.h" |
| 23 #include "base/file_util.h" | 24 #include "base/file_util.h" |
| 24 #include "base/logging.h" | 25 #include "base/logging.h" |
| 25 #include "base/memory/scoped_ptr.h" | 26 #include "base/memory/scoped_ptr.h" |
| 26 #include "base/strings/string_number_conversions.h" | 27 #include "base/strings/string_number_conversions.h" |
| 27 #include "base/strings/string_piece.h" | 28 #include "base/strings/string_piece.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 namespace { | 32 namespace { |
| 32 | 33 |
| 34 class BitSet { |
| 35 public: |
| 36 void resize(size_t nbits) { |
| 37 data_.resize((nbits + 7) / 8); |
| 38 } |
| 39 |
| 40 void set(uint32 bit) { |
| 41 const uint32 byte_idx = bit / 8; |
| 42 CHECK(byte_idx < data_.size()); |
| 43 data_[byte_idx] |= (1 << (bit & 7)); |
| 44 } |
| 45 |
| 46 std::string AsB64String() const { |
| 47 std::string bits(&data_[0], data_.size()); |
| 48 std::string b64_string; |
| 49 base::Base64Encode(bits, &b64_string); |
| 50 return b64_string; |
| 51 } |
| 52 |
| 53 private: |
| 54 std::vector<char> data_; |
| 55 }; |
| 56 |
| 33 // An entry in /proc/<pid>/pagemap. | 57 // An entry in /proc/<pid>/pagemap. |
| 34 struct PageMapEntry { | 58 struct PageMapEntry { |
| 35 uint64 page_frame_number : 55; | 59 uint64 page_frame_number : 55; |
| 36 uint unused : 8; | 60 uint unused : 8; |
| 37 uint present : 1; | 61 uint present : 1; |
| 38 }; | 62 }; |
| 39 | 63 |
| 40 // Describes a memory page. | 64 // Describes a memory page. |
| 41 struct PageInfo { | 65 struct PageInfo { |
| 42 int64 page_frame_number; // Physical page id, also known as PFN. | 66 int64 page_frame_number; // Physical page id, also known as PFN. |
| 43 int64 flags; | 67 int64 flags; |
| 44 int32 times_mapped; | 68 int32 times_mapped; |
| 45 }; | 69 }; |
| 46 | 70 |
| 47 struct MemoryMap { | 71 struct MemoryMap { |
| 48 std::string name; | 72 std::string name; |
| 49 std::string flags; | 73 std::string flags; |
| 50 uint start_address; | 74 uint start_address; |
| 51 uint end_address; | 75 uint end_address; |
| 76 uint offset; |
| 52 int private_count; | 77 int private_count; |
| 53 int unevictable_private_count; | 78 int unevictable_private_count; |
| 54 int other_shared_count; | 79 int other_shared_count; |
| 55 int unevictable_other_shared_count; | 80 int unevictable_other_shared_count; |
| 56 // app_shared_counts[i] contains the number of pages mapped in i+2 processes | 81 // app_shared_counts[i] contains the number of pages mapped in i+2 processes |
| 57 // (only among the processes that are being analyzed). | 82 // (only among the processes that are being analyzed). |
| 58 std::vector<int> app_shared_counts; | 83 std::vector<int> app_shared_counts; |
| 59 std::vector<PageInfo> committed_pages; | 84 std::vector<PageInfo> committed_pages; |
| 85 // committed_pages_bits is a bitset reflecting the present bit for all the |
| 86 // virtual pages of the mapping. |
| 87 BitSet committed_pages_bits; |
| 60 }; | 88 }; |
| 61 | 89 |
| 62 struct ProcessMemory { | 90 struct ProcessMemory { |
| 63 pid_t pid; | 91 pid_t pid; |
| 64 std::vector<MemoryMap> memory_maps; | 92 std::vector<MemoryMap> memory_maps; |
| 65 }; | 93 }; |
| 66 | 94 |
| 67 bool PageIsUnevictable(const PageInfo& page_info) { | 95 bool PageIsUnevictable(const PageInfo& page_info) { |
| 68 // These constants are taken from kernel-page-flags.h. | 96 // These constants are taken from kernel-page-flags.h. |
| 69 const int KPF_DIRTY = 4; // Note that only file-mapped pages can be DIRTY. | 97 const int KPF_DIRTY = 4; // Note that only file-mapped pages can be DIRTY. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 base::StringPiece( | 135 base::StringPiece( |
| 108 addr_range.begin() + end_addr_start_pos, | 136 addr_range.begin() + end_addr_start_pos, |
| 109 addr_range.begin() + end_addr_start_pos + addr_len), | 137 addr_range.begin() + end_addr_start_pos + addr_len), |
| 110 &tmp)) { | 138 &tmp)) { |
| 111 return false; | 139 return false; |
| 112 } | 140 } |
| 113 memory_map->end_address = static_cast<uint>(tmp); | 141 memory_map->end_address = static_cast<uint>(tmp); |
| 114 if (tokens->at(1).size() != strlen("rwxp")) | 142 if (tokens->at(1).size() != strlen("rwxp")) |
| 115 return false; | 143 return false; |
| 116 memory_map->flags.swap(tokens->at(1)); | 144 memory_map->flags.swap(tokens->at(1)); |
| 145 if (!base::HexStringToUInt64(tokens->at(2), &tmp)) |
| 146 return false; |
| 147 memory_map->offset = static_cast<uint>(tmp); |
| 148 memory_map->committed_pages_bits.resize( |
| 149 (memory_map->end_address - memory_map->start_address) / PAGE_SIZE); |
| 117 const int map_name_index = 5; | 150 const int map_name_index = 5; |
| 118 if (tokens->size() >= map_name_index + 1) { | 151 if (tokens->size() >= map_name_index + 1) { |
| 119 for (std::vector<std::string>::const_iterator it = | 152 for (std::vector<std::string>::const_iterator it = |
| 120 tokens->begin() + map_name_index; it != tokens->end(); ++it) { | 153 tokens->begin() + map_name_index; it != tokens->end(); ++it) { |
| 121 if (!it->empty()) { | 154 if (!it->empty()) { |
| 122 if (!memory_map->name.empty()) | 155 if (!memory_map->name.empty()) |
| 123 memory_map->name.append(" "); | 156 memory_map->name.append(" "); |
| 124 memory_map->name.append(*it); | 157 memory_map->name.append(*it); |
| 125 } | 158 } |
| 126 } | 159 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 } | 193 } |
| 161 process_maps->push_back(memory_map); | 194 process_maps->push_back(memory_map); |
| 162 } | 195 } |
| 163 return true; | 196 return true; |
| 164 } | 197 } |
| 165 | 198 |
| 166 // Fills |committed_pages| in with the set of committed pages contained in the | 199 // Fills |committed_pages| in with the set of committed pages contained in the |
| 167 // provided memory map. | 200 // provided memory map. |
| 168 bool GetPagesForMemoryMap(int pagemap_fd, | 201 bool GetPagesForMemoryMap(int pagemap_fd, |
| 169 const MemoryMap& memory_map, | 202 const MemoryMap& memory_map, |
| 170 std::vector<PageInfo>* committed_pages) { | 203 std::vector<PageInfo>* committed_pages, |
| 171 for (uint addr = memory_map.start_address; addr < memory_map.end_address; | 204 BitSet* committed_pages_bits) { |
| 172 addr += PAGE_SIZE) { | 205 for (uint addr = memory_map.start_address, page_index = 0; |
| 206 addr < memory_map.end_address; |
| 207 addr += PAGE_SIZE, ++page_index) { |
| 173 DCHECK_EQ(0, addr % PAGE_SIZE); | 208 DCHECK_EQ(0, addr % PAGE_SIZE); |
| 174 PageMapEntry page_map_entry = {}; | 209 PageMapEntry page_map_entry = {}; |
| 175 COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); | 210 COMPILE_ASSERT(sizeof(PageMapEntry) == sizeof(uint64), unexpected_size); |
| 176 const off64_t offset = addr / PAGE_SIZE; | 211 const off64_t offset = addr / PAGE_SIZE; |
| 177 if (!ReadFromFileAtOffset(pagemap_fd, offset, &page_map_entry)) | 212 if (!ReadFromFileAtOffset(pagemap_fd, offset, &page_map_entry)) |
| 178 return false; | 213 return false; |
| 179 if (page_map_entry.present) { // Ignore non-committed pages. | 214 if (page_map_entry.present) { // Ignore non-committed pages. |
| 180 if (page_map_entry.page_frame_number == 0) | 215 if (page_map_entry.page_frame_number == 0) |
| 181 continue; | 216 continue; |
| 182 PageInfo page_info = {}; | 217 PageInfo page_info = {}; |
| 183 page_info.page_frame_number = page_map_entry.page_frame_number; | 218 page_info.page_frame_number = page_map_entry.page_frame_number; |
| 184 committed_pages->push_back(page_info); | 219 committed_pages->push_back(page_info); |
| 220 committed_pages_bits->set(page_index); |
| 185 } | 221 } |
| 186 } | 222 } |
| 187 return true; | 223 return true; |
| 188 } | 224 } |
| 189 | 225 |
| 190 // Fills |committed_pages| with mapping count and flags information gathered | 226 // Fills |committed_pages| with mapping count and flags information gathered |
| 191 // looking-up /proc/kpagecount and /proc/kpageflags. | 227 // looking-up /proc/kpagecount and /proc/kpageflags. |
| 192 bool SetPagesInfo(int pagecount_fd, | 228 bool SetPagesInfo(int pagecount_fd, |
| 193 int pageflags_fd, | 229 int pageflags_fd, |
| 194 std::vector<PageInfo>* pages) { | 230 std::vector<PageInfo>* pages) { |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 base::SStringPrintf( | 416 base::SStringPrintf( |
| 381 &buf, "%d\t%d\t\t%d\t\t%d\n", | 417 &buf, "%d\t%d\t\t%d\t\t%d\n", |
| 382 process_memory.pid, | 418 process_memory.pid, |
| 383 total_private * KB_PER_PAGE, | 419 total_private * KB_PER_PAGE, |
| 384 static_cast<int>(total_app_shared) * KB_PER_PAGE, | 420 static_cast<int>(total_app_shared) * KB_PER_PAGE, |
| 385 total_other_shared * KB_PER_PAGE); | 421 total_other_shared * KB_PER_PAGE); |
| 386 std::cout << buf; | 422 std::cout << buf; |
| 387 } | 423 } |
| 388 } | 424 } |
| 389 | 425 |
| 426 void DumpProcessesMemoryMapsInExtendedFormat( |
| 427 const std::vector<ProcessMemory>& processes_memory) { |
| 428 std::string buf; |
| 429 std::string app_shared_buf; |
| 430 for (std::vector<ProcessMemory>::const_iterator it = processes_memory.begin(); |
| 431 it != processes_memory.end(); ++it) { |
| 432 const ProcessMemory& process_memory = *it; |
| 433 std::cout << "[ PID=" << process_memory.pid << "]" << '\n'; |
| 434 const std::vector<MemoryMap>& memory_maps = process_memory.memory_maps; |
| 435 for (std::vector<MemoryMap>::const_iterator it = memory_maps.begin(); |
| 436 it != memory_maps.end(); ++it) { |
| 437 const MemoryMap& memory_map = *it; |
| 438 app_shared_buf.clear(); |
| 439 AppendAppSharedField(memory_map.app_shared_counts, &app_shared_buf); |
| 440 base::SStringPrintf( |
| 441 &buf, |
| 442 "%x-%x %s %x private_unevictable=%d private=%d shared_app=%s " |
| 443 "shared_other_unevictable=%d shared_other=%d \"%s\" [%s]\n", |
| 444 memory_map.start_address, |
| 445 memory_map.end_address, |
| 446 memory_map.flags.c_str(), |
| 447 memory_map.offset, |
| 448 memory_map.unevictable_private_count * PAGE_SIZE, |
| 449 memory_map.private_count * PAGE_SIZE, |
| 450 app_shared_buf.c_str(), |
| 451 memory_map.unevictable_other_shared_count * PAGE_SIZE, |
| 452 memory_map.other_shared_count * PAGE_SIZE, |
| 453 memory_map.name.c_str(), |
| 454 memory_map.committed_pages_bits.AsB64String().c_str()); |
| 455 std::cout << buf; |
| 456 } |
| 457 } |
| 458 } |
| 459 |
| 390 bool CollectProcessMemoryInformation(int page_count_fd, | 460 bool CollectProcessMemoryInformation(int page_count_fd, |
| 391 int page_flags_fd, | 461 int page_flags_fd, |
| 392 ProcessMemory* process_memory) { | 462 ProcessMemory* process_memory) { |
| 393 const pid_t pid = process_memory->pid; | 463 const pid_t pid = process_memory->pid; |
| 394 int pagemap_fd = open( | 464 int pagemap_fd = open( |
| 395 base::StringPrintf("/proc/%d/pagemap", pid).c_str(), O_RDONLY); | 465 base::StringPrintf("/proc/%d/pagemap", pid).c_str(), O_RDONLY); |
| 396 if (pagemap_fd < 0) { | 466 if (pagemap_fd < 0) { |
| 397 PLOG(ERROR) << "open"; | 467 PLOG(ERROR) << "open"; |
| 398 return false; | 468 return false; |
| 399 } | 469 } |
| 400 file_util::ScopedFD auto_closer(&pagemap_fd); | 470 file_util::ScopedFD auto_closer(&pagemap_fd); |
| 401 std::vector<MemoryMap>* const process_maps = &process_memory->memory_maps; | 471 std::vector<MemoryMap>* const process_maps = &process_memory->memory_maps; |
| 402 if (!GetProcessMaps(pid, process_maps)) | 472 if (!GetProcessMaps(pid, process_maps)) |
| 403 return false; | 473 return false; |
| 404 for (std::vector<MemoryMap>::iterator it = process_maps->begin(); | 474 for (std::vector<MemoryMap>::iterator it = process_maps->begin(); |
| 405 it != process_maps->end(); ++it) { | 475 it != process_maps->end(); ++it) { |
| 406 std::vector<PageInfo>* const committed_pages = &it->committed_pages; | 476 std::vector<PageInfo>* const committed_pages = &it->committed_pages; |
| 407 GetPagesForMemoryMap(pagemap_fd, *it, committed_pages); | 477 BitSet* const pages_bits = &it->committed_pages_bits; |
| 478 GetPagesForMemoryMap(pagemap_fd, *it, committed_pages, pages_bits); |
| 408 SetPagesInfo(page_count_fd, page_flags_fd, committed_pages); | 479 SetPagesInfo(page_count_fd, page_flags_fd, committed_pages); |
| 409 } | 480 } |
| 410 return true; | 481 return true; |
| 411 } | 482 } |
| 412 | 483 |
| 413 void KillAll(const std::vector<pid_t>& pids, int signal_number) { | 484 void KillAll(const std::vector<pid_t>& pids, int signal_number) { |
| 414 for (std::vector<pid_t>::const_iterator it = pids.begin(); it != pids.end(); | 485 for (std::vector<pid_t>::const_iterator it = pids.begin(); it != pids.end(); |
| 415 ++it) { | 486 ++it) { |
| 416 kill(*it, signal_number); | 487 kill(*it, signal_number); |
| 417 } | 488 } |
| 418 } | 489 } |
| 419 | 490 |
| 420 } // namespace | 491 } // namespace |
| 421 | 492 |
| 422 int main(int argc, char** argv) { | 493 int main(int argc, char** argv) { |
| 423 bool short_output = false; | |
| 424 if (argc == 1) { | 494 if (argc == 1) { |
| 425 LOG(ERROR) << "Usage: " << argv[0] << " [-a] <PID1>... <PIDN>"; | 495 LOG(ERROR) << "Usage: " << argv[0] << " [-a|-x] <PID1>... <PIDN>"; |
| 426 return EXIT_FAILURE; | 496 return EXIT_FAILURE; |
| 427 } | 497 } |
| 428 if (!strncmp(argv[1], "-a", 2)) { | 498 const bool short_output = !strncmp(argv[1], "-a", 2); |
| 499 const bool extended_output = !strncmp(argv[1], "-x", 2); |
| 500 if (short_output || extended_output) { |
| 429 if (argc == 2) { | 501 if (argc == 2) { |
| 430 LOG(ERROR) << "Usage: " << argv[0] << " [-a] <PID1>... <PIDN>"; | 502 LOG(ERROR) << "Usage: " << argv[0] << " [-a|-x] <PID1>... <PIDN>"; |
| 431 return EXIT_FAILURE; | 503 return EXIT_FAILURE; |
| 432 } | 504 } |
| 433 short_output = true; | |
| 434 ++argv; | 505 ++argv; |
| 435 } | 506 } |
| 436 std::vector<pid_t> pids; | 507 std::vector<pid_t> pids; |
| 437 for (const char* const* ptr = argv + 1; *ptr; ++ptr) { | 508 for (const char* const* ptr = argv + 1; *ptr; ++ptr) { |
| 438 pid_t pid; | 509 pid_t pid; |
| 439 if (!base::StringToInt(*ptr, &pid)) | 510 if (!base::StringToInt(*ptr, &pid)) |
| 440 return EXIT_FAILURE; | 511 return EXIT_FAILURE; |
| 441 pids.push_back(pid); | 512 pids.push_back(pid); |
| 442 } | 513 } |
| 443 | 514 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 469 if (!CollectProcessMemoryInformation(page_count_fd, | 540 if (!CollectProcessMemoryInformation(page_count_fd, |
| 470 page_flags_fd, | 541 page_flags_fd, |
| 471 process_memory)) | 542 process_memory)) |
| 472 return EXIT_FAILURE; | 543 return EXIT_FAILURE; |
| 473 } | 544 } |
| 474 } | 545 } |
| 475 | 546 |
| 476 ClassifyPages(&processes_memory); | 547 ClassifyPages(&processes_memory); |
| 477 if (short_output) | 548 if (short_output) |
| 478 DumpProcessesMemoryMapsInShortFormat(processes_memory); | 549 DumpProcessesMemoryMapsInShortFormat(processes_memory); |
| 550 else if (extended_output) |
| 551 DumpProcessesMemoryMapsInExtendedFormat(processes_memory); |
| 479 else | 552 else |
| 480 DumpProcessesMemoryMaps(processes_memory); | 553 DumpProcessesMemoryMaps(processes_memory); |
| 481 return EXIT_SUCCESS; | 554 return EXIT_SUCCESS; |
| 482 } | 555 } |
| OLD | NEW |