proxygen
MemoryIdlerTest.cpp File Reference
#include <folly/detail/MemoryIdler.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
#include <folly/synchronization/Baton.h>
#include <memory>
#include <thread>

Go to the source code of this file.

Classes

struct  MockClock
 
struct  MockAtom< T >
 

Functions

 TEST (MemoryIdler, releaseStack)
 
 TEST (MemoryIdler, releaseStackMinExtra)
 
 TEST (MemoryIdler, releaseStackLargeExtra)
 
 TEST (MemoryIdler, releaseMallocTLS)
 
FutexResult futexWait (const Futex< MockAtom > *futex, uint32_t expected, uint32_t waitMask)
 
template<typename Clock , typename Duration >
FutexResult futexWaitUntil (const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
 
 TEST (MemoryIdler, futexWaitValueChangedEarly)
 
 TEST (MemoryIdler, futexWaitValueChangedLate)
 
 TEST (MemoryIdler, futexWaitAwokenEarly)
 
 TEST (MemoryIdler, futexWaitAwokenLate)
 
 TEST (MemoryIdler, futexWaitImmediateFlush)
 
 TEST (MemoryIdler, futexWaitNeverFlush)
 

Variables

static auto const forever = MockClock::time_point::max()
 

Function Documentation

FutexResult futexWait ( const Futex< MockAtom > *  futex,
uint32_t  expected,
uint32_t  waitMask 
)

Definition at line 97 of file MemoryIdlerTest.cpp.

97  {
98  return futex->futexWait(expected, waitMask);
99 }
template<typename Clock , typename Duration >
FutexResult futexWaitUntil ( const Futex< MockAtom > *  futex,
std::uint32_t  expected,
std::chrono::time_point< Clock, Duration > const &  deadline,
uint32_t  waitMask 
)

Definition at line 101 of file MemoryIdlerTest.cpp.

Referenced by folly::fibers::Baton::timedWaitThread().

105  {
106  return futex->futexWaitUntil(expected, deadline, waitMask);
107 }
TEST ( MemoryIdler  ,
releaseStack   
)

Definition at line 30 of file MemoryIdlerTest.cpp.

References folly::detail::MemoryIdler::unmapUnusedStack().

30  {
31  MemoryIdler::unmapUnusedStack();
32 }
TEST ( MemoryIdler  ,
releaseStackMinExtra   
)

Definition at line 34 of file MemoryIdlerTest.cpp.

References folly::detail::MemoryIdler::unmapUnusedStack().

34  {
35  MemoryIdler::unmapUnusedStack(0);
36 }
TEST ( MemoryIdler  ,
releaseStackLargeExtra   
)

Definition at line 38 of file MemoryIdlerTest.cpp.

References folly::detail::MemoryIdler::unmapUnusedStack().

38  {
39  MemoryIdler::unmapUnusedStack(30000000);
40 }
TEST ( MemoryIdler  ,
releaseMallocTLS   
)

Definition at line 42 of file MemoryIdlerTest.cpp.

References folly::detail::MemoryIdler::flushLocalMallocCaches().

42  {
43  auto p = new int[4];
44  MemoryIdler::flushLocalMallocCaches();
45  delete[] p;
46  MemoryIdler::flushLocalMallocCaches();
47  p = new int[4];
48  MemoryIdler::flushLocalMallocCaches();
49  delete[] p;
50 }
TEST ( MemoryIdler  ,
futexWaitValueChangedEarly   
)

Definition at line 109 of file MemoryIdlerTest.cpp.

References folly::test::begin(), folly::detail::MemoryIdler::defaultIdleTimeout, EXPECT_CALL, EXPECT_EQ, forever, folly::detail::futexWaitUntil(), folly::detail::MemoryIdler::futexWaitUntil(), testing::Ge(), folly::AtomicStruct< T, Atom >::load(), testing::Lt(), testing::Return(), MockClock::setup(), and folly::detail::VALUE_CHANGED.

109  {
110  Futex<MockAtom> fut;
111  auto clock = MockClock::setup();
112  auto begin = MockClock::time_point(std::chrono::seconds(100));
113  auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
114 
115  EXPECT_CALL(*clock, nowImpl()).WillOnce(Return(begin));
116  EXPECT_CALL(
117  fut,
119  1, AllOf(Ge(begin + idleTimeout), Lt(begin + 2 * idleTimeout)), -1))
120  .WillOnce(Return(FutexResult::VALUE_CHANGED));
121  EXPECT_EQ(
122  FutexResult::VALUE_CHANGED, MemoryIdler::futexWaitUntil(fut, 1, forever));
123 }
FutexResult futexWaitUntil(const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto const forever
Atom< std::uint32_t > Futex
Definition: Futex.h:51
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
internal::LtMatcher< Rhs > Lt(Rhs x)
static std::shared_ptr< StrictMock< MockClock > > setup()
Hold on to the returned shared_ptr until the end of the test.
StrictConjunction< T< As >... > AllOf
#define EXPECT_CALL(obj, call)
internal::GeMatcher< Rhs > Ge(Rhs x)
std::chrono::steady_clock::time_point time_point
internal::ReturnAction< R > Return(R value)
TEST ( MemoryIdler  ,
futexWaitValueChangedLate   
)

Definition at line 125 of file MemoryIdlerTest.cpp.

References folly::test::begin(), folly::detail::MemoryIdler::defaultIdleTimeout, EXPECT_CALL, EXPECT_EQ, forever, folly::detail::futexWaitUntil(), folly::detail::MemoryIdler::futexWaitUntil(), testing::Ge(), folly::AtomicStruct< T, Atom >::load(), testing::Lt(), testing::Return(), MockClock::setup(), folly::detail::TIMEDOUT, and folly::detail::VALUE_CHANGED.

125  {
126  Futex<MockAtom> fut;
127  auto clock = MockClock::setup();
128  auto begin = MockClock::time_point(std::chrono::seconds(100));
129  auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
130 
131  EXPECT_CALL(*clock, nowImpl()).WillOnce(Return(begin));
132  EXPECT_CALL(
133  fut,
135  1, AllOf(Ge(begin + idleTimeout), Lt(begin + 2 * idleTimeout)), -1))
136  .WillOnce(Return(FutexResult::TIMEDOUT));
137  EXPECT_CALL(fut, futexWaitUntil(1, forever, -1))
138  .WillOnce(Return(FutexResult::VALUE_CHANGED));
139  EXPECT_EQ(
140  FutexResult::VALUE_CHANGED, MemoryIdler::futexWaitUntil(fut, 1, forever));
141 }
FutexResult futexWaitUntil(const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto const forever
Atom< std::uint32_t > Futex
Definition: Futex.h:51
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
internal::LtMatcher< Rhs > Lt(Rhs x)
static std::shared_ptr< StrictMock< MockClock > > setup()
Hold on to the returned shared_ptr until the end of the test.
StrictConjunction< T< As >... > AllOf
#define EXPECT_CALL(obj, call)
internal::GeMatcher< Rhs > Ge(Rhs x)
std::chrono::steady_clock::time_point time_point
internal::ReturnAction< R > Return(R value)
TEST ( MemoryIdler  ,
futexWaitAwokenEarly   
)

Definition at line 143 of file MemoryIdlerTest.cpp.

References folly::detail::AWOKEN, folly::test::begin(), folly::detail::MemoryIdler::defaultIdleTimeout, EXPECT_CALL, EXPECT_EQ, forever, folly::detail::futexWaitUntil(), folly::detail::MemoryIdler::futexWaitUntil(), testing::Ge(), folly::AtomicStruct< T, Atom >::load(), testing::Return(), and MockClock::setup().

143  {
144  Futex<MockAtom> fut;
145  auto clock = MockClock::setup();
146  auto begin = MockClock::time_point(std::chrono::seconds(100));
147  auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
148 
149  EXPECT_CALL(*clock, nowImpl()).WillOnce(Return(begin));
150  EXPECT_CALL(fut, futexWaitUntil(1, Ge(begin + idleTimeout), -1))
151  .WillOnce(Return(FutexResult::AWOKEN));
152  EXPECT_EQ(FutexResult::AWOKEN, MemoryIdler::futexWaitUntil(fut, 1, forever));
153 }
FutexResult futexWaitUntil(const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto const forever
Atom< std::uint32_t > Futex
Definition: Futex.h:51
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
static std::shared_ptr< StrictMock< MockClock > > setup()
Hold on to the returned shared_ptr until the end of the test.
#define EXPECT_CALL(obj, call)
internal::GeMatcher< Rhs > Ge(Rhs x)
std::chrono::steady_clock::time_point time_point
internal::ReturnAction< R > Return(R value)
TEST ( MemoryIdler  ,
futexWaitAwokenLate   
)

Definition at line 155 of file MemoryIdlerTest.cpp.

References folly::detail::AWOKEN, folly::test::begin(), folly::detail::MemoryIdler::defaultIdleTimeout, EXPECT_CALL, EXPECT_EQ, f, forever, folly::detail::futexWaitUntil(), folly::detail::MemoryIdler::futexWaitUntil(), folly::AtomicStruct< T, Atom >::load(), testing::Return(), MockClock::setup(), and folly::detail::TIMEDOUT.

155  {
156  Futex<MockAtom> fut;
157  auto clock = MockClock::setup();
158  auto begin = MockClock::time_point(std::chrono::seconds(100));
159  auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
160 
161  EXPECT_CALL(*clock, nowImpl()).WillOnce(Return(begin));
162  EXPECT_CALL(fut, futexWaitUntil(1, begin + idleTimeout, -1))
163  .WillOnce(Return(FutexResult::TIMEDOUT));
164  EXPECT_CALL(fut, futexWaitUntil(1, forever, -1))
165  .WillOnce(Return(FutexResult::AWOKEN));
166  EXPECT_EQ(
167  FutexResult::AWOKEN,
168  MemoryIdler::futexWaitUntil(fut, 1, forever, -1, idleTimeout, 100, 0.0f));
169 }
auto f
FutexResult futexWaitUntil(const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto const forever
Atom< std::uint32_t > Futex
Definition: Futex.h:51
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
static std::shared_ptr< StrictMock< MockClock > > setup()
Hold on to the returned shared_ptr until the end of the test.
#define EXPECT_CALL(obj, call)
std::chrono::steady_clock::time_point time_point
internal::ReturnAction< R > Return(R value)
TEST ( MemoryIdler  ,
futexWaitImmediateFlush   
)

Definition at line 171 of file MemoryIdlerTest.cpp.

References folly::detail::AWOKEN, EXPECT_CALL, EXPECT_EQ, forever, folly::detail::futexWaitUntil(), folly::detail::MemoryIdler::futexWaitUntil(), testing::Return(), and MockClock::setup().

171  {
172  Futex<MockAtom> fut;
173  auto clock = MockClock::setup();
174 
175  EXPECT_CALL(fut, futexWaitUntil(2, forever, 0xff))
176  .WillOnce(Return(FutexResult::AWOKEN));
177  EXPECT_EQ(
178  FutexResult::AWOKEN,
180  fut, 2, forever, 0xff, std::chrono::seconds(0)));
181 }
FutexResult futexWaitUntil(const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto const forever
Atom< std::uint32_t > Futex
Definition: Futex.h:51
static std::shared_ptr< StrictMock< MockClock > > setup()
Hold on to the returned shared_ptr until the end of the test.
#define EXPECT_CALL(obj, call)
internal::ReturnAction< R > Return(R value)
TEST ( MemoryIdler  ,
futexWaitNeverFlush   
)

Definition at line 183 of file MemoryIdlerTest.cpp.

References folly::detail::AWOKEN, EXPECT_CALL, EXPECT_EQ, forever, folly::detail::futexWaitUntil(), folly::detail::MemoryIdler::futexWaitUntil(), testing::Return(), and MockClock::setup().

183  {
184  Futex<MockAtom> fut;
185  auto clock = MockClock::setup();
186 
187  EXPECT_CALL(fut, futexWaitUntil(1, forever, -1))
188  .WillOnce(Return(FutexResult::AWOKEN));
189  EXPECT_EQ(
190  FutexResult::AWOKEN,
192  fut, 1, forever, -1, std::chrono::seconds(-7)));
193 }
FutexResult futexWaitUntil(const Futex< MockAtom > *futex, std::uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto const forever
Atom< std::uint32_t > Futex
Definition: Futex.h:51
static std::shared_ptr< StrictMock< MockClock > > setup()
Hold on to the returned shared_ptr until the end of the test.
#define EXPECT_CALL(obj, call)
internal::ReturnAction< R > Return(R value)

Variable Documentation