2 * Copyright 2016 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/Baton.h>
20 #include <folly/portability/GMock.h>
21 #include <folly/portability/GTest.h>
22 #include <folly/portability/Windows.h>
27 #include <semaphore.h>
29 using namespace folly;
30 using namespace folly::detail;
31 using namespace testing;
33 TEST(MemoryIdler, releaseStack) {
34 MemoryIdler::unmapUnusedStack();
37 TEST(MemoryIdler, releaseStackMinExtra) {
38 MemoryIdler::unmapUnusedStack(0);
41 TEST(MemoryIdler, releaseStackLargeExtra) {
42 MemoryIdler::unmapUnusedStack(30000000);
45 TEST(MemoryIdler, releaseMallocTLS) {
47 MemoryIdler::flushLocalMallocCaches();
49 MemoryIdler::flushLocalMallocCaches();
51 MemoryIdler::flushLocalMallocCaches();
56 /// MockedAtom gives us a way to select a mocked Futex implementation
57 /// inside Baton, even though the atom itself isn't exercised by the
60 struct MockAtom : public std::atomic<T> {
61 explicit MockAtom(T init = 0) : std::atomic<T>(init) {}
65 /// MockClock is a bit tricky because we are mocking a static function
66 /// (now()), so we need to find the corresponding mock instance without
67 /// extending its scope beyond that of the test. I generally avoid
68 /// shared_ptr, but a weak_ptr is just the ticket here
70 typedef std::chrono::steady_clock::duration duration;
71 typedef std::chrono::steady_clock::time_point time_point;
73 MOCK_METHOD0(nowImpl, time_point(void));
75 /// Hold on to the returned shared_ptr until the end of the test
76 static std::shared_ptr<StrictMock<MockClock>> setup() {
77 auto rv = std::make_shared<StrictMock<MockClock>>();
78 s_mockClockInstance = rv;
82 static time_point now() {
83 return s_mockClockInstance.lock()->nowImpl();
86 static std::weak_ptr<StrictMock<MockClock>> s_mockClockInstance;
89 std::weak_ptr<StrictMock<MockClock>> MockClock::s_mockClockInstance;
93 namespace folly { namespace detail {
95 /// Futex<MockAtom> is our mocked futex implementation. Note that the
96 /// method signatures differ from the real Futex because we have elided
97 /// unused default params and collapsed templated methods into the
100 struct Futex<MockAtom> {
101 MOCK_METHOD2(futexWait, bool(uint32_t, uint32_t));
102 MOCK_METHOD3(futexWaitUntil,
103 FutexResult(uint32_t, const MockClock::time_point&, uint32_t));
108 TEST(MemoryIdler, futexWaitValueChangedEarly) {
109 StrictMock<Futex<MockAtom>> fut;
110 auto clock = MockClock::setup();
111 auto begin = MockClock::time_point(std::chrono::seconds(100));
112 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
114 EXPECT_CALL(*clock, nowImpl())
115 .WillOnce(Return(begin));
116 EXPECT_CALL(fut, futexWaitUntil(1, AllOf(Ge(begin + idleTimeout),
117 Lt(begin + 2 * idleTimeout)), -1))
118 .WillOnce(Return(FutexResult::VALUE_CHANGED));
119 EXPECT_FALSE((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(false));
135 EXPECT_FALSE((MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
138 TEST(MemoryIdler, futexWaitAwokenEarly) {
139 StrictMock<Futex<MockAtom>> fut;
140 auto clock = MockClock::setup();
141 auto begin = MockClock::time_point(std::chrono::seconds(100));
142 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
144 EXPECT_CALL(*clock, nowImpl())
145 .WillOnce(Return(begin));
146 EXPECT_CALL(fut, futexWaitUntil(1, Ge(begin + idleTimeout), -1))
147 .WillOnce(Return(FutexResult::AWOKEN));
148 EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
151 TEST(MemoryIdler, futexWaitAwokenLate) {
152 StrictMock<Futex<MockAtom>> fut;
153 auto clock = MockClock::setup();
154 auto begin = MockClock::time_point(std::chrono::seconds(100));
155 auto idleTimeout = MemoryIdler::defaultIdleTimeout.load();
157 EXPECT_CALL(*clock, nowImpl())
158 .WillOnce(Return(begin));
159 EXPECT_CALL(fut, futexWaitUntil(1, begin + idleTimeout, -1))
160 .WillOnce(Return(FutexResult::TIMEDOUT));
161 EXPECT_CALL(fut, futexWait(1, -1))
162 .WillOnce(Return(true));
163 EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(
164 fut, 1, -1, idleTimeout, 100, 0.0f)));
167 TEST(MemoryIdler, futexWaitImmediateFlush) {
168 StrictMock<Futex<MockAtom>> fut;
169 auto clock = MockClock::setup();
171 EXPECT_CALL(fut, futexWait(2, 0xff))
172 .WillOnce(Return(true));
173 EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(
174 fut, 2, 0xff, std::chrono::seconds(0))));
177 TEST(MemoryIdler, futexWaitNeverFlush) {
178 StrictMock<Futex<MockAtom>> fut;
179 auto clock = MockClock::setup();
181 EXPECT_CALL(fut, futexWait(1, -1))
182 .WillOnce(Return(true));
183 EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(
184 fut, 1, -1, MockClock::duration::max())));