Disable the CArray RangeFunc tests under MSVC
[folly.git] / folly / MemoryMapping.cpp
index 17f1e760a968f15f22af9c35ba2714f7d249755d..71918c4e34ab067759aaa1a8514b5247492e7f0e 100644 (file)
@@ -21,6 +21,7 @@
 #include <utility>
 
 #include <folly/Format.h>
+#include <folly/portability/GFlags.h>
 #include <folly/portability/SysMman.h>
 
 #ifdef __linux__
 #include <fcntl.h>
 #include <sys/types.h>
 #include <system_error>
-#include <gflags/gflags.h>
 
-DEFINE_int64(mlock_chunk_size, 1 << 20,  // 1MB
+static constexpr ssize_t kDefaultMlockChunkSize =
+#ifndef _MSC_VER
+    // Linux implementations of unmap/mlock/munlock take a kernel
+    // semaphore and block other threads from doing other memory
+    // operations. Split the operations in chunks.
+    (1 << 20) // 1MB
+#else // _MSC_VER
+    // MSVC doesn't have this problem, and calling munmap many times
+    // with the same address is a bad idea with the windows implementation.
+    (-1)
+#endif // _MSC_VER
+    ;
+
+DEFINE_int64(mlock_chunk_size, kDefaultMlockChunkSize,
              "Maximum bytes to mlock/munlock/munmap at once "
-             "(will be rounded up to PAGESIZE)");
+             "(will be rounded up to PAGESIZE). Ignored if negative.");
 
 #ifndef MAP_POPULATE
 #define MAP_POPULATE 0
@@ -211,17 +224,8 @@ off_t memOpChunkSize(off_t length, off_t pageSize) {
 bool memOpInChunks(std::function<int(void*, size_t)> op,
                    void* mem, size_t bufSize, off_t pageSize,
                    size_t& amountSucceeded) {
-#ifdef _MSC_VER
-  // MSVC doesn't have this problem, and calling munmap many times
-  // with the same address is a bad idea with the windows implementation.
-  int ret = op(mem, bufSize);
-  if (ret == 0) {
-    amountSucceeded = bufSize;
-  }
-  return ret == 0;
-#else
-  // unmap/mlock/munlock take a kernel semaphore and block other threads from
-  // doing other memory operations. If the size of the buffer is big the
+  // Linux' unmap/mlock/munlock take a kernel semaphore and block other threads
+  // from doing other memory operations. If the size of the buffer is big the
   // semaphore can be down for seconds (for benchmarks see
   // http://kostja-osipov.livejournal.com/42963.html).  Doing the operations in
   // chunks breaks the locking into intervals and lets other threads do memory
@@ -241,7 +245,6 @@ bool memOpInChunks(std::function<int(void*, size_t)> op,
   }
 
   return true;
-#endif
 }
 
 }  // anonymous namespace
@@ -255,21 +258,20 @@ bool MemoryMapping::mlock(LockMode lock) {
   }
 
   auto msg =
-    folly::sformat("mlock({}) failed at {}", mapLength_, amountSucceeded);
-
-  if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
+      folly::format("mlock({}) failed at {}", mapLength_, amountSucceeded);
+  if (lock == LockMode::TRY_LOCK && errno == EPERM) {
     PLOG(WARNING) << msg;
+  } else if (lock == LockMode::TRY_LOCK && errno == ENOMEM) {
+    VLOG(1) << msg;
   } else {
     PLOG(FATAL) << msg;
   }
 
-#ifndef _MSC_VER
   // only part of the buffer was mlocked, unlock it back
   if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, options_.pageSize,
                      amountSucceeded)) {
     PLOG(WARNING) << "munlock()";
   }
-#endif
 
   return false;
 }