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_dump.h" | 5 #include "base/trace_event/process_memory_dump.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/process/process_metrics.h" | 10 #include "base/process/process_metrics.h" |
| 11 #include "base/trace_event/process_memory_totals.h" | 11 #include "base/trace_event/process_memory_totals.h" |
| 12 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 #if defined(OS_POSIX) | 15 #if defined(OS_POSIX) |
| 16 #include <sys/mman.h> | 16 #include <sys/mman.h> |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 #if defined(OS_WIN) | |
| 20 #include <Psapi.h> | |
| 21 #endif | |
| 22 | |
| 19 namespace base { | 23 namespace base { |
| 20 namespace trace_event { | 24 namespace trace_event { |
| 21 | 25 |
| 22 namespace { | 26 namespace { |
| 23 | 27 |
| 24 const char kEdgeTypeOwnership[] = "ownership"; | 28 const char kEdgeTypeOwnership[] = "ownership"; |
| 25 | 29 |
| 26 std::string GetSharedGlobalAllocatorDumpName( | 30 std::string GetSharedGlobalAllocatorDumpName( |
| 27 const MemoryAllocatorDumpGuid& guid) { | 31 const MemoryAllocatorDumpGuid& guid) { |
| 28 return "global/" + guid.ToString(); | 32 return "global/" + guid.ToString(); |
| 29 } | 33 } |
| 30 | 34 |
| 31 } // namespace | 35 } // namespace |
| 32 | 36 |
| 33 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 37 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
| 34 // static | 38 // static |
| 35 size_t ProcessMemoryDump::CountResidentBytes(void* start_address, | 39 size_t ProcessMemoryDump::CountResidentBytes(void* start_address, |
| 36 size_t mapped_size) { | 40 size_t mapped_size) { |
| 37 const size_t page_size = GetPageSize(); | 41 const size_t page_size = GetPageSize(); |
| 38 const uintptr_t start_pointer = reinterpret_cast<uintptr_t>(start_address); | 42 const uintptr_t start_pointer = reinterpret_cast<uintptr_t>(start_address); |
| 39 DCHECK_EQ(0u, start_pointer % page_size); | 43 DCHECK_EQ(0u, start_pointer % page_size); |
| 40 | 44 |
| 41 // This function allocates a char vector of size number of pages in the given | 45 // This function allocates a char vector of size number of pages in the given |
| 42 // mapped_size. To avoid allocating a large array, the memory is split into | 46 // mapped_size. To avoid allocating a large array, the memory is split into |
| 43 // chunks. Maximum size of vector allocated, will be | 47 // chunks. Maximum size of vector allocated, will be |
| 44 // kPageChunkSize / page_size. | 48 // kPageChunkSize / page_size. |
| 45 const size_t kMaxChunkSize = 32 * 1024 * 1024; | 49 const size_t kMaxChunkSize = 32 * 1024 * 1024; |
| 46 size_t offset = 0; | 50 size_t offset = 0; |
| 47 size_t total_resident_size = 0; | 51 size_t total_resident_size = 0; |
| 48 int result = 0; | 52 bool failure = false; |
| 49 while (offset < mapped_size) { | 53 while (offset < mapped_size) { |
| 50 void* chunk_start = reinterpret_cast<void*>(start_pointer + offset); | 54 uintptr_t chunk_start = (start_pointer + offset); |
| 51 const size_t chunk_size = std::min(mapped_size - offset, kMaxChunkSize); | 55 const size_t chunk_size = std::min(mapped_size - offset, kMaxChunkSize); |
| 52 const size_t page_count = (chunk_size + page_size - 1) / page_size; | 56 const size_t page_count = (chunk_size + page_size - 1) / page_size; |
| 53 size_t resident_page_count = 0; | 57 size_t resident_page_count = 0; |
| 54 | 58 |
| 55 #if defined(OS_MACOSX) || defined(OS_IOS) | 59 #if defined(OS_MACOSX) || defined(OS_IOS) |
| 56 std::vector<char> vec(page_count + 1); | 60 std::vector<char> vec(page_count); |
| 57 // mincore in MAC does not fail with EAGAIN. | 61 // mincore in MAC does not fail with EAGAIN. |
| 58 result = mincore(chunk_start, chunk_size, vec.data()); | 62 failure = |
| 59 if (result) | 63 !!mincore(reinterpret_cast<void*>(chunk_start), chunk_size, vec.data()); |
| 60 break; | 64 for (size_t i = 0; i < page_count; i++) |
| 65 resident_page_count += vec[i] & MINCORE_INCORE ? 1 : 0; | |
| 66 #elif defined(OS_WIN) | |
| 67 std::vector<PSAPI_WORKING_SET_EX_INFORMATION> vec(page_count); | |
| 68 for (size_t i = 0; i < page_count; i++) { | |
| 69 vec[i].VirtualAddress = | |
| 70 reinterpret_cast<void*>(chunk_start + i * page_size); | |
| 71 } | |
| 72 DWORD vec_size = static_cast<DWORD>( | |
| 73 page_count * sizeof(PSAPI_WORKING_SET_EX_INFORMATION)); | |
| 74 failure = !QueryWorkingSetEx(GetCurrentProcess(), vec.data(), vec_size); | |
| 61 | 75 |
| 62 for (size_t i = 0; i < page_count; i++) | 76 for (size_t i = 0; i < page_count; i++) |
| 63 resident_page_count += vec[i] & MINCORE_INCORE ? 1 : 0; | 77 resident_page_count += vec[i].VirtualAttributes.Valid; |
| 64 #else // defined(OS_MACOSX) || defined(OS_IOS) | 78 #elif defined(OS_POSIX) |
| 65 std::vector<unsigned char> vec(page_count + 1); | 79 std::vector<unsigned char> vec(page_count); |
| 66 int error_counter = 0; | 80 int error_counter = 0; |
| 81 int result = 0; | |
| 67 // HANDLE_EINTR tries for 100 times. So following the same pattern. | 82 // HANDLE_EINTR tries for 100 times. So following the same pattern. |
| 68 do { | 83 do { |
| 69 result = mincore(chunk_start, chunk_size, vec.data()); | 84 result = !!mincore(reinterpret_cast<void*>(chunk_start), chunk_size, |
|
brucedawson
2016/01/20 20:48:04
Oops! You can't put the !! here. It has to go on l
ssid
2016/01/20 20:50:51
Sorry, Fixed.
| |
| 85 vec.data()); | |
| 70 } while (result == -1 && errno == EAGAIN && error_counter++ < 100); | 86 } while (result == -1 && errno == EAGAIN && error_counter++ < 100); |
| 71 if (result) | 87 failure = result; |
| 72 break; | |
| 73 | 88 |
| 74 for (size_t i = 0; i < page_count; i++) | 89 for (size_t i = 0; i < page_count; i++) |
| 75 resident_page_count += vec[i]; | 90 resident_page_count += vec[i]; |
| 76 #endif // defined(OS_MACOSX) || defined(OS_IOS) | 91 #endif |
| 92 | |
| 93 if (failure) | |
| 94 break; | |
| 77 | 95 |
| 78 total_resident_size += resident_page_count * page_size; | 96 total_resident_size += resident_page_count * page_size; |
| 79 offset += kMaxChunkSize; | 97 offset += kMaxChunkSize; |
| 80 } | 98 } |
| 81 | 99 |
| 82 DCHECK_EQ(0, result); | 100 DCHECK(!failure); |
| 83 if (result) { | 101 if (failure) { |
| 84 total_resident_size = 0; | 102 total_resident_size = 0; |
| 85 LOG(ERROR) << "mincore() call failed. The resident size is invalid"; | 103 LOG(ERROR) << "CountResidentBytes failed. The resident size is invalid"; |
| 86 } | 104 } |
| 87 return total_resident_size; | 105 return total_resident_size; |
| 88 } | 106 } |
| 89 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 107 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
| 90 | 108 |
| 91 ProcessMemoryDump::ProcessMemoryDump( | 109 ProcessMemoryDump::ProcessMemoryDump( |
| 92 const scoped_refptr<MemoryDumpSessionState>& session_state) | 110 const scoped_refptr<MemoryDumpSessionState>& session_state) |
| 93 : has_process_totals_(false), | 111 : has_process_totals_(false), |
| 94 has_process_mmaps_(false), | 112 has_process_mmaps_(false), |
| 95 session_state_(session_state) { | 113 session_state_(session_state) { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 | 265 |
| 248 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, | 266 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, |
| 249 const std::string& target_node_name) { | 267 const std::string& target_node_name) { |
| 250 std::string child_mad_name = target_node_name + "/__" + source.ToString(); | 268 std::string child_mad_name = target_node_name + "/__" + source.ToString(); |
| 251 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); | 269 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); |
| 252 AddOwnershipEdge(source, target_child_mad->guid()); | 270 AddOwnershipEdge(source, target_child_mad->guid()); |
| 253 } | 271 } |
| 254 | 272 |
| 255 } // namespace trace_event | 273 } // namespace trace_event |
| 256 } // namespace base | 274 } // namespace base |
| OLD | NEW |