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

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

Issue 1409743002: Re-land: base: Use MADV_REMOVE instead of ftruncate to purge discardable memory segments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix problem with segment having been released before we try to purge Created 5 years, 2 months 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/memory/discardable_shared_memory.h" 6 #include "base/memory/discardable_shared_memory.h"
7 #include "base/process/process_metrics.h" 7 #include "base/process/process_metrics.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace base { 10 namespace base {
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 memory.SetNow(Time::FromDoubleT(1)); 336 memory.SetNow(Time::FromDoubleT(1));
337 memory.Unlock(0, 0); 337 memory.Unlock(0, 0);
338 338
339 // Lock and unlock memory. 339 // Lock and unlock memory.
340 auto lock_rv = memory.Lock(0, 0); 340 auto lock_rv = memory.Lock(0, 0);
341 EXPECT_EQ(DiscardableSharedMemory::SUCCESS, lock_rv); 341 EXPECT_EQ(DiscardableSharedMemory::SUCCESS, lock_rv);
342 memory.SetNow(Time::FromDoubleT(2)); 342 memory.SetNow(Time::FromDoubleT(2));
343 memory.Unlock(0, 0); 343 memory.Unlock(0, 0);
344 } 344 }
345 345
346 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING) 346 // This test checks that zero-filled pages are returned after purging a segment
347 TEST(DiscardableSharedMemoryTest, Shrink) { 347 // when DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE is
348 // defined and MADV_REMOVE is supported.
349 #if defined(DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE)
350 TEST(DiscardableSharedMemoryTest, ZeroFilledPagesAfterPurge) {
348 const uint32 kDataSize = 1024; 351 const uint32 kDataSize = 1024;
349 352
350 TestDiscardableSharedMemory memory; 353 TestDiscardableSharedMemory memory1;
351 bool rv = memory.CreateAndMap(kDataSize); 354 bool rv = memory1.CreateAndMap(kDataSize);
352 ASSERT_TRUE(rv); 355 ASSERT_TRUE(rv);
353 356
354 EXPECT_NE(0u, memory.mapped_size()); 357 SharedMemoryHandle shared_handle;
358 ASSERT_TRUE(
359 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
360 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
355 361
356 // Mapped size should be 0 after shrinking memory segment. 362 TestDiscardableSharedMemory memory2(shared_handle);
357 memory.Shrink(); 363 rv = memory2.Map(kDataSize);
358 EXPECT_EQ(0u, memory.mapped_size()); 364 ASSERT_TRUE(rv);
365
366 // Initialize all memory to '0xaa'.
367 memset(memory2.memory(), 0xaa, kDataSize);
368
369 // Unlock memory.
370 memory2.SetNow(Time::FromDoubleT(1));
371 memory2.Unlock(0, 0);
372 EXPECT_FALSE(memory1.IsMemoryLocked());
373
374 // Memory is unlocked, but our usage timestamp is incorrect.
375 rv = memory1.Purge(Time::FromDoubleT(2));
376 EXPECT_FALSE(rv);
377 rv = memory1.Purge(Time::FromDoubleT(3));
378 EXPECT_TRUE(rv);
379
380 // Check that reading memory after it has been purged is returning
381 // zero-filled pages.
382 uint8 expected_data[kDataSize] = {};
383 EXPECT_EQ(memcmp(memory2.memory(), expected_data, kDataSize), 0);
359 } 384 }
360 #endif 385 #endif
361 386
362 } // namespace 387 } // namespace
363 } // namespace base 388 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_shared_memory.cc ('k') | content/common/host_discardable_shared_memory_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698