Fix block overhead estimation in tests
authorNicholas Ormrod <njormrod@fb.com>
Fri, 22 Aug 2014 17:07:25 +0000 (10:07 -0700)
committerSara Golemon <sgolemon@fb.com>
Tue, 9 Sep 2014 21:22:23 +0000 (14:22 -0700)
Summary:
folly/test/ArenaTest.h assumes that goodMallocSize will account
for the block overhead, if the size is +1. However, when compiling
without jemalloc (as, I would imagine, most of our open-source clients
do), goodMallocSize(64 + 1) returns 65, which is less than 64 +
sizeof(Block), and so the tests fail.

I have added a constant to Arena.h which exposes the overhead taken by
the Block, and changed the tests to use that value instead of 1.
(Arena::getTotalSize relies on the totalAllocatedSize_ variable, which
is only non-trivially set in allocateSlow, where the size of the heap is
added to sizeof(Block)).

Test Plan: fbconfig --allocator=malloc folly/test:arena_test && fbmake runtests

Reviewed By: jon.coens@fb.com

Subscribers: sdwilsh, njormrod

FB internal diff: D1512231

folly/Arena.h
folly/test/ArenaTest.cpp

index e08ca06454c323722a6f714068e85cb8cd7e9705..ce758e5b4014fc7a81e7b3a7dec33ba924fd5e10 100644 (file)
@@ -157,6 +157,7 @@ class Arena {
   static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block);
   static constexpr size_t kNoSizeLimit = 0;
   static constexpr size_t kDefaultMaxAlign = alignof(Block);
+  static constexpr size_t kBlockOverhead = sizeof(Block);
 
  private:
   bool isAligned(uintptr_t address) const {
index 4d77e207afa9e4c5aab463fb4ea5f71c45d8df30..6c05fa2e9d175a751065dc1d3d1a43e143343e2e 100644 (file)
@@ -39,7 +39,7 @@ TEST(Arena, SizeSanity) {
   size_t* ptr = static_cast<size_t*>(arena.allocate(sizeof(long)));
   allocatedItems.insert(ptr);
   minimum_size += requestedBlockSize;
-  maximum_size += goodMallocSize(requestedBlockSize + 1);
+  maximum_size += goodMallocSize(requestedBlockSize + SysArena::kBlockOverhead);
   EXPECT_TRUE(arena.totalSize() >= minimum_size);
   EXPECT_TRUE(arena.totalSize() <= maximum_size);
   VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
@@ -59,7 +59,8 @@ TEST(Arena, SizeSanity) {
     allocatedItems.insert(ptr);
   }
   minimum_size += 10 * requestedBlockSize;
-  maximum_size += 10 * goodMallocSize(requestedBlockSize + 1);
+  maximum_size += 10 * goodMallocSize(requestedBlockSize
+                                      + SysArena::kBlockOverhead);
   EXPECT_TRUE(arena.totalSize() >= minimum_size);
   EXPECT_TRUE(arena.totalSize() <= maximum_size);
   VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
@@ -69,7 +70,8 @@ TEST(Arena, SizeSanity) {
   ptr = static_cast<size_t*>(arena.allocate(10 * requestedBlockSize));
   allocatedItems.insert(ptr);
   minimum_size += 10 * requestedBlockSize;
-  maximum_size += goodMallocSize(10 * requestedBlockSize + 1);
+  maximum_size += goodMallocSize(10 * requestedBlockSize
+                                 + SysArena::kBlockOverhead);
   EXPECT_TRUE(arena.totalSize() >= minimum_size);
   EXPECT_TRUE(arena.totalSize() <= maximum_size);
   VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "