Futex::futexWait returns FutexResult
[folly.git] / folly / detail / MemoryIdler.cpp
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/detail/MemoryIdler.h>
18
19 #include <folly/Logging.h>
20 #include <folly/Portability.h>
21 #include <folly/ScopeGuard.h>
22 #include <folly/concurrency/CacheLocality.h>
23 #include <folly/memory/MallctlHelper.h>
24 #include <folly/memory/Malloc.h>
25 #include <folly/portability/PThread.h>
26 #include <folly/portability/SysMman.h>
27 #include <folly/portability/Unistd.h>
28 #include <folly/synchronization/CallOnce.h>
29
30 #include <limits.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <utility>
34
35 namespace folly { namespace detail {
36
37 AtomicStruct<std::chrono::steady_clock::duration>
38 MemoryIdler::defaultIdleTimeout(std::chrono::seconds(5));
39
40 void MemoryIdler::flushLocalMallocCaches() {
41   if (!usingJEMalloc()) {
42     return;
43   }
44   if (!mallctl || !mallctlnametomib || !mallctlbymib) {
45     FB_LOG_EVERY_MS(ERROR, 10000) << "mallctl* weak link failed";
46     return;
47   }
48
49   try {
50     // Not using mallctlCall as this will fail if tcache is disabled.
51     mallctl("thread.tcache.flush", nullptr, nullptr, nullptr, 0);
52
53     // By default jemalloc has 4 arenas per cpu, and then assigns each
54     // thread to one of those arenas.  This means that in any service
55     // that doesn't perform a lot of context switching, the chances that
56     // another thread will be using the current thread's arena (and hence
57     // doing the appropriate dirty-page purging) are low.  Some good
58     // tuned configurations (such as that used by hhvm) use fewer arenas
59     // and then pin threads to avoid contended access.  In that case,
60     // purging the arenas is counter-productive.  We use the heuristic
61     // that if narenas <= 2 * num_cpus then we shouldn't do anything here,
62     // which detects when the narenas has been reduced from the default
63     unsigned narenas;
64     unsigned arenaForCurrent;
65     size_t mib[3];
66     size_t miblen = 3;
67
68     mallctlRead("opt.narenas", &narenas);
69     mallctlRead("thread.arena", &arenaForCurrent);
70     if (narenas > 2 * CacheLocality::system().numCpus &&
71         mallctlnametomib("arena.0.purge", mib, &miblen) == 0) {
72       mib[1] = static_cast<size_t>(arenaForCurrent);
73       mallctlbymib(mib, miblen, nullptr, nullptr, nullptr, 0);
74     }
75   } catch (const std::runtime_error& ex) {
76     FB_LOG_EVERY_MS(WARNING, 10000) << ex.what();
77   }
78 }
79
80
81 // Stack madvise isn't Linux or glibc specific, but the system calls
82 // and arithmetic (and bug compatibility) are not portable.  The set of
83 // platforms could be increased if it was useful.
84 #if (FOLLY_X64 || FOLLY_PPC64) && defined(_GNU_SOURCE) && \
85     defined(__linux__) && !FOLLY_MOBILE && !FOLLY_SANITIZE_ADDRESS
86
87 static FOLLY_TLS uintptr_t tls_stackLimit;
88 static FOLLY_TLS size_t tls_stackSize;
89
90 static size_t pageSize() {
91   static const size_t s_pageSize = sysconf(_SC_PAGESIZE);
92   return s_pageSize;
93 }
94
95 static void fetchStackLimits() {
96   int err;
97   pthread_attr_t attr;
98   if ((err = pthread_getattr_np(pthread_self(), &attr))) {
99     // some restricted environments can't access /proc
100     static folly::once_flag flag;
101     folly::call_once(flag, [err]() {
102       LOG(WARNING) << "pthread_getaddr_np failed errno=" << err;
103     });
104
105     tls_stackSize = 1;
106     return;
107   }
108   SCOPE_EXIT { pthread_attr_destroy(&attr); };
109
110   void* addr;
111   size_t rawSize;
112   if ((err = pthread_attr_getstack(&attr, &addr, &rawSize))) {
113     // unexpected, but it is better to continue in prod than do nothing
114     FB_LOG_EVERY_MS(ERROR, 10000) << "pthread_attr_getstack error " << err;
115     assert(false);
116     tls_stackSize = 1;
117     return;
118   }
119   assert(addr != nullptr);
120   assert(rawSize >= PTHREAD_STACK_MIN);
121
122   // glibc subtracts guard page from stack size, even though pthread docs
123   // seem to imply the opposite
124   size_t guardSize;
125   if (pthread_attr_getguardsize(&attr, &guardSize) != 0) {
126     guardSize = 0;
127   }
128   assert(rawSize > guardSize);
129
130   // stack goes down, so guard page adds to the base addr
131   tls_stackLimit = reinterpret_cast<uintptr_t>(addr) + guardSize;
132   tls_stackSize = rawSize - guardSize;
133
134   assert((tls_stackLimit & (pageSize() - 1)) == 0);
135 }
136
137 FOLLY_NOINLINE static uintptr_t getStackPtr() {
138   char marker;
139   auto rv = reinterpret_cast<uintptr_t>(&marker);
140   return rv;
141 }
142
143 void MemoryIdler::unmapUnusedStack(size_t retain) {
144   if (tls_stackSize == 0) {
145     fetchStackLimits();
146   }
147   if (tls_stackSize <= std::max(static_cast<size_t>(1), retain)) {
148     // covers both missing stack info, and impossibly large retain
149     return;
150   }
151
152   auto sp = getStackPtr();
153   assert(sp >= tls_stackLimit);
154   assert(sp - tls_stackLimit < tls_stackSize);
155
156   auto end = (sp - retain) & ~(pageSize() - 1);
157   if (end <= tls_stackLimit) {
158     // no pages are eligible for unmapping
159     return;
160   }
161
162   size_t len = end - tls_stackLimit;
163   assert((len & (pageSize() - 1)) == 0);
164   if (madvise((void*)tls_stackLimit, len, MADV_DONTNEED) != 0) {
165     // It is likely that the stack vma hasn't been fully grown.  In this
166     // case madvise will apply dontneed to the present vmas, then return
167     // errno of ENOMEM.  We can also get an EAGAIN, theoretically.
168     // EINVAL means either an invalid alignment or length, or that some
169     // of the pages are locked or shared.  Neither should occur.
170     assert(errno == EAGAIN || errno == ENOMEM);
171   }
172 }
173
174 #else
175
176 void MemoryIdler::unmapUnusedStack(size_t /* retain */) {}
177
178 #endif
179
180 } // namespace detail
181 } // namespace folly