forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_event_memory_overhead.cc
179 lines (154 loc) · 6.17 KB
/
trace_event_memory_overhead.cc
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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/memory_usage_estimator.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/values.h"
namespace base {
namespace trace_event {
namespace {
const char* ObjectTypeToString(TraceEventMemoryOverhead::ObjectType type) {
switch (type) {
case TraceEventMemoryOverhead::kOther:
return "(Other)";
case TraceEventMemoryOverhead::kTraceBuffer:
return "TraceBuffer";
case TraceEventMemoryOverhead::kTraceBufferChunk:
return "TraceBufferChunk";
case TraceEventMemoryOverhead::kTraceEvent:
return "TraceEvent";
case TraceEventMemoryOverhead::kUnusedTraceEvent:
return "TraceEvent(Unused)";
case TraceEventMemoryOverhead::kTracedValue:
return "TracedValue";
case TraceEventMemoryOverhead::kConvertableToTraceFormat:
return "ConvertableToTraceFormat";
case TraceEventMemoryOverhead::kHeapProfilerAllocationRegister:
return "AllocationRegister";
case TraceEventMemoryOverhead::kHeapProfilerTypeNameDeduplicator:
return "TypeNameDeduplicator";
case TraceEventMemoryOverhead::kHeapProfilerStackFrameDeduplicator:
return "StackFrameDeduplicator";
case TraceEventMemoryOverhead::kStdString:
return "std::string";
case TraceEventMemoryOverhead::kBaseValue:
return "base::Value";
case TraceEventMemoryOverhead::kTraceEventMemoryOverhead:
return "TraceEventMemoryOverhead";
case TraceEventMemoryOverhead::kFrameMetrics:
return "FrameMetrics";
case TraceEventMemoryOverhead::kLast:
NOTREACHED();
}
NOTREACHED();
return "BUG";
}
} // namespace
TraceEventMemoryOverhead::TraceEventMemoryOverhead() : allocated_objects_() {}
TraceEventMemoryOverhead::~TraceEventMemoryOverhead() = default;
void TraceEventMemoryOverhead::AddInternal(ObjectType object_type,
size_t count,
size_t allocated_size_in_bytes,
size_t resident_size_in_bytes) {
ObjectCountAndSize& count_and_size =
allocated_objects_[static_cast<uint32_t>(object_type)];
count_and_size.count += count;
count_and_size.allocated_size_in_bytes += allocated_size_in_bytes;
count_and_size.resident_size_in_bytes += resident_size_in_bytes;
}
void TraceEventMemoryOverhead::Add(ObjectType object_type,
size_t allocated_size_in_bytes) {
Add(object_type, allocated_size_in_bytes, allocated_size_in_bytes);
}
void TraceEventMemoryOverhead::Add(ObjectType object_type,
size_t allocated_size_in_bytes,
size_t resident_size_in_bytes) {
AddInternal(object_type, 1, allocated_size_in_bytes, resident_size_in_bytes);
}
void TraceEventMemoryOverhead::AddString(const std::string& str) {
Add(kStdString, EstimateMemoryUsage(str));
}
void TraceEventMemoryOverhead::AddRefCountedString(
const RefCountedString& str) {
Add(kOther, sizeof(RefCountedString));
AddString(str.data());
}
void TraceEventMemoryOverhead::AddValue(const Value& value) {
switch (value.type()) {
case Value::Type::NONE:
case Value::Type::BOOLEAN:
case Value::Type::INTEGER:
case Value::Type::DOUBLE:
Add(kBaseValue, sizeof(Value));
break;
case Value::Type::STRING: {
const Value* string_value = nullptr;
value.GetAsString(&string_value);
Add(kBaseValue, sizeof(Value));
AddString(string_value->GetString());
} break;
case Value::Type::BINARY: {
Add(kBaseValue, sizeof(Value) + value.GetBlob().size());
} break;
case Value::Type::DICTIONARY: {
const DictionaryValue* dictionary_value = nullptr;
value.GetAsDictionary(&dictionary_value);
Add(kBaseValue, 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(kBaseValue, sizeof(ListValue));
for (const auto& v : *list_value)
AddValue(v);
} break;
default:
NOTREACHED();
}
}
void TraceEventMemoryOverhead::AddSelf() {
Add(kTraceEventMemoryOverhead, sizeof(*this));
}
size_t TraceEventMemoryOverhead::GetCount(ObjectType object_type) const {
CHECK(object_type < kLast);
return allocated_objects_[static_cast<uint32_t>(object_type)].count;
}
void TraceEventMemoryOverhead::Update(const TraceEventMemoryOverhead& other) {
for (uint32_t i = 0; i < kLast; i++) {
const ObjectCountAndSize& other_entry = other.allocated_objects_[i];
AddInternal(static_cast<ObjectType>(i), other_entry.count,
other_entry.allocated_size_in_bytes,
other_entry.resident_size_in_bytes);
}
}
void TraceEventMemoryOverhead::DumpInto(const char* base_name,
ProcessMemoryDump* pmd) const {
for (uint32_t i = 0; i < kLast; i++) {
const ObjectCountAndSize& count_and_size = allocated_objects_[i];
if (count_and_size.allocated_size_in_bytes == 0)
continue;
std::string dump_name = StringPrintf(
"%s/%s", base_name, ObjectTypeToString(static_cast<ObjectType>(i)));
MemoryAllocatorDump* mad = pmd->CreateAllocatorDump(dump_name);
mad->AddScalar(MemoryAllocatorDump::kNameSize,
MemoryAllocatorDump::kUnitsBytes,
count_and_size.allocated_size_in_bytes);
mad->AddScalar("resident_size", MemoryAllocatorDump::kUnitsBytes,
count_and_size.resident_size_in_bytes);
mad->AddScalar(MemoryAllocatorDump::kNameObjectCount,
MemoryAllocatorDump::kUnitsObjects, count_and_size.count);
}
}
} // namespace trace_event
} // namespace base