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 int result = 0; // 0 on success. |
brucedawson
2016/01/19 20:16:43
Reusing "int result" as a success/error code acros
ssid
2016/01/20 13:03:59
Done.
| |
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 result = |
63 mincore(reinterpret_cast<void*>(chunk_start), chunk_size, vec.data()); | |
59 if (result) | 64 if (result) |
60 break; | 65 break; |
61 | 66 |
62 for (size_t i = 0; i < page_count; i++) | 67 for (size_t i = 0; i < page_count; i++) |
63 resident_page_count += vec[i] & MINCORE_INCORE ? 1 : 0; | 68 resident_page_count += vec[i] & MINCORE_INCORE ? 1 : 0; |
69 #elif defined(OS_WIN) | |
70 std::vector<PSAPI_WORKING_SET_EX_INFORMATION> vec(page_count); | |
brucedawson
2016/01/19 20:16:43
Possibly out of scope for this change, but the std
ssid
2016/01/20 13:03:59
I will add a follow up to this CL.
| |
71 for (size_t i = 0; i < page_count; i++) { | |
72 vec[i].VirtualAddress = | |
73 reinterpret_cast<void*>(chunk_start + i * page_size); | |
74 } | |
75 DWORD vec_size = static_cast<DWORD>( | |
76 page_count * sizeof(PSAPI_WORKING_SET_EX_INFORMATION)); | |
77 result = !QueryWorkingSetEx(GetCurrentProcess(), vec.data(), vec_size); | |
78 if (result) | |
79 break; | |
80 for (size_t i = 0; i < page_count; i++) | |
81 resident_page_count += vec[i].VirtualAttributes.Valid; | |
64 #else // defined(OS_MACOSX) || defined(OS_IOS) | 82 #else // defined(OS_MACOSX) || defined(OS_IOS) |
brucedawson
2016/01/19 20:16:43
This comment is no longer meaningful. Consider cha
ssid
2016/01/20 13:03:59
Done.
| |
65 std::vector<unsigned char> vec(page_count + 1); | 83 std::vector<unsigned char> vec(page_count); |
66 int error_counter = 0; | 84 int error_counter = 0; |
67 // HANDLE_EINTR tries for 100 times. So following the same pattern. | 85 // HANDLE_EINTR tries for 100 times. So following the same pattern. |
68 do { | 86 do { |
69 result = mincore(chunk_start, chunk_size, vec.data()); | 87 result = |
88 mincore(reinterpret_cast<void*>(chunk_start), chunk_size, vec.data()); | |
70 } while (result == -1 && errno == EAGAIN && error_counter++ < 100); | 89 } while (result == -1 && errno == EAGAIN && error_counter++ < 100); |
71 if (result) | 90 if (result) |
72 break; | 91 break; |
73 | 92 |
74 for (size_t i = 0; i < page_count; i++) | 93 for (size_t i = 0; i < page_count; i++) |
75 resident_page_count += vec[i]; | 94 resident_page_count += vec[i]; |
76 #endif // defined(OS_MACOSX) || defined(OS_IOS) | 95 #endif // defined(OS_MACOSX) || defined(OS_IOS) |
77 | 96 |
78 total_resident_size += resident_page_count * page_size; | 97 total_resident_size += resident_page_count * page_size; |
79 offset += kMaxChunkSize; | 98 offset += kMaxChunkSize; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 | 266 |
248 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, | 267 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, |
249 const std::string& target_node_name) { | 268 const std::string& target_node_name) { |
250 std::string child_mad_name = target_node_name + "/__" + source.ToString(); | 269 std::string child_mad_name = target_node_name + "/__" + source.ToString(); |
251 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); | 270 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); |
252 AddOwnershipEdge(source, target_child_mad->guid()); | 271 AddOwnershipEdge(source, target_child_mad->guid()); |
253 } | 272 } |
254 | 273 |
255 } // namespace trace_event | 274 } // namespace trace_event |
256 } // namespace base | 275 } // namespace base |
OLD | NEW |