| 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/trace_event_memory_overhead.h" | 5 #include "base/trace_event/trace_event_memory_overhead.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bits.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/trace_event/memory_allocator_dump.h" | 12 #include "base/trace_event/memory_allocator_dump.h" |
| 12 #include "base/trace_event/process_memory_dump.h" | 13 #include "base/trace_event/process_memory_dump.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 | 15 |
| 15 namespace { | |
| 16 size_t RoundUp(size_t size, size_t alignment) { | |
| 17 return (size + alignment - 1) & ~(alignment - 1); | |
| 18 } | |
| 19 } // namespace | |
| 20 | |
| 21 namespace base { | 16 namespace base { |
| 22 namespace trace_event { | 17 namespace trace_event { |
| 23 | 18 |
| 24 TraceEventMemoryOverhead::TraceEventMemoryOverhead() { | 19 TraceEventMemoryOverhead::TraceEventMemoryOverhead() { |
| 25 } | 20 } |
| 26 | 21 |
| 27 TraceEventMemoryOverhead::~TraceEventMemoryOverhead() { | 22 TraceEventMemoryOverhead::~TraceEventMemoryOverhead() { |
| 28 } | 23 } |
| 29 | 24 |
| 30 void TraceEventMemoryOverhead::AddOrCreateInternal( | 25 void TraceEventMemoryOverhead::AddOrCreateInternal( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 54 size_t allocated_size_in_bytes, | 49 size_t allocated_size_in_bytes, |
| 55 size_t resident_size_in_bytes) { | 50 size_t resident_size_in_bytes) { |
| 56 AddOrCreateInternal(object_type, 1, allocated_size_in_bytes, | 51 AddOrCreateInternal(object_type, 1, allocated_size_in_bytes, |
| 57 resident_size_in_bytes); | 52 resident_size_in_bytes); |
| 58 } | 53 } |
| 59 | 54 |
| 60 void TraceEventMemoryOverhead::AddString(const std::string& str) { | 55 void TraceEventMemoryOverhead::AddString(const std::string& str) { |
| 61 // The number below are empirical and mainly based on profiling of real-world | 56 // The number below are empirical and mainly based on profiling of real-world |
| 62 // std::string implementations: | 57 // std::string implementations: |
| 63 // - even short string end up malloc()-inc at least 32 bytes. | 58 // - even short string end up malloc()-inc at least 32 bytes. |
| 64 // - longer stings seem to malloc() multiples of 16 bytes. | 59 // - longer strings seem to malloc() multiples of 16 bytes. |
| 65 Add("std::string", | 60 const size_t capacity = bits::Align(str.capacity(), 16); |
| 66 sizeof(std::string) + std::max<size_t>(RoundUp(str.capacity(), 16), 32u)); | 61 Add("std::string", sizeof(std::string) + std::max<size_t>(capacity, 32u)); |
| 67 } | 62 } |
| 68 | 63 |
| 69 void TraceEventMemoryOverhead::AddRefCountedString( | 64 void TraceEventMemoryOverhead::AddRefCountedString( |
| 70 const RefCountedString& str) { | 65 const RefCountedString& str) { |
| 71 Add("RefCountedString", sizeof(RefCountedString)); | 66 Add("RefCountedString", sizeof(RefCountedString)); |
| 72 AddString(str.data()); | 67 AddString(str.data()); |
| 73 } | 68 } |
| 74 | 69 |
| 75 void TraceEventMemoryOverhead::AddValue(const Value& value) { | 70 void TraceEventMemoryOverhead::AddValue(const Value& value) { |
| 76 switch (value.GetType()) { | 71 switch (value.GetType()) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 it.second.allocated_size_in_bytes); | 147 it.second.allocated_size_in_bytes); |
| 153 mad->AddScalar("resident_size", MemoryAllocatorDump::kUnitsBytes, | 148 mad->AddScalar("resident_size", MemoryAllocatorDump::kUnitsBytes, |
| 154 it.second.resident_size_in_bytes); | 149 it.second.resident_size_in_bytes); |
| 155 mad->AddScalar(MemoryAllocatorDump::kNameObjectsCount, | 150 mad->AddScalar(MemoryAllocatorDump::kNameObjectsCount, |
| 156 MemoryAllocatorDump::kUnitsObjects, it.second.count); | 151 MemoryAllocatorDump::kUnitsObjects, it.second.count); |
| 157 } | 152 } |
| 158 } | 153 } |
| 159 | 154 |
| 160 } // namespace trace_event | 155 } // namespace trace_event |
| 161 } // namespace base | 156 } // namespace base |
| OLD | NEW |