e2b22fe699a15f1f94e2f836c98c466af341dfae
[folly.git] / folly / test / MallctlHelperTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/MallctlHelper.h>
18 #include <folly/Malloc.h>
19 #include <folly/init/Init.h>
20 #include <folly/portability/GTest.h>
21
22 #ifdef FOLLY_HAVE_LIBJEMALLOC
23 #include <jemalloc/jemalloc.h>
24 #endif
25
26 using namespace folly;
27
28 #ifdef FOLLY_HAVE_LIBJEMALLOC
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";
32 #else
33 static constexpr char const* kDecayCmd = "arena.0.decay_time";
34 const char* malloc_conf = "purge:decay,decay_time:10";
35 #endif
36 #endif
37
38 class MallctlHelperTest : public ::testing::Test {
39  protected:
40   void TearDown() override {
41     // Reset decay_time of arena 0 to 10 seconds.
42     ssize_t decayTime = 10;
43     EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
44   }
45
46   static ssize_t readArena0DecayTime() {
47     ssize_t decayTime = 0;
48     EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
49     return decayTime;
50   }
51 };
52
53 TEST_F(MallctlHelperTest, valid_read) {
54   ssize_t decayTime = 0;
55   EXPECT_NO_THROW(mallctlRead(kDecayCmd, &decayTime));
56   EXPECT_EQ(10, decayTime);
57 }
58
59 TEST_F(MallctlHelperTest, invalid_read) {
60   ssize_t decayTime = 0;
61   EXPECT_THROW(mallctlRead("invalid", &decayTime), std::runtime_error);
62   EXPECT_EQ(0, decayTime);
63 }
64
65 TEST_F(MallctlHelperTest, valid_write) {
66   ssize_t decayTime = 20;
67   EXPECT_NO_THROW(mallctlWrite(kDecayCmd, decayTime));
68   EXPECT_EQ(20, readArena0DecayTime());
69 }
70
71 TEST_F(MallctlHelperTest, invalid_write) {
72   ssize_t decayTime = 20;
73   EXPECT_THROW(mallctlWrite("invalid", decayTime), std::runtime_error);
74   EXPECT_EQ(10, readArena0DecayTime());
75 }
76
77 TEST_F(MallctlHelperTest, valid_read_write) {
78   ssize_t oldDecayTime = 0;
79   ssize_t newDecayTime = 20;
80   EXPECT_NO_THROW(mallctlReadWrite(kDecayCmd, &oldDecayTime, newDecayTime));
81   EXPECT_EQ(10, oldDecayTime);
82   EXPECT_EQ(20, readArena0DecayTime());
83 }
84
85 TEST_F(MallctlHelperTest, invalid_read_write) {
86   ssize_t oldDecayTime = 0;
87   ssize_t newDecayTime = 20;
88   EXPECT_THROW(
89       mallctlReadWrite("invalid", &oldDecayTime, newDecayTime),
90       std::runtime_error);
91   EXPECT_EQ(0, oldDecayTime);
92   EXPECT_EQ(10, readArena0DecayTime());
93 }
94
95 TEST_F(MallctlHelperTest, valid_call) {
96   EXPECT_NO_THROW(mallctlCall("arena.0.decay"));
97 }
98
99 TEST_F(MallctlHelperTest, invalid_call) {
100   EXPECT_THROW(mallctlCall("invalid"), std::runtime_error);
101 }
102
103 int main(int argc, char** argv) {
104   ::testing::InitGoogleTest(&argc, argv);
105   init(&argc, &argv);
106   return usingJEMalloc() ? RUN_ALL_TESTS() : 0;
107 }