Move the clock details over to the Time.h portability header
[folly.git] / folly / detail / MemoryIdler.cpp
index f7af9ee8266c3f24b6f34c62b3c3affe4c054d34..bb9691b97fa36ac2b9396cd24c153d88b5cfeac9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 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.
  */
 
 #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>
 
-
 namespace folly { namespace detail {
 
 AtomicStruct<std::chrono::steady_clock::duration>
@@ -74,11 +75,11 @@ void MemoryIdler::flushLocalMallocCaches() {
     // purging the arenas is counter-productive.  We use the heuristic
     // that if narenas <= 2 * num_cpus then we shouldn't do anything here,
     // which detects when the narenas has been reduced from the default
-    size_t narenas;
+    unsigned narenas;
     unsigned arenaForCurrent;
     size_t mib[3];
     size_t miblen = 3;
-    if (mallctlRead<size_t>("opt.narenas", &narenas) == 0 &&
+    if (mallctlRead<unsigned>("opt.narenas", &narenas) == 0 &&
         narenas > 2 * CacheLocality::system().numCpus &&
         mallctlRead<unsigned>("thread.arena", &arenaForCurrent) == 0 &&
         mallctlnametomib("arena.0.purge", mib, &miblen) == 0) {
@@ -92,12 +93,17 @@ void MemoryIdler::flushLocalMallocCaches() {
 // 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__)
+#if (FOLLY_X64 || FOLLY_PPC64) && defined(_GNU_SOURCE) && \
+    defined(__linux__) && !FOLLY_MOBILE
 
-static const size_t s_pageSize = sysconf(_SC_PAGESIZE);
 static FOLLY_TLS uintptr_t tls_stackLimit;
 static FOLLY_TLS 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;
   pthread_getattr_np(pthread_self(), &attr);
@@ -128,7 +134,7 @@ 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);
 }
 
 FOLLY_NOINLINE static uintptr_t getStackPtr() {
@@ -150,14 +156,14 @@ 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