Move the clock details over to the Time.h portability header
[folly.git] / folly / detail / MemoryIdler.cpp
index 711bd90fe42f89e9bcd4c4ee980f7807c2972b9a..bb9691b97fa36ac2b9396cd24c153d88b5cfeac9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include "MemoryIdler.h"
+#include <folly/detail/MemoryIdler.h>
+
 #include <folly/Logging.h>
 #include <folly/Malloc.h>
+#include <folly/Portability.h>
 #include <folly/ScopeGuard.h>
 #include <folly/detail/CacheLocality.h>
 #include <limits.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <string.h>
-#include <unistd.h>
 #include <sys/mman.h>
+#include <unistd.h>
 #include <utility>
 
-
-// weak linking means the symbol will be null if not available, instead
-// of a link failure
-extern "C" int mallctl(const char *name, void *oldp, size_t *oldlenp,
-                       void *newp, size_t newlen)
-    __attribute__((weak));
-
-
 namespace folly { namespace detail {
 
 AtomicStruct<std::chrono::steady_clock::duration>
 MemoryIdler::defaultIdleTimeout(std::chrono::seconds(5));
 
 
-/// Calls mallctl, optionally reading and/or writing an unsigned value
-/// if in and/or out is non-null.  Logs on error
-static unsigned mallctlWrapper(const char* cmd, const unsigned* in,
-                               unsigned* out) {
-  size_t outLen = sizeof(unsigned);
+// Calls mallctl, optionally reading a value of type <T> if out is
+// non-null.  Logs on error.
+template <typename T>
+static int mallctlRead(const char* cmd, T* out) {
+  size_t outLen = sizeof(T);
   int err = mallctl(cmd,
                     out, out ? &outLen : nullptr,
-                    const_cast<unsigned*>(in), in ? sizeof(unsigned) : 0);
+                    nullptr, 0);
   if (err != 0) {
     FB_LOG_EVERY_MS(WARNING, 10000)
       << "mallctl " << cmd << ": " << strerror(err) << " (" << err << ")";
@@ -56,15 +50,20 @@ static unsigned mallctlWrapper(const char* cmd, const unsigned* in,
   return err;
 }
 
+static int mallctlCall(const char* cmd) {
+  // Use <unsigned> rather than <void> to avoid sizeof(void).
+  return mallctlRead<unsigned>(cmd, nullptr);
+}
+
 void MemoryIdler::flushLocalMallocCaches() {
   if (usingJEMalloc()) {
-    if (!mallctl) {
-      FB_LOG_EVERY_MS(ERROR, 10000) << "mallctl weak link failed";
+    if (!mallctl || !mallctlnametomib || !mallctlbymib) {
+      FB_LOG_EVERY_MS(ERROR, 10000) << "mallctl* weak link failed";
       return;
     }
 
     // "tcache.flush" was renamed to "thread.tcache.flush" in jemalloc 3
-    (void)mallctlWrapper("thread.tcache.flush", nullptr, nullptr);
+    mallctlCall("thread.tcache.flush");
 
     // By default jemalloc has 4 arenas per cpu, and then assigns each
     // thread to one of those arenas.  This means that in any service
@@ -78,28 +77,36 @@ void MemoryIdler::flushLocalMallocCaches() {
     // which detects when the narenas has been reduced from the default
     unsigned narenas;
     unsigned arenaForCurrent;
-    if (mallctlWrapper("arenas.narenas", nullptr, &narenas) == 0 &&
+    size_t mib[3];
+    size_t miblen = 3;
+    if (mallctlRead<unsigned>("opt.narenas", &narenas) == 0 &&
         narenas > 2 * CacheLocality::system().numCpus &&
-        mallctlWrapper("thread.arena", nullptr, &arenaForCurrent) == 0) {
-      (void)mallctlWrapper("arenas.purge", &arenaForCurrent, nullptr);
+        mallctlRead<unsigned>("thread.arena", &arenaForCurrent) == 0 &&
+        mallctlnametomib("arena.0.purge", mib, &miblen) == 0) {
+      mib[1] = size_t(arenaForCurrent);
+      mallctlbymib(mib, miblen, nullptr, nullptr, nullptr, 0);
     }
   }
 }
 
 
-#ifdef __x86_64__
+// Stack madvise isn't Linux or glibc specific, but the system calls
+// and arithmetic (and bug compatibility) are not portable.  The set of
+// platforms could be increased if it was useful.
+#if (FOLLY_X64 || FOLLY_PPC64) && defined(_GNU_SOURCE) && \
+    defined(__linux__) && !FOLLY_MOBILE
+
+static FOLLY_TLS uintptr_t tls_stackLimit;
+static FOLLY_TLS size_t tls_stackSize;
 
-static const size_t s_pageSize = sysconf(_SC_PAGESIZE);
-static __thread uintptr_t tls_stackLimit;
-static __thread size_t tls_stackSize;
+static size_t pageSize() {
+  static const size_t s_pageSize = sysconf(_SC_PAGESIZE);
+  return s_pageSize;
+}
 
 static void fetchStackLimits() {
   pthread_attr_t attr;
-#if defined(_GNU_SOURCE) && defined(__linux__) // Linux+GNU extension
   pthread_getattr_np(pthread_self(), &attr);
-#else
-  pthread_attr_init(&attr);
-#endif
   SCOPE_EXIT { pthread_attr_destroy(&attr); };
 
   void* addr;
@@ -127,10 +134,10 @@ static void fetchStackLimits() {
   tls_stackLimit = uintptr_t(addr) + guardSize;
   tls_stackSize = rawSize - guardSize;
 
-  assert((tls_stackLimit & (s_pageSize - 1)) == 0);
+  assert((tls_stackLimit & (pageSize() - 1)) == 0);
 }
 
-static __attribute__((noinline)) uintptr_t getStackPtr() {
+FOLLY_NOINLINE static uintptr_t getStackPtr() {
   char marker;
   auto rv = uintptr_t(&marker);
   return rv;
@@ -149,22 +156,21 @@ void MemoryIdler::unmapUnusedStack(size_t retain) {
   assert(sp >= tls_stackLimit);
   assert(sp - tls_stackLimit < tls_stackSize);
 
-  auto end = (sp - retain) & ~(s_pageSize - 1);
+  auto end = (sp - retain) & ~(pageSize() - 1);
   if (end <= tls_stackLimit) {
     // no pages are eligible for unmapping
     return;
   }
 
   size_t len = end - tls_stackLimit;
-  assert((len & (s_pageSize - 1)) == 0);
+  assert((len & (pageSize() - 1)) == 0);
   if (madvise((void*)tls_stackLimit, len, MADV_DONTNEED) != 0) {
     // It is likely that the stack vma hasn't been fully grown.  In this
     // case madvise will apply dontneed to the present vmas, then return
     // errno of ENOMEM.  We can also get an EAGAIN, theoretically.
     // EINVAL means either an invalid alignment or length, or that some
     // of the pages are locked or shared.  Neither should occur.
-    int e = errno;
-    assert(e == EAGAIN || e == ENOMEM);
+    assert(errno == EAGAIN || errno == ENOMEM);
   }
 }