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