2 * Copyright 2014-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/detail/MemoryIdler.h>
19 #include <folly/portability/GMock.h>
20 #include <folly/portability/GTest.h>
21 #include <folly/synchronization/Baton.h>
26 using namespace folly;
27 using namespace folly::detail;
28 using namespace testing;
30 TEST(MemoryIdler, releaseStack) {
31 MemoryIdler::unmapUnusedStack();
34 TEST(MemoryIdler, releaseStackMinExtra) {
35 MemoryIdler::unmapUnusedStack(0);
38 TEST(MemoryIdler, releaseStackLargeExtra) {
39 MemoryIdler::unmapUnusedStack(30000000);
42 TEST(MemoryIdler, releaseMallocTLS) {
44 MemoryIdler::flushLocalMallocCaches();
46 MemoryIdler::flushLocalMallocCaches();
48 MemoryIdler::flushLocalMallocCaches();
53 /// MockedAtom gives us a way to select a mocked Futex implementation
54 /// inside Baton, even though the atom itself isn't exercised by the
57 struct MockAtom : public std::atomic<T> {
58 explicit MockAtom(T init = 0) : std::atomic<T>(init) {}
62 /// MockClock is a bit tricky because we are mocking a static function
63 /// (now()), so we need to find the corresponding mock instance without
64 /// extending its scope beyond that of the test. I generally avoid
65 /// shared_ptr, but a weak_ptr is just the ticket here
67 typedef std::chrono::steady_clock::duration duration;
68 typedef std::chrono::steady_clock::time_point time_point;
70 MOCK_METHOD0(nowImpl, time_point(void));
72 /// Hold on to the returned shared_ptr until the end of the test
73 static std::shared_ptr<StrictMock<MockClock>> setup() {
74 auto rv = std::make_shared<StrictMock<MockClock>>();
75 s_mockClockInstance = rv;
79 static time_point now() {
80 return s_mockClockInstance.lock()->nowImpl();
83 static std::weak_ptr<StrictMock<MockClock>> s_mockClockInstance;
86 std::weak_ptr<StrictMock<MockClock>> MockClock::s_mockClockInstance;
90 namespace folly { namespace detail {
92 /// Futex<MockAtom> is our mocked futex implementation. Note that the
93 /// method signatures differ from the real Futex because we have elided
94 /// unused default params and collapsed templated methods into the
97 struct Futex<MockAtom> {
98 MOCK_METHOD2(futexWait, FutexResult(uint32_t, uint32_t));
99 MOCK_METHOD3(futexWaitUntil,
100 FutexResult(uint32_t, const MockClock::time_point&, uint32_t));
103 } // namespace detail
106 TEST(MemoryIdler, futexWaitValueChangedEarly) {
107 StrictMock<Futex<MockAtom>> fut;
108 auto clock = MockClock::setup();
109 auto begin = MockClock::time_point(std::chrono::seconds(100));
110 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
112 EXPECT_CALL(*clock, nowImpl())
113 .WillOnce(Return(begin));
114 EXPECT_CALL(fut, futexWaitUntil(1, AllOf(Ge(begin + idleTimeout),
115 Lt(begin + 2 * idleTimeout)), -1))
116 .WillOnce(Return(FutexResult::VALUE_CHANGED));
118 FutexResult::VALUE_CHANGED,
119 (MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
122 TEST(MemoryIdler, futexWaitValueChangedLate) {
123 StrictMock<Futex<MockAtom>> fut;
124 auto clock = MockClock::setup();
125 auto begin = MockClock::time_point(std::chrono::seconds(100));
126 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
128 EXPECT_CALL(*clock, nowImpl())
129 .WillOnce(Return(begin));
130 EXPECT_CALL(fut, futexWaitUntil(1, AllOf(Ge(begin + idleTimeout),
131 Lt(begin + 2 * idleTimeout)), -1))
132 .WillOnce(Return(FutexResult::TIMEDOUT));
133 EXPECT_CALL(fut, futexWait(1, -1))
134 .WillOnce(Return(FutexResult::VALUE_CHANGED));
136 FutexResult::VALUE_CHANGED,
137 (MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
140 TEST(MemoryIdler, futexWaitAwokenEarly) {
141 StrictMock<Futex<MockAtom>> fut;
142 auto clock = MockClock::setup();
143 auto begin = MockClock::time_point(std::chrono::seconds(100));
144 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
146 EXPECT_CALL(*clock, nowImpl())
147 .WillOnce(Return(begin));
148 EXPECT_CALL(fut, futexWaitUntil(1, Ge(begin + idleTimeout), -1))
149 .WillOnce(Return(FutexResult::AWOKEN));
152 (MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
155 TEST(MemoryIdler, futexWaitAwokenLate) {
156 StrictMock<Futex<MockAtom>> fut;
157 auto clock = MockClock::setup();
158 auto begin = MockClock::time_point(std::chrono::seconds(100));
159 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
161 EXPECT_CALL(*clock, nowImpl())
162 .WillOnce(Return(begin));
163 EXPECT_CALL(fut, futexWaitUntil(1, begin + idleTimeout, -1))
164 .WillOnce(Return(FutexResult::TIMEDOUT));
165 EXPECT_CALL(fut, futexWait(1, -1)).WillOnce(Return(FutexResult::AWOKEN));
168 (MemoryIdler::futexWait<MockAtom, MockClock>(
169 fut, 1, -1, idleTimeout, 100, 0.0f)));
172 TEST(MemoryIdler, futexWaitImmediateFlush) {
173 StrictMock<Futex<MockAtom>> fut;
174 auto clock = MockClock::setup();
176 EXPECT_CALL(fut, futexWait(2, 0xff)).WillOnce(Return(FutexResult::AWOKEN));
179 (MemoryIdler::futexWait<MockAtom, MockClock>(
180 fut, 2, 0xff, std::chrono::seconds(0))));
183 TEST(MemoryIdler, futexWaitNeverFlush) {
184 StrictMock<Futex<MockAtom>> fut;
185 auto clock = MockClock::setup();
187 EXPECT_CALL(fut, futexWait(1, -1)).WillOnce(Return(FutexResult::AWOKEN));
190 (MemoryIdler::futexWait<MockAtom, MockClock>(
191 fut, 1, -1, MockClock::duration::max())));