Move folly/MallctlHelper.h to folly/memory/
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 18 Oct 2017 21:18:47 +0000 (14:18 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 18 Oct 2017 21:21:51 +0000 (14:21 -0700)
Summary: [Folly] Move `folly/MallctlHelper.h` to `folly/memory/`.

Reviewed By: aary

Differential Revision: D6087216

fbshipit-source-id: 4e0fa4aea976e2578127d3c340e0e9b559a224ca

folly/Makefile.am
folly/MallctlHelper.cpp [deleted file]
folly/MallctlHelper.h [deleted file]
folly/detail/MemoryIdler.cpp
folly/memory/MallctlHelper.cpp [new file with mode: 0644]
folly/memory/MallctlHelper.h [new file with mode: 0644]
folly/memory/test/MallctlHelperTest.cpp [new file with mode: 0644]
folly/test/Makefile.am
folly/test/MallctlHelperTest.cpp [deleted file]

index e91dba2..bb2d3e1 100644 (file)
@@ -326,12 +326,12 @@ nobase_follyinclude_HEADERS = \
        LockTraitsBoost.h \
        Logging.h \
        MacAddress.h \
-       MallctlHelper.h \
        Malloc.h \
        MapUtil.h \
        Math.h \
        Memory.h \
        MemoryMapping.h \
+       memory/MallctlHelper.h \
        memory/UninitializedMemoryHacks.h \
        MicroSpinLock.h \
        MicroLock.h \
@@ -482,7 +482,7 @@ libfollybase_la_SOURCES = \
        Format.cpp \
        FormatArg.cpp \
        FormatTables.cpp \
-       MallctlHelper.cpp \
+       memory/MallctlHelper.cpp \
        portability/BitsFunctexcept.cpp \
        String.cpp \
        Unicode.cpp
diff --git a/folly/MallctlHelper.cpp b/folly/MallctlHelper.cpp
deleted file mode 100644 (file)
index 32b7277..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 2017 Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <folly/MallctlHelper.h>
-#include <folly/Format.h>
-#include <folly/String.h>
-
-#include <stdexcept>
-
-namespace folly {
-
-namespace detail {
-
-[[noreturn]] void handleMallctlError(const char* cmd, int err) {
-  assert(err != 0);
-  throw std::runtime_error(
-      sformat("mallctl {}: {} ({})", cmd, errnoStr(err), err));
-}
-
-} // namespace detail
-
-} // namespace folly
diff --git a/folly/MallctlHelper.h b/folly/MallctlHelper.h
deleted file mode 100644 (file)
index f883a5f..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright 2017 Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Some helper functions for mallctl.
-
-#pragma once
-
-#include <folly/Likely.h>
-#include <folly/Malloc.h>
-
-#include <stdexcept>
-
-namespace folly {
-
-namespace detail {
-
-[[noreturn]] void handleMallctlError(const char* cmd, int err);
-
-template <typename T>
-void mallctlHelper(const char* cmd, T* out, T* in) {
-  if (UNLIKELY(!usingJEMalloc())) {
-    throw std::logic_error("Calling mallctl when not using jemalloc.");
-  }
-
-  size_t outLen = sizeof(T);
-  int err = mallctl(cmd, out, out ? &outLen : nullptr, in, in ? sizeof(T) : 0);
-  if (UNLIKELY(err != 0)) {
-    handleMallctlError(cmd, err);
-  }
-}
-
-} // namespace detail
-
-template <typename T>
-void mallctlRead(const char* cmd, T* out) {
-  detail::mallctlHelper(cmd, out, static_cast<T*>(nullptr));
-}
-
-template <typename T>
-void mallctlWrite(const char* cmd, T in) {
-  detail::mallctlHelper(cmd, static_cast<T*>(nullptr), &in);
-}
-
-template <typename T>
-void mallctlReadWrite(const char* cmd, T* out, T in) {
-  detail::mallctlHelper(cmd, out, &in);
-}
-
-inline void mallctlCall(const char* cmd) {
-  // Use <unsigned> rather than <void> to avoid sizeof(void).
-  mallctlRead<unsigned>(cmd, nullptr);
-}
-
-} // namespace folly
index 70c4c05..0171939 100644 (file)
 #include <folly/detail/MemoryIdler.h>
 
 #include <folly/Logging.h>
-#include <folly/MallctlHelper.h>
 #include <folly/Malloc.h>
 #include <folly/Portability.h>
 #include <folly/ScopeGuard.h>
 #include <folly/concurrency/CacheLocality.h>
+#include <folly/memory/MallctlHelper.h>
 #include <folly/portability/PThread.h>
 #include <folly/portability/SysMman.h>
 #include <folly/portability/Unistd.h>
diff --git a/folly/memory/MallctlHelper.cpp b/folly/memory/MallctlHelper.cpp
new file mode 100644 (file)
index 0000000..6593992
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2017 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/memory/MallctlHelper.h>
+#include <folly/Format.h>
+#include <folly/String.h>
+
+#include <stdexcept>
+
+namespace folly {
+
+namespace detail {
+
+[[noreturn]] void handleMallctlError(const char* cmd, int err) {
+  assert(err != 0);
+  throw std::runtime_error(
+      sformat("mallctl {}: {} ({})", cmd, errnoStr(err), err));
+}
+
+} // namespace detail
+
+} // namespace folly
diff --git a/folly/memory/MallctlHelper.h b/folly/memory/MallctlHelper.h
new file mode 100644 (file)
index 0000000..f883a5f
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2017 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Some helper functions for mallctl.
+
+#pragma once
+
+#include <folly/Likely.h>
+#include <folly/Malloc.h>
+
+#include <stdexcept>
+
+namespace folly {
+
+namespace detail {
+
+[[noreturn]] void handleMallctlError(const char* cmd, int err);
+
+template <typename T>
+void mallctlHelper(const char* cmd, T* out, T* in) {
+  if (UNLIKELY(!usingJEMalloc())) {
+    throw std::logic_error("Calling mallctl when not using jemalloc.");
+  }
+
+  size_t outLen = sizeof(T);
+  int err = mallctl(cmd, out, out ? &outLen : nullptr, in, in ? sizeof(T) : 0);
+  if (UNLIKELY(err != 0)) {
+    handleMallctlError(cmd, err);
+  }
+}
+
+} // namespace detail
+
+template <typename T>
+void mallctlRead(const char* cmd, T* out) {
+  detail::mallctlHelper(cmd, out, static_cast<T*>(nullptr));
+}
+
+template <typename T>
+void mallctlWrite(const char* cmd, T in) {
+  detail::mallctlHelper(cmd, static_cast<T*>(nullptr), &in);
+}
+
+template <typename T>
+void mallctlReadWrite(const char* cmd, T* out, T in) {
+  detail::mallctlHelper(cmd, out, &in);
+}
+
+inline void mallctlCall(const char* cmd) {
+  // Use <unsigned> rather than <void> to avoid sizeof(void).
+  mallctlRead<unsigned>(cmd, nullptr);
+}
+
+} // namespace folly
diff --git a/folly/memory/test/MallctlHelperTest.cpp b/folly/memory/test/MallctlHelperTest.cpp
new file mode 100644 (file)
index 0000000..e2d3c2a
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2017 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/memory/MallctlHelper.h>
+#include <folly/Malloc.h>
+#include <folly/init/Init.h>
+#include <folly/portability/GTest.h>
+
+#ifdef FOLLY_HAVE_LIBJEMALLOC
+#include <jemalloc/jemalloc.h>
+#endif
+
+using namespace folly;
+
+#if JEMALLOC_VERSION_MAJOR > 4
+static constexpr char const* kDecayCmd = "arena.0.dirty_decay_ms";
+const char* malloc_conf = "dirty_decay_ms:10";
+#else
+static constexpr char const* kDecayCmd = "arena.0.decay_time";
+const char* malloc_conf = "purge:decay,decay_time:10";
+#endif
+
+class MallctlHelperTest : public ::testing::Test {
+ protected:
+  void TearDown() override {
+    // Reset decay_time of arena 0 to 10 seconds.
+    ssize_t decayTime = 10;
+    EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
+  }
+
+  static ssize_t readArena0DecayTime() {
+    ssize_t decayTime = 0;
+    EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
+    return decayTime;
+  }
+};
+
+TEST_F(MallctlHelperTest, valid_read) {
+  ssize_t decayTime = 0;
+  EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
+  EXPECT_EQ(10, decayTime);
+}
+
+TEST_F(MallctlHelperTest, invalid_read) {
+  ssize_t decayTime = 0;
+  EXPECT_THROW(mallctlRead("invalid", &decayTime), std::runtime_error);
+  EXPECT_EQ(0, decayTime);
+}
+
+TEST_F(MallctlHelperTest, valid_write) {
+  ssize_t decayTime = 20;
+  EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
+  EXPECT_EQ(20, readArena0DecayTime());
+}
+
+TEST_F(MallctlHelperTest, invalid_write) {
+  ssize_t decayTime = 20;
+  EXPECT_THROW(mallctlWrite("invalid", decayTime), std::runtime_error);
+  EXPECT_EQ(10, readArena0DecayTime());
+}
+
+TEST_F(MallctlHelperTest, valid_read_write) {
+  ssize_t oldDecayTime = 0;
+  ssize_t newDecayTime = 20;
+  EXPECT_NO_THROW(mallctlReadWrite(kDecayCmd, &oldDecayTime, newDecayTime));
+  EXPECT_EQ(10, oldDecayTime);
+  EXPECT_EQ(20, readArena0DecayTime());
+}
+
+TEST_F(MallctlHelperTest, invalid_read_write) {
+  ssize_t oldDecayTime = 0;
+  ssize_t newDecayTime = 20;
+  EXPECT_THROW(
+      mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
+      std::runtime_error);
+  EXPECT_EQ(0, oldDecayTime);
+  EXPECT_EQ(10, readArena0DecayTime());
+}
+
+TEST_F(MallctlHelperTest, valid_call) {
+  EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
+}
+
+TEST_F(MallctlHelperTest, invalid_call) {
+  EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
+}
+
+int main(int argc, char** argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  init(&argc, &argv);
+  return usingJEMalloc() ? RUN_ALL_TESTS() : 0;
+}
index eeada6e..a2f5883 100644 (file)
@@ -329,7 +329,7 @@ ssl_test_SOURCES = \
 ssl_test_LDADD = libfollytestmain.la -lcrypto
 TESTS += ssl_test
 
-mallctl_helper_test_SOURCES = MallctlHelperTest.cpp
+mallctl_helper_test_SOURCES = ../memory/test/MallctlHelperTest.cpp
 mallctl_helper_test_LDADD = libfollytestmain.la
 TESTS += mallctl_helper_test
 
diff --git a/folly/test/MallctlHelperTest.cpp b/folly/test/MallctlHelperTest.cpp
deleted file mode 100644 (file)
index e2f2695..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright 2017 Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <folly/MallctlHelper.h>
-#include <folly/Malloc.h>
-#include <folly/init/Init.h>
-#include <folly/portability/GTest.h>
-
-#ifdef FOLLY_HAVE_LIBJEMALLOC
-#include <jemalloc/jemalloc.h>
-#endif
-
-using namespace folly;
-
-#if JEMALLOC_VERSION_MAJOR > 4
-static constexpr char const* kDecayCmd = "arena.0.dirty_decay_ms";
-const char* malloc_conf = "dirty_decay_ms:10";
-#else
-static constexpr char const* kDecayCmd = "arena.0.decay_time";
-const char* malloc_conf = "purge:decay,decay_time:10";
-#endif
-
-class MallctlHelperTest : public ::testing::Test {
- protected:
-  void TearDown() override {
-    // Reset decay_time of arena 0 to 10 seconds.
-    ssize_t decayTime = 10;
-    EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
-  }
-
-  static ssize_t readArena0DecayTime() {
-    ssize_t decayTime = 0;
-    EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
-    return decayTime;
-  }
-};
-
-TEST_F(MallctlHelperTest, valid_read) {
-  ssize_t decayTime = 0;
-  EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
-  EXPECT_EQ(10, decayTime);
-}
-
-TEST_F(MallctlHelperTest, invalid_read) {
-  ssize_t decayTime = 0;
-  EXPECT_THROW(mallctlRead("invalid", &decayTime), std::runtime_error);
-  EXPECT_EQ(0, decayTime);
-}
-
-TEST_F(MallctlHelperTest, valid_write) {
-  ssize_t decayTime = 20;
-  EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
-  EXPECT_EQ(20, readArena0DecayTime());
-}
-
-TEST_F(MallctlHelperTest, invalid_write) {
-  ssize_t decayTime = 20;
-  EXPECT_THROW(mallctlWrite("invalid", decayTime), std::runtime_error);
-  EXPECT_EQ(10, readArena0DecayTime());
-}
-
-TEST_F(MallctlHelperTest, valid_read_write) {
-  ssize_t oldDecayTime = 0;
-  ssize_t newDecayTime = 20;
-  EXPECT_NO_THROW(mallctlReadWrite(kDecayCmd, &oldDecayTime, newDecayTime));
-  EXPECT_EQ(10, oldDecayTime);
-  EXPECT_EQ(20, readArena0DecayTime());
-}
-
-TEST_F(MallctlHelperTest, invalid_read_write) {
-  ssize_t oldDecayTime = 0;
-  ssize_t newDecayTime = 20;
-  EXPECT_THROW(
-      mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
-      std::runtime_error);
-  EXPECT_EQ(0, oldDecayTime);
-  EXPECT_EQ(10, readArena0DecayTime());
-}
-
-TEST_F(MallctlHelperTest, valid_call) {
-  EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
-}
-
-TEST_F(MallctlHelperTest, invalid_call) {
-  EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
-}
-
-int main(int argc, char** argv) {
-  ::testing::InitGoogleTest(&argc, argv);
-  init(&argc, &argv);
-  return usingJEMalloc() ? RUN_ALL_TESTS() : 0;
-}