Relax asserts in arena_test

Summary:
Commit c67d206898 did not fix all test conditions
which use Arena::MemoryAllocatedBytes() (see Travis failure
https://travis-ci.org/facebook/rocksdb/jobs/79957700). The assumption of that
commit was that aligned allocations do not call Arena::AllocateNewBlock(), so
malloc_usable_block_size() would not be used for Arena::MemoryAllocatedBytes().
However, there is a code path where Arena::AllocateAligned() calls
AllocateFallback() which in turn calls Arena::AllocateNewBlock(), so
Arena::MemoryAllocatedBytes() may return a greater value than expected even for
aligned requests.

Test Plan: make arena_test && ./arena_test

Reviewers: rven, anthony, yhchiang, aekmekji, igor, sdong

Reviewed By: sdong

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D46869
main
Andres Noetzli 9 years ago
parent 03ddce9a01
commit ad0d70ca1e
  1. 37
      util/arena_test.cc

@ -21,6 +21,14 @@ class ArenaTest : public testing::Test {};
TEST_F(ArenaTest, Empty) { Arena arena0; } TEST_F(ArenaTest, Empty) { Arena arena0; }
namespace { namespace {
bool CheckMemoryAllocated(size_t allocated, size_t expected) {
// The value returned by Arena::MemoryAllocatedBytes() may be greater than
// the requested memory. We choose a somewhat arbitrary upper bound of
// max_expected = expected * 1.1 to detect critical overallocation.
size_t max_expected = expected * 1.1;
return allocated >= expected && allocated <= max_expected;
}
void MemoryAllocatedBytesTest(size_t huge_page_size) { void MemoryAllocatedBytesTest(size_t huge_page_size) {
const int N = 17; const int N = 17;
size_t req_sz; // requested size size_t req_sz; // requested size
@ -36,7 +44,8 @@ void MemoryAllocatedBytesTest(size_t huge_page_size) {
arena.Allocate(req_sz); arena.Allocate(req_sz);
} }
expected_memory_allocated = req_sz * N + Arena::kInlineSize; expected_memory_allocated = req_sz * N + Arena::kInlineSize;
ASSERT_GE(arena.MemoryAllocatedBytes(), expected_memory_allocated); ASSERT_PRED2(CheckMemoryAllocated, arena.MemoryAllocatedBytes(),
expected_memory_allocated);
arena.Allocate(Arena::kInlineSize - 1); arena.Allocate(Arena::kInlineSize - 1);
@ -49,13 +58,15 @@ void MemoryAllocatedBytesTest(size_t huge_page_size) {
arena.Allocate(req_sz); arena.Allocate(req_sz);
} }
if (huge_page_size) { if (huge_page_size) {
ASSERT_TRUE(arena.MemoryAllocatedBytes() >= ASSERT_TRUE(
expected_memory_allocated + bsz || CheckMemoryAllocated(arena.MemoryAllocatedBytes(),
arena.MemoryAllocatedBytes() >= expected_memory_allocated + bsz) ||
expected_memory_allocated + huge_page_size); CheckMemoryAllocated(arena.MemoryAllocatedBytes(),
expected_memory_allocated + huge_page_size));
} else { } else {
expected_memory_allocated += bsz; expected_memory_allocated += bsz;
ASSERT_GE(arena.MemoryAllocatedBytes(), expected_memory_allocated); ASSERT_PRED2(CheckMemoryAllocated, arena.MemoryAllocatedBytes(),
expected_memory_allocated);
} }
// requested size > size of a block: // requested size > size of a block:
@ -66,7 +77,8 @@ void MemoryAllocatedBytesTest(size_t huge_page_size) {
arena.Allocate(req_sz); arena.Allocate(req_sz);
} }
expected_memory_allocated += req_sz * N; expected_memory_allocated += req_sz * N;
ASSERT_GE(arena.MemoryAllocatedBytes(), expected_memory_allocated); ASSERT_PRED2(CheckMemoryAllocated, arena.MemoryAllocatedBytes(),
expected_memory_allocated);
} }
// Make sure we didn't count the allocate but not used memory space in // Make sure we didn't count the allocate but not used memory space in
@ -83,7 +95,8 @@ static void ApproximateMemoryUsageTest(size_t huge_page_size) {
arena.AllocateAligned(Arena::kInlineSize / 2 - 16); arena.AllocateAligned(Arena::kInlineSize / 2 - 16);
arena.AllocateAligned(Arena::kInlineSize / 2); arena.AllocateAligned(Arena::kInlineSize / 2);
ASSERT_EQ(arena.ApproximateMemoryUsage(), Arena::kInlineSize - 8); ASSERT_EQ(arena.ApproximateMemoryUsage(), Arena::kInlineSize - 8);
ASSERT_EQ(arena.MemoryAllocatedBytes(), Arena::kInlineSize); ASSERT_PRED2(CheckMemoryAllocated, arena.MemoryAllocatedBytes(),
Arena::kInlineSize);
auto num_blocks = kBlockSize / kEntrySize; auto num_blocks = kBlockSize / kEntrySize;
@ -91,10 +104,12 @@ static void ApproximateMemoryUsageTest(size_t huge_page_size) {
arena.AllocateAligned(kEntrySize); arena.AllocateAligned(kEntrySize);
auto mem_usage = arena.MemoryAllocatedBytes(); auto mem_usage = arena.MemoryAllocatedBytes();
if (huge_page_size) { if (huge_page_size) {
ASSERT_TRUE(mem_usage == kBlockSize + Arena::kInlineSize || ASSERT_TRUE(
mem_usage == huge_page_size + Arena::kInlineSize); CheckMemoryAllocated(mem_usage, kBlockSize + Arena::kInlineSize) ||
CheckMemoryAllocated(mem_usage, huge_page_size + Arena::kInlineSize));
} else { } else {
ASSERT_EQ(mem_usage, kBlockSize + Arena::kInlineSize); ASSERT_PRED2(CheckMemoryAllocated, mem_usage,
kBlockSize + Arena::kInlineSize);
} }
auto usage = arena.ApproximateMemoryUsage(); auto usage = arena.ApproximateMemoryUsage();
ASSERT_LT(usage, mem_usage); ASSERT_LT(usage, mem_usage);

Loading…
Cancel
Save