Index: base/trace_event/process_memory_dump.cc |
diff --git a/base/trace_event/process_memory_dump.cc b/base/trace_event/process_memory_dump.cc |
index 67118f10e8c0604d6586b0c54e29497ef08c2242..6541d73bdeac436dc3c82d1a5dfb2748db4c6bc9 100644 |
--- a/base/trace_event/process_memory_dump.cc |
+++ b/base/trace_event/process_memory_dump.cc |
@@ -7,6 +7,10 @@ |
#include "base/trace_event/process_memory_totals.h" |
#include "base/trace_event/trace_event_argument.h" |
+#if defined(OS_POSIX) |
+#include <sys/mman.h> |
+#endif |
+ |
namespace base { |
namespace trace_event { |
@@ -19,6 +23,49 @@ std::string GetSharedGlobalAllocatorDumpName( |
} |
} // namespace |
+#if defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
+// static |
+size_t ProcessMemoryDump::CountResidentBytes(void* start_address, |
+ size_t mapped_size) { |
+ const size_t page_size = base::GetPageSize(); |
+ const uintptr_t start_pointer = reinterpret_cast<uintptr_t>(start_address); |
+ DCHECK_EQ(0u, start_pointer % page_size); |
+ |
+ // This function allocates a char vector of size number of pages in the given |
+ // mapped_size. To avoid allocating a large array, the memory is split into |
+ // chunks. Maximum size of vector allocated, will be |
+ // kPageChunkSize / page_size. |
+ const size_t kMaxChunkSize = 32 * 1024 * 1024; |
+ size_t offset = 0; |
+ size_t total_resident_size = 0; |
+ while (offset < mapped_size) { |
+ void* chunk_start = reinterpret_cast<void*>(start_pointer + offset); |
+ const size_t chunk_size = std::min(mapped_size - offset, kMaxChunkSize); |
+ const size_t page_count = (chunk_size + page_size - 1) / page_size; |
+ size_t resident_page_count = 0; |
+ |
+#if defined(OS_MACOSX) || defined(OS_IOS) |
+ scoped_ptr<char[]> vec(new char[page_count + 1]); |
+ int res = mincore(chunk_start, chunk_size, &vec.get()[0]); |
+ DCHECK(!res); |
+ for (size_t i = 0; i < page_count; i++) |
+ resident_page_count += vec[i] & MINCORE_INCORE ? 1 : 0; |
+ |
+#else // defined(OS_MACOSX) || defined(OS_IOS) |
+ scoped_ptr<unsigned char[]> vec(new unsigned char[page_count + 1]); |
+ int res = mincore(chunk_start, chunk_size, &vec.get()[0]); |
+ DCHECK(!res); |
+ for (size_t i = 0; i < page_count; i++) |
+ resident_page_count += vec[i]; |
+#endif // defined(OS_MACOSX) || defined(OS_IOS) |
+ |
+ total_resident_size += resident_page_count * page_size; |
+ offset += kMaxChunkSize; |
+ } |
+ return total_resident_size; |
+} |
+#endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
+ |
ProcessMemoryDump::ProcessMemoryDump( |
const scoped_refptr<MemoryDumpSessionState>& session_state) |
: has_process_totals_(false), |