From: Yedidya Feldblum Date: Wed, 18 Oct 2017 21:18:47 +0000 (-0700) Subject: Move folly/MallctlHelper.h to folly/memory/ X-Git-Tag: v2017.10.23.00~25 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a936fbd8818cf85f7e0c579cf1c28c395b8fd357;hp=e372e3d34fd57695d9655336b43e13be8784c81d;p=folly.git Move folly/MallctlHelper.h to folly/memory/ Summary: [Folly] Move `folly/MallctlHelper.h` to `folly/memory/`. Reviewed By: aary Differential Revision: D6087216 fbshipit-source-id: 4e0fa4aea976e2578127d3c340e0e9b559a224ca --- diff --git a/folly/Makefile.am b/folly/Makefile.am index e91dba2d..bb2d3e14 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -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 index 32b7277d..00000000 --- a/folly/MallctlHelper.cpp +++ /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 -#include -#include - -#include - -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 index f883a5f6..00000000 --- a/folly/MallctlHelper.h +++ /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 -#include - -#include - -namespace folly { - -namespace detail { - -[[noreturn]] void handleMallctlError(const char* cmd, int err); - -template -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 -void mallctlRead(const char* cmd, T* out) { - detail::mallctlHelper(cmd, out, static_cast(nullptr)); -} - -template -void mallctlWrite(const char* cmd, T in) { - detail::mallctlHelper(cmd, static_cast(nullptr), &in); -} - -template -void mallctlReadWrite(const char* cmd, T* out, T in) { - detail::mallctlHelper(cmd, out, &in); -} - -inline void mallctlCall(const char* cmd) { - // Use rather than to avoid sizeof(void). - mallctlRead(cmd, nullptr); -} - -} // namespace folly diff --git a/folly/detail/MemoryIdler.cpp b/folly/detail/MemoryIdler.cpp index 70c4c055..0171939c 100644 --- a/folly/detail/MemoryIdler.cpp +++ b/folly/detail/MemoryIdler.cpp @@ -17,11 +17,11 @@ #include #include -#include #include #include #include #include +#include #include #include #include diff --git a/folly/memory/MallctlHelper.cpp b/folly/memory/MallctlHelper.cpp new file mode 100644 index 00000000..6593992d --- /dev/null +++ b/folly/memory/MallctlHelper.cpp @@ -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 +#include +#include + +#include + +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 index 00000000..f883a5f6 --- /dev/null +++ b/folly/memory/MallctlHelper.h @@ -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 +#include + +#include + +namespace folly { + +namespace detail { + +[[noreturn]] void handleMallctlError(const char* cmd, int err); + +template +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 +void mallctlRead(const char* cmd, T* out) { + detail::mallctlHelper(cmd, out, static_cast(nullptr)); +} + +template +void mallctlWrite(const char* cmd, T in) { + detail::mallctlHelper(cmd, static_cast(nullptr), &in); +} + +template +void mallctlReadWrite(const char* cmd, T* out, T in) { + detail::mallctlHelper(cmd, out, &in); +} + +inline void mallctlCall(const char* cmd) { + // Use rather than to avoid sizeof(void). + mallctlRead(cmd, nullptr); +} + +} // namespace folly diff --git a/folly/memory/test/MallctlHelperTest.cpp b/folly/memory/test/MallctlHelperTest.cpp new file mode 100644 index 00000000..e2d3c2a7 --- /dev/null +++ b/folly/memory/test/MallctlHelperTest.cpp @@ -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 +#include +#include +#include + +#ifdef FOLLY_HAVE_LIBJEMALLOC +#include +#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; +} diff --git a/folly/test/Makefile.am b/folly/test/Makefile.am index eeada6eb..a2f58830 100644 --- a/folly/test/Makefile.am +++ b/folly/test/Makefile.am @@ -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 index e2f26954..00000000 --- a/folly/test/MallctlHelperTest.cpp +++ /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 -#include -#include -#include - -#ifdef FOLLY_HAVE_LIBJEMALLOC -#include -#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; -}