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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
base / trace_event / memory_usage_estimator_unittest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/memory_usage_estimator.h"
#include <stdlib.h>
#include <string>
#include "base/memory/ptr_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(ARCH_CPU_64_BITS)
#define EXPECT_EQ_32_64(_, e, a) EXPECT_EQ(e, a)
#else
#define EXPECT_EQ_32_64(e, _, a) EXPECT_EQ(e, a)
#endif
namespace base {
namespace trace_event {
namespace {
// Test class with predictable memory usage.
class Data {
public:
explicit Data(size_t size = 17): size_(size) {
}
size_t size() const { return size_; }
size_t EstimateMemoryUsage() const {
return size_;
}
bool operator < (const Data& other) const {
return size_ < other.size_;
}
bool operator == (const Data& other) const {
return size_ == other.size_;
}
struct Hasher {
size_t operator () (const Data& data) const {
return data.size();
}
};
private:
size_t size_;
};
} // namespace
namespace internal {
// This kills variance of bucket_count across STL implementations.
template <>
size_t HashMapBucketCountForTesting<Data>(size_t) {
return 10;
}
template <>
size_t HashMapBucketCountForTesting<std::pair<const Data, short>>(size_t) {
return 10;
}
} // namespace internal
TEST(EstimateMemoryUsageTest, String) {
std::string string(777, 'a');
EXPECT_EQ(string.capacity() + 1, EstimateMemoryUsage(string));
}
TEST(EstimateMemoryUsageTest, String16) {
std::u16string string(777, 'a');
EXPECT_EQ(sizeof(char16_t) * (string.capacity() + 1),
EstimateMemoryUsage(string));
}
TEST(EstimateMemoryUsageTest, Arrays) {
// std::array
{
std::array<Data, 10> array;
EXPECT_EQ(170u, EstimateMemoryUsage(array));
}
// T[N]
{
Data array[10];
EXPECT_EQ(170u, EstimateMemoryUsage(array));
}
// HeapArray
{
struct Item {
char payload[10];
};
auto array = base::HeapArray<Item>::WithSize(7u);
EXPECT_EQ(70u, EstimateMemoryUsage(array));
}
// Owning span
{
struct Item {
char payload[10];
};
auto array = base::HeapArray<Item>::WithSize(7u);
EXPECT_EQ(70u, EstimateMemoryUsage(array.as_span()));
}
}
TEST(EstimateMemoryUsageTest, UniquePtr) {
// Empty
{
std::unique_ptr<Data> ptr;
EXPECT_EQ(0u, EstimateMemoryUsage(ptr));
}
// Not empty
{
std::unique_ptr<Data> ptr(new Data());
EXPECT_EQ_32_64(21u, 25u, EstimateMemoryUsage(ptr));
}
// With a pointer
{
std::unique_ptr<Data*> ptr(new Data*());
EXPECT_EQ(sizeof(void*), EstimateMemoryUsage(ptr));
}
}
TEST(EstimateMemoryUsageTest, Vector) {
std::vector<Data> vector;
vector.reserve(1000);
// For an empty vector we should return memory usage of its buffer
size_t capacity = vector.capacity();
size_t expected_size = capacity * sizeof(Data);
EXPECT_EQ(expected_size, EstimateMemoryUsage(vector));
// If vector is not empty, its size should also include memory usages
// of all elements.
for (size_t i = 0; i != capacity / 2; ++i) {
vector.push_back(Data(i));
expected_size += EstimateMemoryUsage(vector.back());
}
EXPECT_EQ(expected_size, EstimateMemoryUsage(vector));
}
TEST(EstimateMemoryUsageTest, Vector_of_Pointers) {
{
std::unique_ptr<Data> u_ptr = std::make_unique<Data>(11);
std::vector<Data*> vector;
vector.reserve(1000);
vector.push_back(u_ptr.get());
size_t capacity = vector.capacity();
size_t expected_size = capacity * sizeof(Data*);
EXPECT_EQ(expected_size, EstimateMemoryUsage(vector));
}
{
std::unique_ptr<Data> u_ptr = std::make_unique<Data>(11);
std::vector<raw_ptr<Data>> vector;
vector.reserve(1000);
vector.push_back(u_ptr.get());
size_t capacity = vector.capacity();
size_t expected_size = capacity * sizeof(raw_ptr<Data>);
EXPECT_EQ(expected_size, EstimateMemoryUsage(vector));
}
}
TEST(EstimateMemoryUsageTest, List) {
struct POD {
short data;
};
std::list<POD> list;
for (int i = 0; i != 1000; ++i) {
list.push_back(POD());
}
EXPECT_EQ_32_64(12000u, 24000u, EstimateMemoryUsage(list));
}
TEST(EstimateMemoryUsageTest, Set) {
std::set<std::pair<int, Data>> set;
for (int i = 0; i != 1000; ++i) {
set.insert({i, Data(i)});
}
EXPECT_EQ_32_64(523500u, 547500u, EstimateMemoryUsage(set));
}
TEST(EstimateMemoryUsageTest, MultiSet) {
std::multiset<bool> set;
for (int i = 0; i != 1000; ++i) {
set.insert((i & 1) != 0);
}
EXPECT_EQ_32_64(16000u, 32000u, EstimateMemoryUsage(set));
}
TEST(EstimateMemoryUsageTest, Map) {
std::map<Data, int> map;
for (int i = 0; i != 1000; ++i) {
map.insert({Data(i), i});
}
EXPECT_EQ_32_64(523500u, 547500u, EstimateMemoryUsage(map));
}
TEST(EstimateMemoryUsageTest, MultiMap) {
std::multimap<char, Data> map;
for (int i = 0; i != 1000; ++i) {
map.insert({static_cast<char>(i), Data(i)});
}
EXPECT_EQ_32_64(523500u, 547500u, EstimateMemoryUsage(map));
}
TEST(EstimateMemoryUsageTest, UnorderedSet) {
std::unordered_set<Data, Data::Hasher> set;
for (int i = 0; i != 1000; ++i) {
set.insert(Data(i));
}
EXPECT_EQ_32_64(511540u, 523580u, EstimateMemoryUsage(set));
}
TEST(EstimateMemoryUsageTest, UnorderedMultiSet) {
std::unordered_multiset<Data, Data::Hasher> set;
for (int i = 0; i != 500; ++i) {
set.insert(Data(i));
set.insert(Data(i));
}
EXPECT_EQ_32_64(261540u, 273580u, EstimateMemoryUsage(set));
}
TEST(EstimateMemoryUsageTest, UnorderedMap) {
std::unordered_map<Data, short, Data::Hasher> map;
for (int i = 0; i != 1000; ++i) {
map.insert({Data(i), static_cast<short>(i)});
}
EXPECT_EQ_32_64(515540u, 531580u, EstimateMemoryUsage(map));
}
TEST(EstimateMemoryUsageTest, UnorderedMultiMap) {
std::unordered_multimap<Data, short, Data::Hasher> map;
for (int i = 0; i != 1000; ++i) {
map.insert({Data(i), static_cast<short>(i)});
}
EXPECT_EQ_32_64(515540u, 531580u, EstimateMemoryUsage(map));
}
TEST(EstimateMemoryUsageTest, Deque) {
std::deque<Data> deque;
// Pick a large value so that platform-specific accounting
// for deque's blocks is small compared to usage of all items.
constexpr size_t kDataSize = 100000;
for (int i = 0; i != 1500; ++i) {
deque.push_back(Data(kDataSize));
}
// Compare against a reasonable minimum (i.e. no overhead).
size_t min_expected_usage = deque.size() * (sizeof(Data) + kDataSize);
EXPECT_LE(min_expected_usage, EstimateMemoryUsage(deque));
}
TEST(EstimateMemoryUsageTest, IsStandardContainerComplexIteratorTest) {
struct abstract {
virtual void method() = 0;
};
static_assert(
internal::IsIteratorOfStandardContainer<std::list<int>::iterator>, "");
static_assert(
internal::IsIteratorOfStandardContainer<std::list<int>::const_iterator>,
"");
static_assert(
internal::IsIteratorOfStandardContainer<std::list<int>::reverse_iterator>,
"");
static_assert(internal::IsIteratorOfStandardContainer<
std::list<int>::const_reverse_iterator>,
"");
static_assert(!internal::IsIteratorOfStandardContainer<int>, "");
static_assert(!internal::IsIteratorOfStandardContainer<abstract*>, "");
}
} // namespace trace_event
} // namespace base