primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/trace_event/trace_event_memory_overhead.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
primiano | 47c6906 | 2015-07-25 05:13:32 +0900 | [diff] [blame] | 9 | #include "base/bits.h" |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 10 | #include "base/memory/ref_counted_memory.h" |
| 11 | #include "base/strings/stringprintf.h" |
| 12 | #include "base/trace_event/memory_allocator_dump.h" |
| 13 | #include "base/trace_event/process_memory_dump.h" |
| 14 | #include "base/values.h" |
| 15 | |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 16 | namespace base { |
| 17 | namespace trace_event { |
| 18 | |
| 19 | TraceEventMemoryOverhead::TraceEventMemoryOverhead() { |
| 20 | } |
| 21 | |
| 22 | TraceEventMemoryOverhead::~TraceEventMemoryOverhead() { |
| 23 | } |
| 24 | |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 25 | void TraceEventMemoryOverhead::AddOrCreateInternal( |
| 26 | const char* object_type, |
| 27 | size_t count, |
| 28 | size_t allocated_size_in_bytes, |
| 29 | size_t resident_size_in_bytes) { |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 30 | auto it = allocated_objects_.find(object_type); |
| 31 | if (it == allocated_objects_.end()) { |
| 32 | allocated_objects_.insert(std::make_pair( |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 33 | object_type, |
| 34 | ObjectCountAndSize( |
| 35 | {count, allocated_size_in_bytes, resident_size_in_bytes}))); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 36 | return; |
| 37 | } |
| 38 | it->second.count += count; |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 39 | it->second.allocated_size_in_bytes += allocated_size_in_bytes; |
| 40 | it->second.resident_size_in_bytes += resident_size_in_bytes; |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void TraceEventMemoryOverhead::Add(const char* object_type, |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 44 | size_t allocated_size_in_bytes) { |
| 45 | Add(object_type, allocated_size_in_bytes, allocated_size_in_bytes); |
| 46 | } |
| 47 | |
| 48 | void TraceEventMemoryOverhead::Add(const char* object_type, |
| 49 | size_t allocated_size_in_bytes, |
| 50 | size_t resident_size_in_bytes) { |
| 51 | AddOrCreateInternal(object_type, 1, allocated_size_in_bytes, |
| 52 | resident_size_in_bytes); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void TraceEventMemoryOverhead::AddString(const std::string& str) { |
| 56 | // The number below are empirical and mainly based on profiling of real-world |
| 57 | // std::string implementations: |
| 58 | // - even short string end up malloc()-inc at least 32 bytes. |
primiano | 47c6906 | 2015-07-25 05:13:32 +0900 | [diff] [blame] | 59 | // - longer strings seem to malloc() multiples of 16 bytes. |
| 60 | const size_t capacity = bits::Align(str.capacity(), 16); |
| 61 | Add("std::string", sizeof(std::string) + std::max<size_t>(capacity, 32u)); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void TraceEventMemoryOverhead::AddRefCountedString( |
| 65 | const RefCountedString& str) { |
| 66 | Add("RefCountedString", sizeof(RefCountedString)); |
| 67 | AddString(str.data()); |
| 68 | } |
| 69 | |
| 70 | void TraceEventMemoryOverhead::AddValue(const Value& value) { |
| 71 | switch (value.GetType()) { |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 72 | case Value::Type::NONE: |
| 73 | case Value::Type::BOOLEAN: |
| 74 | case Value::Type::INTEGER: |
| 75 | case Value::Type::DOUBLE: |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 76 | Add("FundamentalValue", sizeof(Value)); |
| 77 | break; |
| 78 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 79 | case Value::Type::STRING: { |
jdoerrie | 0d1295b | 2017-03-06 20:12:04 +0900 | [diff] [blame] | 80 | const Value* string_value = nullptr; |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 81 | value.GetAsString(&string_value); |
jdoerrie | 0d1295b | 2017-03-06 20:12:04 +0900 | [diff] [blame] | 82 | Add("StringValue", sizeof(Value)); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 83 | AddString(string_value->GetString()); |
| 84 | } break; |
| 85 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 86 | case Value::Type::BINARY: { |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 87 | const Value* binary_value = nullptr; |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 88 | value.GetAsBinary(&binary_value); |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 89 | Add("BinaryValue", sizeof(Value) + binary_value->GetSize()); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 90 | } break; |
| 91 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 92 | case Value::Type::DICTIONARY: { |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 93 | const DictionaryValue* dictionary_value = nullptr; |
| 94 | value.GetAsDictionary(&dictionary_value); |
| 95 | Add("DictionaryValue", sizeof(DictionaryValue)); |
| 96 | for (DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd(); |
| 97 | it.Advance()) { |
| 98 | AddString(it.key()); |
| 99 | AddValue(it.value()); |
| 100 | } |
| 101 | } break; |
| 102 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 103 | case Value::Type::LIST: { |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 104 | const ListValue* list_value = nullptr; |
| 105 | value.GetAsList(&list_value); |
| 106 | Add("ListValue", sizeof(ListValue)); |
dcheng | 1fa44fb | 2016-05-26 03:30:47 +0900 | [diff] [blame] | 107 | for (const auto& v : *list_value) |
jdoerrie | 0143ef0 | 2017-04-11 23:20:20 +0900 | [diff] [blame^] | 108 | AddValue(v); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 109 | } break; |
| 110 | |
| 111 | default: |
| 112 | NOTREACHED(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void TraceEventMemoryOverhead::AddSelf() { |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 117 | size_t estimated_size = sizeof(*this); |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 118 | // If the SmallMap did overflow its static capacity, its elements will be |
| 119 | // allocated on the heap and have to be accounted separately. |
| 120 | if (allocated_objects_.UsingFullMap()) |
| 121 | estimated_size += sizeof(map_type::value_type) * allocated_objects_.size(); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 122 | Add("TraceEventMemoryOverhead", estimated_size); |
| 123 | } |
| 124 | |
oysteine | d49670a | 2015-08-07 03:41:47 +0900 | [diff] [blame] | 125 | size_t TraceEventMemoryOverhead::GetCount(const char* object_type) const { |
| 126 | const auto& it = allocated_objects_.find(object_type); |
| 127 | if (it == allocated_objects_.end()) |
| 128 | return 0u; |
| 129 | return it->second.count; |
| 130 | } |
| 131 | |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 132 | void TraceEventMemoryOverhead::Update(const TraceEventMemoryOverhead& other) { |
| 133 | for (const auto& it : other.allocated_objects_) { |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 134 | AddOrCreateInternal(it.first, it.second.count, |
| 135 | it.second.allocated_size_in_bytes, |
| 136 | it.second.resident_size_in_bytes); |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | void TraceEventMemoryOverhead::DumpInto(const char* base_name, |
| 141 | ProcessMemoryDump* pmd) const { |
| 142 | for (const auto& it : allocated_objects_) { |
| 143 | std::string dump_name = StringPrintf("%s/%s", base_name, it.first); |
| 144 | MemoryAllocatorDump* mad = pmd->CreateAllocatorDump(dump_name); |
| 145 | mad->AddScalar(MemoryAllocatorDump::kNameSize, |
primiano | fb2bebf | 2015-06-13 05:38:20 +0900 | [diff] [blame] | 146 | MemoryAllocatorDump::kUnitsBytes, |
| 147 | it.second.allocated_size_in_bytes); |
| 148 | mad->AddScalar("resident_size", MemoryAllocatorDump::kUnitsBytes, |
| 149 | it.second.resident_size_in_bytes); |
bratell | 38bb9ff | 2015-09-11 01:28:43 +0900 | [diff] [blame] | 150 | mad->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
primiano | f599925 | 2015-06-11 09:43:15 +0900 | [diff] [blame] | 151 | MemoryAllocatorDump::kUnitsObjects, it.second.count); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } // namespace trace_event |
| 156 | } // namespace base |