2 * Copyright 2016-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/memory/MallctlHelper.h>
18 #include <folly/CPortability.h>
19 #include <folly/init/Init.h>
20 #include <folly/memory/Malloc.h>
21 #include <folly/portability/GTest.h>
23 #if defined(FOLLY_HAVE_LIBJEMALLOC) && !FOLLY_SANITIZE
24 #include <jemalloc/jemalloc.h>
27 using namespace folly;
29 #if JEMALLOC_VERSION_MAJOR > 4
30 static constexpr char const* kDecayCmd = "arena.0.dirty_decay_ms";
31 const char* malloc_conf = "dirty_decay_ms:10";
33 static constexpr char const* kDecayCmd = "arena.0.decay_time";
34 const char* malloc_conf = "purge:decay,decay_time:10";
37 class MallctlHelperTest : public ::testing::Test {
39 void TearDown() override {
40 // Reset decay_time of arena 0 to 10 seconds.
41 ssize_t decayTime = 10;
42 EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
45 static ssize_t readArena0DecayTime() {
46 ssize_t decayTime = 0;
47 EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
52 TEST_F(MallctlHelperTest, valid_read) {
53 ssize_t decayTime = 0;
54 EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
55 EXPECT_EQ(10, decayTime);
58 TEST_F(MallctlHelperTest, invalid_read) {
59 ssize_t decayTime = 0;
60 EXPECT_THROW(mallctlRead("invalid", &decayTime), std::runtime_error);
61 EXPECT_EQ(0, decayTime);
64 TEST_F(MallctlHelperTest, valid_write) {
65 ssize_t decayTime = 20;
66 EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
67 EXPECT_EQ(20, readArena0DecayTime());
70 TEST_F(MallctlHelperTest, invalid_write) {
71 ssize_t decayTime = 20;
72 EXPECT_THROW(mallctlWrite("invalid", decayTime), std::runtime_error);
73 EXPECT_EQ(10, readArena0DecayTime());
76 TEST_F(MallctlHelperTest, valid_read_write) {
77 ssize_t oldDecayTime = 0;
78 ssize_t newDecayTime = 20;
79 EXPECT_NO_THROW(mallctlReadWrite(kDecayCmd, &oldDecayTime, newDecayTime));
80 EXPECT_EQ(10, oldDecayTime);
81 EXPECT_EQ(20, readArena0DecayTime());
84 TEST_F(MallctlHelperTest, invalid_read_write) {
85 ssize_t oldDecayTime = 0;
86 ssize_t newDecayTime = 20;
88 mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
90 EXPECT_EQ(0, oldDecayTime);
91 EXPECT_EQ(10, readArena0DecayTime());
94 TEST_F(MallctlHelperTest, valid_call) {
95 EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
98 TEST_F(MallctlHelperTest, invalid_call) {
99 EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
102 int main(int argc, char** argv) {
103 ::testing::InitGoogleTest(&argc, argv);
105 return usingJEMalloc() ? RUN_ALL_TESTS() : 0;