Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: base/memory/persistent_memory_allocator_unittest.cc

Issue 1410213004: Create "persistent memory allocator" for persisting and sharing objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: SharedMemoryAllocator -> PersistentMemoryAllocator Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/memory/persistent_memory_allocator.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/rand_util.h"
10 #include "base/threading/simple_thread.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12
13 namespace {
14
15 const uint32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB
16 const uint32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB
17
18 } // namespace
19
20 namespace base {
21
22 typedef PersistentMemoryAllocator::Reference Reference;
23
24 class PersistentMemoryAllocatorTest : public testing::Test {
25 public:
26 struct TestObject1 {
27 int32_t onething;
28 char oranother;
29 };
30
31 struct TestObject2 {
32 int thiis;
33 long that;
34 float andthe;
35 char other;
36 double thing;
37 };
38
39 PersistentMemoryAllocatorTest() {
40 mem_segment_.reset(new char[TEST_MEMORY_SIZE]);
41 }
42
43 void SetUp() override {
44 allocator_.reset();
45 memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE);
46 allocator_.reset(new PersistentMemoryAllocator(
47 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE,
48 "TestAllocator"));
49 }
50
51 void TearDown() override {
52 allocator_.reset();
53 }
54
55 int CountIterables() {
56 PersistentMemoryAllocator::Iterator iter;
57 uint32_t type;
58 int count = 0;
59 for (allocator_->CreateIterator(&iter);
60 allocator_->GetNextIterable(&iter, &type) != 0;) {
61 count++;
62 }
63 return count;
64 }
65
66 scoped_ptr<char[]> mem_segment_;
67 scoped_ptr<PersistentMemoryAllocator> allocator_;
68 };
69
70 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) {
71 PersistentMemoryAllocator::MemoryInfo meminfo0;
72 allocator_->GetMemoryInfo(&meminfo0);
73 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total);
74 EXPECT_GT(meminfo0.total, meminfo0.free);
75
76 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1);
77 EXPECT_NE(0, block1);
78 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject1>(block1, 1));
79 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1));
80 EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1));
81 EXPECT_GE(sizeof(TestObject1) + 7, allocator_->GetAllocSize(block1));
82 PersistentMemoryAllocator::MemoryInfo meminfo1;
83 allocator_->GetMemoryInfo(&meminfo1);
84 EXPECT_EQ(meminfo0.total, meminfo1.total);
85 EXPECT_GT(meminfo0.free, meminfo1.free);
86
87 PersistentMemoryAllocator::Iterator iter;
88 uint32_t type;
89 allocator_->CreateIterator(&iter);
90 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
91 allocator_->MakeIterable(block1);
92 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type));
93 EXPECT_EQ(1U, type);
94 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
95
96 Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2);
97 EXPECT_NE(0, block2);
98 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2));
99 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1));
100 EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2));
101 EXPECT_GE(sizeof(TestObject2) + 7, allocator_->GetAllocSize(block2));
102 PersistentMemoryAllocator::MemoryInfo meminfo2;
103 allocator_->GetMemoryInfo(&meminfo2);
104 EXPECT_EQ(meminfo1.total, meminfo2.total);
105 EXPECT_GT(meminfo1.free, meminfo2.free);
106
107 allocator_->MakeIterable(block2);
108 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type));
109 EXPECT_EQ(2U, type);
110 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
111
112 EXPECT_FALSE(allocator_->IsFull());
113 EXPECT_FALSE(allocator_->IsCorrupt());
114
115 allocator_->UpdateStaticHistograms();
116 scoped_ptr<HistogramSamples> used_samples(
117 allocator_->used_histogram_->SnapshotSamples());
118 EXPECT_TRUE(used_samples);
119 EXPECT_EQ(1, used_samples->TotalCount());
120
121 scoped_ptr<HistogramSamples> allocs_samples(
122 allocator_->allocs_histogram_->SnapshotSamples());
123 EXPECT_TRUE(allocs_samples);
124 EXPECT_EQ(2, allocs_samples->TotalCount());
125 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject1)));
126 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject2)));
127 }
128
129 TEST_F(PersistentMemoryAllocatorTest, PageTest) {
130 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1);
131 EXPECT_LT(0, block1);
132 EXPECT_GT((int)TEST_MEMORY_PAGE, block1);
133
134 Reference block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2);
135 EXPECT_EQ((int)TEST_MEMORY_PAGE, block2);
136
137 Reference block3 = allocator_->Allocate(99, 3);
138 EXPECT_EQ(2 * (int)TEST_MEMORY_PAGE, block3);
139 }
140
141 class AllocatorThread : public SimpleThread {
142 public:
143 AllocatorThread(const std::string& name,
144 void* base,
145 int32_t size,
146 int32_t page_size)
147 : SimpleThread(name, Options()),
148 count_(0),
149 iterable_(0),
150 allocator_(base, size, page_size, std::string()) {}
151
152 void Run() override {
153 for (;;) {
154 int32_t size = (int32_t)base::RandInt(1, 99);
155 uint32_t type = (int32_t)base::RandInt(100, 999);
156 Reference block = allocator_.Allocate(size, type);
157 if (!block)
158 break;
159
160 count_++;
161 if (base::RandInt(0, 1)) {
162 allocator_.MakeIterable(block);
163 iterable_++;
164 }
165 }
166 }
167
168 int count_;
169 int iterable_;
170
171 private:
172 PersistentMemoryAllocator allocator_;
173 };
174
175 TEST_F(PersistentMemoryAllocatorTest, ParallelismTest) {
176 void* memory = mem_segment_.get();
177 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
178 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
179 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
180 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
181 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
182
183 t1.Start();
184 t2.Start();
185 t3.Start();
186 t4.Start();
187 t5.Start();
188
189 int last_count = 0;
190 do {
191 int count = CountIterables();
192 EXPECT_LE(last_count, count);
193 } while (!allocator_->IsCorrupt() && !allocator_->IsFull());
194
195 t1.Join();
196 t2.Join();
197 t3.Join();
198 t4.Join();
199 t5.Join();
200
201 EXPECT_FALSE(allocator_->IsCorrupt());
202 EXPECT_EQ(CountIterables(),
203 t1.iterable_ + t2.iterable_ + t3.iterable_ + t4.iterable_ +
204 t5.iterable_);
205 }
206
207 // This test doesn't verify anything other than it doesn't crash.
208 TEST_F(PersistentMemoryAllocatorTest, CorruptionTest) {
209 char* memory = mem_segment_.get();
210 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
211 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
212 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
213 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
214 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE);
215
216 t1.Start();
217 t2.Start();
218 t3.Start();
219 t4.Start();
220 t5.Start();
221
222 do {
223 size_t offset = base::RandInt(0, TEST_MEMORY_SIZE - 1);
224 char value = base::RandInt(0, 255);
225 memory[offset] = value;
226 } while (!allocator_->IsCorrupt() && !allocator_->IsFull());
227
228 t1.Join();
229 t2.Join();
230 t3.Join();
231 t4.Join();
232 t5.Join();
233
234 CountIterables();
235 }
236
237 // Attempt to cause crashes or loops by expressly creating dangerous coditions.
238 TEST_F(PersistentMemoryAllocatorTest, MaliciousTest) {
239 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1);
240 Reference block2 = allocator_->Allocate(sizeof(TestObject1), 2);
241 Reference block3 = allocator_->Allocate(sizeof(TestObject1), 3);
242 Reference block4 = allocator_->Allocate(sizeof(TestObject1), 3);
243 Reference block5 = allocator_->Allocate(sizeof(TestObject1), 3);
244 allocator_->MakeIterable(block1);
245 allocator_->MakeIterable(block2);
246 allocator_->MakeIterable(block3);
247 allocator_->MakeIterable(block4);
248 allocator_->MakeIterable(block5);
249 EXPECT_EQ(5, CountIterables());
250 EXPECT_FALSE(allocator_->IsCorrupt());
251
252 // Create loop in iterable list and ensure it doesn't hang.
253 int32_t* header4 = (int32_t*)(mem_segment_.get() + block4);
254 EXPECT_EQ(block5, header4[3]);
255 header4[3] = block3;
256 CountIterables(); // loop: 1-2-3-4-3
257 header4[3] = block2;
258 CountIterables(); // loop: 1-2-3-4-2
259 header4[3] = block1;
260 CountIterables(); // loop: 1-2-3-4-1
261 EXPECT_TRUE(allocator_->IsCorrupt());
262 }
263
264 } // namespace base
OLDNEW
« base/memory/persistent_memory_allocator.cc ('K') | « base/memory/persistent_memory_allocator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698