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