@ -57,67 +57,67 @@ TEST_F(WriteBufferManagerTest, CacheCost) {
std : : unique_ptr < WriteBufferManager > wbf (
new WriteBufferManager ( 50 * 1024 * 1024 , cache ) ) ;
// Allocate 1.5MB will allocate 2M B
wbf - > ReserveMem ( 1536 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 2 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 2 * 1024 * 1024 + 10000 ) ;
// Allocate 333KB will allocate 512K B
wbf - > ReserveMem ( 333 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 2 * 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 2 * 256 * 1024 + 10000 ) ;
// Allocate another 2M B
wbf - > ReserveMem ( 2 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 4 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 4 * 1024 * 1024 + 10000 ) ;
// Allocate another 512K B
wbf - > ReserveMem ( 512 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 4 * 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 4 * 256 * 1024 + 10000 ) ;
// Allocate another 2 0MB
wbf - > ReserveMem ( 2 0 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 24 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 24 * 1024 * 1024 + 10000 ) ;
// Allocate another 1 0MB
wbf - > ReserveMem ( 1 0 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 11 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 11 * 1024 * 1024 + 10000 ) ;
// Free 2 MB will not cause any change in cache cost
wbf - > FreeMem ( 2 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 24 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 24 * 1024 * 1024 + 10000 ) ;
// Free 1 MB will not cause any change in cache cost
wbf - > FreeMem ( 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 11 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 11 * 1024 * 1024 + 10000 ) ;
ASSERT_FALSE ( wbf - > ShouldFlush ( ) ) ;
// Allocate another 30 MB
wbf - > ReserveMem ( 30 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 52 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 52 * 1024 * 1024 + 10000 ) ;
// Allocate another 41 MB
wbf - > ReserveMem ( 41 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 + 10000 ) ;
ASSERT_TRUE ( wbf - > ShouldFlush ( ) ) ;
ASSERT_TRUE ( wbf - > ShouldFlush ( ) ) ;
wbf - > ScheduleFreeMem ( 20 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 52 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 52 * 1024 * 1024 + 10000 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 + 10000 ) ;
// Still need flush as the hard limit hits
ASSERT_TRUE ( wbf - > ShouldFlush ( ) ) ;
// Free 20MB will releae 1M B from cache
// Free 20MB will releae 256K B from cache
wbf - > FreeMem ( 20 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 + 10000 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 - 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 1024 * 1024 - 256 * 1024 + 10000 ) ;
ASSERT_FALSE ( wbf - > ShouldFlush ( ) ) ;
// Every free will release 1M B if still not hit 3/4
// Every free will release 256K B if still not hit 3/4
wbf - > FreeMem ( 16 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 50 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 50 * 1024 * 1024 + 10000 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 1 024 * 1024 - 2 * 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 1 024 * 1024 - 2 * 256 * 1024 + 10000 ) ;
wbf - > FreeMem ( 16 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 49 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 49 * 1024 * 1024 + 10000 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 102 4 * 1024 - 3 * 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 102 4 * 1024 - 3 * 256 * 1024 + 10000 ) ;
// Free 2M B will not cause any change in cache cost
wbf - > ReserveMem ( 2 * 1024 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 49 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 49 * 1024 * 1024 + 10000 ) ;
// Reserve 512K B will not cause any change in cache cost
wbf - > ReserveMem ( 512 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 102 4 * 1024 - 3 * 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 102 4 * 1024 - 3 * 256 * 1024 + 10000 ) ;
wbf - > FreeMem ( 16 * 1024 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 48 * 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 48 * 1024 * 1024 + 10000 ) ;
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 51 * 102 4 * 1024 - 4 * 256 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 51 * 102 4 * 1024 - 4 * 256 * 1024 + 10000 ) ;
// Destory write buffer manger should free everything
wbf . reset ( ) ;
@ -136,8 +136,8 @@ TEST_F(WriteBufferManagerTest, NoCapCacheCost) {
ASSERT_FALSE ( wbf - > ShouldFlush ( ) ) ;
wbf - > FreeMem ( 9 * 1024 * 1024 ) ;
for ( int i = 0 ; i < 1 0; i + + ) {
wbf - > FreeMem ( 16 * 1024 ) ;
for ( int i = 0 ; i < 4 0; i + + ) {
wbf - > FreeMem ( 4 * 1024 ) ;
}
ASSERT_GE ( cache - > GetPinnedUsage ( ) , 1024 * 1024 ) ;
ASSERT_LT ( cache - > GetPinnedUsage ( ) , 1024 * 1024 + 10000 ) ;