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